rsnext/packages/next/build/webpack/plugins/serverless-plugin.ts
Tim Neutkens 351d225fc5
Remove isWebpack5 checks (#29677)
* Remove isWebpack5 checks

* Remove next-babel-loader (replaced by babel-turbo-loader), hotSelfAccept, and BuildStatsPlugin

* Remove cacache file

* Remove unused variable

* remove old test

* lint-fix

* update babel-loader tests

* lint-fix

* update compiled

Co-authored-by: jj@jjsweb.site <jj@jjsweb.site>
2021-10-06 18:46:46 -05:00

36 lines
1.2 KiB
TypeScript

import { webpack } from 'next/dist/compiled/webpack/webpack'
/**
* 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: webpack.Compiler) {
compiler.hooks.compilation.tap('ServerlessPlugin', (compilation) => {
const hook = compilation.hooks.optimizeChunks
hook.tap('ServerlessPlugin', (chunks) => {
for (const chunk of chunks) {
// If chunk is not an entry point skip them
if (!chunk.hasEntryModule()) {
continue
}
// Async chunks are usages of import() for example
const dynamicChunks = chunk.getAllAsyncChunks()
for (const dynamicChunk of dynamicChunks) {
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
for (const module of compilation.chunkGraph.getChunkModulesIterable(
dynamicChunk
)) {
// Add module back into the entry chunk
chunk.addModule(module)
}
}
}
})
})
}
}