Skip to content

Commit e023160

Browse files
fix: reduce runtime
1 parent 07fcf6b commit e023160

File tree

7 files changed

+387
-10
lines changed

7 files changed

+387
-10
lines changed

src/index.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -265,20 +265,24 @@ class MiniCssExtractPlugin {
265265

266266
compilation.hooks.contentHash.tap(pluginName, (chunk) => {
267267
const { outputOptions, chunkGraph } = compilation;
268-
const { hashFunction, hashDigest, hashDigestLength } = outputOptions;
269-
const hash = createHash(hashFunction);
268+
const modules = chunkGraph.getChunkModulesIterableBySourceType(
269+
chunk,
270+
MODULE_TYPE
271+
);
270272

271-
for (const m of this.getChunkModules(chunk, chunkGraph)) {
272-
if (m.type === MODULE_TYPE) {
273+
if (modules) {
274+
const { hashFunction, hashDigest, hashDigestLength } = outputOptions;
275+
const hash = createHash(hashFunction);
276+
277+
for (const m of modules) {
273278
m.updateHash(hash, { chunkGraph });
274279
}
275-
}
276280

277-
const { contentHash } = chunk;
278-
279-
contentHash[MODULE_TYPE] = hash
280-
.digest(hashDigest)
281-
.substring(0, hashDigestLength);
281+
// eslint-disable-next-line no-param-reassign
282+
chunk.contentHash[MODULE_TYPE] = hash
283+
.digest(hashDigest)
284+
.substring(0, hashDigestLength);
285+
}
282286
});
283287

284288
const { mainTemplate } = compilation;
@@ -465,6 +469,7 @@ class MiniCssExtractPlugin {
465469
const CssLoadingRuntimeModule = require('./CssLoadingRuntimeModule');
466470

467471
set.add(webpack.RuntimeGlobals.publicPath);
472+
468473
compilation.addRuntimeModule(
469474
chunk,
470475
new webpack.runtime.GetChunkFilenameRuntimeModule(
@@ -483,6 +488,7 @@ class MiniCssExtractPlugin {
483488
new CssLoadingRuntimeModule(set, this.runtimeOptions)
484489
);
485490
};
491+
486492
compilation.hooks.runtimeRequirementInTree
487493
.for(webpack.RuntimeGlobals.ensureChunkHandlers)
488494
.tap(pluginName, handler);

test/cases/runtime/async.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('HERE');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
background: red;
3+
}
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,334 @@
1+
/******/ (() => { // webpackBootstrap
2+
/******/ "use strict";
3+
/******/ var __webpack_modules__ = ({});
4+
/************************************************************************/
5+
/******/ // The module cache
6+
/******/ var __webpack_module_cache__ = {};
7+
/******/
8+
/******/ // The require function
9+
/******/ function __webpack_require__(moduleId) {
10+
/******/ // Check if module is in cache
11+
/******/ if(__webpack_module_cache__[moduleId]) {
12+
/******/ return __webpack_module_cache__[moduleId].exports;
13+
/******/ }
14+
/******/ // Create a new module (and put it into the cache)
15+
/******/ var module = __webpack_module_cache__[moduleId] = {
16+
/******/ // no module.id needed
17+
/******/ // no module.loaded needed
18+
/******/ exports: {}
19+
/******/ };
20+
/******/
21+
/******/ // Execute the module function
22+
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
23+
/******/
24+
/******/ // Return the exports of the module
25+
/******/ return module.exports;
26+
/******/ }
27+
/******/
28+
/******/ // expose the modules object (__webpack_modules__)
29+
/******/ __webpack_require__.m = __webpack_modules__;
30+
/******/
31+
/******/ // the startup function
32+
/******/ // It's empty as some runtime module handles the default behavior
33+
/******/ __webpack_require__.x = x => {}
34+
/************************************************************************/
35+
/******/ /* webpack/runtime/create fake namespace object */
36+
/******/ (() => {
37+
/******/ var getProto = Object.getPrototypeOf ? (obj) => Object.getPrototypeOf(obj) : (obj) => obj.__proto__;
38+
/******/ var leafPrototypes;
39+
/******/ // create a fake namespace object
40+
/******/ // mode & 1: value is a module id, require it
41+
/******/ // mode & 2: merge all properties of value into the ns
42+
/******/ // mode & 4: return value when already ns object
43+
/******/ // mode & 16: return value when it's Promise-like
44+
/******/ // mode & 8|1: behave like require
45+
/******/ __webpack_require__.t = function(value, mode) {
46+
/******/ if(mode & 1) value = this(value);
47+
/******/ if(mode & 8) return value;
48+
/******/ if(typeof value === 'object' && value) {
49+
/******/ if((mode & 4) && value.__esModule) return value;
50+
/******/ if((mode & 16) && typeof value.then === 'function') return value;
51+
/******/ }
52+
/******/ var ns = Object.create(null);
53+
/******/ __webpack_require__.r(ns);
54+
/******/ var def = {};
55+
/******/ leafPrototypes = leafPrototypes || [null, getProto({}), getProto([]), getProto(getProto)];
56+
/******/ for(var current = mode & 2 && value; typeof current == 'object' && !~leafPrototypes.indexOf(current); current = getProto(current)) {
57+
/******/ Object.getOwnPropertyNames(current).forEach(key => def[key] = () => value[key]);
58+
/******/ }
59+
/******/ def['default'] = () => value;
60+
/******/ __webpack_require__.d(ns, def);
61+
/******/ return ns;
62+
/******/ };
63+
/******/ })();
64+
/******/
65+
/******/ /* webpack/runtime/define property getters */
66+
/******/ (() => {
67+
/******/ // define getter functions for harmony exports
68+
/******/ __webpack_require__.d = (exports, definition) => {
69+
/******/ for(var key in definition) {
70+
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
71+
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
72+
/******/ }
73+
/******/ }
74+
/******/ };
75+
/******/ })();
76+
/******/
77+
/******/ /* webpack/runtime/ensure chunk */
78+
/******/ (() => {
79+
/******/ __webpack_require__.f = {};
80+
/******/ // This file contains only the entry chunk.
81+
/******/ // The chunk loading function for additional chunks
82+
/******/ __webpack_require__.e = (chunkId) => {
83+
/******/ return Promise.all(Object.keys(__webpack_require__.f).reduce((promises, key) => {
84+
/******/ __webpack_require__.f[key](chunkId, promises);
85+
/******/ return promises;
86+
/******/ }, []));
87+
/******/ };
88+
/******/ })();
89+
/******/
90+
/******/ /* webpack/runtime/get javascript chunk filename */
91+
/******/ (() => {
92+
/******/ // This function allow to reference async chunks
93+
/******/ __webpack_require__.u = (chunkId) => {
94+
/******/ // return url for filenames based on template
95+
/******/ return "" + chunkId + ".js";
96+
/******/ };
97+
/******/ })();
98+
/******/
99+
/******/ /* webpack/runtime/get mini-css chunk filename */
100+
/******/ (() => {
101+
/******/ // This function allow to reference all chunks
102+
/******/ __webpack_require__.miniCssF = (chunkId) => {
103+
/******/ // return url for filenames based on template
104+
/******/ return "" + ({"0":"main","1":"runtime~main"}[chunkId] || chunkId) + "." + {"0":"a7263f8f763dcf4051bc"}[chunkId] + ".css";
105+
/******/ };
106+
/******/ })();
107+
/******/
108+
/******/ /* webpack/runtime/global */
109+
/******/ (() => {
110+
/******/ __webpack_require__.g = (function() {
111+
/******/ if (typeof globalThis === 'object') return globalThis;
112+
/******/ try {
113+
/******/ return this || new Function('return this')();
114+
/******/ } catch (e) {
115+
/******/ if (typeof window === 'object') return window;
116+
/******/ }
117+
/******/ })();
118+
/******/ })();
119+
/******/
120+
/******/ /* webpack/runtime/hasOwnProperty shorthand */
121+
/******/ (() => {
122+
/******/ __webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
123+
/******/ })();
124+
/******/
125+
/******/ /* webpack/runtime/load script */
126+
/******/ (() => {
127+
/******/ var inProgress = {};
128+
/******/ // data-webpack is not used as build has no uniqueName
129+
/******/ // loadScript function to load a script via script tag
130+
/******/ __webpack_require__.l = (url, done, key, chunkId) => {
131+
/******/ if(inProgress[url]) { inProgress[url].push(done); return; }
132+
/******/ var script, needAttach;
133+
/******/ if(key !== undefined) {
134+
/******/ var scripts = document.getElementsByTagName("script");
135+
/******/ for(var i = 0; i < scripts.length; i++) {
136+
/******/ var s = scripts[i];
137+
/******/ if(s.getAttribute("src") == url) { script = s; break; }
138+
/******/ }
139+
/******/ }
140+
/******/ if(!script) {
141+
/******/ needAttach = true;
142+
/******/ script = document.createElement('script');
143+
/******/
144+
/******/ script.charset = 'utf-8';
145+
/******/ script.timeout = 120;
146+
/******/ if (__webpack_require__.nc) {
147+
/******/ script.setAttribute("nonce", __webpack_require__.nc);
148+
/******/ }
149+
/******/
150+
/******/ script.src = url;
151+
/******/ }
152+
/******/ inProgress[url] = [done];
153+
/******/ var onScriptComplete = (prev, event) => {
154+
/******/ // avoid mem leaks in IE.
155+
/******/ script.onerror = script.onload = null;
156+
/******/ clearTimeout(timeout);
157+
/******/ var doneFns = inProgress[url];
158+
/******/ delete inProgress[url];
159+
/******/ script.parentNode && script.parentNode.removeChild(script);
160+
/******/ doneFns && doneFns.forEach((fn) => fn(event));
161+
/******/ if(prev) return prev(event);
162+
/******/ }
163+
/******/ ;
164+
/******/ var timeout = setTimeout(onScriptComplete.bind(null, undefined, { type: 'timeout', target: script }), 120000);
165+
/******/ script.onerror = onScriptComplete.bind(null, script.onerror);
166+
/******/ script.onload = onScriptComplete.bind(null, script.onload);
167+
/******/ needAttach && document.head.appendChild(script);
168+
/******/ };
169+
/******/ })();
170+
/******/
171+
/******/ /* webpack/runtime/make namespace object */
172+
/******/ (() => {
173+
/******/ // define __esModule on exports
174+
/******/ __webpack_require__.r = (exports) => {
175+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
176+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
177+
/******/ }
178+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
179+
/******/ };
180+
/******/ })();
181+
/******/
182+
/******/ /* webpack/runtime/publicPath */
183+
/******/ (() => {
184+
/******/ var scriptUrl;
185+
/******/ if (__webpack_require__.g.importScripts) scriptUrl = __webpack_require__.g.location + "";
186+
/******/ var document = __webpack_require__.g.document;
187+
/******/ if (!scriptUrl && document) {
188+
/******/ if (document.currentScript)
189+
/******/ scriptUrl = document.currentScript.src
190+
/******/ if (!scriptUrl) {
191+
/******/ var scripts = document.getElementsByTagName("script");
192+
/******/ if(scripts.length) scriptUrl = scripts[scripts.length - 1].src
193+
/******/ }
194+
/******/ }
195+
/******/ // When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration
196+
/******/ // or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic.
197+
/******/ if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");
198+
/******/ scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\?.*$/, "").replace(/\/[^\/]+$/, "/");
199+
/******/ __webpack_require__.p = scriptUrl;
200+
/******/ })();
201+
/******/
202+
/******/ /* webpack/runtime/jsonp chunk loading */
203+
/******/ (() => {
204+
/******/ // no baseURI
205+
/******/
206+
/******/ // object to store loaded and loading chunks
207+
/******/ // undefined = chunk not loaded, null = chunk preloaded/prefetched
208+
/******/ // Promise = chunk loading, 0 = chunk loaded
209+
/******/ var installedChunks = {
210+
/******/ 1: 0
211+
/******/ };
212+
/******/
213+
/******/ var deferredModules = [
214+
/******/
215+
/******/ ];
216+
/******/ __webpack_require__.f.j = (chunkId, promises) => {
217+
/******/ // JSONP chunk loading for javascript
218+
/******/ var installedChunkData = __webpack_require__.o(installedChunks, chunkId) ? installedChunks[chunkId] : undefined;
219+
/******/ if(installedChunkData !== 0) { // 0 means "already installed".
220+
/******/
221+
/******/ // a Promise means "currently loading".
222+
/******/ if(installedChunkData) {
223+
/******/ promises.push(installedChunkData[2]);
224+
/******/ } else {
225+
/******/ if(true) { // all chunks have JS
226+
/******/ // setup Promise in chunk cache
227+
/******/ var promise = new Promise((resolve, reject) => {
228+
/******/ installedChunkData = installedChunks[chunkId] = [resolve, reject];
229+
/******/ });
230+
/******/ promises.push(installedChunkData[2] = promise);
231+
/******/
232+
/******/ // start chunk loading
233+
/******/ var url = __webpack_require__.p + __webpack_require__.u(chunkId);
234+
/******/ // create error before stack unwound to get useful stacktrace later
235+
/******/ var error = new Error();
236+
/******/ var loadingEnded = (event) => {
237+
/******/ if(__webpack_require__.o(installedChunks, chunkId)) {
238+
/******/ installedChunkData = installedChunks[chunkId];
239+
/******/ if(installedChunkData !== 0) installedChunks[chunkId] = undefined;
240+
/******/ if(installedChunkData) {
241+
/******/ var errorType = event && (event.type === 'load' ? 'missing' : event.type);
242+
/******/ var realSrc = event && event.target && event.target.src;
243+
/******/ error.message = 'Loading chunk ' + chunkId + ' failed.\n(' + errorType + ': ' + realSrc + ')';
244+
/******/ error.name = 'ChunkLoadError';
245+
/******/ error.type = errorType;
246+
/******/ error.request = realSrc;
247+
/******/ installedChunkData[1](error);
248+
/******/ }
249+
/******/ }
250+
/******/ };
251+
/******/ __webpack_require__.l(url, loadingEnded, "chunk-" + chunkId, chunkId);
252+
/******/ } else installedChunks[chunkId] = 0;
253+
/******/ }
254+
/******/ }
255+
/******/ };
256+
/******/
257+
/******/ // no prefetching
258+
/******/
259+
/******/ // no preloaded
260+
/******/
261+
/******/ // no HMR
262+
/******/
263+
/******/ // no HMR manifest
264+
/******/
265+
/******/ var checkDeferredModules = x => {};
266+
/******/
267+
/******/ // install a JSONP callback for chunk loading
268+
/******/ var webpackJsonpCallback = (parentChunkLoadingFunction, data) => {
269+
/******/ var [chunkIds, moreModules, runtime, executeModules] = data;
270+
/******/ // add "moreModules" to the modules object,
271+
/******/ // then flag all "chunkIds" as loaded and fire callback
272+
/******/ var moduleId, chunkId, i = 0, resolves = [];
273+
/******/ for(;i < chunkIds.length; i++) {
274+
/******/ chunkId = chunkIds[i];
275+
/******/ if(__webpack_require__.o(installedChunks, chunkId) && installedChunks[chunkId]) {
276+
/******/ resolves.push(installedChunks[chunkId][0]);
277+
/******/ }
278+
/******/ installedChunks[chunkId] = 0;
279+
/******/ }
280+
/******/ for(moduleId in moreModules) {
281+
/******/ if(__webpack_require__.o(moreModules, moduleId)) {
282+
/******/ __webpack_require__.m[moduleId] = moreModules[moduleId];
283+
/******/ }
284+
/******/ }
285+
/******/ if(runtime) runtime(__webpack_require__);
286+
/******/ if(parentChunkLoadingFunction) parentChunkLoadingFunction(data);
287+
/******/ while(resolves.length) {
288+
/******/ resolves.shift()();
289+
/******/ }
290+
/******/
291+
/******/ // add entry modules from loaded chunk to deferred list
292+
/******/ if(executeModules) deferredModules.push.apply(deferredModules, executeModules);
293+
/******/
294+
/******/ // run deferred modules when all chunks ready
295+
/******/ return checkDeferredModules();
296+
/******/ }
297+
/******/
298+
/******/ var chunkLoadingGlobal = self["webpackChunk"] = self["webpackChunk"] || [];
299+
/******/ chunkLoadingGlobal.forEach(webpackJsonpCallback.bind(null, 0));
300+
/******/ chunkLoadingGlobal.push = webpackJsonpCallback.bind(null, chunkLoadingGlobal.push.bind(chunkLoadingGlobal));
301+
/******/
302+
/******/ function checkDeferredModulesImpl() {
303+
/******/ var result;
304+
/******/ for(var i = 0; i < deferredModules.length; i++) {
305+
/******/ var deferredModule = deferredModules[i];
306+
/******/ var fulfilled = true;
307+
/******/ for(var j = 1; j < deferredModule.length; j++) {
308+
/******/ var depId = deferredModule[j];
309+
/******/ if(installedChunks[depId] !== 0) fulfilled = false;
310+
/******/ }
311+
/******/ if(fulfilled) {
312+
/******/ deferredModules.splice(i--, 1);
313+
/******/ result = __webpack_require__(__webpack_require__.s = deferredModule[0]);
314+
/******/ }
315+
/******/ }
316+
/******/ if(deferredModules.length === 0) {
317+
/******/ __webpack_require__.x();
318+
/******/ __webpack_require__.x = x => {};
319+
/******/ }
320+
/******/ return result;
321+
/******/ }
322+
/******/ var startup = __webpack_require__.x;
323+
/******/ __webpack_require__.x = () => {
324+
/******/ // reset startup function so it can be called again when more startup code is added
325+
/******/ __webpack_require__.x = startup || (x => {});
326+
/******/ return (checkDeferredModules = checkDeferredModulesImpl)();
327+
/******/ };
328+
/******/ })();
329+
/******/
330+
/************************************************************************/
331+
/******/ // run startup
332+
/******/ __webpack_require__.x();
333+
/******/ })()
334+
;

test/cases/runtime/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// eslint-disable-next-line import/no-unresolved
2+
import './style.css';
3+
4+
import('./async.js');

0 commit comments

Comments
 (0)