Skip to content

Commit 6ef39a7

Browse files
committed
Propagate source maps through combine assets step
1 parent f7d2b96 commit 6ef39a7

File tree

3 files changed

+86
-80
lines changed

3 files changed

+86
-80
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
}

0 commit comments

Comments
 (0)