Unverified Commit 06f3d398 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure NODE_ENV is replaced correctly with swc (#31274)

parent f796ea3e
...@@ -44,6 +44,10 @@ function getBaseSWCOptions({ ...@@ -44,6 +44,10 @@ function getBaseSWCOptions({
typeofs: { typeofs: {
window: globalWindow ? 'object' : 'undefined', window: globalWindow ? 'object' : 'undefined',
}, },
envs: {
NODE_ENV: development ? '"development"' : '"production"',
},
// TODO: handle process.browser to match babel replacing as well
}, },
}, },
regenerator: { regenerator: {
......
/* eslint-env jest */ /* eslint-env jest */
import glob from 'glob'
import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import { import {
findPort, findPort,
...@@ -7,6 +8,7 @@ import { ...@@ -7,6 +8,7 @@ import {
killApp, killApp,
waitFor, waitFor,
initNextServerScript, initNextServerScript,
nextBuild,
} from 'next-test-utils' } from 'next-test-utils'
const appDir = join(__dirname, '..') const appDir = join(__dirname, '..')
...@@ -77,6 +79,29 @@ describe('Non-Standard NODE_ENV', () => { ...@@ -77,6 +79,29 @@ describe('Non-Standard NODE_ENV', () => {
expect(output).not.toContain(warningText) expect(output).not.toContain(warningText)
}) })
it('should still DCE NODE_ENV specific code', async () => {
await nextBuild(appDir, undefined, {
env: {
NODE_ENV: 'test',
},
})
const staticFiles = glob.sync('**/*.js', {
cwd: join(appDir, '.next/static'),
})
expect(staticFiles.length).toBeGreaterThan(0)
for (const file of staticFiles) {
const content = await fs.readFile(
join(appDir, '.next/static', file),
'utf8'
)
if (content.match(/cannot find module/i)) {
throw new Error(`${file} contains module not found error`)
}
}
})
it('should show the warning with NODE_ENV set to invalid value', async () => { it('should show the warning with NODE_ENV set to invalid value', async () => {
let output = '' let output = ''
......
...@@ -129,10 +129,10 @@ export function runNextCommand(argv, options = {}) { ...@@ -129,10 +129,10 @@ export function runNextCommand(argv, options = {}) {
// Let Next.js decide the environment // Let Next.js decide the environment
const env = { const env = {
...process.env, ...process.env,
...options.env,
NODE_ENV: '', NODE_ENV: '',
__NEXT_TEST_MODE: 'true', __NEXT_TEST_MODE: 'true',
NEXT_PRIVATE_OUTPUT_TRACE_ROOT: path.join(__dirname, '../../'), NEXT_PRIVATE_OUTPUT_TRACE_ROOT: path.join(__dirname, '../../'),
...options.env,
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment