forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic-chunks-plugin.js
More file actions
39 lines (34 loc) · 1.16 KB
/
dynamic-chunks-plugin.js
File metadata and controls
39 lines (34 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
export default class PagesPlugin {
apply (compiler) {
const isImportChunk = /^chunks[/\\].*\.js$/
const matchChunkName = /^chunks[/\\](.*)$/
compiler.plugin('after-compile', (compilation, callback) => {
const chunks = Object
.keys(compilation.namedChunks)
.map(key => compilation.namedChunks[key])
.filter(chunk => isImportChunk.test(chunk.name))
chunks.forEach((chunk) => {
const asset = compilation.assets[chunk.name]
if (!asset) return
const chunkName = matchChunkName.exec(chunk.name)[1]
const content = asset.source()
const newContent = `
window.__NEXT_REGISTER_CHUNK('${chunkName}', function() {
${content}
})
`
// Replace the exisiting chunk with the new content
compilation.assets[chunk.name] = {
source: () => newContent,
size: () => newContent.length
}
// This is to support, webpack dynamic import support with HMR
compilation.assets[`chunks/${chunk.id}`] = {
source: () => newContent,
size: () => newContent.length
}
})
callback()
})
}
}