Skip to content

Commit 3413439

Browse files
fix: compatibility with webpack@4 and webpack@5 for monorepos (#636)
1 parent 89e7a0a commit 3413439

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/index.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ const {
2020
util: { createHash },
2121
} = webpack;
2222

23-
const isWebpack4 = webpackVersion[0] === '4';
24-
2523
const pluginName = 'mini-css-extract-plugin';
2624

2725
const REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/i;
@@ -97,16 +95,12 @@ class MiniCssExtractPlugin {
9795
this.options.chunkFilename = '[id].css';
9896
}
9997
}
100-
101-
if (!isWebpack4 && 'hmr' in this.options) {
102-
throw new Error(
103-
"The 'hmr' option doesn't exist for the mini-css-extract-plugin when using webpack 5 (it's automatically determined)"
104-
);
105-
}
10698
}
10799

108100
/** @param {import("webpack").Compiler} compiler */
109101
apply(compiler) {
102+
const isWebpack4 = compiler.webpack ? false : webpackVersion[0] === '4';
103+
110104
if (!isWebpack4) {
111105
const { splitChunks } = compiler.options.optimization;
112106
if (splitChunks) {
@@ -200,7 +194,9 @@ class MiniCssExtractPlugin {
200194

201195
// We don't need hot update chunks for css
202196
// We will use the real asset instead to update
203-
if (chunk instanceof webpack.HotUpdateChunk) return;
197+
if (chunk instanceof webpack.HotUpdateChunk) {
198+
return;
199+
}
204200

205201
const renderedModules = Array.from(
206202
this.getChunkModules(chunk, chunkGraph)
@@ -439,7 +435,10 @@ class MiniCssExtractPlugin {
439435
} else {
440436
const enabledChunks = new WeakSet();
441437
const handler = (chunk, set) => {
442-
if (enabledChunks.has(chunk)) return;
438+
if (enabledChunks.has(chunk)) {
439+
return;
440+
}
441+
443442
enabledChunks.add(chunk);
444443

445444
// eslint-disable-next-line global-require

src/loader.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import schema from './loader-options.json';
1616

1717
const pluginName = 'mini-css-extract-plugin';
1818

19-
const isWebpack4 = webpackVersion[0] === '4';
20-
2119
function hotLoader(content, context) {
2220
const accept = context.locals
2321
? ''
@@ -105,6 +103,8 @@ export function pitch(request) {
105103

106104
let source;
107105

106+
const isWebpack4 = childCompiler.webpack ? false : webpackVersion[0] === '4';
107+
108108
if (isWebpack4) {
109109
childCompiler.hooks.afterCompile.tap(pluginName, (compilation) => {
110110
source =

0 commit comments

Comments
 (0)