Unverified Commit 10408241 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Make serverless-plugin compatible with webpack 5 (#15010)

Co-authored-by: 's avatarZack Jackson <zackary.l.jackson@gmail.com>
parent 6d7cf2a9
import { Compiler } from 'webpack'
import { connectChunkAndModule } from 'webpack/lib/GraphHelpers'
import webpack from 'webpack'
const isWebpack5 = parseInt(webpack.version!) === 5
const GraphHelpers = isWebpack5
? undefined
: require('webpack/lib/GraphHelpers')
/**
* Makes sure there are no dynamic chunks when the target is serverless
......@@ -10,24 +15,33 @@ import { connectChunkAndModule } from 'webpack/lib/GraphHelpers'
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)
}
}
const hook = isWebpack5
? compilation.hooks.optimizeChunks
: compilation.hooks.optimizeChunksBasic
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) {
for (const module of dynamicChunk.modulesIterable) {
if (isWebpack5) {
// Add module back into the entry chunk
chunk.addModule(module)
continue
}
// Webpack 4 has separate GraphHelpers
GraphHelpers.connectChunkAndModule(chunk, module)
}
})
}
}
)
})
})
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment