fix(build): replace process.env.NODE_ENV in 1st-gen and 2nd-gen builds#6264
fix(build): replace process.env.NODE_ENV in 1st-gen and 2nd-gen builds#6264rubencarvalho wants to merge 2 commits into
Conversation
|
Adds process.env.NODE_ENV to the esbuild define config so built .js and .dev.js files never contain raw process.env references. This fixes ReferenceError: process is not defined for downstream consumers that load SWC packages in a browser without a bundler (e.g. Sass playground), while keeping dev/prod behaviour correct in built output.
4d61596 to
41c4139
Compare
📚 Branch Preview Links🔍 First Generation Visual Regression Test ResultsWhen a visual regression test fails (or has previously failed while working on this branch), its results can be found in the following URLs:
Deployed to Azure Blob Storage: If the changes are expected, update the |
Vite's library mode does not auto-replace process.env.NODE_ENV, so 2nd-gen
dist files contained raw process.env.NODE_ENV references. This caused
ReferenceError: process is not defined for downstream consumers that load
SWC packages in a browser without a bundler (e.g. Sass playground).
Adds a mode-aware define so dist files contain the literal mode value
('production' or 'development') instead of the runtime expression.
Description
Ensures built artifacts in both 1st-gen and 2nd-gen never contain raw
process.env.NODE_ENVreferences, so downstream consumers (e.g. Sass playground) loading SWC packages in a browser without a bundler don't hitReferenceError: process is not defined.ts-tools.js): adds an explicitdefineforprocess.env.NODE_ENVin the esbuild config. esbuild'sbuild()API auto-replaces this by default, but making it explicit is defensive and self-documenting.vite.config.js/vite.config.ts): adds adefineto the Vite config. Vite's library mode (build.lib) does not auto-replaceprocess.env.NODE_ENV, which is why the published 2nd-gendist/element/spectrum-element.jswas shipping with the raw expression — this is the actual customer-facing bug.Motivation and context
PR #6250 fixes
ReferenceError: process is not definedfor downstream consumers by adding atypeof process !== 'undefined'runtime guard inBase.tsandspectrum-element.ts. That approach fixes the symptom but breaks 1st-gen CI tests: esbuild doesn't replacetypeof process, so the guard evaluates tofalsein the browser test environment,window.__swcis never initialized, andwindow.__swc.warn()crashes in components likeDialogBase.Verification — built dist files no longer contain any
process.Xortypeof processreferences:In the rebuilt 2nd-gen
spectrum-element.js, the entire dev-mode setup block is dead-code eliminated because"production" === 'development'isfalse.Important: PR #6250 must also revert its
typeof process !== 'undefined'source-level guards inBase.tsandspectrum-element.ts. Withprocess.env.NODE_ENVreplaced at build time in both build systems, the guards are unnecessary, and keeping them would still break 1st-gen tests (the built output becomesif (typeof process !== 'undefined' && true), stillfalsein the browser).Related issue(s)
processis defined before trying to use it #6250Author's checklist
Accessibility testing checklist
N/A — build configuration change only.