diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index dea83c2f163..e3a7167b3ba 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -69,8 +69,10 @@ define_hook!(CompilationOptimizeChunkModules: AsyncSeriesBail(compilation: &mut define_hook!(CompilationModuleIds: SyncSeries(compilation: &mut Compilation)); define_hook!(CompilationChunkIds: SyncSeries(compilation: &mut Compilation)); define_hook!(CompilationRuntimeModule: AsyncSeries(compilation: &mut Compilation, module: &ModuleIdentifier, chunk: &ChunkUkey)); +define_hook!(CompilationAdditionalModuleRuntimeRequirements: SyncSeries(compilation: &Compilation, module_identifier: &ModuleIdentifier, runtime_requirements: &mut RuntimeGlobals)); define_hook!(CompilationRuntimeRequirementInModule: SyncSeriesBail(compilation: &Compilation, module_identifier: &ModuleIdentifier, all_runtime_requirements: &RuntimeGlobals, runtime_requirements: &RuntimeGlobals, runtime_requirements_mut: &mut RuntimeGlobals)); define_hook!(CompilationAdditionalChunkRuntimeRequirements: SyncSeries(compilation: &mut Compilation, chunk_ukey: &ChunkUkey, runtime_requirements: &mut RuntimeGlobals)); +define_hook!(CompilationRuntimeRequirementInChunk: SyncSeriesBail(compilation: &mut Compilation, chunk_ukey: &ChunkUkey, all_runtime_requirements: &RuntimeGlobals, runtime_requirements: &RuntimeGlobals, runtime_requirements_mut: &mut RuntimeGlobals)); define_hook!(CompilationAdditionalTreeRuntimeRequirements: AsyncSeries(compilation: &mut Compilation, chunk_ukey: &ChunkUkey, runtime_requirements: &mut RuntimeGlobals)); define_hook!(CompilationRuntimeRequirementInTree: SyncSeriesBail(compilation: &mut Compilation, chunk_ukey: &ChunkUkey, all_runtime_requirements: &RuntimeGlobals, runtime_requirements: &RuntimeGlobals, runtime_requirements_mut: &mut RuntimeGlobals)); define_hook!(CompilationOptimizeCodeGeneration: SyncSeries(compilation: &mut Compilation)); @@ -100,8 +102,10 @@ pub struct CompilationHooks { pub module_ids: CompilationModuleIdsHook, pub chunk_ids: CompilationChunkIdsHook, pub runtime_module: CompilationRuntimeModuleHook, + pub additional_module_runtime_requirements: CompilationAdditionalModuleRuntimeRequirementsHook, pub runtime_requirement_in_module: CompilationRuntimeRequirementInModuleHook, pub additional_chunk_runtime_requirements: CompilationAdditionalChunkRuntimeRequirementsHook, + pub runtime_requirement_in_chunk: CompilationRuntimeRequirementInChunkHook, pub additional_tree_runtime_requirements: CompilationAdditionalTreeRuntimeRequirementsHook, pub runtime_requirement_in_tree: CompilationRuntimeRequirementInTreeHook, pub optimize_code_generation: CompilationOptimizeCodeGenerationHook, @@ -1566,6 +1570,12 @@ impl Compilation { let mut runtime_requirements = self .code_generation_results .get_runtime_requirements(&module, Some(runtime)); + + plugin_driver + .compilation_hooks + .additional_module_runtime_requirements + .call(self, &module, &mut runtime_requirements)?; + process_runtime_requirement_hook( &mut runtime_requirements, |all_runtime_requirements, runtime_requirements, runtime_requirements_mut| { @@ -1627,6 +1637,23 @@ impl Compilation { .additional_chunk_runtime_requirements .call(self, chunk_ukey, &mut set)?; + process_runtime_requirement_hook( + &mut set, + |all_runtime_requirements, runtime_requirements, runtime_requirements_mut| { + plugin_driver + .compilation_hooks + .runtime_requirement_in_chunk + .call( + self, + chunk_ukey, + all_runtime_requirements, + runtime_requirements, + runtime_requirements_mut, + )?; + Ok(()) + }, + )?; + ChunkGraph::set_chunk_runtime_requirements(self, *chunk_ukey, set); } logger.time_end(start); diff --git a/crates/rspack_plugin_devtool/src/eval_dev_tool_module_plugin.rs b/crates/rspack_plugin_devtool/src/eval_dev_tool_module_plugin.rs index c3d483d6442..0828b55fee7 100644 --- a/crates/rspack_plugin_devtool/src/eval_dev_tool_module_plugin.rs +++ b/crates/rspack_plugin_devtool/src/eval_dev_tool_module_plugin.rs @@ -10,7 +10,9 @@ use rspack_core::{ ApplyContext, BoxModule, ChunkInitFragments, ChunkUkey, Compilation, CompilationParams, CompilerCompilation, CompilerOptions, Plugin, PluginContext, }; -use rspack_core::{CompilationAdditionalTreeRuntimeRequirements, RuntimeGlobals}; +use rspack_core::{ + CompilationAdditionalModuleRuntimeRequirements, ModuleIdentifier, RuntimeGlobals, +}; use rspack_error::Result; use rspack_hash::RspackHash; use rspack_hook::{plugin, plugin_hook}; @@ -190,17 +192,17 @@ impl Plugin for EvalDevToolModulePlugin { ctx .context .compilation_hooks - .additional_tree_runtime_requirements - .tap(eval_devtool_plugin_additional_tree_runtime_requirements::new(self)); + .additional_module_runtime_requirements + .tap(eval_devtool_plugin_additional_module_runtime_requirements::new(self)); Ok(()) } } -#[plugin_hook(CompilationAdditionalTreeRuntimeRequirements for EvalDevToolModulePlugin)] -async fn eval_devtool_plugin_additional_tree_runtime_requirements( +#[plugin_hook(CompilationAdditionalModuleRuntimeRequirements for EvalDevToolModulePlugin)] +fn eval_devtool_plugin_additional_module_runtime_requirements( &self, - compilation: &mut Compilation, - _chunk_ukey: &ChunkUkey, + compilation: &Compilation, + _module: &ModuleIdentifier, runtime_requirements: &mut RuntimeGlobals, ) -> Result<()> { if compilation.options.output.trusted_types.is_some() { diff --git a/crates/rspack_plugin_devtool/src/eval_source_map_dev_tool_plugin.rs b/crates/rspack_plugin_devtool/src/eval_source_map_dev_tool_plugin.rs index 10d6a4cc92f..672eda42b24 100644 --- a/crates/rspack_plugin_devtool/src/eval_source_map_dev_tool_plugin.rs +++ b/crates/rspack_plugin_devtool/src/eval_source_map_dev_tool_plugin.rs @@ -6,7 +6,7 @@ use futures::future::join_all; use rspack_core::{ rspack_sources::{BoxSource, MapOptions, RawSource, Source, SourceExt}, ApplyContext, BoxModule, ChunkInitFragments, ChunkUkey, Compilation, - CompilationAdditionalTreeRuntimeRequirements, CompilationParams, CompilerCompilation, + CompilationAdditionalModuleRuntimeRequirements, CompilationParams, CompilerCompilation, CompilerOptions, ModuleIdentifier, Plugin, PluginContext, RuntimeGlobals, }; use rspack_error::Result; @@ -212,11 +212,11 @@ fn eval_source_map_devtool_plugin_inline_in_runtime_bailout( Ok(Some("the eval-source-map devtool is used.".to_string())) } -#[plugin_hook(CompilationAdditionalTreeRuntimeRequirements for EvalSourceMapDevToolPlugin)] -async fn eval_source_map_devtool_plugin_additional_tree_runtime_requirements( +#[plugin_hook(CompilationAdditionalModuleRuntimeRequirements for EvalSourceMapDevToolPlugin)] +fn eval_source_map_devtool_plugin_additional_module_runtime_requirements( &self, - compilation: &mut Compilation, - _chunk_ukey: &ChunkUkey, + compilation: &Compilation, + _module: &ModuleIdentifier, runtime_requirements: &mut RuntimeGlobals, ) -> Result<()> { if compilation.options.output.trusted_types.is_some() { @@ -241,8 +241,8 @@ impl Plugin for EvalSourceMapDevToolPlugin { ctx .context .compilation_hooks - .additional_tree_runtime_requirements - .tap(eval_source_map_devtool_plugin_additional_tree_runtime_requirements::new(self)); + .additional_module_runtime_requirements + .tap(eval_source_map_devtool_plugin_additional_module_runtime_requirements::new(self)); Ok(()) } } diff --git a/packages/rspack-test-tools/tests/configCases/trusted-types/module-runtime-requirement/index.js b/packages/rspack-test-tools/tests/configCases/trusted-types/module-runtime-requirement/index.js new file mode 100644 index 00000000000..82f933fc415 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/trusted-types/module-runtime-requirement/index.js @@ -0,0 +1,19 @@ +const fs = __non_webpack_require__("fs"); +const path = __non_webpack_require__("path"); + + +function createWorker() { + new Worker(new URL("./worker.js", import.meta.url), { + type: "module", + name: "test-worker" + }); +} + +createWorker; + +it("should generate correct new Worker statement", async () => { + const content = fs.readFileSync(path.resolve(path.dirname(__filename), './test-worker.js'), "utf-8"); + expect(content).toContain(`this is worker`); + expect(content).toContain(`(function (__unused_webpack_module, __unused_webpack_exports, __webpack_require__)`); + expect(content).toContain(`eval(__webpack_require__.ts(`); +}); \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/trusted-types/module-runtime-requirement/rspack.config.js b/packages/rspack-test-tools/tests/configCases/trusted-types/module-runtime-requirement/rspack.config.js new file mode 100644 index 00000000000..d3bfed52cbd --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/trusted-types/module-runtime-requirement/rspack.config.js @@ -0,0 +1,13 @@ +module.exports = { + output: { + filename: "[name].js", + chunkFilename: "[name].js", + trustedTypes: true + }, + node: { + __dirname: false, + __filename: false + }, + devtool: "eval-source-map", + target: "web" +}; diff --git a/packages/rspack-test-tools/tests/configCases/trusted-types/module-runtime-requirement/test.config.js b/packages/rspack-test-tools/tests/configCases/trusted-types/module-runtime-requirement/test.config.js new file mode 100644 index 00000000000..2e3be0636e9 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/trusted-types/module-runtime-requirement/test.config.js @@ -0,0 +1,5 @@ +module.exports = { + findBundle: function (i, options) { + return ["main.js"]; + } +}; diff --git a/packages/rspack-test-tools/tests/configCases/trusted-types/module-runtime-requirement/worker.js b/packages/rspack-test-tools/tests/configCases/trusted-types/module-runtime-requirement/worker.js new file mode 100644 index 00000000000..357e4fc8975 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/trusted-types/module-runtime-requirement/worker.js @@ -0,0 +1 @@ +"this is worker"; \ No newline at end of file