2017-10-30 14:57:35 +00:00
|
|
|
import { ConcatSource } from 'webpack-sources'
|
|
|
|
|
2017-12-27 18:59:17 +00:00
|
|
|
const isImportChunk = /^chunks[/\\]/
|
2017-10-30 14:57:35 +00:00
|
|
|
const matchChunkName = /^chunks[/\\](.*)$/
|
|
|
|
|
|
|
|
class DynamicChunkTemplatePlugin {
|
|
|
|
apply (chunkTemplate) {
|
|
|
|
chunkTemplate.plugin('render', function (modules, chunk) {
|
|
|
|
if (!isImportChunk.test(chunk.name)) {
|
|
|
|
return modules
|
|
|
|
}
|
|
|
|
|
|
|
|
const chunkName = matchChunkName.exec(chunk.name)[1]
|
|
|
|
const source = new ConcatSource()
|
|
|
|
|
|
|
|
source.add(`
|
|
|
|
__NEXT_REGISTER_CHUNK('${chunkName}', function() {
|
|
|
|
`)
|
|
|
|
source.add(modules)
|
|
|
|
source.add(`
|
|
|
|
})
|
|
|
|
`)
|
|
|
|
|
|
|
|
return source
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class DynamicChunksPlugin {
|
2017-04-17 20:15:50 +00:00
|
|
|
apply (compiler) {
|
2017-10-30 14:57:35 +00:00
|
|
|
compiler.plugin('compilation', (compilation) => {
|
|
|
|
compilation.chunkTemplate.apply(new DynamicChunkTemplatePlugin())
|
|
|
|
|
|
|
|
compilation.plugin('additional-chunk-assets', (chunks) => {
|
|
|
|
chunks = chunks.filter(chunk =>
|
|
|
|
isImportChunk.test(chunk.name) && compilation.assets[chunk.name]
|
|
|
|
)
|
|
|
|
|
|
|
|
chunks.forEach((chunk) => {
|
|
|
|
// This is to support, webpack dynamic import support with HMR
|
|
|
|
const copyFilename = `chunks/${chunk.name}`
|
|
|
|
compilation.additionalChunkAssets.push(copyFilename)
|
|
|
|
compilation.assets[copyFilename] = compilation.assets[chunk.name]
|
|
|
|
})
|
2017-04-17 20:15:50 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|