Skip to content

Commit

Permalink
feat: support devtool in compiler options builder (#8940)
Browse files Browse the repository at this point in the history
feat: init
  • Loading branch information
h-a-n-a authored Jan 6, 2025
1 parent de06488 commit 1e9fc0e
Show file tree
Hide file tree
Showing 3 changed files with 348 additions and 78 deletions.
156 changes: 78 additions & 78 deletions crates/rspack_core/src/options/compiler_options_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use super::{
ByDependency, CacheOptions, ChunkLoading, ChunkLoadingType, CleanOptions, CompilerOptions,
Context, CrossOriginLoading, CssAutoGeneratorOptions, CssAutoParserOptions, CssExportsConvention,
CssGeneratorOptions, CssModuleGeneratorOptions, CssModuleParserOptions, CssParserOptions,
DynamicImportMode, EntryDescription, Environment, ExperimentCacheOptions, Experiments, Filename,
FilenameTemplate, GeneratorOptions, GeneratorOptionsMap, JavascriptParserOptions,
Devtool, DynamicImportMode, EntryDescription, Environment, ExperimentCacheOptions, Experiments,
Filename, FilenameTemplate, GeneratorOptions, GeneratorOptionsMap, JavascriptParserOptions,
JavascriptParserOrder, JavascriptParserUrl, LibraryName, LibraryNonUmdObject, LibraryOptions,
Mode, ModuleNoParseRules, ModuleOptions, ModuleRule, ModuleRuleEffect, OutputOptions,
ParserOptions, ParserOptionsMap, PathInfo, PublicPath, Resolve, RspackFuture, RuleSetCondition,
Expand All @@ -24,6 +24,12 @@ macro_rules! d {
}};
}

macro_rules! w {
($o:expr, $v:expr) => {{
$o.get_or_insert($v)
}};
}

