1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/server/build/plugins/dynamic-chunks-plugin.js
Arunoda Susiripala 337fb6a9aa Remove buildId from dynamic import URLs (#3498)
* Use without .js for the filename.

* Modify the chunk filename to add .js via webpack

* Add import chunk's hash to the filename via webpack.

* Remove buildId from dynamic import urls.

* Make sure next-export work with dynamic imports
2017-12-27 19:59:17 +01:00

49 lines
1.3 KiB
JavaScript

import { ConcatSource } from 'webpack-sources'
const isImportChunk = /^chunks[/\\]/
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 {
apply (compiler) {
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]
})
})
})
}
}