Add import-meta-env contrib plugin#640
Conversation
|
PR Review: Add
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0e8e508281
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| if let Expr::Member(member) = n { | ||
| if is_import_meta_env(member) { | ||
| member.obj = Box::new(Ident::new_no_ctxt("process".into(), DUMMY_SP).into()); |
There was a problem hiding this comment.
Mark inserted process as unresolved
This injects a fresh process identifier with no_ctxt while the plugin metadata's unresolved_mark is ignored. In files that declare a local process binding, or when this pass is composed with SWC resolver/hygiene, the emitted process.env can be treated as a normal user identifier instead of the intended global, so import.meta.env.MODE may read the local binding rather than the environment object. Thread metadata.unresolved_mark into the visitor and create the inserted global with that syntax context, as other plugins in this repo do for injected globals.
Useful? React with 👍 / 👎.
| impl VisitMut for TransformVisitor { | ||
| noop_visit_mut_type!(); | ||
|
|
||
| fn visit_mut_expr(&mut self, n: &mut Expr) { |
There was a problem hiding this comment.
Rewrite import.meta.env when it is the assignment target
Because this visitor only rewrites Expr::Member, the exact left-hand target import.meta.env in an assignment is visited as a MemberExpr target rather than as an expression, so code such as import.meta.env = { MODE: "test" } is left unchanged. That still leaves import.meta in the emitted code for test/setup files that replace the env bag, defeating the plugin's stated transform for this expression shape; add a member-expression visitor or shared helper so assignment targets are rewritten too.
Useful? React with 👍 / 👎.
Summary
@swc-contrib/plugin-import-meta-envundercontrib/import-meta-envimport.meta.envtoprocess.envtransform fromswc-plugin-import-meta-envCloses #612.
Validation
cargo fmt -p swc_plugin_import_meta_env --checkcorepack pnpm exec taplo format --check contrib/import-meta-env/Cargo.tomlcargo test -p swc_plugin_import_meta_env --color alwaysRUSTFLAGS='--cfg swc_ast_unknown' cargo check -p swc_plugin_import_meta_envcorepack pnpm -F @swc-contrib/plugin-import-meta-env run buildcorepack pnpm exec lint-staged