From b0a780ca5e87f16338d3c3e6e8815301a2d16ae0 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Wed, 3 Jan 2024 20:00:18 +0800 Subject: [PATCH] fix: bump rspack verison to 0.4.5 (#28) --- .github/actions/clone-crates/action.yml | 2 +- Cargo.lock | 28 ++++ Cargo.toml | 4 +- crates/binding_options/src/options/mod.rs | 124 ++++-------------- .../src/options/raw_features.rs | 45 +------ .../src/options/raw_optimization.rs | 61 ++------- crates/node_binding/src/hook.rs | 2 + crates/node_binding/src/lib.rs | 8 +- crates/node_binding/src/plugins/mod.rs | 27 +++- 9 files changed, 101 insertions(+), 200 deletions(-) diff --git a/.github/actions/clone-crates/action.yml b/.github/actions/clone-crates/action.yml index 1eba130..815eaa1 100644 --- a/.github/actions/clone-crates/action.yml +++ b/.github/actions/clone-crates/action.yml @@ -12,7 +12,7 @@ inputs: required: false type: string ref: - default: 'v0.4.3' + default: 'v0.4.5' required: false type: string temp: diff --git a/Cargo.lock b/Cargo.lock index 1d95c25..e3ef610 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -804,6 +804,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "digest" version = "0.10.7" @@ -2241,6 +2247,16 @@ dependencies = [ "tracing", ] +[[package]] +name = "pretty_assertions" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -2729,12 +2745,14 @@ dependencies = [ "hashlink", "indexmap 1.9.3", "itertools", + "json", "mime_guess", "nodejs-resolver", "once_cell", "oxc_resolver", "paste", "petgraph", + "pretty_assertions", "rayon", "regex", "rkyv", @@ -2787,6 +2805,7 @@ dependencies = [ "rspack_binding_options", "rspack_core", "rspack_fs", + "rspack_identifier", "rspack_sources", "rspack_testing", "rspack_tracing", @@ -3166,6 +3185,7 @@ dependencies = [ "bitflags 1.3.2", "either", "indexmap 1.9.3", + "itertools", "linked_hash_set", "num-bigint", "once_cell", @@ -3181,6 +3201,7 @@ dependencies = [ "rspack_regex", "rspack_swc_visitors", "rspack_testing", + "rspack_util", "rustc-hash", "serde_json", "sourcemap", @@ -3538,6 +3559,7 @@ dependencies = [ "once_cell", "regex", "sugar_path", + "swc_core", ] [[package]] @@ -6018,6 +6040,12 @@ dependencies = [ "linked-hash-map", ] +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" + [[package]] name = "zerocopy" version = "0.7.25" diff --git a/Cargo.toml b/Cargo.toml index 1072ab8..837f834 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -81,10 +81,12 @@ tikv-jemallocator = { version = "=0.5.4", features = ["disable_initial_exec_tl codegen-units = 16 # debug build will cause runtime panic if codegen-unints is default debug = 2 incremental = true +panic = "abort" [profile.release] codegen-units = 1 debug = false -lto = false # disabled by now, because it will significantly increase our compile time. +lto = false # disabled by now, because it will significantly increase our compile time. opt-level = 3 +panic = "abort" strip = true diff --git a/crates/binding_options/src/options/mod.rs b/crates/binding_options/src/options/mod.rs index c4e21ad..09764c7 100644 --- a/crates/binding_options/src/options/mod.rs +++ b/crates/binding_options/src/options/mod.rs @@ -1,19 +1,15 @@ use napi_derive::napi; use rspack_core::{ - BoxPlugin, CompilerOptions, Context, DevServerOptions, Devtool, Experiments, IncrementalRebuild, - IncrementalRebuildMakeState, MangleExportsOption, ModuleOptions, ModuleType, OutputOptions, - PluginExt, TreeShaking, Optimization, -}; -use rspack_plugin_javascript::{ - FlagDependencyExportsPlugin, FlagDependencyUsagePlugin, MangleExportsPlugin, - SideEffectsFlagPlugin, + CompilerOptions, Context, DevServerOptions, Devtool, Experiments, IncrementalRebuild, + IncrementalRebuildMakeState, ModuleOptions, Target, OutputOptions, TreeShaking, Optimization, }; + use serde::Deserialize; use rspack_binding_options::{ - RawBuiltins, RawCacheOptions, RawContext, RawDevServer, RawDevtool, RawExperiments, - RawMode, RawNodeOption, RawOutputOptions, RawResolveOptions, RawOptionsApply, - RawSnapshotOptions, RawStatsOptions, RawTarget, RawModuleOptions, + RawBuiltins, RawCacheOptions, RawDevServer, RawExperiments, + RawMode, RawNodeOption, RawOutputOptions, RawResolveOptions, + RawSnapshotOptions, RawStatsOptions, RawModuleOptions, }; mod raw_module; @@ -32,16 +28,13 @@ pub use raw_optimization::*; pub struct RSPackRawOptions { #[napi(ts_type = "undefined | 'production' | 'development' | 'none'")] pub mode: Option, - #[napi(ts_type = "Array")] - pub target: RawTarget, - #[napi(ts_type = "string")] - pub context: RawContext, + pub target: Vec, + pub context: String, pub output: RawOutputOptions, pub resolve: RawResolveOptions, pub resolve_loader: RawResolveOptions, pub module: RawModuleOptions, - #[napi(ts_type = "string")] - pub devtool: RawDevtool, + pub devtool: String, pub optimization: RspackRawOptimizationOptions, pub stats: RawStatsOptions, pub dev_server: RawDevServer, @@ -50,25 +43,26 @@ pub struct RSPackRawOptions { pub experiments: RawExperiments, pub node: Option, pub profile: bool, + pub bail: bool, pub builtins: RawBuiltins, pub features: RawFeatures, } -impl RawOptionsApply for RSPackRawOptions { - type Options = CompilerOptions; - - fn apply(self, plugins: &mut Vec) -> Result { +impl RSPackRawOptions { + pub fn apply( + self, + plugins: &mut Vec, + ) -> rspack_error::Result { let context: Context = self.context.into(); - let output: OutputOptions = self.output.apply(plugins)?; + let output: OutputOptions = self.output.try_into()?; let resolve = self.resolve.try_into()?; let resolve_loader = self.resolve_loader.try_into()?; let devtool: Devtool = self.devtool.into(); let mode = self.mode.unwrap_or_default().into(); - let module: ModuleOptions = self.module.apply(plugins)?; - let target = self.target.apply(plugins)?; + let module: ModuleOptions = self.module.try_into()?; + let target = Target::new(&self.target)?; let cache = self.cache.into(); let experiments = Experiments { - lazy_compilation: self.experiments.lazy_compilation, incremental_rebuild: IncrementalRebuild { make: self .experiments @@ -77,7 +71,6 @@ impl RawOptionsApply for RSPackRawOptions { .then(IncrementalRebuildMakeState::default), emit_asset: self.experiments.incremental_rebuild.emit_asset, }, - async_web_assembly: self.experiments.async_web_assembly, new_split_chunks: self.experiments.new_split_chunks, top_level_await: self.experiments.top_level_await, rspack_future: self.experiments.rspack_future.into(), @@ -90,92 +83,24 @@ impl RawOptionsApply for RSPackRawOptions { ); optimization = split_chunk_strategy.apply(plugins, context.to_string())?; } else { - optimization = IS_ENABLE_NEW_SPLIT_CHUNKS.set(&experiments.new_split_chunks, || { - self.optimization.apply(plugins) - })?; + optimization = IS_ENABLE_NEW_SPLIT_CHUNKS + .set(&experiments.new_split_chunks, || { + self.optimization.try_into() + })?; } - let stats = self.stats.into(); let snapshot = self.snapshot.into(); let node = self.node.map(|n| n.into()); let dev_server: DevServerOptions = self.dev_server.into(); - plugins.push(rspack_plugin_schemes::DataUriPlugin.boxed()); - plugins.push(rspack_plugin_schemes::FileUriPlugin.boxed()); - - plugins.push( - rspack_plugin_asset::AssetPlugin::new(rspack_plugin_asset::AssetConfig { - parse_options: module - .parser - .as_ref() - .and_then(|x| x.get(&ModuleType::Asset)) - .and_then(|x| x.get_asset(&ModuleType::Asset).cloned()), - }) - .boxed(), - ); - plugins.push(rspack_plugin_json::JsonPlugin {}.boxed()); - plugins.push(rspack_plugin_runtime::RuntimePlugin {}.boxed()); - if experiments.lazy_compilation { - plugins.push(rspack_plugin_runtime::LazyCompilationPlugin {}.boxed()); - } - if experiments.async_web_assembly { - plugins.push(rspack_plugin_wasm::AsyncWasmPlugin::new().boxed()); - } - plugins.push(rspack_plugin_javascript::JsPlugin::new().boxed()); - plugins.push(rspack_plugin_javascript::InferAsyncModulesPlugin {}.boxed()); - - if devtool.source_map() { - plugins.push( - rspack_plugin_devtool::DevtoolPlugin::new(rspack_plugin_devtool::DevtoolPluginOptions { - inline: devtool.inline(), - append: !devtool.hidden(), - namespace: output.unique_name.clone(), - columns: !devtool.cheap(), - no_sources: devtool.no_sources(), - public_path: None, - }) - .boxed(), - ); - } - - if experiments.rspack_future.new_treeshaking { - if optimization.side_effects.is_enable() { - plugins.push(SideEffectsFlagPlugin::default().boxed()); - } - if optimization.provided_exports { - plugins.push(FlagDependencyExportsPlugin::default().boxed()); - } - if optimization.used_exports.is_enable() { - plugins.push(FlagDependencyUsagePlugin::default().boxed()); - } - } - if optimization.mangle_exports.is_enable() { - // We already know mangle_exports != false - plugins.push( - MangleExportsPlugin::new(!matches!( - optimization.mangle_exports, - MangleExportsOption::Size - )) - .boxed(), - ); - } - - // Notice the plugin need to be placed after SplitChunksPlugin - if optimization.remove_empty_chunks { - plugins.push(rspack_plugin_remove_empty_chunks::RemoveEmptyChunksPlugin.boxed()); - } - - plugins.push(rspack_plugin_ensure_chunk_conditions::EnsureChunkConditionsPlugin.boxed()); - - plugins.push(rspack_plugin_warn_sensitive_module::WarnCaseSensitiveModulesPlugin.boxed()); // Add custom plugins. - plugins.push(plugin_manifest::ManifestPlugin::new().boxed()); + plugins.push(Box::new(plugin_manifest::ManifestPlugin::new())); let mut builtins = self.builtins.apply(plugins)?; if experiments.rspack_future.new_treeshaking { builtins.tree_shaking = TreeShaking::False; } - Ok(Self::Options { + Ok(CompilerOptions { context, mode, module, @@ -192,6 +117,7 @@ impl RawOptionsApply for RSPackRawOptions { node, dev_server, profile: self.profile, + bail: self.bail, builtins, }) } diff --git a/crates/binding_options/src/options/raw_features.rs b/crates/binding_options/src/options/raw_features.rs index c57735d..8eedb9f 100644 --- a/crates/binding_options/src/options/raw_features.rs +++ b/crates/binding_options/src/options/raw_features.rs @@ -5,31 +5,19 @@ use rspack_core::{ Optimization, PluginExt, SideEffectOption, UsedExportsOption, SourceType, BoxPlugin, Module, ModuleType, MangleExportsOption, Filename, }; -use rspack_error::internal_error; -use rspack_ids::{ - DeterministicChunkIdsPlugin, DeterministicModuleIdsPlugin, NamedChunkIdsPlugin, - NamedModuleIdsPlugin, -}; use crate::RspackRawOptimizationOptions; use rspack_plugin_split_chunks_new::{PluginOptions, CacheGroup, CacheGroupTest, CacheGroupTestFnCtx, ChunkNameGetter}; use rspack_regex::RspackRegex; use rspack_hash::{RspackHash, HashFunction, HashDigest}; -use rspack_binding_options::RawSplitChunksOptions; pub struct SplitChunksStrategy { strategy: RawStrategyOptions, - // Get the neccessary options from RawOptimizationOptions. - chunk_ids: String, - module_ids: String, side_effects: String, used_exports: String, provided_exports: bool, - real_content_hash: bool, - remove_empty_chunks: bool, remove_available_modules: bool, inner_graph: bool, mangle_exports: String, - split_chunks: Option } fn get_modules_size(module: &dyn Module) -> f64 { @@ -40,7 +28,7 @@ fn get_modules_size(module: &dyn Module) -> f64 { size } -fn get_plugin_options(strategy: RawStrategyOptions, split_chunks: Option, context: String) -> rspack_plugin_split_chunks_new::PluginOptions { +fn get_plugin_options(strategy: RawStrategyOptions, context: String) -> rspack_plugin_split_chunks_new::PluginOptions { use rspack_plugin_split_chunks_new::SplitChunkSizes; let default_size_types = [SourceType::JavaScript, SourceType::Unknown]; let create_sizes = |size: Option| { @@ -141,17 +129,12 @@ impl SplitChunksStrategy { pub fn new(strategy: RawStrategyOptions, option: RspackRawOptimizationOptions) -> Self { Self { strategy, - chunk_ids: option.chunk_ids, - module_ids: option.module_ids, remove_available_modules: option.remove_available_modules, - remove_empty_chunks: option.remove_empty_chunks, side_effects: option.side_effects, used_exports: option.used_exports, provided_exports: option.provided_exports, - real_content_hash: option.real_content_hash, inner_graph: option.inner_graph, mangle_exports: option.mangle_exports, - split_chunks: option.split_chunks, } } } @@ -161,36 +144,12 @@ impl FeatureApply for SplitChunksStrategy { fn apply(self, plugins: &mut Vec>, context: String) -> Result { let split_chunks_plugin = rspack_plugin_split_chunks_new::SplitChunksPlugin::new( - get_plugin_options(self.strategy, self.split_chunks, context), + get_plugin_options(self.strategy, context), ).boxed(); plugins.push(split_chunks_plugin); - let chunk_ids_plugin = match self.chunk_ids.as_ref() { - "named" => NamedChunkIdsPlugin::new(None, None).boxed(), - "deterministic" => DeterministicChunkIdsPlugin::default().boxed(), - _ => { - return Err(internal_error!( - "'chunk_ids' should be 'named' or 'deterministic'." - )) - } - }; - plugins.push(chunk_ids_plugin); - let module_ids_plugin = match self.module_ids.as_ref() { - "named" => NamedModuleIdsPlugin::default().boxed(), - "deterministic" => DeterministicModuleIdsPlugin::default().boxed(), - _ => { - return Err(internal_error!( - "'module_ids' should be 'named' or 'deterministic'." - )) - } - }; - plugins.push(module_ids_plugin); - if self.real_content_hash { - plugins.push(rspack_plugin_real_content_hash::RealContentHashPlugin.boxed()); - } Ok(Optimization { remove_available_modules: self.remove_available_modules, - remove_empty_chunks: self.remove_empty_chunks, side_effects: SideEffectOption::from(self.side_effects.as_str()), provided_exports: self.provided_exports, used_exports: UsedExportsOption::from(self.used_exports.as_str()), diff --git a/crates/binding_options/src/options/raw_optimization.rs b/crates/binding_options/src/options/raw_optimization.rs index 8943f4d..5fcbf48 100644 --- a/crates/binding_options/src/options/raw_optimization.rs +++ b/crates/binding_options/src/options/raw_optimization.rs @@ -1,74 +1,33 @@ use better_scoped_tls::scoped_tls; use napi_derive::napi; -use rspack_core::{ - MangleExportsOption, Optimization, PluginExt, SideEffectOption, UsedExportsOption, -}; -use rspack_error::internal_error; -use rspack_ids::{ - DeterministicChunkIdsPlugin, DeterministicModuleIdsPlugin, NamedChunkIdsPlugin, - NamedModuleIdsPlugin, -}; +use rspack_core::{MangleExportsOption, Optimization, SideEffectOption, UsedExportsOption}; use serde::Deserialize; -use rspack_binding_options::{RawOptionsApply, RawSplitChunksOptions}; - scoped_tls!(pub(crate) static IS_ENABLE_NEW_SPLIT_CHUNKS: bool); #[derive(Deserialize, Debug, Default)] #[serde(rename_all = "camelCase")] #[napi(object)] pub struct RspackRawOptimizationOptions { - pub split_chunks: Option, - pub module_ids: String, - pub chunk_ids: String, pub remove_available_modules: bool, - pub remove_empty_chunks: bool, pub side_effects: String, pub used_exports: String, pub provided_exports: bool, pub inner_graph: bool, - pub real_content_hash: bool, pub mangle_exports: String, } -impl RawOptionsApply for RspackRawOptimizationOptions { - type Options = Optimization; +impl TryFrom for Optimization { + type Error = rspack_error::Error; - fn apply( - self, - plugins: &mut Vec>, - ) -> Result { - let chunk_ids_plugin = match self.chunk_ids.as_ref() { - "named" => NamedChunkIdsPlugin::new(None, None).boxed(), - "deterministic" => DeterministicChunkIdsPlugin::default().boxed(), - _ => { - return Err(internal_error!( - "'chunk_ids' should be 'named' or 'deterministic'." - )) - } - }; - plugins.push(chunk_ids_plugin); - let module_ids_plugin = match self.module_ids.as_ref() { - "named" => NamedModuleIdsPlugin::default().boxed(), - "deterministic" => DeterministicModuleIdsPlugin::default().boxed(), - _ => { - return Err(internal_error!( - "'module_ids' should be 'named' or 'deterministic'." - )) - } - }; - plugins.push(module_ids_plugin); - if self.real_content_hash { - plugins.push(rspack_plugin_real_content_hash::RealContentHashPlugin.boxed()); - } + fn try_from(value: RspackRawOptimizationOptions) -> rspack_error::Result { Ok(Optimization { - remove_available_modules: self.remove_available_modules, - remove_empty_chunks: self.remove_empty_chunks, - side_effects: SideEffectOption::from(self.side_effects.as_str()), - provided_exports: self.provided_exports, - used_exports: UsedExportsOption::from(self.used_exports.as_str()), - inner_graph: self.inner_graph, - mangle_exports: MangleExportsOption::from(self.mangle_exports.as_str()), + remove_available_modules: value.remove_available_modules, + side_effects: SideEffectOption::from(value.side_effects.as_str()), + provided_exports: value.provided_exports, + used_exports: UsedExportsOption::from(value.used_exports.as_str()), + inner_graph: value.inner_graph, + mangle_exports: MangleExportsOption::from(value.mangle_exports.as_str()), }) } } diff --git a/crates/node_binding/src/hook.rs b/crates/node_binding/src/hook.rs index 167ee40..578482b 100644 --- a/crates/node_binding/src/hook.rs +++ b/crates/node_binding/src/hook.rs @@ -33,6 +33,7 @@ pub enum Hook { AfterCompile, FinishModules, OptimizeModules, + AfterOptimizeModules, OptimizeTree, /// webpack `compilation.hooks.chunkAsset` ChunkAsset, @@ -77,6 +78,7 @@ impl From for Hook { "afterCompile" => Hook::AfterCompile, "finishModules" => Hook::FinishModules, "optimizeModules" => Hook::OptimizeModules, + "afterOptimizeModules" => Hook::AfterOptimizeModules, "optimizeTree" => Hook::OptimizeTree, "chunkAsset" => Hook::ChunkAsset, "normalModuleFactoryResolveForScheme" => Hook::NormalModuleFactoryResolveForScheme, diff --git a/crates/node_binding/src/lib.rs b/crates/node_binding/src/lib.rs index 5319ab4..e7c1068 100644 --- a/crates/node_binding/src/lib.rs +++ b/crates/node_binding/src/lib.rs @@ -74,13 +74,13 @@ impl Rspack { let disabled_hooks: DisabledHooks = Default::default(); let mut plugins = Vec::new(); - for bp in builtin_plugins { - bp.apply(&mut plugins) - .map_err(|e| Error::from_reason(format!("{e}")))?; - } if let Some(js_hooks) = js_hooks { plugins.push(JsHooksAdapter::from_js_hooks(env, js_hooks, disabled_hooks.clone())?.boxed()); } + for bp in builtin_plugins { + bp.append_to(&mut plugins) + .map_err(|e| Error::from_reason(format!("{e}")))?; + } let js_loader_runner: JsLoaderRunner = JsLoaderRunner::try_from(js_loader_runner)?; plugins.push(JsLoaderResolver { js_loader_runner }.boxed()); diff --git a/crates/node_binding/src/plugins/mod.rs b/crates/node_binding/src/plugins/mod.rs index dc54964..276d6f1 100644 --- a/crates/node_binding/src/plugins/mod.rs +++ b/crates/node_binding/src/plugins/mod.rs @@ -45,6 +45,7 @@ pub struct JsHooksAdapter { pub should_emit_tsfn: ThreadsafeFunction>, pub after_emit_tsfn: ThreadsafeFunction<(), ()>, pub optimize_modules_tsfn: ThreadsafeFunction, + pub after_optimize_modules_tsfn: ThreadsafeFunction, pub optimize_tree_tsfn: ThreadsafeFunction<(), ()>, pub optimize_chunk_modules_tsfn: ThreadsafeFunction, pub before_compile_tsfn: ThreadsafeFunction<(), ()>, @@ -72,7 +73,7 @@ impl Debug for JsHooksAdapter { #[async_trait] impl rspack_core::Plugin for JsHooksAdapter { fn name(&self) -> &'static str { - "rspack_plugin_js_hooks_adapter" + "rspack.JsHooksAdapterPlugin" } async fn compilation( @@ -531,6 +532,26 @@ impl rspack_core::Plugin for JsHooksAdapter { .unwrap_or_else(|err| panic!("Failed to call optimize modules: {err}")) } + async fn after_optimize_modules( + &self, + compilation: &mut rspack_core::Compilation, + ) -> rspack_error::Result<()> { + if self.is_hook_disabled(&Hook::AfterOptimizeModules) { + return Ok(()); + } + let compilation = JsCompilation::from_compilation(unsafe { + std::mem::transmute::<&'_ mut rspack_core::Compilation, &'static mut rspack_core::Compilation>( + compilation, + ) + }); + self + .after_optimize_modules_tsfn + .call(compilation, ThreadsafeFunctionCallMode::Blocking) + .into_rspack_result()? + .await + .unwrap_or_else(|err| panic!("Failed to call optimize modules: {err}")) + } + async fn optimize_tree( &self, _compilation: &mut rspack_core::Compilation, @@ -815,6 +836,7 @@ impl JsHooksAdapter { asset_emitted, after_emit, optimize_modules, + after_optimize_modules, optimize_tree, optimize_chunk_modules, before_resolve, @@ -877,6 +899,8 @@ impl JsHooksAdapter { let make_tsfn: ThreadsafeFunction<(), ()> = js_fn_into_threadsafe_fn!(make, env); let optimize_modules_tsfn: ThreadsafeFunction = js_fn_into_threadsafe_fn!(optimize_modules, env); + let after_optimize_modules_tsfn: ThreadsafeFunction = + js_fn_into_threadsafe_fn!(after_optimize_modules, env); let optimize_tree_tsfn: ThreadsafeFunction<(), ()> = js_fn_into_threadsafe_fn!(optimize_tree, env); let optimize_chunk_modules_tsfn: ThreadsafeFunction = @@ -936,6 +960,7 @@ impl JsHooksAdapter { asset_emitted_tsfn, after_emit_tsfn, optimize_modules_tsfn, + after_optimize_modules_tsfn, optimize_tree_tsfn, optimize_chunk_modules_tsfn, before_compile_tsfn,