Skip to content

Commit

Permalink
feat: support sourcemapIgnoreList (#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin authored Apr 19, 2024
1 parent 706162a commit 9795d0a
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 4 deletions.
17 changes: 16 additions & 1 deletion crates/rolldown/src/stages/generate_stage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,24 @@ impl<'a> GenerateStage<'a> {
{
if let Some(map) = map.as_mut() {
map.set_file(&rendered_chunk.file_name);

let map_file_name = format!("{}.map", rendered_chunk.file_name);

if let Some(source_map_ignore_list) = &self.options.sourcemap_ignore_list {
let mut x_google_ignore_list = vec![];
for (index, source) in map.get_sources().enumerate() {
if source_map_ignore_list.call(source, map_file_name.as_str()).await? {
#[allow(clippy::cast_possible_truncation)]
x_google_ignore_list.push(index as u32);
}
}
if !x_google_ignore_list.is_empty() {
map.set_x_google_ignore_list(x_google_ignore_list);
}
}

match self.options.sourcemap {
SourceMapType::File => {
let map_file_name = format!("{}.map", rendered_chunk.file_name);
let source = match map.to_json_string().map_err(BuildError::sourcemap_error) {
Ok(source) => source,
Err(e) => {
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/src/utils/normalize_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub fn normalize_options(mut raw_options: crate::BundlerOptions) -> NormalizeOpt
dir: raw_options.dir.unwrap_or_else(|| "dist".to_string()),
format: raw_options.format.unwrap_or(crate::OutputFormat::Esm),
sourcemap: raw_options.sourcemap.unwrap_or(SourceMapType::Hidden),
sourcemap_ignore_list: raw_options.sourcemap_ignore_list,
shim_missing_exports: raw_options.shim_missing_exports.unwrap_or(false),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::super::types::binding_rendered_chunk::RenderedChunk;
use super::plugin::BindingPluginOrParallelJsPluginPlaceholder;
use crate::types::js_callback::MaybeAsyncJsCallback;
use derivative::Derivative;
use napi::threadsafe_function::ThreadsafeFunction;
use napi_derive::napi;
use serde::Deserialize;

Expand Down Expand Up @@ -66,6 +67,10 @@ pub struct BindingOutputOptions {
// sanitizeFileName: (fileName: string) => string;
#[napi(ts_type = "'file' | 'inline' | 'hidden'")]
pub sourcemap: Option<String>,
#[derivative(Debug = "ignore")]
#[serde(skip_deserializing)]
#[napi(ts_type = "(source: string, sourcemapPath: string) => boolean")]
pub sourcemap_ignore_list: Option<ThreadsafeFunction<(String, String), bool, false>>,
// sourcemapExcludeSources: boolean;
// sourcemapFile: string | undefined;
// sourcemapPathTransform: SourcemapPathTransformOption | undefined;
Expand Down
12 changes: 12 additions & 0 deletions crates/rolldown_binding/src/utils/normalize_binding_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ pub fn normalize_binding_options(
}))
});

let sourcemap_ignore_list = output_options.sourcemap_ignore_list.map(|ts_fn| {
rolldown::SourceMapIgnoreList::new(Box::new(move |source, sourcemap_path| {
let ts_fn = ts_fn.clone();
let source = source.to_string();
let sourcemap_path = sourcemap_path.to_string();
Box::pin(async move {
ts_fn.call_async((source, sourcemap_path)).await.map_err(anyhow::Error::from)
})
}))
});

let bundler_options = BundlerOptions {
input: Some(input_options.input.into_iter().map(Into::into).collect()),
cwd: cwd.into(),
Expand All @@ -71,6 +82,7 @@ pub fn normalize_binding_options(
sourcemap: output_options.sourcemap.map(Into::into),
banner: normalize_addon_option(output_options.banner),
footer: normalize_addon_option(output_options.footer),
sourcemap_ignore_list,
// TODO(hyf0): remove this line, all options should set explicitly
..Default::default()
};
Expand Down
8 changes: 8 additions & 0 deletions crates/rolldown_common/src/inner_bundler_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use schemars::JsonSchema;
#[cfg(feature = "deserialize_bundler_options")]
use serde::{Deserialize, Deserializer};

use crate::SourceMapIgnoreList;

use self::types::{
external::External, input_item::InputItem, output_format::OutputFormat,
output_option::AddonOutputOption, platform::Platform, resolve_options::ResolveOptions,
Expand Down Expand Up @@ -50,6 +52,12 @@ pub struct BundlerOptions {
schemars(with = "Option<String>")
)]
pub footer: Option<AddonOutputOption>,
#[cfg_attr(
feature = "deserialize_bundler_options",
serde(default, skip_deserializing),
schemars(skip)
)]
pub sourcemap_ignore_list: Option<SourceMapIgnoreList>,
// --- options for resolve
pub resolve: Option<ResolveOptions>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pub mod output_option;
pub mod platform;
pub mod resolve_options;
pub mod source_map_type;
pub mod sourcemap_ignore_list;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::PathBuf;
use super::{
external::External, file_name_template::FileNameTemplate, input_item::InputItem,
output_format::OutputFormat, output_option::AddonOutputOption, platform::Platform,
source_map_type::SourceMapType,
source_map_type::SourceMapType, sourcemap_ignore_list::SourceMapIgnoreList,
};

#[derive(Debug)]
Expand All @@ -26,4 +26,5 @@ pub struct NormalizedBundlerOptions {
pub sourcemap: SourceMapType,
pub banner: Option<AddonOutputOption>,
pub footer: Option<AddonOutputOption>,
pub sourcemap_ignore_list: Option<SourceMapIgnoreList>,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::fmt::Debug;
use std::{future::Future, pin::Pin};

pub type SourceMapIgnoreListFn = dyn Fn(&str, &str) -> Pin<Box<(dyn Future<Output = anyhow::Result<bool>> + Send + 'static)>>
+ Send
+ Sync;

pub struct SourceMapIgnoreList(Box<SourceMapIgnoreListFn>);

impl Debug for SourceMapIgnoreList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "SourceMapIgnoreList::Fn(...)")
}
}

impl SourceMapIgnoreList {
pub fn new(f: Box<SourceMapIgnoreListFn>) -> Self {
Self(f)
}

pub async fn call(&self, source: &str, sourcemap_path: &str) -> anyhow::Result<bool> {
self.0(source, sourcemap_path).await
}
}
1 change: 1 addition & 0 deletions crates/rolldown_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod bundler_options {
platform::Platform,
resolve_options::ResolveOptions,
source_map_type::SourceMapType,
sourcemap_ignore_list::SourceMapIgnoreList,
},
BundlerOptions,
};
Expand Down
1 change: 1 addition & 0 deletions packages/rolldown/src/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export interface BindingOutputOptions {
format?: 'es' | 'cjs'
plugins: Array<BindingPluginOrParallelJsPluginPlaceholder>
sourcemap?: 'file' | 'inline' | 'hidden'
sourcemapIgnoreList?: (source: string, sourcemapPath: string) => boolean
}

export interface BindingPluginContextResolveOptions {
Expand Down
24 changes: 22 additions & 2 deletions packages/rolldown/src/options/output-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface OutputOptions {
format?: 'es'
exports?: RollupOutputOptions['exports']
sourcemap?: RollupOutputOptions['sourcemap']
sourcemapIgnoreList?: RollupOutputOptions['sourcemapIgnoreList']
banner?: RollupOutputOptions['banner']
footer?: RollupOutputOptions['footer']
entryFileNames?: string
Expand Down Expand Up @@ -45,6 +46,17 @@ function normalizeSourcemap(
}
}

function normalizeSourcemapIgnoreList(
sourcemapIgnoreList: OutputOptions['sourcemapIgnoreList'],
): BindingOutputOptions['sourcemapIgnoreList'] {
return typeof sourcemapIgnoreList === 'function'
? sourcemapIgnoreList
: sourcemapIgnoreList === false
? () => false
: (relativeSourcePath: string, sourcemapPath: string) =>
relativeSourcePath.includes('node_modules')
}

const getAddon = <T extends 'banner' | 'footer'>(
config: OutputOptions,
name: T,
Expand All @@ -60,13 +72,21 @@ const getAddon = <T extends 'banner' | 'footer'>(
export function normalizeOutputOptions(
opts: OutputOptions,
): BindingOutputOptions {
const { dir, format, exports, sourcemap, entryFileNames, chunkFileNames } =
opts
const {
dir,
format,
exports,
sourcemap,
sourcemapIgnoreList,
entryFileNames,
chunkFileNames,
} = opts
return {
dir: dir,
format: normalizeFormat(format),
exports,
sourcemap: normalizeSourcemap(sourcemap),
sourcemapIgnoreList: normalizeSourcemapIgnoreList(sourcemapIgnoreList),
// TODO(sapphi-red): support parallel plugins
plugins: [],
banner: getAddon(opts, 'banner'),
Expand Down

0 comments on commit 9795d0a

Please sign in to comment.