1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

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

Fixes #4664
This commit is contained in:
Tim Neutkens 2018-06-25 23:06:46 +02:00 committed by GitHub
parent e70c59517b
commit 85769c3d32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 12 deletions

View file

@ -5,14 +5,14 @@ import del from 'del'
import onDemandEntryHandler from './on-demand-entry-handler'
import webpack from 'webpack'
import getBaseWebpackConfig from '../build/webpack'
import UUID from 'uuid'
import {
addCorsSupport
} from './utils'
import {IS_BUNDLED_PAGE_REGEX} from '../lib/constants'
export default class HotReloader {
constructor (dir, { quiet, config } = {}) {
constructor (dir, { quiet, config, buildId } = {}) {
this.buildId = buildId
this.dir = dir
this.quiet = quiet
this.middlewares = []
@ -25,11 +25,6 @@ export default class HotReloader {
this.prevChunkNames = null
this.prevFailedChunkNames = 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
}
@ -62,8 +57,8 @@ export default class HotReloader {
await this.clean()
const configs = await Promise.all([
getBaseWebpackConfig(this.dir, { dev: true, isServer: false, config: this.config }),
getBaseWebpackConfig(this.dir, { dev: true, isServer: true, 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, buildId: this.buildId })
])
const compiler = webpack(configs)

View file

@ -37,8 +37,6 @@ export default class Server {
this.nextConfig = loadConfig(phase, this.dir, conf)
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
// publicRuntimeConfig gets it's default in client/index.js
const {serverRuntimeConfig = {}, publicRuntimeConfig, assetPrefix, generateEtags} = this.nextConfig
@ -48,6 +46,7 @@ export default class Server {
process.exit(1)
}
this.buildId = !dev ? this.readBuildId() : '-'
this.hotReloader = dev ? this.getHotReloader(this.dir, { quiet, config: this.nextConfig, buildId: this.buildId }) : null
this.renderOpts = {
dev,
staticMarkup,

View file

@ -1,5 +1,5 @@
const withCSS = require('@zeit/next-css')
const webpack = require('webpack')
module.exports = withCSS({
onDemandEntries: {
// Make sure entries are not getting disposed.
@ -11,5 +11,14 @@ module.exports = withCSS({
},
publicRuntimeConfig: {
staticFolder: '/static'
},
webpack (config, {buildId}) {
config.plugins.push(
new webpack.DefinePlugin({
'process.env.CONFIG_BUILD_ID': JSON.stringify(buildId)
})
)
return config
}
})

View file

@ -0,0 +1,3 @@
export default () => {
return <p id='buildId'>{process.env.CONFIG_BUILD_ID}</p>
}

View file

@ -24,6 +24,7 @@ describe('Configuration', () => {
// pre-build all pages at the start
await Promise.all([
renderViaHTTP(context.appPort, '/next-config'),
renderViaHTTP(context.appPort, '/build-id'),
renderViaHTTP(context.appPort, '/webpack-css')
])
})

View file

@ -28,5 +28,10 @@ export default function ({ app }, suiteName, render, fetch) {
const $ = await get$('/next-config')
expect($('#server-and-client').text() === '/static')
})
test('renders the build id in development mode', async () => {
const $ = await get$('/build-id')
expect($('#buildId').text() === '-')
})
})
}