Skip to content

Commit

Permalink
Ensure NODE_ENV is replaced correctly with swc (#31274)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Nov 12, 2021
1 parent f796ea3 commit 06f3d39
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/next/build/swc/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ function getBaseSWCOptions({
typeofs: {
window: globalWindow ? 'object' : 'undefined',
},
envs: {
NODE_ENV: development ? '"development"' : '"production"',
},
// TODO: handle process.browser to match babel replacing as well
},
},
regenerator: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* eslint-env jest */

import glob from 'glob'
import fs from 'fs-extra'
import { join } from 'path'
import {
findPort,
launchApp,
killApp,
waitFor,
initNextServerScript,
nextBuild,
} from 'next-test-utils'

const appDir = join(__dirname, '..')
Expand Down Expand Up @@ -77,6 +79,29 @@ describe('Non-Standard NODE_ENV', () => {
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 () => {
let output = ''

Expand Down
2 changes: 1 addition & 1 deletion test/lib/next-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ export function runNextCommand(argv, options = {}) {
// Let Next.js decide the environment
const env = {
...process.env,
...options.env,
NODE_ENV: '',
__NEXT_TEST_MODE: 'true',
NEXT_PRIVATE_OUTPUT_TRACE_ROOT: path.join(__dirname, '../../'),
...options.env,
}

return new Promise((resolve, reject) => {
Expand Down

0 comments on commit 06f3d39

Please sign in to comment.