Skip to content

Commit

Permalink
fix(define): allow define process.env (#15173)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Nov 29, 2023
1 parent 642f9bc commit ec401da
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
6 changes: 1 addition & 5 deletions packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ export function definePlugin(config: ResolvedConfig): Plugin {

// ignore replace process.env in lib build
const processEnv: Record<string, string> = {}
const processNodeEnv: Record<string, string> = {}
if (!isBuildLib) {
const nodeEnv = process.env.NODE_ENV || config.mode
Object.assign(processEnv, {
'process.env': `{}`,
'global.process.env': `{}`,
'globalThis.process.env': `{}`,
})
Object.assign(processNodeEnv, {
'process.env.NODE_ENV': JSON.stringify(nodeEnv),
'global.process.env.NODE_ENV': JSON.stringify(nodeEnv),
'globalThis.process.env.NODE_ENV': JSON.stringify(nodeEnv),
Expand Down Expand Up @@ -60,11 +57,10 @@ export function definePlugin(config: ResolvedConfig): Plugin {
const replaceProcessEnv = !ssr || config.ssr?.target === 'webworker'

const define: Record<string, string> = {
...(replaceProcessEnv ? processNodeEnv : {}),
...(replaceProcessEnv ? processEnv : {}),
...importMetaKeys,
...userDefine,
...importMetaFallbackKeys,
...(replaceProcessEnv ? processEnv : {}),
}

// Additional define fixes based on `ssr` value
Expand Down
3 changes: 3 additions & 0 deletions playground/define/__tests__/define.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ test('string', async () => {
expect(await page.textContent('.process-node-env')).toBe(
JSON.parse(defines['process.env.NODE_ENV']),
)
expect(await page.textContent('.process-env')).toBe(
JSON.stringify(defines['process.env'], null, 2),
)
expect(await page.textContent('.env-var')).toBe(
JSON.parse(defines['process.env.SOMEVAR']),
)
Expand Down
2 changes: 2 additions & 0 deletions playground/define/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h1>Define</h1>
<p>Object <span class="pre object"></span></p>
<p>Env Var <code class="env-var"></code></p>
<p>process node env: <code class="process-node-env"></code></p>
<p>process env: <code class="process-env"></code></p>
<p>process as property: <code class="process-as-property"></code></p>
<p>spread object: <code class="spread-object"></code></p>
<p>spread array: <code class="spread-array"></code></p>
Expand Down Expand Up @@ -68,6 +69,7 @@ <h2>Define replaces constants in template literal expressions</h2>
text('.undefined', __UNDEFINED__)
text('.object', JSON.stringify(__OBJ__, null, 2))
text('.process-node-env', process.env.NODE_ENV)
text('.process-env', JSON.stringify(process.env, null, 2))
text('.env-var', process.env.SOMEVAR)
text('.process-as-property', __OBJ__.process.env.SOMEVAR)
text(
Expand Down
5 changes: 5 additions & 0 deletions playground/define/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export default defineConfig({
},
'process.env.NODE_ENV': '"dev"',
'process.env.SOMEVAR': '"SOMEVAR"',
'process.env': {
NODE_ENV: 'dev',
SOMEVAR: 'SOMEVAR',
OTHER: 'works',
},
$DOLLAR: 456,
ÖUNICODE_LETTERɵ: 789,
__VAR_NAME__: false,
Expand Down

0 comments on commit ec401da

Please sign in to comment.