8000 Revert "Sourcemap and Breakpoint Fixes (#3121)" · JavaScriptExpert/next.js@1cc3dbe · GitHub
[go: up one dir, main page]

Skip to content

Commit 1cc3dbe

Browse files
committed
Revert "Sourcemap and Breakpoint Fixes (vercel#3121)"
This reverts commit 964f229.
1 parent 81479eb commit 1cc3dbe

File tree

5 files changed

+88
-97
lines changed

5 files changed

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

1210
apply (compiler) {
13-
compiler.plugin('compilation', (compilation) => {
14-
compilation.plugin('optimize-chunk-assets', (chunks, 8000 callback) => {
15-
const concat = new ConcatSource()
16-
17-
this.input.forEach((name) => {
18-
const asset = compilation.assets[name]
19-
if (!asset) return
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
2016

21-
concat.add(asset)
17+
newSource += `${asset.source()}\n`
2218

23-
// We keep existing assets since that helps when analyzing the bundle
24-
})
19+
// We keep existing assets since that helps when analyzing the bundle
20+
})
2521

26-
compilation.assets[this.output] = concat
22+
compilation.assets[this.output] = {
23+
source: () => newSource,
24+
size: () => newSource.length
25+
}
2726

28-
callback()
29-
})
27+
callback()
3028
})
3129
}
3230
}
Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1-
import { ConcatSource } from 'webpack-sources'
2-
3-
export default class DynamicChunksPlugin {
1+
export default class PagesPlugin {
42
apply (compiler) {
53
const isImportChunk = /^chunks[/\\].*\.js$/
64
const matchChunkName = /^chunks[/\\](.*)$/
75

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()
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+
}
3435
})
36+
callback()
3537
})
3638
}
3739
}

server/build/plugins/pages-plugin.js

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,47 @@
1-
import { ConcatSource } from 'webpack-sources'
21
import {
32
IS_BUNDLED_PAGE,
43
MATCH_ROUTE_NAME
54
} from '../../utils'
65

76
export default class PagesPlugin {
87
apply (compiler) {
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()
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+
}
5043
})
44+
callback()
5145
})
5246
}
5347
}

server/index.js

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

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

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

440433
if (hash !== this.buildStats[filename].hash) {
441434
throw new Error(`Invalid Build File Hash(${hash}) for chunk: ${filename}`)
442435
}
443436

444437
res.setHeader('Cache-Control', 'max-age=365000000, immutable')
445-
return true
446438
}
447439

448440
send404 (res) {

server/render.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ 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()
96101

97102
if (res.finished) return
98103

@@ -102,7 +107,7 @@ async function doRender (req, res, pathname, query, {
102107
props,
103108
pathname,
104109
query,
105-
buildId,
110+
buildId: dev ? devBuildId : buildId,
106111
buildStats,
107112
assetPrefix,
108113
nextExport,

0 commit comments

Comments
 (0)
0