Skip to content

Add import-meta-env contrib plugin#640

Open
kdy1 wants to merge 1 commit into
mainfrom
kdy1/import-meta-env-contrib
Open

Add import-meta-env contrib plugin#640
kdy1 wants to merge 1 commit into
mainfrom
kdy1/import-meta-env-contrib

Conversation

@kdy1

@kdy1 kdy1 commented Jun 30, 2026

Copy link
Copy Markdown
Member

Summary

  • Add @swc-contrib/plugin-import-meta-env under contrib/import-meta-env
  • Port the import.meta.env to process.env transform from swc-plugin-import-meta-env
  • Add Rust snapshot tests, package metadata, README, changelog, and workspace lockfile entries

Closes #612.

Validation

  • cargo fmt -p swc_plugin_import_meta_env --check
  • corepack pnpm exec taplo format --check contrib/import-meta-env/Cargo.toml
  • cargo test -p swc_plugin_import_meta_env --color always
  • RUSTFLAGS='--cfg swc_ast_unknown' cargo check -p swc_plugin_import_meta_env
  • corepack pnpm -F @swc-contrib/plugin-import-meta-env run build
  • corepack pnpm exec lint-staged

@changeset-bot

changeset-bot Bot commented Jun 30, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 0e8e508

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@claude

claude Bot commented Jun 30, 2026

Copy link
Copy Markdown

PR Review: Add import-meta-env contrib plugin

Thanks for the well-structured contribution — the metadata, snapshot tests, changelog, and validation steps in the description are all appreciated. The transform itself is small and correct. A few notes below.

🐛 Bug — README.md has duplicated changelog content

The README ends with what looks like an accidental paste of CHANGELOG.md:

The plugin only rewrites the `import.meta.env` expression shape. ...another tool such as `dotenv`.

# @swc-contrib/plugin-import-meta-env

## 1.15.41

### Minor Changes

- Add `@swc-contrib/plugin-import-meta-env`, based on `swc-plugin-import-meta-env`.

This second heading block duplicates the package title and dumps changelog text into the published README (the README ships to npm via files/.npmignore). It should be removed.

✅ Things that look good

  • License/attributionApache-2.0 and the Alex Miller author attribution correctly match the upstream Codex-/swc-plugin-import-meta-env project. 👍
  • Transform correctnessvisit_mut_children_with before the check means nested forms collapse correctly (import.meta.env.MODEprocess.env.MODE, import.meta.env["PROP"]process.env["PROP"]), and import.meta / import.meta.glob are correctly left untouched. Snapshots confirm this.
  • Workspace wiring — picked up automatically by the contrib/* glob in the root Cargo.toml; Cargo.lock and pnpm-lock.yaml are updated consistently.

💬 Minor suggestions (non-blocking)

  1. Computed import.meta["env"] is not transformed. is_import_meta_env requires n.prop.as_ident(), so only dot-access matches. This mirrors upstream and is likely fine, but consider adding a no_transform-style snapshot to document the intentional limitation.
  2. import_attributes: true in the test syntax() isn't needed to parse import.meta and could be dropped for clarity (harmless if kept for parity with upstream).
  3. Style consistencyCargo.toml hardcodes authors/edition/license/rust-version/homepage instead of using *.workspace = true. That's acceptable here (it overrides the workspace Apache-2.0... which is the same value, and graphql-codegen-client-preset similarly overrides with MIT), but inheriting the shared fields where they match would reduce drift.
  4. Build target uses wasm32-wasip1 while mut-cjs-exports uses wasm32-unknown-unknown — just confirming wasip1 is the intended/current target convention for new contrib plugins.

Test coverage

Good for a transform this size: positive cases (env, .MODE, ["PROP"]) and negative cases (import.meta, import.meta.glob) are all covered. The only gap worth considering is the computed-["env"] case noted above.

Overall this is in good shape — the README duplication is the one item I'd consider blocking before merge.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

[Proposal] Promote swc-plugin-import-meta-env

1 participant