Skip to content

Commit

Permalink
feat: improve compatibility with compiler options (#9067)
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a authored Jan 21, 2025
1 parent b6c0af8 commit 285e8e3
Show file tree
Hide file tree
Showing 9 changed files with 912 additions and 337 deletions.
311 changes: 311 additions & 0 deletions crates/rspack/src/builder/builder_context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,311 @@
use enum_tag::EnumTag;
use rspack_core::{
BoxPlugin, ChunkLoadingType, CompilerOptions, EntryOptions, ExternalItem, ExternalType,
LibraryType, PluginExt as _, WasmLoadingType,
};

/// Options of builtin plugins
///
/// The order of this list is strictly ordered with respect to `rspackOptionsApply`.
#[allow(clippy::enum_variant_names)]
#[derive(Debug, EnumTag)]
#[repr(u8)]
pub(super) enum BuiltinPluginOptions {
// External handling plugins
ExternalsPlugin((ExternalType, Vec<ExternalItem>)),
NodeTargetPlugin,
ElectronTargetPlugin(rspack_plugin_externals::ElectronTargetContext),
HttpExternalsRspackPlugin((bool /* css */, bool /* web_async */)),

// Chunk format and loading plugins
ChunkPrefetchPreloadPlugin,
CommonJsChunkFormatPlugin,
ArrayPushCallbackChunkFormatPlugin,
ModuleChunkFormatPlugin,
EnableChunkLoadingPlugin(ChunkLoadingType),
EnableWasmLoadingPlugin(WasmLoadingType),

// Runtime and error handling
RuntimeChunkPlugin(rspack_plugin_runtime_chunk::RuntimeChunkOptions),
NoEmitOnErrorsPlugin,

// DevTool plugins
SourceMapDevToolPlugin(rspack_plugin_devtool::SourceMapDevToolPluginOptions),
EvalSourceMapDevToolPlugin(rspack_plugin_devtool::SourceMapDevToolPluginOptions),
EvalDevToolModulePlugin(rspack_plugin_devtool::EvalDevToolModulePluginOptions),

// Core module plugins
JavascriptModulesPlugin,
JsonModulesPlugin,
AssetModulesPlugin,
AsyncWebAssemblyModulesPlugin,
CssModulesPlugin,

// Entry and runtime plugins
EntryPlugin((String /* entry request */, EntryOptions)),
RuntimePlugin,
// TODO: add bundler info plugin
// BundlerInfoRspackPlugin,

// Core functionality plugins
InferAsyncModulesPlugin,
APIPlugin,
DataUriPlugin,
FileUriPlugin,

// Optimization plugins
EnsureChunkConditionsPlugin,
MergeDuplicateChunksPlugin,
SideEffectsFlagPlugin,
FlagDependencyExportsPlugin,
FlagDependencyUsagePlugin(bool),
ModuleConcatenationPlugin,
MangleExportsPlugin(bool),

// Experiments
// TODO: support lazy compilation
// LazyCompilationPlugin,

// Output plugins
EnableLibraryPlugin(LibraryType),
// TODO: support split chunks
// SplitChunksPlugin,
RemoveEmptyChunksPlugin,
RealContentHashPlugin,

// Module and chunk ID plugins
NamedModuleIdsPlugin,
NaturalModuleIdsPlugin,
DeterministicModuleIdsPlugin,
NaturalChunkIdsPlugin,
NamedChunkIdsPlugin,
DeterministicChunkIdsPlugin,
OccurrenceChunkIdsPlugin(rspack_ids::OccurrenceChunkIdsPluginOptions),

// Define and optimization plugins
DefinePlugin(rspack_plugin_javascript::define_plugin::DefineValue),
AnyMinimizerRspackPlugin(BoxPlugin),

// TODO: support performance
// SizeLimitsPlugin,

// Cache plugins
// MemoryCachePlugin,

// Worker plugins
WorkerPlugin,
}

/// Context used to build plugins
#[derive(Default, Debug)]
pub struct BuilderContext {
pub(super) plugins: Vec<BuiltinPluginOptions>,
}

impl BuilderContext {
/// Take plugins from the context.
///
/// The plugins are sorted by their tag.
pub fn take_plugins(&mut self, compiler_options: &CompilerOptions) -> Vec<BoxPlugin> {
self.plugins.sort_by_key(|p| p.tag());
let mut plugins = Vec::new();
self.plugins.drain(..).for_each(|plugin| match plugin {
// External handling plugins
BuiltinPluginOptions::ExternalsPlugin((external_type, externals)) => {
plugins
.push(rspack_plugin_externals::ExternalsPlugin::new(external_type, externals).boxed());
}
BuiltinPluginOptions::NodeTargetPlugin => {
plugins.push(rspack_plugin_externals::node_target_plugin())
}
BuiltinPluginOptions::ElectronTargetPlugin(context) => {
rspack_plugin_externals::electron_target_plugin(context, &mut plugins)
}
BuiltinPluginOptions::HttpExternalsRspackPlugin((css, web_async)) => {
plugins.push(rspack_plugin_externals::http_externals_rspack_plugin(
css, web_async,
));
}

// Chunk format and loading plugins
BuiltinPluginOptions::ChunkPrefetchPreloadPlugin => {
plugins.push(rspack_plugin_runtime::ChunkPrefetchPreloadPlugin::default().boxed());
}
BuiltinPluginOptions::CommonJsChunkFormatPlugin => {
plugins.push(rspack_plugin_runtime::CommonJsChunkFormatPlugin::default().boxed());
}
BuiltinPluginOptions::ArrayPushCallbackChunkFormatPlugin => {
plugins.push(rspack_plugin_runtime::ArrayPushCallbackChunkFormatPlugin::default().boxed());
}
BuiltinPluginOptions::ModuleChunkFormatPlugin => {
plugins.push(rspack_plugin_runtime::ModuleChunkFormatPlugin::default().boxed());
}
BuiltinPluginOptions::EnableChunkLoadingPlugin(chunk_loading_type) => {
rspack_plugin_runtime::enable_chunk_loading_plugin(chunk_loading_type, &mut plugins);
}
BuiltinPluginOptions::EnableWasmLoadingPlugin(wasm_loading_type) => {
plugins.push(rspack_plugin_wasm::enable_wasm_loading_plugin(
wasm_loading_type,
));
}

// Runtime and error handling plugins
BuiltinPluginOptions::RuntimeChunkPlugin(options) => {
plugins.push(rspack_plugin_runtime_chunk::RuntimeChunkPlugin::new(options).boxed());
}
BuiltinPluginOptions::NoEmitOnErrorsPlugin => {
plugins.push(rspack_plugin_no_emit_on_errors::NoEmitOnErrorsPlugin::default().boxed());
}

// DevTool plugins
BuiltinPluginOptions::SourceMapDevToolPlugin(options) => {
plugins.push(rspack_plugin_devtool::SourceMapDevToolPlugin::new(options).boxed());
}
BuiltinPluginOptions::EvalSourceMapDevToolPlugin(options) => {
plugins.push(rspack_plugin_devtool::EvalSourceMapDevToolPlugin::new(options).boxed());
}
BuiltinPluginOptions::EvalDevToolModulePlugin(options) => {
plugins.push(rspack_plugin_devtool::EvalDevToolModulePlugin::new(options).boxed());
}

// Core module plugins
BuiltinPluginOptions::JavascriptModulesPlugin => {
plugins.push(rspack_plugin_javascript::JsPlugin::default().boxed());
}
BuiltinPluginOptions::JsonModulesPlugin => {
plugins.push(rspack_plugin_json::JsonPlugin.boxed());
}
BuiltinPluginOptions::AssetModulesPlugin => {
plugins.push(rspack_plugin_asset::AssetPlugin::default().boxed());
}
BuiltinPluginOptions::AsyncWebAssemblyModulesPlugin => {
plugins.push(rspack_plugin_wasm::AsyncWasmPlugin::default().boxed());
}
BuiltinPluginOptions::CssModulesPlugin => {
plugins.push(rspack_plugin_css::CssPlugin::default().boxed());
}

// Entry and runtime plugins
BuiltinPluginOptions::EntryPlugin((entry_request, options)) => {
plugins.push(
rspack_plugin_entry::EntryPlugin::new(
compiler_options.context.clone(),
entry_request,
options,
)
.boxed(),
);
}
BuiltinPluginOptions::RuntimePlugin => {
plugins.push(rspack_plugin_runtime::RuntimePlugin::default().boxed())
}
// TODO: add bundler info plugin
// BuiltinPluginOptions::BundlerInfoRspackPlugin => {}

// Core functionality plugins
BuiltinPluginOptions::InferAsyncModulesPlugin => {
plugins.push(rspack_plugin_javascript::InferAsyncModulesPlugin::default().boxed())
}
BuiltinPluginOptions::APIPlugin => {
plugins.push(rspack_plugin_javascript::api_plugin::APIPlugin::default().boxed())
}
BuiltinPluginOptions::DataUriPlugin => {
plugins.push(rspack_plugin_schemes::DataUriPlugin::default().boxed());
}
BuiltinPluginOptions::FileUriPlugin => {
plugins.push(rspack_plugin_schemes::FileUriPlugin::default().boxed());
}

// Optimization plugins
BuiltinPluginOptions::EnsureChunkConditionsPlugin => {
plugins.push(
rspack_plugin_ensure_chunk_conditions::EnsureChunkConditionsPlugin::default().boxed(),
);
}
BuiltinPluginOptions::MergeDuplicateChunksPlugin => {
plugins.push(
rspack_plugin_merge_duplicate_chunks::MergeDuplicateChunksPlugin::default().boxed(),
);
}
BuiltinPluginOptions::SideEffectsFlagPlugin => {
plugins.push(rspack_plugin_javascript::SideEffectsFlagPlugin::default().boxed());
}
BuiltinPluginOptions::FlagDependencyExportsPlugin => {
plugins.push(rspack_plugin_javascript::FlagDependencyExportsPlugin::default().boxed());
}
BuiltinPluginOptions::FlagDependencyUsagePlugin(value) => {
plugins.push(rspack_plugin_javascript::FlagDependencyUsagePlugin::new(value).boxed())
}
BuiltinPluginOptions::ModuleConcatenationPlugin => {
plugins.push(rspack_plugin_javascript::ModuleConcatenationPlugin::default().boxed());
}
BuiltinPluginOptions::MangleExportsPlugin(value) => {
plugins.push(rspack_plugin_javascript::MangleExportsPlugin::new(value).boxed())
}

// Experiments
// TODO: support lazy compilation
// BuiltinPluginOptions::LazyCompilationPlugin => {
// plugins
// .push(rspack_plugin_lazy_compilation::plugin::LazyCompilationPlugin::default().boxed());
// }

// Output plugins
BuiltinPluginOptions::EnableLibraryPlugin(library_type) => {
rspack_plugin_library::enable_library_plugin(library_type, &mut plugins)
}
// BuiltinPluginOptions::SplitChunksPlugin => {
// plugins.push(rspack_plugin_split_chunks::SplitChunksPlugin::default().boxed())
// }
BuiltinPluginOptions::RemoveEmptyChunksPlugin => {
plugins.push(rspack_plugin_remove_empty_chunks::RemoveEmptyChunksPlugin::default().boxed())
}
BuiltinPluginOptions::RealContentHashPlugin => {
plugins.push(rspack_plugin_real_content_hash::RealContentHashPlugin::default().boxed())
}

// Module and chunk ID plugins
BuiltinPluginOptions::NamedModuleIdsPlugin => {
plugins.push(rspack_ids::NamedModuleIdsPlugin::default().boxed())
}
BuiltinPluginOptions::NaturalModuleIdsPlugin => {
plugins.push(rspack_ids::NaturalModuleIdsPlugin::default().boxed())
}
BuiltinPluginOptions::DeterministicModuleIdsPlugin => {
plugins.push(rspack_ids::DeterministicModuleIdsPlugin::default().boxed())
}
BuiltinPluginOptions::NaturalChunkIdsPlugin => {
plugins.push(rspack_ids::NaturalChunkIdsPlugin::default().boxed())
}
BuiltinPluginOptions::NamedChunkIdsPlugin => {
plugins.push(rspack_ids::NamedChunkIdsPlugin::new(None, None).boxed())
}
BuiltinPluginOptions::DeterministicChunkIdsPlugin => {
plugins.push(rspack_ids::DeterministicChunkIdsPlugin::default().boxed())
}
BuiltinPluginOptions::OccurrenceChunkIdsPlugin(options) => {
plugins.push(rspack_ids::OccurrenceChunkIdsPlugin::new(options).boxed())
}

// Define and optimization plugins
BuiltinPluginOptions::DefinePlugin(values) => {
plugins.push(rspack_plugin_javascript::define_plugin::DefinePlugin::new(values).boxed())
}
BuiltinPluginOptions::AnyMinimizerRspackPlugin(plugin) => plugins.push(plugin),

// TODO: support performance
// BuiltinPluginOptions::SizeLimitsPlugin => {
// plugins.push(rspack_plugin_size_limits::SizeLimitsPlugin::default().boxed())
// }

// Cache plugins
// BuiltinPluginOptions::MemoryCachePlugin => MemoryCachePlugin::default().boxed(),

// Worker plugins
BuiltinPluginOptions::WorkerPlugin => {
plugins.push(rspack_plugin_worker::WorkerPlugin::default().boxed())
}
});
plugins
}
}
27 changes: 27 additions & 0 deletions crates/rspack/src/builder/devtool.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,59 @@
use bitflags::bitflags;

/// Devtool options used to control the behavior of Source Map generation.
#[derive(Debug, PartialEq)]
pub enum Devtool {
/// Disable source map.
False,
/// Use eval to generate source map.
Eval,
/// Use cheap-source-map to generate source map.
CheapSourceMap,
/// Use cheap-module-source-map to generate source map.
CheapModuleSourceMap,
/// Use source-map to generate source map.
SourceMap,
/// Use inline-cheap-source-map to generate source map.
InlineCheapSourceMap,
/// Use inline-cheap-module-source-map to generate source map.
InlineCheapModuleSourceMap,
/// Use inline-source-map to generate source map.
InlineSourceMap,
/// Use inline-nosources-cheap-source-map to generate source map.
InlineNosourcesCheapSourceMap,
/// Use inline-nosources-cheap-module-source-map to generate source map.
InlineNosourcesCheapModuleSourceMap,
/// Use inline-nosources-source-map to generate source map.
InlineNosourcesSourceMap,
/// Use nosources-cheap-source-map to generate source map.
NosourcesCheapSourceMap,
/// Use nosources-cheap-module-source-map to generate source map.
NosourcesCheapModuleSourceMap,
/// Use nosources-source-map to generate source map.
NosourcesSourceMap,
/// Use hidden-nosources-cheap-source-map to generate source map.
HiddenNosourcesCheapSourceMap,
/// Use hidden-nosources-cheap-module-source-map to generate source map.
HiddenNosourcesCheapModuleSourceMap,
/// Use hidden-nosources-source-map to generate source map.
HiddenNosourcesSourceMap,
/// Use hidden-cheap-source-map to generate source map.
HiddenCheapSourceMap,
/// Use hidden-cheap-module-source-map to generate source map.
HiddenCheapModuleSourceMap,
/// Use hidden-source-map to generate source map.
HiddenSourceMap,
/// Use eval-cheap-source-map to generate source map.
EvalCheapSourceMap,
/// Use eval-cheap-module-source-map to generate source map.
EvalCheapModuleSourceMap,
/// Use eval-source-map to generate source map.
EvalSourceMap,
/// Use eval-nosources-cheap-source-map to generate source map.
EvalNosourcesCheapSourceMap,
/// Use eval-nosources-cheap-module-source-map to generate source map.
EvalNosourcesCheapModuleSourceMap,
/// Use eval-nosources-source-map to generate source map.
EvalNosourcesSourceMap,
}

Expand Down
Loading

2 comments on commit 285e8e3

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 285e8e3 Jan 21, 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-21 c06787b) Current Change
10000_big_production-mode_disable-minimize + exec 36.7 s ± 515 ms 37.9 s ± 1 s +3.26 %
10000_development-mode + exec 1.86 s ± 52 ms 1.85 s ± 100 ms -0.90 %
10000_development-mode_hmr + exec 681 ms ± 9.8 ms 690 ms ± 35 ms +1.26 %
10000_production-mode + exec 2.45 s ± 138 ms 2.37 s ± 31 ms -3.09 %
10000_production-mode_persistent-cold + exec 2.61 s ± 155 ms 2.55 s ± 65 ms -2.50 %
10000_production-mode_persistent-hot + exec 1.77 s ± 116 ms 1.72 s ± 35 ms -2.86 %
arco-pro_development-mode + exec 1.76 s ± 98 ms 1.77 s ± 147 ms +0.83 %
arco-pro_development-mode_hmr + exec 388 ms ± 2.3 ms 389 ms ± 3.5 ms +0.27 %
arco-pro_production-mode + exec 3.64 s ± 257 ms 3.62 s ± 111 ms -0.52 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.75 s ± 314 ms 3.77 s ± 270 ms +0.52 %
arco-pro_production-mode_persistent-cold + exec 3.8 s ± 165 ms 3.81 s ± 164 ms +0.16 %
arco-pro_production-mode_persistent-hot + exec 2.34 s ± 79 ms 2.47 s ± 122 ms +5.58 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.67 s ± 99 ms 3.71 s ± 176 ms +1.35 %
large-dyn-imports_development-mode + exec 2.11 s ± 29 ms 2.08 s ± 43 ms -1.18 %
large-dyn-imports_production-mode + exec 2.16 s ± 40 ms 2.14 s ± 31 ms -0.75 %
threejs_development-mode_10x + exec 1.53 s ± 22 ms 1.52 s ± 9.6 ms -0.24 %
threejs_development-mode_10x_hmr + exec 788 ms ± 39 ms 788 ms ± 10 ms -0.05 %
threejs_production-mode_10x + exec 5.25 s ± 61 ms 5.29 s ± 149 ms +0.70 %
threejs_production-mode_10x_persistent-cold + exec 5.35 s ± 65 ms 5.39 s ± 221 ms +0.84 %
threejs_production-mode_10x_persistent-hot + exec 4.5 s ± 44 ms 4.58 s ± 160 ms +1.65 %
10000_big_production-mode_disable-minimize + rss memory 8680 MiB ± 34.2 MiB 8684 MiB ± 42.3 MiB +0.04 %
10000_development-mode + rss memory 656 MiB ± 19.6 MiB 646 MiB ± 22.1 MiB -1.47 %
10000_development-mode_hmr + rss memory 1264 MiB ± 163 MiB 1249 MiB ± 185 MiB -1.22 %
10000_production-mode + rss memory 639 MiB ± 31 MiB 629 MiB ± 25.4 MiB -1.67 %
10000_production-mode_persistent-cold + rss memory 744 MiB ± 15.6 MiB 738 MiB ± 25.3 MiB -0.91 %
10000_production-mode_persistent-hot + rss memory 723 MiB ± 37.7 MiB 722 MiB ± 19.8 MiB -0.17 %
arco-pro_development-mode + rss memory 553 MiB ± 38.1 MiB 561 MiB ± 16.6 MiB +1.51 %
arco-pro_development-mode_hmr + rss memory 631 MiB ± 36 MiB 644 MiB ± 111 MiB +2.12 %
arco-pro_production-mode + rss memory 738 MiB ± 66.3 MiB 721 MiB ± 32.5 MiB -2.38 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 730 MiB ± 23.1 MiB 719 MiB ± 42.6 MiB -1.44 %
arco-pro_production-mode_persistent-cold + rss memory 844 MiB ± 36.6 MiB 837 MiB ± 41.2 MiB -0.78 %
arco-pro_production-mode_persistent-hot + rss memory 695 MiB ± 19.9 MiB 690 MiB ± 19 MiB -0.71 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 727 MiB ± 32.5 MiB 709 MiB ± 34.1 MiB -2.42 %
large-dyn-imports_development-mode + rss memory 642 MiB ± 4.5 MiB 640 MiB ± 3.39 MiB -0.29 %
large-dyn-imports_production-mode + rss memory 523 MiB ± 4.79 MiB 524 MiB ± 6.71 MiB +0.17 %
threejs_development-mode_10x + rss memory 551 MiB ± 32.9 MiB 551 MiB ± 18.5 MiB -0.02 %
threejs_development-mode_10x_hmr + rss memory 1145 MiB ± 125 MiB 1117 MiB ± 96.1 MiB -2.45 %
threejs_production-mode_10x + rss memory 865 MiB ± 36.9 MiB 855 MiB ± 58 MiB -1.20 %
threejs_production-mode_10x_persistent-cold + rss memory 968 MiB ± 136 MiB 976 MiB ± 73.2 MiB +0.81 %
threejs_production-mode_10x_persistent-hot + rss memory 870 MiB ± 25.8 MiB 858 MiB ± 36.9 MiB -1.39 %

Threshold exceeded: ["arco-pro_production-mode_persistent-hot + exec"]

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 285e8e3 Jan 21, 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 ❌ failure
rsbuild ✅ success
rsdoctor ✅ success
examples ✅ success
devserver ✅ success
nuxt ✅ success

Please sign in to comment.