Repository navigation
feat: add Vite-compatible import.meta.env defaults (MODE, DEV, PROD, SSR, BASE_URL) - #21440
Conversation
Define import.meta.env.MODE, .DEV, .PROD, .SSR and .BASE_URL alongside the existing NODE_ENV default, gated on optimization.nodeEnv. MODE/DEV/PROD derive from mode, SSR from a node (non-browser) platform, BASE_URL from a static output.publicPath. User DefinePlugin/EnvironmentPlugin/DotenvPlugin values still override each key.
🦋 Changeset detectedLatest commit: b54865c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
This PR is packaged and the instant preview is available (1f37434). Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@1f37434
yarn add -D webpack@https://pkg.pr.new/webpack@1f37434
pnpm add -D webpack@https://pkg.pr.new/webpack@1f37434 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #21440 +/- ##
=======================================
Coverage 93.19% 93.19%
=======================================
Files 610 610
Lines 70955 70963 +8
Branches 20202 20208 +6
=======================================
+ Hits 66129 66137 +8
Misses 4826 4826
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Add a web+node universal configCase asserting SSR is false (a universal bundle runs in both environments, so it must not be marked server-only), and reword the changeset to drop the Vite reference.
Merging this PR will degrade performance by 31.65%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ❌ | Memory | benchmark "context-commonjs", scenario '{"name":"mode-development","mode":"development"}' |
424.9 KB | 1,833.2 KB | -76.82% |
| ❌ | Memory | benchmark "many-modules-commonjs", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
440.4 KB | 1,722.1 KB | -74.43% |
| ❌ | Memory | benchmark "asset-modules-inline", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
336.1 KB | 1,034.7 KB | -67.52% |
| ❌ | Memory | benchmark "react", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
161 KB | 303.7 KB | -46.99% |
| ⚡ | Memory | benchmark "asset-modules-resource", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
1,477.6 KB | 179.3 KB | ×8.2 |
| ⚡ | Memory | benchmark "future-defaults", scenario '{"name":"mode-production","mode":"production"}' |
9.4 MB | 7.7 MB | +21.28% |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing feat/import-meta-env-defaults (b54865c) with main (0558d6e)
A function publicPath is resolved per-asset at runtime, so BASE_URL has no static value and falls back to "/" (while __webpack_public_path__ stays the runtime value).
|
I’m a bit confused by having DEV, PROD, BASE_URL, and SSR as built-in variables—especially SSR. A pure Node.js environment doesn’t necessarily mean SSR. For example, CLI tools and regular Node.js server builds don’t need an SSR variable at all. The concept of SSR feels more like an application-/framework-level concern, so I’m not sure it belongs as a built-in concept in a low-level bundler. |
|
@xiaoxiaojx it is just for compat right now, as you can see I am preparing ability to make it out of scope with extra plugin, you can see it in other PR |
Summary
Aligns
import.meta.envwith Vite/Rsbuild by definingMODE,DEV,PROD,SSRandBASE_URLalongside the existingNODE_ENVdefault, all gated onoptimization.nodeEnv. This gives library authors bundler-agnostic dev/prod guards without requiring each consumer to add custom bundler config.MODE/DEV/PRODderive frommode,SSRfrom a node (non-browser) platform, andBASE_URLfrom a staticoutput.publicPath(falling back to/). Refs #18876.What kind of change does this PR introduce?
feat — new default
import.meta.envproperties.Did you add tests for your changes?
Yes — new
configCases/plugins/import-meta-env-defaults-developmentandimport-meta-env-defaults-base-url, plus extendedimport-meta-envandimport-meta-env-object-defineto cover every mode/target/publicPath branch.Does this PR introduce a breaking change?
No. The values are additive, overridable by
DefinePlugin/EnvironmentPlugin/DotenvPlugin, and disabled together viaoptimization.nodeEnv: false(ormode: "none"). Code that previously read these keys asundefinedwill now receive real values, which is the intended behavior.If relevant, what needs to be documented once your changes are merged or what have you already documented?
The new
MODE,DEV,PROD,SSRandBASE_URLproperties should be added to https://webpack.js.org/api/module-variables/.Use of AI
AI was used: implemented with Claude (Claude Code). I directed the design decisions (value semantics, the
SSR/BASE_URLderivation, gating onoptimization.nodeEnv), reviewed the diff and tests, and verified the full ConfigTestCases and StatsTestCases suites locally.Generated by Claude Code