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 { 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 * Makes sure there are no dynamic chunks when the target is serverless
...@@ -10,24 +15,33 @@ import { connectChunkAndModule } from 'webpack/lib/GraphHelpers' ...@@ -10,24 +15,33 @@ import { connectChunkAndModule } from 'webpack/lib/GraphHelpers'
export class ServerlessPlugin { export class ServerlessPlugin {
apply(compiler: Compiler) { apply(compiler: Compiler) {
compiler.hooks.compilation.tap('ServerlessPlugin', (compilation) => { compiler.hooks.compilation.tap('ServerlessPlugin', (compilation) => {
compilation.hooks.optimizeChunksBasic.tap( const hook = isWebpack5
'ServerlessPlugin', ? compilation.hooks.optimizeChunks
(chunks) => { : compilation.hooks.optimizeChunksBasic
chunks.forEach((chunk) => {
// If chunk is not an entry point skip them hook.tap('ServerlessPlugin', (chunks) => {
if (chunk.hasEntryModule()) { for (const chunk of chunks) {
const dynamicChunks = chunk.getAllAsyncChunks() // If chunk is not an entry point skip them
if (dynamicChunks.size !== 0) { if (!chunk.hasEntryModule()) {
for (const dynamicChunk of dynamicChunks) { continue
for (const module of dynamicChunk.modulesIterable) { }
connectChunkAndModule(chunk, module)
} // 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