-
-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: trusted type with devtool eval (#7866)
- Loading branch information
1 parent
b1bf153
commit f6f1798
Showing
13 changed files
with
635 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
crates/rspack_plugin_javascript/src/dependency/worker/create_script_url_dependency.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use rspack_core::{ | ||
AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyCategory, | ||
DependencyId, DependencyTemplate, DependencyType, RealDependencyLocation, RuntimeGlobals, | ||
RuntimeSpec, TemplateContext, TemplateReplaceSource, | ||
}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct CreateScriptUrlDependency { | ||
id: DependencyId, | ||
range: RealDependencyLocation, | ||
range_path: (u32, u32), | ||
} | ||
|
||
impl CreateScriptUrlDependency { | ||
pub fn new(range: RealDependencyLocation, range_path: (u32, u32)) -> Self { | ||
Self { | ||
id: DependencyId::new(), | ||
range, | ||
range_path, | ||
} | ||
} | ||
} | ||
|
||
impl Dependency for CreateScriptUrlDependency { | ||
fn id(&self) -> &DependencyId { | ||
&self.id | ||
} | ||
|
||
fn category(&self) -> &DependencyCategory { | ||
&DependencyCategory::Worker | ||
} | ||
|
||
fn dependency_type(&self) -> &DependencyType { | ||
&DependencyType::CreateScriptUrl | ||
} | ||
|
||
fn range(&self) -> Option<&RealDependencyLocation> { | ||
Some(&self.range) | ||
} | ||
|
||
fn could_affect_referencing_module(&self) -> rspack_core::AffectType { | ||
rspack_core::AffectType::False | ||
} | ||
} | ||
|
||
impl DependencyTemplate for CreateScriptUrlDependency { | ||
fn apply( | ||
&self, | ||
source: &mut TemplateReplaceSource, | ||
code_generatable_context: &mut TemplateContext, | ||
) { | ||
code_generatable_context | ||
.runtime_requirements | ||
.insert(RuntimeGlobals::CREATE_SCRIPT_URL); | ||
|
||
source.insert( | ||
self.range_path.0, | ||
format!("{}(", RuntimeGlobals::CREATE_SCRIPT_URL).as_str(), | ||
None, | ||
); | ||
source.insert(self.range_path.1, ")", None); | ||
} | ||
|
||
fn dependency_id(&self) -> Option<DependencyId> { | ||
Some(self.id) | ||
} | ||
|
||
fn update_hash( | ||
&self, | ||
_hasher: &mut dyn std::hash::Hasher, | ||
_compilation: &Compilation, | ||
_runtime: Option<&RuntimeSpec>, | ||
) { | ||
} | ||
} | ||
|
||
impl AsModuleDependency for CreateScriptUrlDependency {} | ||
impl AsContextDependency for CreateScriptUrlDependency {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
crates/rspack_plugin_runtime/src/runtime_module/create_script.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use rspack_collections::Identifier; | ||
use rspack_core::{ | ||
impl_runtime_module, | ||
rspack_sources::{BoxSource, RawSource, SourceExt}, | ||
Compilation, RuntimeGlobals, RuntimeModule, | ||
}; | ||
|
||
#[impl_runtime_module] | ||
#[derive(Debug)] | ||
pub struct CreateScriptRuntimeModule { | ||
id: Identifier, | ||
} | ||
|
||
impl Default for CreateScriptRuntimeModule { | ||
fn default() -> Self { | ||
Self::with_default(Identifier::from("webpack/runtime/create_script")) | ||
} | ||
} | ||
|
||
impl RuntimeModule for CreateScriptRuntimeModule { | ||
fn name(&self) -> Identifier { | ||
self.id | ||
} | ||
|
||
fn generate(&self, compilation: &Compilation) -> rspack_error::Result<BoxSource> { | ||
Ok( | ||
RawSource::from(format!( | ||
r#" | ||
{} = function(script){{ | ||
return {}; | ||
}}; | ||
"#, | ||
RuntimeGlobals::CREATE_SCRIPT, | ||
if compilation.options.output.trusted_types.is_some() { | ||
format!( | ||
"{}().createScript(script)", | ||
RuntimeGlobals::GET_TRUSTED_TYPES_POLICY | ||
) | ||
} else { | ||
"script".to_string() | ||
} | ||
)) | ||
.boxed(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
f6f1798
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Benchmark detail: Open
f6f1798
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Ran ecosystem CI: Open