Unverified Commit 85769c3d authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Pass through buildId to webpack config in development (#4665)

Fixes #4664
parent e70c5951
...@@ -5,14 +5,14 @@ import del from 'del' ...@@ -5,14 +5,14 @@ import del from 'del'
import onDemandEntryHandler from './on-demand-entry-handler' import onDemandEntryHandler from './on-demand-entry-handler'
import webpack from 'webpack' import webpack from 'webpack'
import getBaseWebpackConfig from '../build/webpack' import getBaseWebpackConfig from '../build/webpack'
import UUID from 'uuid'
import { import {
addCorsSupport addCorsSupport
} from './utils' } from './utils'
import {IS_BUNDLED_PAGE_REGEX} from '../lib/constants' import {IS_BUNDLED_PAGE_REGEX} from '../lib/constants'
export default class HotReloader { export default class HotReloader {
constructor (dir, { quiet, config } = {}) { constructor (dir, { quiet, config, buildId } = {}) {
this.buildId = buildId
this.dir = dir this.dir = dir
this.quiet = quiet this.quiet = quiet
this.middlewares = [] this.middlewares = []
...@@ -25,11 +25,6 @@ export default class HotReloader { ...@@ -25,11 +25,6 @@ export default class HotReloader {
this.prevChunkNames = null this.prevChunkNames = null
this.prevFailedChunkNames = null this.prevFailedChunkNames = null
this.prevChunkHashes = null this.prevChunkHashes = null
// Here buildId could be any value.
// Our router accepts any value in the dev mode.
// But for the webpack-compiler and for the webpack-dev-server
// it should be the same value.
this.buildId = UUID.v4()
this.config = config this.config = config
} }
...@@ -62,8 +57,8 @@ export default class HotReloader { ...@@ -62,8 +57,8 @@ export default class HotReloader {
await this.clean() await this.clean()
const configs = await Promise.all([ const configs = await Promise.all([
getBaseWebpackConfig(this.dir, { dev: true, isServer: false, config: this.config }), getBaseWebpackConfig(this.dir, { dev: true, isServer: false, config: this.config, buildId: this.buildId }),
getBaseWebpackConfig(this.dir, { dev: true, isServer: true, config: this.config }) getBaseWebpackConfig(this.dir, { dev: true, isServer: true, config: this.config, buildId: this.buildId })
]) ])
const compiler = webpack(configs) const compiler = webpack(configs)
......
...@@ -37,8 +37,6 @@ export default class Server { ...@@ -37,8 +37,6 @@ export default class Server {
this.nextConfig = loadConfig(phase, this.dir, conf) this.nextConfig = loadConfig(phase, this.dir, conf)
this.distDir = join(this.dir, this.nextConfig.distDir) this.distDir = join(this.dir, this.nextConfig.distDir)
this.hotReloader = dev ? this.getHotReloader(this.dir, { quiet, config: this.nextConfig }) : null
// Only serverRuntimeConfig needs the default // Only serverRuntimeConfig needs the default
// publicRuntimeConfig gets it's default in client/index.js // publicRuntimeConfig gets it's default in client/index.js
const {serverRuntimeConfig = {}, publicRuntimeConfig, assetPrefix, generateEtags} = this.nextConfig const {serverRuntimeConfig = {}, publicRuntimeConfig, assetPrefix, generateEtags} = this.nextConfig
...@@ -48,6 +46,7 @@ export default class Server { ...@@ -48,6 +46,7 @@ export default class Server {
process.exit(1) process.exit(1)
} }
this.buildId = !dev ? this.readBuildId() : '-' this.buildId = !dev ? this.readBuildId() : '-'
this.hotReloader = dev ? this.getHotReloader(this.dir, { quiet, config: this.nextConfig, buildId: this.buildId }) : null
this.renderOpts = { this.renderOpts = {
dev, dev,
staticMarkup, staticMarkup,
......
const withCSS = require('@zeit/next-css') const withCSS = require('@zeit/next-css')
const webpack = require('webpack')
module.exports = withCSS({ module.exports = withCSS({
onDemandEntries: { onDemandEntries: {
// Make sure entries are not getting disposed. // Make sure entries are not getting disposed.
...@@ -11,5 +11,14 @@ module.exports = withCSS({ ...@@ -11,5 +11,14 @@ module.exports = withCSS({
}, },
publicRuntimeConfig: { publicRuntimeConfig: {
staticFolder: '/static' staticFolder: '/static'
},
webpack (config, {buildId}) {
config.plugins.push(
new webpack.DefinePlugin({
'process.env.CONFIG_BUILD_ID': JSON.stringify(buildId)
})
)
return config
} }
}) })
export default () => {
return <p id='buildId'>{process.env.CONFIG_BUILD_ID}</p>
}
...@@ -24,6 +24,7 @@ describe('Configuration', () => { ...@@ -24,6 +24,7 @@ describe('Configuration', () => {
// pre-build all pages at the start // pre-build all pages at the start
await Promise.all([ await Promise.all([
renderViaHTTP(context.appPort, '/next-config'), renderViaHTTP(context.appPort, '/next-config'),
renderViaHTTP(context.appPort, '/build-id'),
renderViaHTTP(context.appPort, '/webpack-css') renderViaHTTP(context.appPort, '/webpack-css')
]) ])
}) })
......
...@@ -28,5 +28,10 @@ export default function ({ app }, suiteName, render, fetch) { ...@@ -28,5 +28,10 @@ export default function ({ app }, suiteName, render, fetch) {
const $ = await get$('/next-config') const $ = await get$('/next-config')
expect($('#server-and-client').text() === '/static') expect($('#server-and-client').text() === '/static')
}) })
test('renders the build id in development mode', async () => {
const $ = await get$('/build-id')
expect($('#buildId').text() === '-')
})
}) })
} }
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