Skip to content

Commit

Permalink
fix: use NODE_ENV in optimizer (#7673)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Apr 27, 2022
1 parent 206e1f2 commit 50672e4
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/playground/optimize-deps/.env
@@ -0,0 +1 @@
NODE_ENV=production
Expand Up @@ -62,6 +62,10 @@ test('import * from optimized dep', async () => {
expect(await page.textContent('.import-star')).toMatch(`[success]`)
})

test('import from dep with process.env.NODE_ENV', async () => {
expect(await page.textContent('.node-env')).toMatch(`prod`)
})

test('import from dep with .notjs files', async () => {
expect(await page.textContent('.not-js')).toMatch(`[success]`)
})
Expand Down
1 change: 1 addition & 0 deletions packages/playground/optimize-deps/dep-node-env/index.js
@@ -0,0 +1 @@
export const env = process.env.NODE_ENV === 'production' ? 'prod' : 'dev'
5 changes: 5 additions & 0 deletions packages/playground/optimize-deps/dep-node-env/package.json
@@ -0,0 +1,5 @@
{
"name": "dep-node-env",
"private": true,
"version": "1.0.0"
}
6 changes: 6 additions & 0 deletions packages/playground/optimize-deps/index.html
Expand Up @@ -38,6 +38,9 @@ <h2>Optimizing force included dep even when it's linked</h2>
<h2>import * as ...</h2>
<div class="import-star"></div>

<h2>Import from dependency with process.env.NODE_ENV</h2>
<div class="node-env"></div>

<h2>Import from dependency with .notjs files</h2>
<div class="not-js"></div>

Expand Down Expand Up @@ -88,6 +91,9 @@ <h2>Reused variable names</h2>
text('.import-star', `[success] ${keys.join(', ')}`)
}

import { env } from 'dep-node-env'
text('.node-env', env)

import { notjsValue } from 'dep-not-js'
text('.not-js', notjsValue)

Expand Down
1 change: 1 addition & 0 deletions packages/playground/optimize-deps/package.json
Expand Up @@ -17,6 +17,7 @@
"dep-esbuild-plugin-transform": "file:./dep-esbuild-plugin-transform",
"dep-linked": "link:./dep-linked",
"dep-linked-include": "link:./dep-linked-include",
"dep-node-env": "file:./dep-node-env",
"dep-not-js": "file:./dep-not-js",
"dep-with-dynamic-import": "file:./dep-with-dynamic-import",
"lodash-es": "^4.17.21",
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -445,7 +445,7 @@ export async function runOptimizeDeps(
}

const define: Record<string, string> = {
'process.env.NODE_ENV': JSON.stringify(config.mode)
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || config.mode)
}
for (const key in config.define) {
const value = config.define[key]
Expand Down Expand Up @@ -790,7 +790,7 @@ export function getDepHash(config: ResolvedConfig): string {
// only a subset of config options that can affect dep optimization
content += JSON.stringify(
{
mode: config.mode,
mode: process.env.NODE_ENV || config.mode,
root: config.root,
define: config.define,
resolve: config.resolve,
Expand Down
5 changes: 5 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 50672e4

Please sign in to comment.