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

Ensure NODE_ENV is replaced correctly with swc #31274

Merged
merged 3 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
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