diff --git a/crates/rspack_plugin_split_chunks/src/plugin/chunk.rs b/crates/rspack_plugin_split_chunks/src/plugin/chunk.rs index 0bbdc0b6cf91..6d6e10f5452e 100644 --- a/crates/rspack_plugin_split_chunks/src/plugin/chunk.rs +++ b/crates/rspack_plugin_split_chunks/src/plugin/chunk.rs @@ -167,6 +167,15 @@ impl SplitChunksPlugin { compilation: &mut Compilation, ) { for module_identifier in &item.modules { + if let Some(module) = compilation.module_by_identifier(module_identifier) { + if module + .chunk_condition(&new_chunk, compilation) + .is_some_and(|condition| !condition) + { + continue; + } + } + // First, we remove modules from old chunks // Remove module from old chunks diff --git a/crates/rspack_plugin_split_chunks/src/plugin/max_size.rs b/crates/rspack_plugin_split_chunks/src/plugin/max_size.rs index 4e9d4c213470..13bfb08ade0f 100644 --- a/crates/rspack_plugin_split_chunks/src/plugin/max_size.rs +++ b/crates/rspack_plugin_split_chunks/src/plugin/max_size.rs @@ -374,6 +374,7 @@ impl SplitChunksPlugin { .chunk_by_ukey .get_many_mut([&new_chunk_ukey, &old_chunk]) .expect("split_from_original_chunks failed"); + let new_part_ukey = new_part.ukey(); chunk.split(new_part, &mut compilation.chunk_group_by_ukey); if let Some(mutations) = compilation.incremental.mutations_write() { mutations.add(Mutation::ChunkSplit { @@ -383,12 +384,21 @@ impl SplitChunksPlugin { } group.nodes.iter().for_each(|module| { - compilation.chunk_graph.add_chunk(new_part.ukey()); + compilation.chunk_graph.add_chunk(new_part_ukey); + + if let Some(module) = compilation.module_by_identifier(&module.module) { + if module + .chunk_condition(&new_part_ukey, compilation) + .is_some_and(|condition| !condition) + { + return; + } + } // Add module to new chunk compilation .chunk_graph - .connect_chunk_and_module(new_part.ukey(), module.module); + .connect_chunk_and_module(new_part_ukey, module.module); // Remove module from used chunks compilation .chunk_graph diff --git a/packages/rspack-test-tools/tests/configCases/split-chunks/external-modules/external.js b/packages/rspack-test-tools/tests/configCases/split-chunks/external-modules/external.js new file mode 100644 index 000000000000..5925027dad68 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/split-chunks/external-modules/external.js @@ -0,0 +1,3 @@ +const external = 'external'; + +export default external; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/split-chunks/external-modules/index.js b/packages/rspack-test-tools/tests/configCases/split-chunks/external-modules/index.js new file mode 100644 index 000000000000..bb365655a325 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/split-chunks/external-modules/index.js @@ -0,0 +1,3 @@ +import external from './external'; + +external; diff --git a/packages/rspack-test-tools/tests/configCases/split-chunks/external-modules/rspack.config.js b/packages/rspack-test-tools/tests/configCases/split-chunks/external-modules/rspack.config.js new file mode 100644 index 000000000000..938c766a3d03 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/split-chunks/external-modules/rspack.config.js @@ -0,0 +1,50 @@ +const rspack = require("@rspack/core"); + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + entry: { + main: { + import: ["./index"], + library: { + type: "system" + }, + } + }, + node: { + __dirname: false, + __filename: false, + }, + target: "web", + output: { + filename: "[name].js" + }, + optimization: { + sideEffects: false, + concatenateModules: false, + splitChunks: { + cacheGroups: { + default: { + chunks: "all", + minSize: 0, + maxSize: 0, + } + } + } + }, + plugins: [ + new rspack.ExternalsPlugin("system", ({ request }, callback) => { + if (request === './external') { + callback(null, "system " + request); + return; + } + callback(); + }), + compiler => { + compiler.hooks.afterEmit.tap("PLUGIN", compilation => { + const stats = compilation.getStats().toJson(); + const entryChunk = stats.chunks.find(chunk => chunk.entry); + expect(entryChunk.modules[0].name).toBe('external "./external"'); + }); + } + ] +};