-
-
Notifications
You must be signed in to change notification settings - Fork 356
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
expression replacement - replace process.env.NODE_ENV
#1330
Comments
In what stages does esbuild and rollup do this? These features should be builtin features provided by oxc. Oxc already has a work-in-proess transformer and minimizer, ideally we just have to add these features into these two AST passes and let the bundler use these features. |
That's tricky to say. Rollup support it using the plugin, so rollup itself doesn't provide this feature.
I think so. But the problems I mentioned above would still exist. |
This is why I am interested in how esbuild and rollup solves these problems, even it's inside a plugin. |
It's quite complex, which makes it a challenge for writing it down. Maybe we could discuss it using voice chat. |
process.env.NODE_ENV
I think I can figure this out if we reduce the scope to inlining |
@hyf0 Can you give me test case in Rolldown so I can test the whole thing end to end? |
I could give one or two cases, but I don't have a test list for this feature. Maybe the tests of swc could be a start: https://github.com/swc-project/swc/blob/94da1241720ed029bc41b6068a9267e2c86347d0/crates/swc_ecma_transforms_optimization/src/inline_globals.rs#L252-L319. |
Yeah. This could be start for implementing expression replacement, but in the end we gonna need a general-used expression replacement feature that could almost replace everything not only |
Skeleton is done in #3803, I'll port esbuild's behaviour and tests. |
What's expression replacement
There's need for users that is replacing some expression with another expression in source code.
A typical use case is to replace
process.env.NODE_ENV
to"production"
.Input with typical config
would be transformed to
Thoughts
The processes of this feature in general are
process.env.NODE_ENV
in'process.env.NODE_ENV': '"production"'
toExpression
, which we called ittarget
now."production"
in'process.env.NODE_ENV': '"production"'
toExpression
, which we called itreplacement
now.Expression
should stored in the same arena that used in step1. How we do this?Expression
.VisitMut#visit_expression
, if someexpr == target
, thenstd::mem::replace(&mut expr, replacement.clone())
.expr
==target
is a problem, this comparison should ignore theSpan
,SymbolId
andReferenceId
replacement.clone()
doesn't seem to works.The text was updated successfully, but these errors were encountered: