Unverified Commit 4d193ea3 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update to only apply experimental process.env stub in developm… (#12055)

parent 5592e64c
...@@ -867,15 +867,10 @@ export default async function getBaseWebpackConfig( ...@@ -867,15 +867,10 @@ export default async function getBaseWebpackConfig(
} }
: undefined), : undefined),
// stub process.env with proxy to warn a missing value is // stub process.env with proxy to warn a missing value is
// being accessed // being accessed in development mode
...(config.experimental.pageEnv ...(config.experimental.pageEnv && process.env.NODE_ENV !== 'production'
? { ? {
'process.env': 'process.env': `
process.env.NODE_ENV === 'production'
? isServer
? 'process.env'
: '{}'
: `
new Proxy(${isServer ? 'process.env' : '{}'}, { new Proxy(${isServer ? 'process.env' : '{}'}, {
get(target, prop) { get(target, prop) {
if (typeof target[prop] === 'undefined') { if (typeof target[prop] === 'undefined') {
......
import { PHASE_PRODUCTION_BUILD } from 'next/constants'
const { NODE_ENV } = process.env
const { NEXT_PHASE } = process.env
const SENTRY_DSN = process.env.NEXT_PUBLIC_SENTRY_DSN
if (NODE_ENV !== 'development' && NEXT_PHASE !== PHASE_PRODUCTION_BUILD) {
console.log('next start', SENTRY_DSN)
}
export default () => process.env.HELLO || 'hi'
import dynamic from 'next/dynamic'
const Hello = dynamic(() => import('../components/hello'), { ssr: true })
export default () => { export default () => {
console.log(process.env.hi) // make sure we handle this syntax correctly
return <p>hi there 👋</p> const { hi } = process.env
console.log(hi)
return (
<>
<p>hi there 👋</p>
<Hello />
</>
)
} }
...@@ -6,6 +6,8 @@ import { ...@@ -6,6 +6,8 @@ import {
launchApp, launchApp,
renderViaHTTP, renderViaHTTP,
waitFor, waitFor,
nextBuild,
nextStart,
} from 'next-test-utils' } from 'next-test-utils'
import { join } from 'path' import { join } from 'path'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
...@@ -54,57 +56,113 @@ const checkMissingClient = async (pathname, prop, shouldWarn = false) => { ...@@ -54,57 +56,113 @@ const checkMissingClient = async (pathname, prop, shouldWarn = false) => {
} }
describe('process.env stubbing', () => { describe('process.env stubbing', () => {
beforeAll(async () => { describe('dev mode', () => {
appPort = await findPort() beforeAll(async () => {
app = await launchApp(appDir, appPort, { appPort = await findPort()
env: { app = await launchApp(appDir, appPort, {
NEXT_PUBLIC_HI: 'hi', env: {
I_SHOULD_BE_HERE: 'hello', NEXT_PUBLIC_HI: 'hi',
}, I_SHOULD_BE_HERE: 'hello',
onStderr(msg) { },
stderr += msg || '' onStderr(msg) {
}, stderr += msg || ''
},
})
}) })
}) afterAll(() => killApp(app))
afterAll(() => killApp(app))
describe('server side', () => { describe('server side', () => {
it('should not show missing env value when its not missing public', async () => { it('should not show missing env value when its not missing public', async () => {
await checkMissing('/not-missing', 'NEXT_PUBLIC_HI') await checkMissing('/not-missing', 'NEXT_PUBLIC_HI')
}) })
it('should not show missing env value when its not missing runtime', async () => { it('should not show missing env value when its not missing runtime', async () => {
await checkMissing('/also-not-missing', 'I_SHOULD_BE_HERE') await checkMissing('/also-not-missing', 'I_SHOULD_BE_HERE')
}) })
it('should show missing env value when its missing normal', async () => { it('should show missing env value when its missing normal', async () => {
await checkMissing('/missing', 'hi', true) await checkMissing('/missing', 'hi', true)
}) })
it('should show missing env value when its missing GSP', async () => { it('should show missing env value when its missing GSP', async () => {
await checkMissing('/missing-gsp', 'SECRET', true) await checkMissing('/missing-gsp', 'SECRET', true)
}) })
it('should show missing env value when its missing GSSP', async () => {
await checkMissing('/missing-gssp', 'SECRET', true)
})
it('should show missing env value when its missing GSSP', async () => { it('should show missing env value when its missing API', async () => {
await checkMissing('/missing-gssp', 'SECRET', true) await checkMissing('/api/hi', 'where_is_it', true)
})
}) })
it('should show missing env value when its missing API', async () => { describe('client side', () => {
await checkMissing('/api/hi', 'where_is_it', true) it('should not show missing env value when its not missing public', async () => {
await checkMissingClient('/not-missing', 'NEXT_PUBLIC_HI')
})
it('should show missing env value when its missing runtime', async () => {
await checkMissingClient('/also-not-missing', 'I_SHOULD_BE_HERE', true)
})
it('should show missing env value when its missing normal', async () => {
await checkMissingClient('/missing', 'hi', true)
})
}) })
}) })
describe('client side', () => { describe('production mode', () => {
it('should not show missing env value when its not missing public', async () => { beforeAll(async () => {
await checkMissingClient('/not-missing', 'NEXT_PUBLIC_HI') const { code } = await nextBuild(appDir)
if (code !== 0) throw new Error(`build failed with code ${code}`)
appPort = await findPort()
app = await nextStart(appDir, appPort, {
onStderr(msg) {
stderr += msg || ''
},
})
}) })
afterAll(() => killApp(app))
it('should show missing env value when its missing runtime', async () => { describe('server side', () => {
await checkMissingClient('/also-not-missing', 'I_SHOULD_BE_HERE', true) it('should not show missing env value when its not missing public', async () => {
await checkMissing('/not-missing', 'NEXT_PUBLIC_HI')
})
it('should not show missing env value when its not missing runtime', async () => {
await checkMissing('/also-not-missing', 'I_SHOULD_BE_HERE')
})
it('should not show missing env value when its missing normal', async () => {
await checkMissing('/missing', 'hi')
})
it('should not show missing env value when its missing GSP', async () => {
await checkMissing('/missing-gsp', 'SECRET')
})
it('should not show missing env value when its missing GSSP', async () => {
await checkMissing('/missing-gssp', 'SECRET')
})
it('should not show missing env value when its missing API', async () => {
await checkMissing('/api/hi', 'where_is_it')
})
}) })
it('should show missing env value when its missing normal', async () => { describe('client side', () => {
await checkMissingClient('/missing', 'hi', true) it('should not show missing env value when its not missing public', async () => {
await checkMissingClient('/not-missing', 'NEXT_PUBLIC_HI')
})
it('should not show missing env value when its missing runtime', async () => {
await checkMissingClient('/also-not-missing', 'I_SHOULD_BE_HERE')
})
it('should not show missing env value when its missing normal', async () => {
await checkMissingClient('/missing', 'hi')
})
}) })
}) })
}) })
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