Skip to content

Commit

Permalink
perf: on-demand sourcemap generation
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a committed May 6, 2024
1 parent 80d05de commit 49d6521
Show file tree
Hide file tree
Showing 72 changed files with 128 additions and 95 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/rspack_binding_options/src/options/raw_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ impl BuiltinPlugin {
plugins.push(
SourceMapDevToolModuleOptionsPlugin::new(SourceMapDevToolModuleOptionsPluginOptions {
module: options.module,
cheap: !options.columns,
})
.boxed(),
);
Expand All @@ -366,6 +367,7 @@ impl BuiltinPlugin {
plugins.push(
SourceMapDevToolModuleOptionsPlugin::new(SourceMapDevToolModuleOptionsPluginOptions {
module: options.module,
cheap: !options.columns,
})
.boxed(),
);
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/compiler/make/repair/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Task<MakeTaskContext> for BuildTask {
resolver_factory: resolver_factory.clone(),
module: module.identifier(),
module_context: module.as_normal_module().and_then(|m| m.get_context()),
module_source_map_kind: module.get_source_map_kind().clone(),
module_source_map_kind: *module.get_source_map_kind(),
plugin_driver: plugin_driver.clone(),
cache: cache.clone(),
},
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ impl ConcatenatedModule {
diagnostics: Mutex::new(vec![]),
cached_hash: OnceCell::default(),
build_info: None,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl ContextModule {
factory_meta: None,
build_info: None,
build_meta: None,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl ExternalModule {
factory_meta: None,
build_info: None,
build_meta: None,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl NormalModule {
build_meta: None,
parsed: false,

source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
}
}

Expand Down Expand Up @@ -507,7 +507,7 @@ impl Module for NormalModule {
module_parser_options: self.parser_options.as_ref(),
module_type: &self.module_type,
module_user_request: &self.user_request,
module_source_map_kind: self.get_source_map_kind().clone(),
module_source_map_kind: *self.get_source_map_kind(),
loaders: &self.loaders,
resource_data: &self.resource_data,
compiler_options: build_context.compiler_options,
Expand Down Expand Up @@ -754,7 +754,7 @@ impl NormalModule {
return Ok(RawSource::Buffer(content.into_bytes()).boxed());
}
let source_map_kind = self.get_source_map_kind();
if !matches!(source_map_kind, SourceMapKind::None)
if source_map_kind.enabled()
&& let Some(source_map) = source_map
{
let content = content.into_string_lossy();
Expand All @@ -767,7 +767,7 @@ impl NormalModule {
.boxed(),
);
}
if !matches!(source_map_kind, SourceMapKind::None)
if source_map_kind.enabled()
&& let Content::String(content) = content
{
return Ok(OriginalSource::new(content, self.request()).boxed());
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/raw_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl RawModule {
factory_meta: None,
build_info: None,
build_meta: None,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/self_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl SelfModule {
factory_meta: None,
build_info: None,
build_meta: None,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/rspack_loader_swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ impl Loader<LoaderRunnerContext> for SwcLoader {
swc_options
};

let source_map_kind: &SourceMapKind = match swc_options.config.source_maps {
Some(SourceMapsConfig::Bool(false)) => &SourceMapKind::None,
_ => &loader_context.context.module_source_map_kind,
let source_map_kind: SourceMapKind = match swc_options.config.source_maps {
Some(SourceMapsConfig::Bool(false)) => SourceMapKind::empty(),
_ => loader_context.context.module_source_map_kind,
};

let source = content.try_into_string()?;
Expand Down Expand Up @@ -122,9 +122,9 @@ impl Loader<LoaderRunnerContext> for SwcLoader {
.as_ref()
.map(|v| matches!(v, OutputCharset::Ascii)),
source_map_config: SourceMapConfig {
enable: !matches!(source_map_kind, SourceMapKind::None),
inline_sources_content: true,
emit_columns: matches!(source_map_kind, SourceMapKind::SourceMap),
enable: source_map_kind.source_map(),
inline_sources_content: source_map_kind.source_map(),
emit_columns: !source_map_kind.cheap(),
names: Default::default(),
},
inline_script: Some(false),
Expand Down
9 changes: 4 additions & 5 deletions crates/rspack_plugin_css/src/parser_and_generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use rspack_core::{
};
use rspack_core::{ModuleInitFragments, RuntimeGlobals};
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_util::source_map::SourceMapKind;
use rustc_hash::FxHashSet;
use swc_core::{
css::{parser::parser::ParserConfig, visit::VisitWith},
Expand Down Expand Up @@ -154,9 +153,9 @@ impl ParserAndGenerator for CssParserAndGenerator {
let (code, map) = swc_compiler.codegen(
&stylesheet,
SwcCssSourceMapGenConfig {
enable: !matches!(module_source_map_kind, SourceMapKind::None),
inline_sources_content: false,
emit_columns: matches!(module_source_map_kind, SourceMapKind::SourceMap),
enable: module_source_map_kind.enabled(),
inline_sources_content: module_source_map_kind.source_map(),
emit_columns: !module_source_map_kind.cheap(),
},
)?;
source_code = code;
Expand Down Expand Up @@ -249,7 +248,7 @@ impl ParserAndGenerator for CssParserAndGenerator {
dependencies
};

let new_source = if !matches!(module_source_map_kind, SourceMapKind::None) {
let new_source = if module_source_map_kind.enabled() {
if let Some(source_map) = source_map {
SourceMapSource::new(SourceMapSourceOptions {
value: source_code,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_css/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Default for CssLoadingRuntimeModule {
Self {
id: Identifier::from("webpack/runtime/css_loading"),
chunk: None,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
custom_source: None,
}
}
Expand Down
10 changes: 9 additions & 1 deletion crates/rspack_plugin_devtool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,17 +941,19 @@ impl Plugin for EvalSourceMapDevToolPlugin {

pub struct SourceMapDevToolModuleOptionsPluginOptions {
pub module: bool,
pub cheap: bool,
}

#[plugin]
#[derive(Debug)]
pub struct SourceMapDevToolModuleOptionsPlugin {
module: bool,
cheap: bool,
}

impl SourceMapDevToolModuleOptionsPlugin {
pub fn new(options: SourceMapDevToolModuleOptionsPluginOptions) -> Self {
Self::new_inner(options.module)
Self::new_inner(options.module, options.cheap)
}
}

Expand All @@ -962,6 +964,9 @@ async fn build_module(&self, module: &mut BoxModule) -> Result<()> {
} else {
module.set_source_map_kind(SourceMapKind::SimpleSourceMap);
}
if self.cheap {
module.set_source_map_kind(*module.get_source_map_kind() | SourceMapKind::Cheap)
}
Ok(())
}

Expand All @@ -980,6 +985,9 @@ async fn runtime_module(
} else {
module.set_source_map_kind(SourceMapKind::SimpleSourceMap);
}
if self.cheap {
module.set_source_map_kind(*module.get_source_map_kind() | SourceMapKind::Cheap)
}
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_extract_css/src/css_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl CssModule {
factory_meta: None,
build_info: None,
build_meta: None,
source_map_kind: rspack_util::source_map::SourceMapKind::None,
source_map_kind: rspack_util::source_map::SourceMapKind::empty(),
identifier__,
filepath: dep.filepath,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_extract_css/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl CssLoadingRuntimeModule {
insert,
loading,
hmr,
source_map_kind: rspack_util::source_map::SourceMapKind::None,
source_map_kind: rspack_util::source_map::SourceMapKind::empty(),
custom_source: None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_hmr/src/hot_module_replacement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Default for HotModuleReplacementRuntimeModule {
fn default() -> Self {
Self {
id: Identifier::from("webpack/runtime/hot_module_replacement"),
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
custom_source: None,
}
}
Expand Down
12 changes: 4 additions & 8 deletions crates/rspack_plugin_javascript/src/ast/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ impl CodegenOptions {
pub fn new(source_map_kind: &SourceMapKind, keep_comments: Option<bool>) -> Self {
Self {
source_map_config: SourceMapConfig {
enable: !matches!(source_map_kind, SourceMapKind::None),
inline_sources_content: true,
emit_columns: matches!(source_map_kind, SourceMapKind::SourceMap),
enable: source_map_kind.enabled(),
inline_sources_content: source_map_kind.source_map(),
emit_columns: !source_map_kind.cheap(),
names: Default::default(),
},
keep_comments,
Expand Down Expand Up @@ -92,11 +92,7 @@ pub fn print(
source_map.clone(),
"\n",
&mut buf,
if source_map_config.enable {
Some(&mut src_map_buf)
} else {
None
},
source_map_config.enable.then_some(&mut src_map_buf),
)) as Box<dyn WriteJs>;

if minify {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use rspack_core::{
};
use rspack_error::miette::Diagnostic;
use rspack_error::{DiagnosticExt, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_util::source_map::SourceMapKind;
use swc_core::common::{Span, SyntaxContext};
use swc_core::ecma::parser::{EsConfig, Syntax};

Expand Down Expand Up @@ -106,8 +105,8 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator {
allow_super_outside_method: true,
..Default::default()
});
let use_source_map = matches!(module_source_map_kind, SourceMapKind::SourceMap);
let enable_source_map = !matches!(module_source_map_kind, SourceMapKind::None);
let use_source_map = module_source_map_kind.source_map();
let enable_source_map = module_source_map_kind.enabled();
let original_map = source.map(&MapOptions::new(use_source_map));
let source = source.source();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ impl ModuleConcatenationPlugin {
resolver_factory: compilation.resolver_factory.clone(),
module: new_module.id(),
module_context: None,
module_source_map_kind: rspack_util::source_map::SourceMapKind::None,
module_source_map_kind: rspack_util::source_map::SourceMapKind::empty(),
cache: compilation.cache.clone(),
plugin_driver: compilation.plugin_driver.clone(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl ContainerEntryModule {
factory_meta: None,
build_info: None,
build_meta: None,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
enhanced,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl ExposeRuntimeModule {
Self {
id: Identifier::from("webpack/runtime/initialize_exposes"),
chunk: None,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
custom_source: None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_mf/src/container/fallback_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl FallbackModule {
factory_meta: None,
build_info: None,
build_meta: None,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Default for FederationRuntimeModule {
Self {
id: Identifier::from("module_federation/runtime"),
chunk: None,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
custom_source: None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_mf/src/container/remote_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl RemoteModule {
factory_meta: None,
build_info: None,
build_meta: None,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl RemoteRuntimeModule {
id: Identifier::from("webpack/runtime/remotes_loading"),
chunk: None,
enhanced,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
custom_source: None,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl ConsumeSharedModule {
factory_meta: None,
build_info: None,
build_meta: None,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl ConsumeSharedRuntimeModule {
id: Identifier::from("webpack/runtime/consumes_loading"),
chunk: None,
enhanced,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
custom_source: None,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl ProvideSharedModule {
factory_meta: None,
build_info: None,
build_meta: None,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl ShareRuntimeModule {
id: Identifier::from("webpack/runtime/sharing"),
chunk: None,
enhanced,
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
custom_source: None,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Default for AsyncRuntimeModule {
fn default() -> Self {
AsyncRuntimeModule {
id: Identifier::from("webpack/runtime/async_module"),
source_map_kind: SourceMapKind::None,
source_map_kind: SourceMapKind::empty(),
custom_source: None,
}
}
Expand Down
Loading

0 comments on commit 49d6521

Please sign in to comment.