Skip to content

Commit

Permalink
perf: fix (#774)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 28, 2021
1 parent 5551fbd commit 6e1bb67
Show file tree
Hide file tree
Showing 33 changed files with 60 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 55 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@ class MiniCssExtractPlugin {
this.content = content;
this.media = media;
this.sourceMap = sourceMap;
this.buildInfo = {
assets,
assetsInfo,
};
this.buildMeta = {};
this.assets = assets;
this.assetsInfo = assetsInfo;
this._needBuild = true;
}

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

updateCacheModule(module) {
this.content = module.content;
this.media = module.media;
this.sourceMap = module.sourceMap;
if (
this.content !== module.content ||
this.media !== module.media ||
this.sourceMap !== module.sourceMap ||
this.assets !== module.assets ||
this.assetsInfo !== module.assetsInfo
) {
this._needBuild = true;

this.content = module.content;
this.media = module.media;
this.sourceMap = module.sourceMap;
this.assets = module.assets;
this.assetsInfo = module.assetsInfo;
}
}

// eslint-disable-next-line class-methods-use-this
needRebuild() {
return true;
return this._needBuild;
}

// eslint-disable-next-line class-methods-use-this
needBuild(context, callback) {
callback(null, false);
callback(null, this._needBuild);
}

build(options, compilation, resolver, fileSystem, callback) {
this.buildInfo = {};
this.buildInfo = {
assets: this.assets,
assetsInfo: this.assetsInfo,
cacheable: true,
hash: this._computeHash(compilation.outputOptions.hashFunction),
};
this.buildMeta = {};
this._needBuild = false;

callback();
}

updateHash(hash, context) {
super.updateHash(hash, context);
_computeHash(hashFunction) {
const hash = webpack.util.createHash(hashFunction);

hash.update(this.content);
hash.update(this.media || '');
hash.update(this.sourceMap ? JSON.stringify(this.sourceMap) : '');
hash.update(this.sourceMap || '');

return hash.digest('hex');
}

updateHash(hash, context) {
super.updateHash(hash, context);

hash.update(this.buildInfo.hash);
}

serialize(context) {
Expand All @@ -142,12 +166,17 @@ class MiniCssExtractPlugin {
write(this.content);
write(this.media);
write(this.sourceMap);
write(this.buildInfo);
write(this.assets);
write(this.assetsInfo);

write(this._needBuild);

super.serialize(context);
}

deserialize(context) {
this._needBuild = context.read();

super.deserialize(context);
}
}
Expand Down Expand Up @@ -176,7 +205,8 @@ class MiniCssExtractPlugin {
const content = read();
const media = read();
const sourceMap = read();
const { assets, assetsInfo } = read();
const assets = read();
const assetsInfo = read();

const dep = new CssModule({
context: contextModule,
Expand Down Expand Up @@ -364,7 +394,7 @@ class MiniCssExtractPlugin {
if (this.options.experimentalUseImportModule) {
if (!compiler.options.experiments) {
throw new Error(
'experimentalUseImportModule is only support for webpack >= 5.32.0'
'experimentalUseImportModule is only support for webpack >= 5.33.2'
);
}
if (typeof compiler.options.experiments.executeModule === 'undefined') {
Expand Down Expand Up @@ -613,8 +643,14 @@ class MiniCssExtractPlugin {
: webpack.util.createHash;
const hash = createHash(hashFunction);

for (const m of modules) {
m.updateHash(hash, { chunkGraph });
if (isWebpack4) {
for (const m of modules) {
m.updateHash(hash);
}
} else {
for (const m of modules) {
hash.update(chunkGraph.getModuleHash(m, chunk.runtime));
}
}

// eslint-disable-next-line no-param-reassign
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ __webpack_require__.r(__webpack_exports__);
/******/
/******/ /* webpack/runtime/getFullHash */
/******/ (() => {
/******/ __webpack_require__.h = () => ("619e4b98882ea3a2aba3")
/******/ __webpack_require__.h = () => ("a0b77e3a9967a95b3ac2")
/******/ })();
/******/
/******/ /* webpack/runtime/global */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ __webpack_require__.r(__webpack_exports__);
/******/
/******/ /* webpack/runtime/getFullHash */
/******/ (() => {
/******/ __webpack_require__.h = () => ("962555dd7355e6c261df")
/******/ __webpack_require__.h = () => ("f8dea56ca966b9c551f2")
/******/ })();
/******/
/******/ /* webpack/runtime/global */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
/******/ // This function allow to reference all chunks
/******/ __webpack_require__.miniCssF = (chunkId) => {
/******/ // return url for filenames based on template
/******/ return "" + "main" + "." + "a7263f8f763dcf4051bc" + ".css";
/******/ return "" + "main" + "." + "a45a4571ab5cece12cf0" + ".css";
/******/ };
/******/ })();
/******/
Expand Down
2 changes: 1 addition & 1 deletion test/cases/runtime/expected/webpack-5/runtime~main.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
/******/ // This function allow to reference all chunks
/******/ __webpack_require__.miniCssF = (chunkId) => {
/******/ // return url for filenames based on template
/******/ return "" + "main" + "." + "a7263f8f763dcf4051bc" + ".css";
/******/ return "" + "main" + "." + "a45a4571ab5cece12cf0" + ".css";
/******/ };
/******/ })();
/******/
Expand Down

0 comments on commit 6e1bb67

Please sign in to comment.