diff --git a/Cargo.lock b/Cargo.lock index 45049d5..cfb8e97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -116,7 +116,7 @@ dependencies = [ "argh_shared", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -143,7 +143,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -154,7 +154,7 @@ checksum = "5fd55a5ba1179988837d24ab4c7cc8ed6efdeff578ede0416b4225a5fca35bd0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -177,7 +177,7 @@ checksum = "a66537f1bb974b254c98ed142ff995236e81b9d0fe4db0575f46612cb15eb0f9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -278,6 +278,7 @@ dependencies = [ "async-trait", "better_scoped_tls 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "derivative", + "futures-util", "glob", "loader_compilation", "napi", @@ -287,6 +288,7 @@ dependencies = [ "rspack_binding_options", "rspack_core", "rspack_error", + "rspack_hash", "rspack_identifier", "rspack_ids", "rspack_loader_react_refresh", @@ -670,7 +672,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37e366bff8cd32dd8754b0991fb66b279dc48f598c3a18914852a6673deef583" dependencies = [ "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -828,9 +830,9 @@ checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b" [[package]] name = "dyn-clone" -version = "1.0.14" +version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23d2f3407d9a573d666de4b5bdf10569d73ca9478087346697dcbae6244bfbcd" +checksum = "545b22097d44f8a9581187cdf93de7a71e4722bf51200cfaba810865b49a495d" [[package]] name = "either" @@ -861,7 +863,7 @@ checksum = "eecf8589574ce9b895052fa12d69af7a233f99e6107f5cb8dd1044f2a17bfdcb" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -872,9 +874,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860" +checksum = "7c18ee0ed65a5f1f81cac6b1d213b69c35fa47d4252ad41f1486dbd8226fe36e" dependencies = [ "libc", "windows-sys 0.48.0", @@ -909,7 +911,7 @@ dependencies = [ "pmutil", "proc-macro2", "swc_macros_common", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -974,7 +976,7 @@ checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -1028,9 +1030,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.10" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" +checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f" dependencies = [ "cfg-if", "js-sys", @@ -1221,9 +1223,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.0.2" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" +checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" dependencies = [ "equivalent", "hashbrown 0.14.2", @@ -1276,7 +1278,7 @@ dependencies = [ "pmutil", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -1313,9 +1315,9 @@ checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "js-sys" -version = "0.3.64" +version = "0.3.65" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5f195fe497f702db0f318b07fdd68edb16955aed830df8363d837542f8f935a" +checksum = "54c0c35952f67de54bb584e9fd912b3023117cbafc0a77d8f3dee1fb5f572fe8" dependencies = [ "wasm-bindgen", ] @@ -1416,9 +1418,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.149" +version = "0.2.150" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" +checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" [[package]] name = "libloading" @@ -1447,9 +1449,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.4.10" +version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f" +checksum = "969488b55f8ac402214f3f5fd243ebb7206cf82de60d3172994707a4bcc2b829" [[package]] name = "loader_compilation" @@ -1851,7 +1853,7 @@ checksum = "9657cba6ac0894a8acf3aca5b9a4b7668ce8486042588cf2bf0f34eb5f37ebc6" dependencies = [ "dashmap", "dunce", - "indexmap 2.0.2", + "indexmap 2.1.0", "once_cell", "rustc-hash", "serde", @@ -1982,7 +1984,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -2003,7 +2005,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9" dependencies = [ "fixedbitset", - "indexmap 2.0.2", + "indexmap 2.1.0", "serde", "serde_derive", ] @@ -2069,7 +2071,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -2123,6 +2125,17 @@ dependencies = [ "tracing", ] +[[package]] +name = "plugin_specilize_module_name" +version = "0.1.0" +dependencies = [ + "async-trait", + "rspack_core", + "rspack_error", + "rspack_testing", + "tracing", +] + [[package]] name = "pmutil" version = "0.6.1" @@ -2131,7 +2144,7 @@ checksum = "52a40bc70c2c58040d2d8b167ba9a5ff59fc9dab7ad44771cfde3dcfde7a09c6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -3469,9 +3482,9 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.190" +version = "1.0.192" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91d3c334ca1ee894a2c6f6ad698fe8c435b76d504b13d436f0685d648d6d96f7" +checksum = "bca2a08484b285dcb282d0f67b26cadc0df8b19f8c12502c13d966bf9482f001" dependencies = [ "serde_derive", ] @@ -3499,13 +3512,13 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.190" +version = "1.0.192" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67c5609f394e5c2bd7fc51efda478004ea80ef42fee983d5c67a65e34f32c0e3" +checksum = "d6c7207fbec9faa48073f3e3074cbe553af6ea512d7c21ba46e434e70ea9fbc1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -3521,11 +3534,11 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.107" +version = "1.0.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" +checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" dependencies = [ - "indexmap 2.0.2", + "indexmap 2.1.0", "itoa", "ryu", "serde", @@ -3689,7 +3702,7 @@ dependencies = [ "pmutil", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -3700,9 +3713,9 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "str_indices" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8eeaedde8e50d8a331578c9fa9a288df146ce5e16173ad26ce82f6e263e2be4" +checksum = "e9557cb6521e8d009c51a8666f09356f4b817ba9ba0981a305bd86aee47bd35c" [[package]] name = "string_cache" @@ -3739,7 +3752,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -3916,7 +3929,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -3990,7 +4003,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -4134,7 +4147,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -4280,7 +4293,7 @@ dependencies = [ "swc_ecma_ast", "swc_ecma_parser", "swc_macros_common", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -4371,7 +4384,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -4554,7 +4567,7 @@ dependencies = [ "pmutil", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -4626,7 +4639,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -4700,7 +4713,7 @@ dependencies = [ "pmutil", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -4738,7 +4751,7 @@ source = "git+https://github.com/swc-project/swc.git?rev=5c00525#5c005256d640270 dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -4760,7 +4773,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -4776,9 +4789,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.38" +version = "2.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" +checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" dependencies = [ "proc-macro2", "quote", @@ -4824,7 +4837,7 @@ dependencies = [ "quote", "regex", "relative-path", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -4864,7 +4877,7 @@ checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -4972,7 +4985,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -4994,7 +5007,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] [[package]] @@ -5223,9 +5236,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.87" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342" +checksum = "7daec296f25a1bae309c0cd5c29c4b260e510e6d813c286b19eaadf409d40fce" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -5233,24 +5246,24 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.87" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd" +checksum = "e397f4664c0e4e428e8313a469aaa58310d302159845980fd23b0f22a847f217" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-macro" -version = "0.2.87" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d" +checksum = "5961017b3b08ad5f3fe39f1e79877f8ee7c23c5e5fd5eb80de95abc41f1f16b2" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -5258,22 +5271,22 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.87" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" +checksum = "c5353b8dab669f5e10f5bd76df26a9360c748f054f862ff5f3f8aae0c7fb3907" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.87" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" +checksum = "0d046c5d029ba91a1ed14da14dca44b68bf2f124cfbaf741c54151fdb3e0750b" [[package]] name = "wasmparser" @@ -5287,9 +5300,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.64" +version = "0.3.65" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b" +checksum = "5db499c5f66323272151db0e666cd34f78617522fb0c1604d31a27c50c206a85" dependencies = [ "js-sys", "wasm-bindgen", @@ -5508,20 +5521,20 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.7.20" +version = "0.7.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd66a62464e3ffd4e37bd09950c2b9dd6c4f8767380fabba0d523f9a775bc85a" +checksum = "8cd369a67c0edfef15010f980c3cbe45d7f651deac2cd67ce097cd801de16557" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.7.20" +version = "0.7.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "255c4596d41e6916ced49cfafea18727b24d67878fa180ddfd69b9df34fd1726" +checksum = "c2f140bda219a26ccc0cdb03dba58af72590c53b22642577d88a927bc5c87d6b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.39", ] diff --git a/crates/binding_options/Cargo.toml b/crates/binding_options/Cargo.toml index 6f13b5a..2a21109 100644 --- a/crates/binding_options/Cargo.toml +++ b/crates/binding_options/Cargo.toml @@ -41,10 +41,12 @@ rspack_plugin_swc_js_minimizer = { path = "../.rspack_crates/rspack_plu rspack_plugin_wasm = { path = "../.rspack_crates/rspack_plugin_wasm" } rspack_plugin_worker = { path = "../.rspack_crates/rspack_plugin_worker" } rspack_regex = { path = "../.rspack_crates/rspack_regex" } +rspack_hash = { path = "../.rspack_crates/rspack_hash" } rspack_swc_visitors = { path = "../.rspack_crates/rspack_swc_visitors" } loader_compilation = { path = "../loader_compilation" } -plugin_manifest = { path = "../plugin_manifest" } +plugin_manifest = { path = "../plugin_manifest" } +futures-util = { workspace = true } anyhow = { workspace = true, features = ["backtrace"] } async-trait = { workspace = true } better_scoped_tls = { workspace = true } diff --git a/crates/binding_options/src/options/mod.rs b/crates/binding_options/src/options/mod.rs index 786396a..b5db707 100644 --- a/crates/binding_options/src/options/mod.rs +++ b/crates/binding_options/src/options/mod.rs @@ -13,9 +13,11 @@ use rspack_plugin_javascript::{FlagDependencyExportsPlugin, FlagDependencyUsageP use serde::Deserialize; mod raw_module; +mod raw_features; mod js_loader; pub use raw_module::*; +pub use raw_features::*; pub use js_loader::*; scoped_tls!(pub(crate) static IS_ENABLE_NEW_SPLIT_CHUNKS: bool); @@ -45,6 +47,7 @@ pub struct RSPackRawOptions { pub node: Option, pub profile: bool, pub builtins: RawBuiltins, + pub features: RawFeatures, } impl RawOptionsApply for RSPackRawOptions { @@ -74,9 +77,19 @@ impl RawOptionsApply for RSPackRawOptions { new_split_chunks: self.experiments.new_split_chunks, rspack_future: self.experiments.rspack_future.into(), }; - let optimization = IS_ENABLE_NEW_SPLIT_CHUNKS.set(&experiments.new_split_chunks, || { - self.optimization.apply(plugins) - })?; + let optimization; + if self.features.split_chunks_strategy.is_some() { + let split_chunk_strategy = SplitChunksStrategy::new( + self.features.split_chunks_strategy.unwrap(), + self.optimization, + ); + 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) + })?; + } + let stats = self.stats.into(); let snapshot = self.snapshot.into(); let node = self.node.map(|n| n.into()); diff --git a/crates/binding_options/src/options/raw_features.rs b/crates/binding_options/src/options/raw_features.rs new file mode 100644 index 0000000..ceec742 --- /dev/null +++ b/crates/binding_options/src/options/raw_features.rs @@ -0,0 +1,199 @@ +use std::{sync::Arc, hash::Hasher}; +use futures_util::{FutureExt, future}; +use napi_derive::napi; +use serde::Deserialize; +use rspack_core::{ + Optimization, PluginExt, SideEffectOption, UsedExportsOption, SourceType, + BoxPlugin, Module, ModuleType, +}; +use rspack_error::internal_error; +use rspack_ids::{ + DeterministicChunkIdsPlugin, DeterministicModuleIdsPlugin, NamedChunkIdsPlugin, + NamedModuleIdsPlugin, +}; +use rspack_binding_options::RawOptimizationOptions; +use rspack_plugin_split_chunks_new::{PluginOptions, CacheGroup}; +use rspack_regex::RspackRegex; +use rspack_hash::{RspackHash, HashFunction, HashDigest}; + +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, +} + +fn get_modules_size(module: &dyn Module) -> f64 { + let mut size = 0f64; + for source_type in module.source_types() { + size += module.size(source_type); + } + size +} + +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| { + size + .map(|size| SplitChunkSizes::with_initial_value(&default_size_types, size)) + .unwrap_or_else(SplitChunkSizes::default) + }; + + let re_node_modules = RspackRegex::new("node_modules").unwrap(); + + let cache_groups = vec![ + CacheGroup { + key: String::from("framework"), + name: rspack_plugin_split_chunks_new::create_chunk_name_getter_by_const_name("framework".to_string()), + chunk_filter: rspack_plugin_split_chunks_new::create_all_chunk_filter(), + priority: 40.0, + test: Arc::new(move |module: &dyn Module| -> bool { + module + .name_for_condition() + .map_or(false, |name| { + strategy.top_level_frameworks.iter().any(|framework| name.starts_with(framework)) + }) + }), + max_initial_requests: 25, + max_async_requests: 25, + reuse_existing_chunk: true, + min_chunks: 1, + // When enfoce is true, all size should be set to SplitChunkSizes::empty(). + min_size: SplitChunkSizes::empty(), + max_async_size: SplitChunkSizes::empty(), + max_initial_size: SplitChunkSizes::empty(), + id_hint: String::from("framework"), + r#type: rspack_plugin_split_chunks_new::create_default_module_type_filter(), + }, + CacheGroup { + key: String::from("lib"), + name: Arc::new(move |module| { + let mut hash = RspackHash::new(&HashFunction::Xxhash64); + match module.module_type() { + ModuleType::Css | ModuleType::CssModule | ModuleType::CssAuto => { + module.update_hash(&mut hash); + }, + _ => { + let options = rspack_core::LibIdentOptions { context: &context }; + let lib_ident = module.lib_ident(options); + hash.write(lib_ident.unwrap().as_bytes()); + }, + } + future::ready(Some(hash.digest(&HashDigest::Hex).rendered(8).to_string())).boxed() + }), + chunk_filter: rspack_plugin_split_chunks_new::create_all_chunk_filter(), + test: Arc::new(move |module: &dyn Module| -> bool { + module + .name_for_condition() + .map_or(false, |name| re_node_modules.test(&name)) + && get_modules_size(module) > 160000.0 + }), + priority: 30.0, + min_chunks: 1, + reuse_existing_chunk: true, + max_initial_requests: 25, + max_async_requests: 25, + min_size: create_sizes(Some(20000.0)), + max_async_size: SplitChunkSizes::default(), + max_initial_size: SplitChunkSizes::default(), + id_hint: String::from("lib"), + r#type: rspack_plugin_split_chunks_new::create_default_module_type_filter(), + }, + ]; + + PluginOptions { + cache_groups, + fallback_cache_group: rspack_plugin_split_chunks_new::FallbackCacheGroup { + chunks_filter: rspack_plugin_split_chunks_new::create_all_chunk_filter(), + min_size: SplitChunkSizes::default(), + max_async_size: SplitChunkSizes::default(), + max_initial_size: SplitChunkSizes::default(), + }, + } +} + + +pub trait FeatureApply { + type Options; + fn apply(self, plugins: &mut Vec, context: String) -> Result; +} + +impl SplitChunksStrategy { + pub fn new(strategy: RawStrategyOptions, option: RawOptimizationOptions) -> 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, + } + } +} + +impl FeatureApply for SplitChunksStrategy { + type Options = Optimization; + + 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, 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(Self::Options { + 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()), + }) + } +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +#[napi(object)] +pub struct RawStrategyOptions { + pub name: String, + pub top_level_frameworks: Vec, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +#[napi(object)] +pub struct RawFeatures { + pub split_chunks_strategy: Option, +}