Skip to content

Commit 6e1bb67

Browse files
perf: fix (#774)
1 parent 5551fbd commit 6e1bb67

33 files changed

+60
-24
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

+55-19
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@ class MiniCssExtractPlugin {
5858
this.content = content;
5959
this.media = media;
6060
this.sourceMap = sourceMap;
61-
this.buildInfo = {
62-
assets,
63-
assetsInfo,
64-
};
65-
this.buildMeta = {};
61+
this.assets = assets;
62+
this.assetsInfo = assetsInfo;
63+
this._needBuild = true;
6664
}
6765

6866
// no source() so webpack 4 doesn't do add stuff to the bundle
@@ -103,34 +101,60 @@ class MiniCssExtractPlugin {
103101
}
104102

105103
updateCacheModule(module) {
106-
this.content = module.content;
107-
this.media = module.media;
108-
this.sourceMap = module.sourceMap;
104+
if (
105+
this.content !== module.content ||
106+
this.media !== module.media ||
107+
this.sourceMap !== module.sourceMap ||
108+
this.assets !== module.assets ||
109+
this.assetsInfo !== module.assetsInfo
110+
) {
111+
this._needBuild = true;
112+
113+
this.content = module.content;
114+
this.media = module.media;
115+
this.sourceMap = module.sourceMap;
116+
this.assets = module.assets;
117+
this.assetsInfo = module.assetsInfo;
118+
}
109119
}
110120

111121
// eslint-disable-next-line class-methods-use-this
112122
needRebuild() {
113-
return true;
123+
return this._needBuild;
114124
}
115125

116126
// eslint-disable-next-line class-methods-use-this
117127
needBuild(context, callback) {
118-
callback(null, false);
128+
callback(null, this._needBuild);
119129
}
120130

121131
build(options, compilation, resolver, fileSystem, callback) {
122-
this.buildInfo = {};
132+
this.buildInfo = {
133+
assets: this.assets,
134+
assetsInfo: this.assetsInfo,
135+
cacheable: true,
136+
hash: this._computeHash(compilation.outputOptions.hashFunction),
137+
};
123138
this.buildMeta = {};
139+
this._needBuild = false;
124140

125141
callback();
126142
}
127143

128-
updateHash(hash, context) {
129-
super.updateHash(hash, context);
144+
_computeHash(hashFunction) {
145+
const hash = webpack.util.createHash(hashFunction);
130146

131147
hash.update(this.content);
132148
hash.update(this.media || '');
133-
hash.update(this.sourceMap ? JSON.stringify(this.sourceMap) : '');
149+
hash.update(this.sourceMap || '');
150+
151+
return hash.digest('hex');
152+
}
153+
154+
updateHash(hash, context) {
155+
super.updateHash(hash, context);
156+
157+
hash.update(this.buildInfo.hash);
134158
}
135159

136160
serialize(context) {
@@ -142,12 +166,17 @@ class MiniCssExtractPlugin {
142166
write(this.content);
143167
write(this.media);
144168
write(this.sourceMap);
145-
write(this.buildInfo);
169+
write(this.assets);
170+
write(this.assetsInfo);
171+
172+
write(this._needBuild);
146173

147174
super.serialize(context);
148175
}
149176

150177
deserialize(context) {
178+
this._needBuild = context.read();
179+
151180
super.deserialize(context);
152181
}
153182
}
@@ -176,7 +205,8 @@ class MiniCssExtractPlugin {
176205
const content = read();
177206
const media = read();
178207
const sourceMap = read();
179-
const { assets, assetsInfo } = read();
208+
const assets = read();
209+
const assetsInfo = read();
180210

181211
const dep = new CssModule({
182212
context: contextModule,
@@ -364,7 +394,7 @@ class MiniCssExtractPlugin {
364394
if (this.options.experimentalUseImportModule) {
365395
if (!compiler.options.experiments) {
366396
throw new Error(
367-
'experimentalUseImportModule is only support for webpack >= 5.32.0'
397+
'experimentalUseImportModule is only support for webpack >= 5.33.2'
368398
);
369399
}
370400
if (typeof compiler.options.experiments.executeModule === 'undefined') {
@@ -613,8 +643,14 @@ class MiniCssExtractPlugin {
613643
: webpack.util.createHash;
614644
const hash = createHash(hashFunction);
615645

616-
for (const m of modules) {
617-
m.updateHash(hash, { chunkGraph });
646+
if (isWebpack4) {
647+
for (const m of modules) {
648+
m.updateHash(hash);
649+
}
650+
} else {
651+
for (const m of modules) {
652+
hash.update(chunkGraph.getModuleHash(m, chunk.runtime));
653+
}
618654
}
619655

620656
// eslint-disable-next-line no-param-reassign

test/cases/chunkFilename-fullhash/expected/webpack-5-importModule/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ __webpack_require__.r(__webpack_exports__);
7373
/******/
7474
/******/ /* webpack/runtime/getFullHash */
7575
/******/ (() => {
76-
/******/ __webpack_require__.h = () => ("619e4b98882ea3a2aba3")
76+
/******/ __webpack_require__.h = () => ("a0b77e3a9967a95b3ac2")
7777
/******/ })();
7878
/******/
7979
/******/ /* webpack/runtime/global */

test/cases/chunkFilename-fullhash/expected/webpack-5/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ __webpack_require__.r(__webpack_exports__);
7373
/******/
7474
/******/ /* webpack/runtime/getFullHash */
7575
/******/ (() => {
76-
/******/ __webpack_require__.h = () => ("962555dd7355e6c261df")
76+
/******/ __webpack_require__.h = () => ("f8dea56ca966b9c551f2")
7777
/******/ })();
7878
/******/
7979
/******/ /* webpack/runtime/global */

test/cases/runtime/expected/webpack-5-importModule/runtime~main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
/******/ // This function allow to reference all chunks
131131
/******/ __webpack_require__.miniCssF = (chunkId) => {
132132
/******/ // return url for filenames based on template
133-
/******/ return "" + "main" + "." + "a7263f8f763dcf4051bc" + ".css";
133+
/******/ return "" + "main" + "." + "a45a4571ab5cece12cf0" + ".css";
134134
/******/ };
135135
/******/ })();
136136
/******/

test/cases/runtime/expected/webpack-5/runtime~main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
/******/ // This function allow to reference all chunks
131131
/******/ __webpack_require__.miniCssF = (chunkId) => {
132132
/******/ // return url for filenames based on template
133-
/******/ return "" + "main" + "." + "a7263f8f763dcf4051bc" + ".css";
133+
/******/ return "" + "main" + "." + "a45a4571ab5cece12cf0" + ".css";
134134
/******/ };
135135
/******/ })();
136136
/******/

0 commit comments

Comments
 (0)