Commit 964f229f authored by Kevin Decker's avatar Kevin Decker Committed by Tim Neutkens

Sourcemap and Breakpoint Fixes (#3121)

* Propagate source maps through combine assets step

* Use constant development build id
parent f1f83507
import { ConcatSource } from 'webpack-sources'
// This plugin combines a set of assets into a single asset // This plugin combines a set of assets into a single asset
// This should be only used with text assets, // This should be only used with text assets,
// otherwise the result is unpredictable. // otherwise the result is unpredictable.
...@@ -8,23 +10,23 @@ export default class CombineAssetsPlugin { ...@@ -8,23 +10,23 @@ export default class CombineAssetsPlugin {
} }
apply (compiler) { apply (compiler) {
compiler.plugin('after-compile', (compilation, callback) => { compiler.plugin('compilation', (compilation) => {
let newSource = '' compilation.plugin('optimize-chunk-assets', (chunks, callback) => {
this.input.forEach((name) => { const concat = new ConcatSource()
const asset = compilation.assets[name]
if (!asset) return
newSource += `${asset.source()}\n` this.input.forEach((name) => {
const asset = compilation.assets[name]
if (!asset) return
// We keep existing assets since that helps when analyzing the bundle concat.add(asset)
})
compilation.assets[this.output] = { // We keep existing assets since that helps when analyzing the bundle
source: () => newSource, })
size: () => newSource.length
}
callback() compilation.assets[this.output] = concat
callback()
})
}) })
} }
} }
export default class PagesPlugin { import { ConcatSource } from 'webpack-sources'
export default class DynamicChunksPlugin {
apply (compiler) { apply (compiler) {
const isImportChunk = /^chunks[/\\].*\.js$/ const isImportChunk = /^chunks[/\\].*\.js$/
const matchChunkName = /^chunks[/\\](.*)$/ const matchChunkName = /^chunks[/\\](.*)$/
compiler.plugin('after-compile', (compilation, callback) => { compiler.plugin('compilation', (compilation) => {
const chunks = Object compilation.plugin('optimize-chunk-assets', (chunks, callback) => {
.keys(compilation.namedChunks) chunks = chunks.filter(chunk => isImportChunk.test(chunk.name))
.map(key => compilation.namedChunks[key])
.filter(chunk => isImportChunk.test(chunk.name)) chunks.forEach((chunk) => {
const asset = compilation.assets[chunk.name]
chunks.forEach((chunk) => { if (!asset) return
const asset = compilation.assets[chunk.name]
if (!asset) return const chunkName = matchChunkName.exec(chunk.name)[1]
const concat = new ConcatSource()
const chunkName = matchChunkName.exec(chunk.name)[1]
concat.add(`__NEXT_REGISTER_CHUNK('${chunkName}', function() {
const content = asset.source() `)
const newContent = ` concat.add(asset)
window.__NEXT_REGISTER_CHUNK('${chunkName}', function() { concat.add(`
${content} })
}) `)
`
// Replace the exisiting chunk with the new content // Replace the exisiting chunk with the new content
compilation.assets[chunk.name] = { compilation.assets[chunk.name] = concat
source: () => newContent,
size: () => newContent.length // This is to support, webpack dynamic import support with HMR
} compilation.assets[`chunks/${chunk.name}`] = concat
})
// This is to support, webpack dynamic import support with HMR
compilation.assets[`chunks/${chunk.id}`] = { callback()
source: () => newContent,
size: () => newContent.length
}
}) })
callback()
}) })
} }
} }
import { ConcatSource } from 'webpack-sources'
import { import {
IS_BUNDLED_PAGE, IS_BUNDLED_PAGE,
MATCH_ROUTE_NAME MATCH_ROUTE_NAME
...@@ -5,43 +6,48 @@ import { ...@@ -5,43 +6,48 @@ import {
export default class PagesPlugin { export default class PagesPlugin {
apply (compiler) { apply (compiler) {
compiler.plugin('after-compile', (compilation, callback) => { compiler.plugin('compilation', (compilation) => {
const pages = Object compilation.plugin('optimize-chunk-assets', (chunks, callback) => {
.keys(compilation.namedChunks) const pages = chunks.filter(chunk => IS_BUNDLED_PAGE.test(chunk.name))
.map(key => compilation.namedChunks[key])
.filter(chunk => IS_BUNDLED_PAGE.test(chunk.name)) pages.forEach((chunk) => {
const pageName = MATCH_ROUTE_NAME.exec(chunk.name)[1]
pages.forEach((chunk) => { let routeName = pageName
const page = compilation.assets[chunk.name]
const pageName = MATCH_ROUTE_NAME.exec(chunk.name)[1] // We need to convert \ into / when we are in windows
let routeName = pageName // to get the proper route name
// Here we need to do windows check because it's possible
// We need to convert \ into / when we are in windows // to have "\" in the filename in unix.
// to get the proper route name // Anyway if someone did that, he'll be having issues here.
// Here we need to do windows check because it's possible // But that's something we cannot avoid.
// to have "\" in the filename in unix. if (/^win/.test(process.platform)) {
// Anyway if someone did that, he'll be having issues here. routeName = routeName.replace(/\\/g, '/')
// But that's something we cannot avoid. }
if (/^win/.test(process.platform)) {
routeName = routeName.replace(/\\/g, '/') routeName = `/${routeName.replace(/(^|\/)index$/, '')}`
}
// Replace the exisiting chunk with the new content
routeName = `/${routeName.replace(/(^|\/)index$/, '')}` const asset = compilation.assets[chunk.name]
if (!asset) return
const content = page.source()
const newContent = ` const concat = new ConcatSource()
window.__NEXT_REGISTER_PAGE('${routeName}', function() {
var comp = ${content} concat.add(`
return { page: comp.default } __NEXT_REGISTER_PAGE('${routeName}', function() {
}) var comp =
` `)
// Replace the exisiting chunk with the new content concat.add(asset)
compilation.assets[chunk.name] = { concat.add(`
source: () => newContent, return { page: comp.default }
size: () => newContent.length })
} `)
// Replace the exisiting chunk with the new content
compilation.assets[chunk.name] = concat
})
callback()
}) })
callback()
}) })
} }
} }
...@@ -408,7 +408,11 @@ export default class Server { ...@@ -408,7 +408,11 @@ export default class Server {
} }
handleBuildId (buildId, res) { handleBuildId (buildId, res) {
if (this.dev) return true if (this.dev) {
res.setHeader('Cache-Control', 'no-store, must-revalidate')
return true
}
if (buildId !== this.renderOpts.buildId) { if (buildId !== this.renderOpts.buildId) {
return false return false
} }
...@@ -428,13 +432,17 @@ export default class Server { ...@@ -428,13 +432,17 @@ export default class Server {
} }
handleBuildHash (filename, hash, res) { handleBuildHash (filename, hash, res) {
if (this.dev) return if (this.dev) {
res.setHeader('Cache-Control', 'no-store, must-revalidate')
return true
}
if (hash !== this.buildStats[filename].hash) { if (hash !== this.buildStats[filename].hash) {
throw new Error(`Invalid Build File Hash(${hash}) for chunk: ${filename}`) throw new Error(`Invalid Build File Hash(${hash}) for chunk: ${filename}`)
} }
res.setHeader('Cache-Control', 'max-age=365000000, immutable') res.setHeader('Cache-Control', 'max-age=365000000, immutable')
return true
} }
send404 (res) { send404 (res) {
......
...@@ -93,11 +93,6 @@ async function doRender (req, res, pathname, query, { ...@@ -93,11 +93,6 @@ async function doRender (req, res, pathname, query, {
} }
const docProps = await loadGetInitialProps(Document, { ...ctx, renderPage }) const docProps = await loadGetInitialProps(Document, { ...ctx, renderPage })
// While developing, we should not cache any assets.
// So, we use a different buildId for each page load.
// With that we can ensure, we have unique URL for assets per every page load.
// So, it'll prevent issues like this: https://git.io/vHLtb
const devBuildId = Date.now()
if (res.finished) return if (res.finished) return
...@@ -107,7 +102,7 @@ async function doRender (req, res, pathname, query, { ...@@ -107,7 +102,7 @@ async function doRender (req, res, pathname, query, {
props, props,
pathname, pathname,
query, query,
buildId: dev ? devBuildId : buildId, buildId,
buildStats, buildStats,
assetPrefix, assetPrefix,
nextExport, nextExport,
......
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