-
-
Notifications
You must be signed in to change notification settings - Fork 602
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
feat: replace top-level this in esm #4497
Conversation
I think now |
string replacing-based codegen required ast to be untouched (some plugins needs span info), if we add an extra transforming pass, it may affect the optimization result. Can we move the transforming work to the loader side? |
Or just like the webpack, use a |
Turns out the origin impl doesn't work at all. Two separate AST is used for codegen and dep analysis, and modify the latter one doesn't have any effect. |
Seems like most passes in |
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.
It would be better if you can implement it by string-replacement way (use ConstDependency
to replace this
to undefined
), this is pretty similar with ReplaceNestWebpackRequireVisitor
, so you can reference it at
rspack/crates/rspack_plugin_javascript/src/visitors/dependency/compatibility_scanner.rs
Lines 52 to 75 in 3c0c6ca
pub struct ReplaceNestWebpackRequireVisitor<'a> { | |
unresolved_ctxt: &'a SyntaxContext, | |
name_map: &'a FxHashMap<SyntaxContext, u8>, | |
presentational_dependencies: &'a mut Vec<Box<dyn DependencyTemplate>>, | |
} | |
impl Visit for ReplaceNestWebpackRequireVisitor<'_> { | |
noop_visit_type!(); | |
fn visit_ident(&mut self, ident: &Ident) { | |
if &ident.sym == "__webpack_require__" && ident.span.ctxt != *self.unresolved_ctxt { | |
if let Some(count) = self.name_map.get(&ident.span.ctxt) { | |
self | |
.presentational_dependencies | |
.push(Box::new(ConstDependency::new( | |
ident.span.real_lo(), | |
ident.span.real_hi(), | |
format!("__nested_webpack_require_{count}__").into(), | |
None, | |
))); | |
} | |
} | |
} | |
} |
crates/rspack_plugin_javascript/src/visitors/harmony_top_level_this.rs
Outdated
Show resolved
Hide resolved
Done. But what's the difference between AST plugin and text plugin? What's the necessity of the latter? |
We used ast-based replacement for all our previous implementations, but we found it challenging to implement caching and persistent caching using ast compared to string replacement. Therefore, we switched to the implementation of string replacement, which is currently in an intermediate stage. We currently have both string replacement and ast replacement, as there are still some blockers for the remaining ast replacement. In the long term, we will replace all of it with string replacement. |
LGTM, can you make the CI passes |
Interesting. Maybe it would be doable after SWC finishes it's plugin story which involves of handling of serializing with rykv |
Thanks! |
Summary
Closes #4421
Test Plan
Require Documentation?