Commit 7ebcc5be authored by Henrik Wenz's avatar Henrik Wenz Committed by Tim Neutkens

Eliminate context code based on process.browser (#5159)

* Allow deadCodeElemination when using process.browser

* Add process.browser test
parent 80a6160c
...@@ -274,6 +274,10 @@ export default async function getBaseWebpackConfig (dir: string, {dev = false, i ...@@ -274,6 +274,10 @@ export default async function getBaseWebpackConfig (dir: string, {dev = false, i
// required not to cache removed files // required not to cache removed files
useHashIndex: false useHashIndex: false
}), }),
// Removes server/client code by minifier
new webpack.DefinePlugin({
'process.browser': JSON.stringify(!isServer)
}),
// This is used in client/dev-error-overlay/hot-dev-client.js to replace the dist directory // This is used in client/dev-error-overlay/hot-dev-client.js to replace the dist directory
!isServer && dev && new webpack.DefinePlugin({ !isServer && dev && new webpack.DefinePlugin({
'process.env.__NEXT_DIST_DIR': JSON.stringify(distDir) 'process.env.__NEXT_DIST_DIR': JSON.stringify(distDir)
......
if (process.browser) {
global.__THIS_SHOULD_ONLY_BE_DEFINED_IN_BROWSER_CONTEXT__ = true
}
if (!process.browser) {
global.__THIS_SHOULD_ONLY_BE_DEFINED_IN_SERVER_CONTEXT__ = true
}
export default () => ( export default () => (
<div id='node-env'>{process.env.NODE_ENV}</div> <div id='node-env'>{process.env.NODE_ENV}</div>
) )
/* global describe, it, expect */ /* global describe, it, expect */
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { readFile } from 'fs'
import { promisify } from 'util'
import { join } from 'path'
const readFileAsync = promisify(readFile)
const readNextBuildFile = (relativePath) => readFileAsync(join(__dirname, '../.next', relativePath), 'utf8')
export default (context) => { export default (context) => {
describe('process.env', () => { describe('process.env', () => {
...@@ -10,4 +16,20 @@ export default (context) => { ...@@ -10,4 +16,20 @@ export default (context) => {
browser.close() browser.close()
}) })
}) })
describe('process.browser', () => {
it('should eliminate server only code on the client', async () => {
const buildId = await readNextBuildFile('./BUILD_ID')
const clientCode = await readNextBuildFile(`./static/${buildId}/pages/process-env.js`)
expect(clientCode).toMatch(/__THIS_SHOULD_ONLY_BE_DEFINED_IN_BROWSER_CONTEXT__/)
expect(clientCode).not.toMatch(/__THIS_SHOULD_ONLY_BE_DEFINED_IN_SERVER_CONTEXT__/)
})
it('should eliminate client only code on the server', async () => {
const buildId = await readNextBuildFile('./BUILD_ID')
const serverCode = await readNextBuildFile(`./server/static/${buildId}/pages/process-env.js`)
expect(serverCode).not.toMatch(/__THIS_SHOULD_ONLY_BE_DEFINED_IN_BROWSER_CONTEXT__/)
expect(serverCode).toMatch(/__THIS_SHOULD_ONLY_BE_DEFINED_IN_SERVER_CONTEXT__/)
})
})
} }
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