Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,11 @@ async function prepareEsbuildOptimizerRun(
if (optimizerContext.cancelled) return { context: undefined, idToExports }

const define = {
'process.env.NODE_ENV': JSON.stringify(
process.env.NODE_ENV || environment.config.mode,
),
'process.env.NODE_ENV': environment.config.keepProcessEnv
? // define process.env.NODE_ENV even for keepProcessEnv === true
// as esbuild will replace it automatically when `platform` is `'browser'`
'process.env.NODE_ENV'
Comment on lines +778 to +781
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'process.env.NODE_ENV': environment.config.keepProcessEnv
? // define process.env.NODE_ENV even for keepProcessEnv === true
// as esbuild will replace it automatically when `platform` is `'browser'`
'process.env.NODE_ENV'
'process.env.NODE_ENV': environment.config.keepProcessEnv
? 'process.env.NODE_ENV'

is the comment really helpful? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it isn't obvious that we cannot write it like below without a comment.

const define = environment.config.keepProcessEnv
    ? {}
    : {
        'process.env.NODE_ENV': JSON.stringify(
          process.env.NODE_ENV || environment.config.mode,
        ),
      }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, yeah that's not obvious 😅

I actually didn't understand this before, I thought that your comment was practically saying that process.env.NODE_ENV can be defined even for keepProcessEnv === true (since esbuild just replaces it) and not that it needs to

: JSON.stringify(process.env.NODE_ENV || environment.config.mode),
}

const platform =
Expand Down Expand Up @@ -1210,7 +1212,9 @@ function getConfigHash(environment: Environment): string {
const { optimizeDeps } = config
const content = JSON.stringify(
{
mode: process.env.NODE_ENV || config.mode,
define: !config.keepProcessEnv
? process.env.NODE_ENV || config.mode
: null,
root: config.root,
resolve: config.resolve,
assetsInclude: config.assetsInclude,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ describe.runIf(!isBuild)('pre-bundling', () => {
expect(metaJson.optimized['react/jsx-dev-runtime']).toBeTruthy()

expect(metaJson.optimized['react-dom/client']).toBeFalsy()

// process.env.NODE_ENV should be kept as keepProcessEnv is true
const depsFiles = fs
.readdirSync(path.resolve(testDir, 'node_modules/.vite/deps_ssr'), {
withFileTypes: true,
})
.filter((file) => file.isFile() && file.name.endsWith('.js'))
.map((file) => path.join(file.parentPath, file.name))
const depsFilesWithProcessEnvNodeEnv = depsFiles.filter((file) =>
fs.readFileSync(file, 'utf-8').includes('process.env.NODE_ENV'),
)

expect(depsFilesWithProcessEnvNodeEnv.length).toBeGreaterThan(0)
})

test('deps reload', async () => {
Expand Down
Loading