Skip to content

Commit 964f229

Browse files
kpdeckertimneutkens
authored andcommitted
Sourcemap and Breakpoint Fixes (#3121)
* Propagate source maps through combine assets step * Use constant development build id
1 parent f1f8350 commit 964f229

File tree

5 files changed

+97
-88
lines changed

5 files changed

+97
-88
lines changed
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ConcatSource } from 'webpack-sources'
2+
13
// This plugin combines a set of assets into a single asset
24
// This should be only used with text assets,
35
// otherwise the result is unpredictable.
@@ -8,23 +10,23 @@ export default class CombineAssetsPlugin {
810
}
911

1012
apply (compiler) {
11-
compiler.plugin('after-compile', (compilation, callback) => {
12-
let newSource = ''
13-
this.input.forEach((name) => {
14-
const asset = compilation.assets[name]
15-
if (!asset) return
13+
compiler.plugin('compilation', (compilation) => {
14+
compilation.plugin('optimize-chunk-assets', (chunks, callback) => {
15+
const concat = new ConcatSource()
1616

17-
newSource += `${asset.source()}\n`
17+
this.input.forEach((name) => {
18+
const asset = compilation.assets[name]
19+
if (!asset) return
1820

19-
// We keep existing assets since that helps when analyzing the bundle
20-
})
21+
concat.add(asset)
2122

22-
compilation.assets[this.output] = {
23-
source: () => newSource,
24-
size: () => newSource.length
25-
}
23+
// We keep existing assets since that helps when analyzing the bundle
24+
})
2625

27-
callback()
26+
compilation.assets[this.output] = concat
27+
28+
callback()
29+
})
2830
})
2931
}
3032
}
Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
1-
export default class PagesPlugin {
1+
import { ConcatSource } from 'webpack-sources'
2+
3+
export default class DynamicChunksPlugin {
24
apply (compiler) {
35
const isImportChunk = /^chunks[/\\].*\.js$/
46
const matchChunkName = /^chunks[/\\](.*)$/
57

6-
compiler.plugin('after-compile', (compilation, callback) => {
7-
const chunks = Object
8-
.keys(compilation.namedChunks)
9-
.map(key => compilation.namedChunks[key])
10-
.filter(chunk => isImportChunk.test(chunk.name))
11-
12-
chunks.forEach((chunk) => {
13-
const asset = compilation.assets[chunk.name]
14-
if (!asset) return
15-
16-
const chunkName = matchChunkName.exec(chunk.name)[1]
17-
18-
const content = asset.source()
19-
const newContent = `
20-
window.__NEXT_REGISTER_CHUNK('${chunkName}', function() {
21-
${content}
22-
})
23-
`
24-
// Replace the exisiting chunk with the new content
25-
compilation.assets[chunk.name] = {
26-
source: () => newContent,
27-
size: () => newContent.length
28-
}
29-
30-
// This is to support, webpack dynamic import support with HMR
31-
compilation.assets[`chunks/${chunk.id}`] = {
32-
source: () => newContent,
33-
size: () => newContent.length
34-
}
8+
compiler.plugin('compilation', (compilation) => {
9+
compilation.plugin('optimize-chunk-assets', (chunks, callback) => {
10+
chunks = chunks.filter(chunk => isImportChunk.test(chunk.name))
11+
12+
chunks.forEach((chunk) => {
13+
const asset = compilation.assets[chunk.name]
14+
if (!asset) return
15+
16+
const chunkName = matchChunkName.exec(chunk.name)[1]
17+
const concat = new ConcatSource()
18+
19+
concat.add(`__NEXT_REGISTER_CHUNK('${chunkName}', function() {
20+
`)
21+
concat.add(asset)
22+
concat.add(`
23+
})
24+
`)
25+
26+
// Replace the exisiting chunk with the new content
27+
compilation.assets[chunk.name] = concat
28+
29+
// This is to support, webpack dynamic import support with HMR
30+
compilation.assets[`chunks/${chunk.name}`] = concat
31+
})
32+
33+
callback()
3534
})
36-
callback()
3735
})
3836
}
3937
}
Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,53 @@
1+
import { ConcatSource } from 'webpack-sources'
12
import {
23
IS_BUNDLED_PAGE,
34
MATCH_ROUTE_NAME
45
} from '../../utils'
56

67
export default class PagesPlugin {
78
apply (compiler) {
8-
compiler.plugin('after-compile', (compilation, callback) => {
9-
const pages = Object
10-
.keys(compilation.namedChunks)
11-
.map(key => compilation.namedChunks[key])
12-
.filter(chunk => IS_BUNDLED_PAGE.test(chunk.name))
13-
14-
pages.forEach((chunk) => {
15-
const page = compilation.assets[chunk.name]
16-
const pageName = MATCH_ROUTE_NAME.exec(chunk.name)[1]
17-
let routeName = pageName
18-
19-
// We need to convert \ into / when we are in windows
20-
// to get the proper route name
21-
// Here we need to do windows check because it's possible
22-
// to have "\" in the filename in unix.
23-
// Anyway if someone did that, he'll be having issues here.
24-
// But that's something we cannot avoid.
25-
if (/^win/.test(process.platform)) {
26-
routeName = routeName.replace(/\\/g, '/')
27-
}
28-
29-
routeName = `/${routeName.replace(/(^|\/)index$/, '')}`
30-
31-
const content = page.source()
32-
const newContent = `
33-
window.__NEXT_REGISTER_PAGE('${routeName}', function() {
34-
var comp = ${content}
35-
return { page: comp.default }
36-
})
37-
`
38-
// Replace the exisiting chunk with the new content
39-
compilation.assets[chunk.name] = {
40-
source: () => newContent,
41-
size: () => newContent.length
42-
}
9+
compiler.plugin('compilation', (compilation) => {
10+
compilation.plugin('optimize-chunk-assets', (chunks, callback) => {
11+
const pages = chunks.filter(chunk => IS_BUNDLED_PAGE.test(chunk.name))
12+
13+
pages.forEach((chunk) => {
14+
const pageName = MATCH_ROUTE_NAME.exec(chunk.name)[1]
15+
let routeName = pageName
16+
17+
// We need to convert \ into / when we are in windows
18+
// to get the proper route name
19+
// Here we need to do windows check because it's possible
20+
// to have "\" in the filename in unix.
21+
// Anyway if someone did that, he'll be having issues here.
22+
// But that's something we cannot avoid.
23+
if (/^win/.test(process.platform)) {
24+
routeName = routeName.replace(/\\/g, '/')
25+
}
26+
27+
routeName = `/${routeName.replace(/(^|\/)index$/, '')}`
28+
29+
// Replace the exisiting chunk with the new content
30+
const asset = compilation.assets[chunk.name]
31+
if (!asset) return
32+
33+
const concat = new ConcatSource()
34+
35+
concat.add(`
36+
__NEXT_REGISTER_PAGE('${routeName}', function() {
37+
var comp =
38+
`)
39+
concat.add(asset)
40+
concat.add(`
41+
return { page: comp.default }
42+
})
43+
`)
44+
45+
// Replace the exisiting chunk with the new content
46+
compilation.assets[chunk.name] = concat
47+
})
48+
49+
callback()
4350
})
44-
callback()
4551
})
4652
}
4753
}

server/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,11 @@ export default class Server {
408408
}
409409

410410
handleBuildId (buildId, res) {
411-
if (this.dev) return true
411+
if (this.dev) {
412+
res.setHeader('Cache-Control', 'no-store, must-revalidate')
413+
return true
414+
}
415+
412416
if (buildId !== this.renderOpts.buildId) {
413417
return false
414418
}
@@ -428,13 +432,17 @@ export default class Server {
428432
}
429433

430434
handleBuildHash (filename, hash, res) {
431-
if (this.dev) return
435+
if (this.dev) {
436+
res.setHeader('Cache-Control', 'no-store, must-revalidate')
437+
return true
438+
}
432439

433440
if (hash !== this.buildStats[filename].hash) {
434441
throw new Error(`Invalid Build File Hash(${hash}) for chunk: ${filename}`)
435442
}
436443

437444
res.setHeader('Cache-Control', 'max-age=365000000, immutable')
445+
return true
438446
}
439447

440448
send404 (res) {

server/render.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@ async function doRender (req, res, pathname, query, {
9393
}
9494

9595
const docProps = await loadGetInitialProps(Document, { ...ctx, renderPage })
96-
// While developing, we should not cache any assets.
97-
// So, we use a different buildId for each page load.
98-
// With that we can ensure, we have unique URL for assets per every page load.
99-
// So, it'll prevent issues like this: https://git.io/vHLtb
100-
const devBuildId = Date.now()
10196

10297
if (res.finished) return
10398

@@ -107,7 +102,7 @@ async function doRender (req, res, pathname, query, {
107102
props,
108103
pathname,
109104
query,
110-
buildId: dev ? devBuildId : buildId,
105+
buildId,
111106
buildStats,
112107
assetPrefix,
113108
nextExport,

0 commit comments

Comments
 (0)