rsnext/packages/next/build/webpack/plugins/serverless-plugin.ts
2020-05-18 15:24:37 -04:00

33 lines
1.1 KiB
TypeScript

import { Compiler } from 'webpack'
import { connectChunkAndModule } from 'webpack/lib/GraphHelpers'
/**
* Makes sure there are no dynamic chunks when the target is serverless
* The dynamic chunks are integrated back into their parent chunk
* This is to make sure there is a single render bundle instead of that bundle importing dynamic chunks
*/
export class ServerlessPlugin {
apply(compiler: Compiler) {
compiler.hooks.compilation.tap('ServerlessPlugin', (compilation) => {
compilation.hooks.optimizeChunksBasic.tap(
'ServerlessPlugin',
(chunks) => {
chunks.forEach((chunk) => {
// If chunk is not an entry point skip them
if (chunk.hasEntryModule()) {
const dynamicChunks = chunk.getAllAsyncChunks()
if (dynamicChunks.size !== 0) {
for (const dynamicChunk of dynamicChunks) {
for (const module of dynamicChunk.modulesIterable) {
connectChunkAndModule(chunk, module)
}
}
}
}
})
}
)
})
}
}