diff --git a/crates/rspack_binding_options/src/plugins/buildtime_plugins.rs b/crates/rspack_binding_options/src/plugins/buildtime_plugins.rs index eee85f1aadbb..321cb6bdd2e7 100644 --- a/crates/rspack_binding_options/src/plugins/buildtime_plugins.rs +++ b/crates/rspack_binding_options/src/plugins/buildtime_plugins.rs @@ -1,15 +1,10 @@ -use rspack_core::{BoxPlugin, ChunkLoading, ChunkLoadingType, PluginExt}; +use rspack_core::{BoxPlugin, PluginExt}; use rspack_plugin_javascript::JsPlugin; -use rspack_plugin_runtime::{ - CommonJsChunkLoadingPlugin, RuntimePlugin, StartupChunkDependenciesPlugin, -}; +use rspack_plugin_runtime::RuntimePlugin; pub fn buildtime_plugins() -> Vec { vec![ RuntimePlugin::default().boxed(), JsPlugin::default().boxed(), - // StartupChunkDependenciesPlugin::new(ChunkLoading::Enable(ChunkLoadingType::Require), false) - // .boxed(), - // CommonJsChunkLoadingPlugin::new(false).boxed(), ] } diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index 3019109e1932..4e946cb4db3c 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -161,6 +161,7 @@ pub struct Compilation { diagnostics: Vec, logging: CompilationLogging, pub plugin_driver: SharedPluginDriver, + pub buildtime_plugin_driver: SharedPluginDriver, pub resolver_factory: Arc, pub loader_resolver_factory: Arc, pub named_chunks: HashMap, @@ -224,6 +225,7 @@ impl Compilation { pub fn new( options: Arc, plugin_driver: SharedPluginDriver, + buildtime_plugin_driver: SharedPluginDriver, resolver_factory: Arc, loader_resolver_factory: Arc, records: Option, @@ -257,6 +259,7 @@ impl Compilation { diagnostics: Default::default(), logging: Default::default(), plugin_driver, + buildtime_plugin_driver, resolver_factory, loader_resolver_factory, named_chunks: Default::default(), diff --git a/crates/rspack_core/src/compiler/hmr.rs b/crates/rspack_core/src/compiler/hmr.rs index 7923ba1eb3c2..e4a644879bb4 100644 --- a/crates/rspack_core/src/compiler/hmr.rs +++ b/crates/rspack_core/src/compiler/hmr.rs @@ -65,6 +65,7 @@ impl Compiler { let mut new_compilation = Compilation::new( self.options.clone(), self.plugin_driver.clone(), + self.buildtime_plugin_driver.clone(), self.resolver_factory.clone(), self.loader_resolver_factory.clone(), Some(records), diff --git a/crates/rspack_core/src/compiler/make/repair/mod.rs b/crates/rspack_core/src/compiler/make/repair/mod.rs index d2a590adc606..963c3d6eafdd 100644 --- a/crates/rspack_core/src/compiler/make/repair/mod.rs +++ b/crates/rspack_core/src/compiler/make/repair/mod.rs @@ -22,6 +22,7 @@ use crate::{ pub struct MakeTaskContext { // compilation info pub plugin_driver: SharedPluginDriver, + pub buildtime_plugin_driver: SharedPluginDriver, pub fs: Arc, pub compiler_options: Arc, pub resolver_factory: Arc, @@ -37,6 +38,7 @@ impl MakeTaskContext { pub fn new(compilation: &Compilation, artifact: MakeArtifact) -> Self { Self { plugin_driver: compilation.plugin_driver.clone(), + buildtime_plugin_driver: compilation.buildtime_plugin_driver.clone(), compiler_options: compilation.options.clone(), resolver_factory: compilation.resolver_factory.clone(), loader_resolver_factory: compilation.loader_resolver_factory.clone(), @@ -63,6 +65,7 @@ impl MakeTaskContext { let mut compilation = Compilation::new( self.compiler_options.clone(), self.plugin_driver.clone(), + self.buildtime_plugin_driver.clone(), self.resolver_factory.clone(), self.loader_resolver_factory.clone(), None, diff --git a/crates/rspack_core/src/compiler/mod.rs b/crates/rspack_core/src/compiler/mod.rs index cf914eadafcc..643080cd63ac 100644 --- a/crates/rspack_core/src/compiler/mod.rs +++ b/crates/rspack_core/src/compiler/mod.rs @@ -119,6 +119,7 @@ impl Compiler { compilation: Compilation::new( options, plugin_driver.clone(), + buildtime_plugin_driver.clone(), resolver_factory.clone(), loader_resolver_factory.clone(), None, @@ -159,6 +160,7 @@ impl Compiler { Compilation::new( self.options.clone(), self.plugin_driver.clone(), + self.buildtime_plugin_driver.clone(), self.resolver_factory.clone(), self.loader_resolver_factory.clone(), None, diff --git a/crates/rspack_core/src/compiler/module_executor/entry.rs b/crates/rspack_core/src/compiler/module_executor/entry.rs index 6c6cc3abe3d3..f1e70a36a431 100644 --- a/crates/rspack_core/src/compiler/module_executor/entry.rs +++ b/crates/rspack_core/src/compiler/module_executor/entry.rs @@ -52,7 +52,7 @@ impl Task for EntryTask { ) }) .clone(), - original_module_identifier: dep.original_module, + original_module_identifier: None, original_module_source: None, issuer: None, issuer_layer: None, diff --git a/crates/rspack_core/src/compiler/module_executor/execute.rs b/crates/rspack_core/src/compiler/module_executor/execute.rs index 86a811e52d37..363dd4b6f678 100644 --- a/crates/rspack_core/src/compiler/module_executor/execute.rs +++ b/crates/rspack_core/src/compiler/module_executor/execute.rs @@ -70,15 +70,16 @@ impl Task for ExecuteTask { } = *self; let mut compilation = context.transform_to_temp_compilation(); + let main_compilation_plugin_driver = compilation.plugin_driver.clone(); + compilation.plugin_driver = compilation.buildtime_plugin_driver.clone(); let id = EXECUTE_MODULE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed); let mg = compilation.get_module_graph_mut(); - let Some(entry_module_identifier) = mg.get_module_by_dependency_id(&entry_dep_id) else { - return Err(rspack_error::error!("entry module not found")); - }; - - let entry_module_identifier = entry_module_identifier.identifier(); + let entry_module_identifier = mg + .get_module_by_dependency_id(&entry_dep_id) + .expect("should have module") + .identifier(); let mut queue = vec![entry_module_identifier]; let mut modules = IdentifierSet::default(); @@ -110,7 +111,7 @@ impl Task for ExecuteTask { name: Some("build time".into()), runtime: Some("runtime".into()), chunk_loading: Some(crate::ChunkLoading::Disable), - async_chunks: None, + async_chunks: Some(false), public_path, base_uri, filename: None, @@ -211,8 +212,7 @@ impl Task for ExecuteTask { } let codegen_results = compilation.code_generation_results.clone(); - let exports = compilation - .plugin_driver + let exports = main_compilation_plugin_driver .compilation_hooks .execute_module .call( diff --git a/crates/rspack_core/src/compiler/module_executor/mod.rs b/crates/rspack_core/src/compiler/module_executor/mod.rs index 7837131ef472..5c4272324e41 100644 --- a/crates/rspack_core/src/compiler/module_executor/mod.rs +++ b/crates/rspack_core/src/compiler/module_executor/mod.rs @@ -182,7 +182,6 @@ impl ModuleExecutor { let dep = LoaderImportDependency::new( request.clone(), original_module_context.unwrap_or(Context::from("")), - original_module_identifier, ); let dep_id = *dep.id(); v.insert(dep_id); diff --git a/crates/rspack_core/src/dependency/loader_import.rs b/crates/rspack_core/src/dependency/loader_import.rs index 097ce9c305a9..014f6fa34cff 100644 --- a/crates/rspack_core/src/dependency/loader_import.rs +++ b/crates/rspack_core/src/dependency/loader_import.rs @@ -1,23 +1,21 @@ use super::AffectType; use crate::{ AsContextDependency, AsDependencyTemplate, Context, Dependency, DependencyCategory, DependencyId, - DependencyType, ModuleDependency, ModuleIdentifier, + DependencyType, ModuleDependency, }; #[derive(Debug, Hash, PartialEq, Eq, Clone)] pub struct LoaderImportDependency { id: DependencyId, pub context: Context, - pub original_module: Option, request: String, } impl LoaderImportDependency { - pub fn new(request: String, context: Context, original_module: Option) -> Self { + pub fn new(request: String, context: Context) -> Self { Self { request, context, - original_module, id: DependencyId::new(), } } @@ -31,6 +29,10 @@ impl Dependency for LoaderImportDependency { &self.id } + fn get_context(&self) -> Option<&Context> { + Some(&self.context) + } + fn category(&self) -> &DependencyCategory { &DependencyCategory::LoaderImport } diff --git a/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/a.js b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/a.js new file mode 100644 index 000000000000..59bcdf64ff1c --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/a.js @@ -0,0 +1,2 @@ +import(/* webpackChunkName: "chunk-b" */'./b'); +export default "a"; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/b.js b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/b.js new file mode 100644 index 000000000000..d9affbb9225b --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/b.js @@ -0,0 +1,2 @@ +import(/* webpackChunkName: "chunk-c" */'./c'); +export default "b"; diff --git a/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/c.js b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/c.js new file mode 100644 index 000000000000..b2091de76d64 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/c.js @@ -0,0 +1 @@ +export default "c"; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/execute-module.js b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/execute-module.js new file mode 100644 index 000000000000..b0d35f3a137a --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/execute-module.js @@ -0,0 +1 @@ +export default 1 diff --git a/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/imported-module.js b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/imported-module.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/index.js b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/index.js new file mode 100644 index 000000000000..5c19090bff83 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/index.js @@ -0,0 +1,9 @@ +import v from './imported-module' + +import(/* webpackChunkName: "chunk-a" */'./a'); + +it("should inject mock runtime module", async function () { + expect(v).toBe(1) + expect(typeof __webpack_require__.mock).toBe("function"); + expect(__webpack_require__.mock("chunk-a")).toBe("chunk-a.bundle0.js"); +}); diff --git a/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/loader.js b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/loader.js new file mode 100644 index 000000000000..956d0debaad4 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/loader.js @@ -0,0 +1,7 @@ +const path = require('path') + +module.exports = async function loader() { + const callback = this.async() + const result = await this.importModule(path.resolve(__dirname, './execute-module.js')) + callback(null, `export default ${result.default}`) +} diff --git a/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/rspack.config.js b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/rspack.config.js new file mode 100644 index 000000000000..b147fa8f458a --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/add-runtime-module-with-import-module/rspack.config.js @@ -0,0 +1,63 @@ +const { RuntimeModule, RuntimeGlobals } = require("@rspack/core"); + +class MockRuntimeModule extends RuntimeModule { + constructor() { + super("mock"); + } + + generate(compilation) { + const chunkIdToName = this.chunk.getChunkMaps(false).name; + const chunkNameToId = Object.fromEntries( + Object.entries(chunkIdToName).map(([chunkId, chunkName]) => [ + chunkName, + chunkId, + ]), + ); + + return ` + __webpack_require__.mock = function(chunkId) { + chunkId = (${JSON.stringify( + chunkNameToId, + )})[chunkId]||chunkId; + return ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkScriptFilename}(chunkId); + }; + `; + } +} + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + entry: "./index.js", + mode: "development", + devtool: false, + module: { + rules: [ + { test: /imported-module\.js/, use: ['./loader']} + ] + }, + optimization: { + minimize: false, + sideEffects: false, + concatenateModules: false, + usedExports: false, + innerGraph: false, + providedExports: false + }, + plugins: [ + compiler => { + compiler.hooks.thisCompilation.tap( + "MockRuntimePlugin", + (compilation) => { + compilation.hooks.runtimeRequirementInTree.tap("MockRuntimePlugin", (chunk, set) => { + set.add(RuntimeGlobals.publicPath); + set.add(RuntimeGlobals.getChunkScriptFilename); + compilation.addRuntimeModule( + chunk, + new MockRuntimeModule(chunk) + ); + }) + } + ); + } + ], +}; diff --git a/packages/rspack/src/loader-runner/index.ts b/packages/rspack/src/loader-runner/index.ts index 7085c33346ce..91486262407b 100644 --- a/packages/rspack/src/loader-runner/index.ts +++ b/packages/rspack/src/loader-runner/index.ts @@ -404,9 +404,10 @@ export async function runLoaders( }; loaderContext.importModule = function importModule( request, - options, + userOptions, callback ) { + const options = userOptions ? userOptions : {}; if (!callback) { return new Promise((resolve, reject) => { compiler