Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: trusted type should add module runtime requirements #8617

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
Loading