78cb07a4ca
## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [x] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [x] Make sure the linting passes by running `pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
37 lines
1.3 KiB
TypeScript
37 lines
1.3 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 (compilation.chunkGraph.getNumberOfEntryModules(chunk) === 0) {
|
|
continue
|
|
}
|
|
|
|
// Async chunks are usages of import() for example
|
|
const dynamicChunks = chunk.getAllAsyncChunks()
|
|
for (const dynamicChunk of dynamicChunks) {
|
|
for (const module of compilation.chunkGraph.getChunkModulesIterable(
|
|
dynamicChunk
|
|
)) {
|
|
// Add module back into the entry chunk
|
|
if (!compilation.chunkGraph.isModuleInChunk(module, chunk)) {
|
|
compilation.chunkGraph.connectChunkAndModule(chunk, module)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|