2017-04-17 20:15:50 +00:00
|
|
|
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
|
|
|
|
}
|
2017-04-27 19:41:02 +00:00
|
|
|
|
|
|
|
// This is to support, webpack dynamic import support with HMR
|
|
|
|
compilation.assets[`chunks/${chunk.id}`] = {
|
|
|
|
source: () => newContent,
|
|
|
|
size: () => newContent.length
|
|
|
|
}
|
2017-04-17 20:15:50 +00:00
|
|
|
})
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|