macro_rules! f {
($o:expr, $v:expr) => {{
$o.unwrap_or_else($v)
Expand All @@ -38,6 +44,8 @@ pub struct CompilerOptionsBuilder {
context: Option<Context>,
cache: Option<CacheOptions>,
mode: Option<Mode>,
devtool: Option<Devtool>,
profile: Option<bool>,
bail: Option<bool>,
experiments: Option<ExperimentsBuilder>,
module: Option<ModuleOptionsBuilder>,
Expand Down Expand Up @@ -79,6 +87,11 @@ impl CompilerOptionsBuilder {
self
}

pub fn devtool(&mut self, devtool: Devtool) -> &mut Self {
self.devtool = Some(devtool);
self
}

pub fn mode(&mut self, mode: Mode) -> &mut Self {
self.mode = Some(mode);
self
Expand All @@ -89,6 +102,11 @@ impl CompilerOptionsBuilder {
self
}

pub fn profile(&mut self, profile: bool) -> &mut Self {
self.profile = Some(profile);
self
}

pub fn module<V>(&mut self, module: V) -> &mut Self
where
V: Into<ModuleOptionsBuilder>,
Expand All @@ -115,7 +133,7 @@ impl CompilerOptionsBuilder {

pub fn build(&mut self) -> CompilerOptions {
let name = self.name.take();
let context = self.context.take().unwrap_or_else(|| {
let context = f!(self.context.take(), || {
std::env::current_dir()
.expect("`current_dir` should be available")
.assert_utf8()
Expand All @@ -124,13 +142,22 @@ impl CompilerOptionsBuilder {

// TODO: support browserlist default target
let target = f!(self.target.take(), || vec!["web".to_string()]);

let target_properties = get_targets_properties(&target, &context);

let development = matches!(self.mode, Some(Mode::Development));
let production = matches!(self.mode, Some(Mode::Production) | None);
let mode = d!(self.mode.take(), Mode::Production);

let bail = self.bail.unwrap_or(false);
// TODO: support entry
let _devtool = f!(self.devtool.take(), || {
if development {
Devtool::Eval
} else {
Devtool::False
}
});
let profile = d!(self.profile.take(), false);
let bail = d!(self.bail.take(), false);
let cache = d!(self.cache.take(), {
if development {
CacheOptions::Memory
Expand All @@ -139,31 +166,39 @@ impl CompilerOptionsBuilder {
}
});

let mut experiments = self.apply_experiments(development, production);
let mut experiments_builder = f!(self.experiments.take(), Experiments::builder);
let mut experiments = experiments_builder.build(development, production);
// Disable experiments cache if global cache is set to `Disabled`
if matches!(cache, CacheOptions::Disabled) {
experiments.cache = ExperimentCacheOptions::Disabled;
}

// TODO: support css
let css = true;
// TODO: support async web assembly
let async_web_assembly = false;
// TODO: support experiment output module
let output_module = Some(false);

let module = self.apply_module(async_web_assembly, css, Some(&target_properties));
let async_web_assembly = experiments_builder
.async_web_assembly
.expect("should apply default value");
let css = experiments_builder.css.expect("should apply default value");
let future_defaults = experiments_builder
.future_defaults
.expect("should apply default value");
let output_module = experiments_builder
.output_module
.expect("should apply default value");

let module = f!(self.module.take(), ModuleOptions::builder).build(
async_web_assembly,
css,
&target_properties,
);

// TODO: options
let entry = self.entry.clone();
let output = self.apply_output(
context.clone(),
let is_affected_by_browserslist = target.iter().any(|t| t.starts_with("browserslist"));
let output = f!(self.output.take(), OutputOptions::builder).build(
&context,
output_module,
Some(&target_properties),
target.iter().any(|t| t.starts_with("browserslist")),
is_affected_by_browserslist,
development,
&entry,
false,
&self.entry,
future_defaults,
);

CompilerOptions {
Expand All @@ -179,61 +214,12 @@ impl CompilerOptionsBuilder {
experiments,
node: Default::default(),
optimization: Default::default(),
profile: Default::default(),
profile,
amd: None,
bail,
__references: Default::default(),
}
}

fn apply_module(
&mut self,
async_web_assembly: bool,
css: bool,
target_properties: Option<&TargetProperties>,
) -> ModuleOptions {
self
.module
.take()
.unwrap_or_else(ModuleOptions::builder)
.build(async_web_assembly, css, target_properties)
}

#[allow(clippy::too_many_arguments)]
fn apply_output(
&mut self,
context: Context,
output_module: Option<bool>,
target_properties: Option<&TargetProperties>,
is_affected_by_browserslist: bool,
development: bool,
entry: &IndexMap<String, EntryDescription>,
future_defaults: bool,
) -> OutputOptions {
self
.output
.take()
.unwrap_or_else(OutputOptions::builder)
.build(
context.clone(),
output_module,
target_properties,
is_affected_by_browserslist,
development,
entry,
future_defaults,
)
}

fn apply_experiments(&mut self, development: bool, production: bool) -> Experiments {
self
.experiments
.take()
.unwrap_or_else(Experiments::builder)
.build(development, production)
}

// fn apply_output()
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -286,7 +272,7 @@ impl ModuleOptionsBuilder {
&mut self,
async_web_assembly: bool,
css: bool,
target_properties: Option<&TargetProperties>,
target_properties: &TargetProperties,
) -> ModuleOptions {
let parser = self.parser.get_or_insert(ParserOptionsMap::default());

Expand Down Expand Up @@ -344,7 +330,7 @@ impl ModuleOptionsBuilder {
parser.insert("css/module".to_string(), css_module_parser_options);

// CSS generator options
let exports_only = target_properties.map_or(true, |t| !t.document());
let exports_only = !target_properties.document();

generator.insert(
"css".to_string(),
Expand Down Expand Up @@ -926,18 +912,17 @@ impl OutputOptionsBuilder {
self
}

#[allow(clippy::too_many_arguments)]
#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
pub fn build(
&mut self,
context: Context,
output_module: Option<bool>,
context: &Context,
output_module: bool,
target_properties: Option<&TargetProperties>,
is_affected_by_browserslist: bool,
development: bool,
_entry: &IndexMap<String, EntryDescription>,
_future_defaults: bool,
) -> OutputOptions {
let output_module = output_module.unwrap_or(false);
let tp = target_properties;

let path = f!(self.path.take(), || { context.as_path().join("dist") });
Expand Down Expand Up @@ -1321,6 +1306,10 @@ pub struct ExperimentsBuilder {
top_level_await: Option<bool>,
rspack_future: Option<RspackFuture>,
cache: Option<ExperimentCacheOptions>,

// Builder specific
output_module: Option<bool>,
future_defaults: Option<bool>,
css: Option<bool>,
async_web_assembly: Option<bool>,
}
Expand Down Expand Up @@ -1351,6 +1340,11 @@ impl ExperimentsBuilder {
self
}

pub fn future_defaults(&mut self, future_defaults: bool) -> &mut Self {
self.future_defaults = Some(future_defaults);
self
}

pub fn css(&mut self, css: bool) -> &mut Self {
self.css = Some(css);
self
Expand All @@ -1371,14 +1365,20 @@ impl ExperimentsBuilder {
}
});
let top_level_await = d!(self.top_level_await, true);
let rspack_future = d!(self.rspack_future.take(), RspackFuture {});
let cache = f!(self.cache.take(), || {
if development {
ExperimentCacheOptions::Memory
} else {
ExperimentCacheOptions::Disabled
}
});
let rspack_future = d!(self.rspack_future.take(), RspackFuture {});

// Builder specific
let future_defaults = w!(self.future_defaults, false);
w!(self.css, *future_defaults);
w!(self.async_web_assembly, *future_defaults);
w!(self.output_module, false);

Experiments {
layers,
Expand Down
Loading

2 comments on commit 1e9fc0e

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 1e9fc0e Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2025-01-06 f81f1c7) Current Change
10000_big_production-mode_disable-minimize + exec 38 s ± 659 ms 38.3 s ± 813 ms +0.79 %
10000_development-mode + exec 1.91 s ± 39 ms 1.85 s ± 19 ms -3.20 %
10000_development-mode_hmr + exec 688 ms ± 25 ms 678 ms ± 5.3 ms -1.50 %
10000_production-mode + exec 2.51 s ± 29 ms 2.51 s ± 84 ms +0.02 %
arco-pro_development-mode + exec 1.78 s ± 139 ms 1.74 s ± 83 ms -2.73 %
arco-pro_development-mode_hmr + exec 378 ms ± 4.1 ms 378 ms ± 3.8 ms +0.14 %
arco-pro_production-mode + exec 3.61 s ± 65 ms 3.54 s ± 79 ms -1.89 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.68 s ± 100 ms 3.62 s ± 89 ms -1.63 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.64 s ± 80 ms 3.58 s ± 41 ms -1.69 %
threejs_development-mode_10x + exec 1.5 s ± 15 ms 1.52 s ± 16 ms +1.42 %
threejs_development-mode_10x_hmr + exec 771 ms ± 14 ms 784 ms ± 16 ms +1.75 %
threejs_production-mode_10x + exec 5.36 s ± 68 ms 5.39 s ± 88 ms +0.62 %
10000_big_production-mode_disable-minimize + rss memory 9499 MiB ± 279 MiB 9568 MiB ± 204 MiB +0.73 %
10000_development-mode + rss memory 666 MiB ± 20.7 MiB 680 MiB ± 18.8 MiB +2.04 %
10000_development-mode_hmr + rss memory 1433 MiB ± 386 MiB 1350 MiB ± 332 MiB -5.77 %
10000_production-mode + rss memory 623 MiB ± 22.8 MiB 633 MiB ± 21.4 MiB +1.64 %
arco-pro_development-mode + rss memory 567 MiB ± 29.5 MiB 552 MiB ± 39.8 MiB -2.58 %
arco-pro_development-mode_hmr + rss memory 625 MiB ± 64.7 MiB 571 MiB ± 91.2 MiB -8.63 %
arco-pro_production-mode + rss memory 722 MiB ± 54.8 MiB 710 MiB ± 50.5 MiB -1.55 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 712 MiB ± 54.5 MiB 701 MiB ± 45.6 MiB -1.56 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 725 MiB ± 62.4 MiB 696 MiB ± 44.7 MiB -3.98 %
threejs_development-mode_10x + rss memory 574 MiB ± 11.9 MiB 564 MiB ± 34.7 MiB -1.78 %
threejs_development-mode_10x_hmr + rss memory 1141 MiB ± 70.6 MiB 1115 MiB ± 197 MiB -2.29 %
threejs_production-mode_10x + rss memory 842 MiB ± 37.9 MiB 855 MiB ± 43.5 MiB +1.56 %

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 1e9fc0e Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ecosystem CI detail: Open

suite result
modernjs ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ❌ failure
rsdoctor ❌ failure
examples ❌ failure
devserver ✅ success
nuxt ✅ success

Please sign in to comment.