Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use NODE_ENV in optimizer #7673

Merged
merged 1 commit into from Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -438,7 +438,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 @@ -783,7 +783,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.