Skip to content

Commit

Permalink
fix: trusted type should add module runtime requirements (#8617)
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder authored Dec 4, 2024
1 parent 0bf7360 commit 4cedd61
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 14 deletions.
27 changes: 27 additions & 0 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 9 additions & 7 deletions crates/rspack_plugin_devtool/src/eval_dev_tool_module_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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(())
}
}
Original file line number Diff line number Diff line change
@@ -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(`);
});
Original file line number Diff line number Diff line change
@@ -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"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
findBundle: function (i, options) {
return ["main.js"];
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"this is worker";

2 comments on commit 4cedd61

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
rsdoctor ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success
devserver ✅ success
nuxt ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-12-04 044e8e3) Current Change
10000_big_production-mode_disable-minimize + exec 42.6 s ± 1.25 s 43 s ± 1.59 s +0.97 %
10000_development-mode + exec 1.83 s ± 47 ms 1.82 s ± 37 ms -0.69 %
10000_development-mode_hmr + exec 647 ms ± 3.9 ms 649 ms ± 6.2 ms +0.35 %
10000_production-mode + exec 2.41 s ± 41 ms 2.41 s ± 14 ms -0.10 %
arco-pro_development-mode + exec 1.75 s ± 70 ms 1.75 s ± 70 ms -0.31 %
arco-pro_development-mode_hmr + exec 426 ms ± 2 ms 426 ms ± 2.4 ms -0.01 %
arco-pro_production-mode + exec 3.1 s ± 71 ms 3.09 s ± 77 ms -0.32 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.13 s ± 77 ms 3.12 s ± 98 ms -0.28 %
threejs_development-mode_10x + exec 1.63 s ± 20 ms 1.63 s ± 15 ms +0.14 %
threejs_development-mode_10x_hmr + exec 807 ms ± 11 ms 806 ms ± 10 ms -0.15 %
threejs_production-mode_10x + exec 4.91 s ± 26 ms 4.93 s ± 24 ms +0.54 %
10000_big_production-mode_disable-minimize + rss memory 13449 MiB ± 447 MiB 13486 MiB ± 495 MiB +0.28 %
10000_development-mode + rss memory 784 MiB ± 25.4 MiB 776 MiB ± 40.1 MiB -0.99 %
10000_development-mode_hmr + rss memory 1896 MiB ± 541 MiB 1927 MiB ± 389 MiB +1.64 %
10000_production-mode + rss memory 663 MiB ± 27.2 MiB 655 MiB ± 30.2 MiB -1.19 %
arco-pro_development-mode + rss memory 731 MiB ± 34.9 MiB 726 MiB ± 40.2 MiB -0.59 %
arco-pro_development-mode_hmr + rss memory 926 MiB ± 68.8 MiB 944 MiB ± 173 MiB +1.90 %
arco-pro_production-mode + rss memory 834 MiB ± 35.9 MiB 863 MiB ± 55.1 MiB +3.42 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 879 MiB ± 37.5 MiB 852 MiB ± 60.2 MiB -3.02 %
threejs_development-mode_10x + rss memory 823 MiB ± 46.7 MiB 825 MiB ± 39.3 MiB +0.20 %
threejs_development-mode_10x_hmr + rss memory 2147 MiB ± 152 MiB 2197 MiB ± 272 MiB +2.34 %
threejs_production-mode_10x + rss memory 1034 MiB ± 53.5 MiB 1026 MiB ± 64.6 MiB -0.73 %

Please sign in to comment.