webpack 5.59.0 → 5.71.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of webpack might be problematic. Click here for more details.

Files changed (209) hide show
  1. package/README.md +1 -7
  2. package/hot/lazy-compilation-node.js +3 -1
  3. package/hot/poll.js +1 -1
  4. package/hot/signal.js +1 -1
  5. package/lib/APIPlugin.js +33 -0
  6. package/lib/BannerPlugin.js +10 -4
  7. package/lib/Cache.js +1 -1
  8. package/lib/CacheFacade.js +4 -11
  9. package/lib/Chunk.js +6 -3
  10. package/lib/ChunkGraph.js +1 -2
  11. package/lib/ChunkGroup.js +1 -1
  12. package/lib/CleanPlugin.js +81 -20
  13. package/lib/Compilation.js +179 -91
  14. package/lib/Compiler.js +86 -17
  15. package/lib/ConstPlugin.js +2 -2
  16. package/lib/ContextModule.js +142 -51
  17. package/lib/ContextModuleFactory.js +65 -25
  18. package/lib/DelegatedModule.js +1 -1
  19. package/lib/DelegatedModuleFactoryPlugin.js +1 -1
  20. package/lib/Dependency.js +17 -0
  21. package/lib/DependencyTemplate.js +9 -0
  22. package/lib/DependencyTemplates.js +1 -1
  23. package/lib/DllModule.js +1 -1
  24. package/lib/EntryOptionPlugin.js +2 -0
  25. package/lib/ErrorHelpers.js +2 -2
  26. package/lib/EvalDevToolModulePlugin.js +16 -1
  27. package/lib/EvalSourceMapDevToolPlugin.js +18 -1
  28. package/lib/ExportsInfo.js +4 -4
  29. package/lib/ExternalModule.js +94 -54
  30. package/lib/ExternalModuleFactoryPlugin.js +5 -5
  31. package/lib/FileSystemInfo.js +89 -44
  32. package/lib/Generator.js +3 -0
  33. package/lib/HookWebpackError.js +1 -1
  34. package/lib/HotModuleReplacementPlugin.js +3 -1
  35. package/lib/LoaderOptionsPlugin.js +1 -1
  36. package/lib/Module.js +27 -4
  37. package/lib/ModuleFilenameHelpers.js +8 -4
  38. package/lib/ModuleHashingError.js +29 -0
  39. package/lib/MultiCompiler.js +1 -1
  40. package/lib/MultiWatching.js +1 -1
  41. package/lib/NodeStuffPlugin.js +10 -0
  42. package/lib/NormalModule.js +41 -26
  43. package/lib/NormalModuleFactory.js +42 -37
  44. package/lib/ProgressPlugin.js +4 -5
  45. package/lib/RawModule.js +1 -1
  46. package/lib/RuntimeGlobals.js +29 -1
  47. package/lib/RuntimeModule.js +1 -1
  48. package/lib/RuntimePlugin.js +77 -1
  49. package/lib/RuntimeTemplate.js +114 -2
  50. package/lib/Template.js +2 -1
  51. package/lib/TemplatedPathPlugin.js +48 -23
  52. package/lib/WatchIgnorePlugin.js +19 -7
  53. package/lib/Watching.js +33 -19
  54. package/lib/WebpackOptionsApply.js +57 -11
  55. package/lib/asset/AssetGenerator.js +193 -63
  56. package/lib/asset/AssetModulesPlugin.js +3 -0
  57. package/lib/asset/RawDataUrlModule.js +148 -0
  58. package/lib/async-modules/AwaitDependenciesInitFragment.js +4 -4
  59. package/lib/buildChunkGraph.js +36 -6
  60. package/lib/cache/PackFileCacheStrategy.js +7 -4
  61. package/lib/cache/ResolverCachePlugin.js +90 -29
  62. package/lib/cli.js +44 -3
  63. package/lib/config/browserslistTargetHandler.js +41 -6
  64. package/lib/config/defaults.js +115 -19
  65. package/lib/config/normalization.js +9 -0
  66. package/lib/config/target.js +10 -0
  67. package/lib/container/ContainerEntryModule.js +8 -5
  68. package/lib/container/FallbackModule.js +4 -4
  69. package/lib/container/RemoteModule.js +4 -2
  70. package/lib/css/CssExportsGenerator.js +139 -0
  71. package/lib/css/CssGenerator.js +109 -0
  72. package/lib/css/CssLoadingRuntimeModule.js +440 -0
  73. package/lib/css/CssModulesPlugin.js +462 -0
  74. package/lib/css/CssParser.js +618 -0
  75. package/lib/css/walkCssTokens.js +659 -0
  76. package/lib/debug/ProfilingPlugin.js +24 -21
  77. package/lib/dependencies/AMDRequireDependency.js +6 -6
  78. package/lib/dependencies/CommonJsExportsParserPlugin.js +1 -2
  79. package/lib/dependencies/CommonJsFullRequireDependency.js +5 -1
  80. package/lib/dependencies/CommonJsImportsParserPlugin.js +5 -3
  81. package/lib/dependencies/CommonJsRequireContextDependency.js +5 -1
  82. package/lib/dependencies/ContextDependency.js +1 -0
  83. package/lib/dependencies/ContextDependencyHelpers.js +3 -3
  84. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +4 -1
  85. package/lib/dependencies/ContextElementDependency.js +41 -3
  86. package/lib/dependencies/CreateScriptUrlDependency.js +12 -0
  87. package/lib/dependencies/CssExportDependency.js +85 -0
  88. package/lib/dependencies/CssImportDependency.js +75 -0
  89. package/lib/dependencies/CssLocalIdentifierDependency.js +119 -0
  90. package/lib/dependencies/CssSelfLocalIdentifierDependency.js +101 -0
  91. package/lib/dependencies/CssUrlDependency.js +132 -0
  92. package/lib/dependencies/ExportsInfoDependency.js +6 -0
  93. package/lib/dependencies/HarmonyAcceptImportDependency.js +5 -3
  94. package/lib/dependencies/HarmonyCompatibilityDependency.js +5 -5
  95. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +95 -0
  96. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +12 -3
  97. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +25 -17
  98. package/lib/dependencies/HarmonyExportInitFragment.js +4 -1
  99. package/lib/dependencies/HarmonyImportDependency.js +23 -0
  100. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +142 -45
  101. package/lib/dependencies/HarmonyImportSpecifierDependency.js +46 -22
  102. package/lib/dependencies/HarmonyModulesPlugin.js +10 -0
  103. package/lib/dependencies/ImportContextDependency.js +0 -2
  104. package/lib/dependencies/ImportMetaContextDependency.js +35 -0
  105. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +252 -0
  106. package/lib/dependencies/ImportMetaContextPlugin.js +59 -0
  107. package/lib/dependencies/ImportMetaPlugin.js +22 -3
  108. package/lib/dependencies/LoaderPlugin.js +4 -2
  109. package/lib/dependencies/RequireContextDependency.js +0 -16
  110. package/lib/dependencies/RequireEnsureDependency.js +2 -2
  111. package/lib/dependencies/URLDependency.js +3 -8
  112. package/lib/dependencies/URLPlugin.js +1 -1
  113. package/lib/esm/ModuleChunkFormatPlugin.js +74 -49
  114. package/lib/esm/ModuleChunkLoadingPlugin.js +3 -1
  115. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +25 -9
  116. package/lib/hmr/HotModuleReplacement.runtime.js +29 -14
  117. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +4 -3
  118. package/lib/hmr/LazyCompilationPlugin.js +54 -26
  119. package/lib/hmr/lazyCompilationBackend.js +51 -12
  120. package/lib/ids/DeterministicModuleIdsPlugin.js +55 -35
  121. package/lib/ids/HashedModuleIdsPlugin.js +11 -14
  122. package/lib/ids/IdHelpers.js +25 -11
  123. package/lib/ids/NamedModuleIdsPlugin.js +6 -9
  124. package/lib/ids/NaturalModuleIdsPlugin.js +10 -13
  125. package/lib/ids/OccurrenceModuleIdsPlugin.js +13 -10
  126. package/lib/ids/SyncModuleIdsPlugin.js +140 -0
  127. package/lib/index.js +13 -0
  128. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +2 -2
  129. package/lib/javascript/BasicEvaluatedExpression.js +5 -2
  130. package/lib/javascript/ChunkHelpers.js +33 -0
  131. package/lib/javascript/JavascriptGenerator.js +1 -0
  132. package/lib/javascript/JavascriptModulesPlugin.js +27 -2
  133. package/lib/javascript/JavascriptParser.js +82 -48
  134. package/lib/javascript/StartupHelpers.js +7 -30
  135. package/lib/library/AssignLibraryPlugin.js +39 -15
  136. package/lib/library/EnableLibraryPlugin.js +11 -0
  137. package/lib/library/UmdLibraryPlugin.js +5 -3
  138. package/lib/node/NodeTargetPlugin.js +3 -0
  139. package/lib/node/NodeWatchFileSystem.js +85 -31
  140. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +23 -8
  141. package/lib/node/RequireChunkLoadingRuntimeModule.js +24 -9
  142. package/lib/optimize/ConcatenatedModule.js +21 -9
  143. package/lib/optimize/ModuleConcatenationPlugin.js +5 -2
  144. package/lib/optimize/SplitChunksPlugin.js +8 -1
  145. package/lib/runtime/AsyncModuleRuntimeModule.js +27 -17
  146. package/lib/runtime/BaseUriRuntimeModule.js +31 -0
  147. package/lib/runtime/CreateScriptRuntimeModule.js +36 -0
  148. package/lib/runtime/CreateScriptUrlRuntimeModule.js +9 -34
  149. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +76 -0
  150. package/lib/schemes/HttpUriPlugin.js +77 -14
  151. package/lib/serialization/FileMiddleware.js +44 -9
  152. package/lib/sharing/ConsumeSharedModule.js +8 -2
  153. package/lib/sharing/ConsumeSharedRuntimeModule.js +26 -5
  154. package/lib/sharing/ProvideSharedModule.js +4 -2
  155. package/lib/sharing/ShareRuntimeModule.js +1 -1
  156. package/lib/sharing/utils.js +1 -1
  157. package/lib/stats/DefaultStatsFactoryPlugin.js +113 -68
  158. package/lib/stats/DefaultStatsPrinterPlugin.js +89 -24
  159. package/lib/util/ArrayHelpers.js +30 -0
  160. package/lib/util/AsyncQueue.js +1 -1
  161. package/lib/util/compileBooleanMatcher.js +1 -1
  162. package/lib/util/create-schema-validation.js +9 -2
  163. package/lib/util/createHash.js +12 -0
  164. package/lib/util/deprecation.js +10 -2
  165. package/lib/util/deterministicGrouping.js +1 -1
  166. package/lib/util/extractUrlAndGlobal.js +3 -0
  167. package/lib/util/fs.js +11 -0
  168. package/lib/util/hash/BatchedHash.js +7 -4
  169. package/lib/util/hash/md4.js +20 -0
  170. package/lib/util/hash/wasm-hash.js +163 -0
  171. package/lib/util/hash/xxhash64.js +5 -139
  172. package/lib/util/identifier.js +65 -44
  173. package/lib/util/internalSerializables.js +15 -0
  174. package/lib/util/nonNumericOnlyHash.js +22 -0
  175. package/lib/util/semver.js +17 -10
  176. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +9 -3
  177. package/lib/web/JsonpChunkLoadingRuntimeModule.js +20 -9
  178. package/lib/webpack.js +10 -3
  179. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -11
  180. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +33 -22
  181. package/module.d.ts +215 -0
  182. package/package.json +23 -28
  183. package/schemas/WebpackOptions.check.js +1 -1
  184. package/schemas/WebpackOptions.json +254 -29
  185. package/schemas/plugins/DllReferencePlugin.check.js +1 -1
  186. package/schemas/plugins/HashedModuleIdsPlugin.check.js +1 -1
  187. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  188. package/schemas/plugins/asset/AssetGeneratorOptions.check.js +1 -1
  189. package/schemas/plugins/asset/AssetParserOptions.check.js +1 -1
  190. package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.js +1 -1
  191. package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
  192. package/schemas/plugins/container/ContainerPlugin.json +2 -1
  193. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  194. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  195. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  196. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  197. package/schemas/plugins/container/ModuleFederationPlugin.json +3 -1
  198. package/schemas/plugins/css/CssGeneratorOptions.check.d.ts +7 -0
  199. package/schemas/plugins/css/CssGeneratorOptions.check.js +6 -0
  200. package/schemas/plugins/css/CssGeneratorOptions.json +3 -0
  201. package/schemas/plugins/css/CssParserOptions.check.d.ts +7 -0
  202. package/schemas/plugins/css/CssParserOptions.check.js +6 -0
  203. package/schemas/plugins/css/CssParserOptions.json +3 -0
  204. package/schemas/plugins/optimize/AggressiveSplittingPlugin.check.js +1 -1
  205. package/schemas/plugins/optimize/LimitChunkCountPlugin.check.js +1 -1
  206. package/schemas/plugins/optimize/MinChunkSizePlugin.check.js +1 -1
  207. package/schemas/plugins/schemes/HttpUriPlugin.check.js +1 -1
  208. package/schemas/plugins/schemes/HttpUriPlugin.json +4 -0
  209. package/types.d.ts +628 -179
@@ -0,0 +1,440 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const { SyncWaterfallHook } = require("tapable");
9
+ const Compilation = require("../Compilation");
10
+ const RuntimeGlobals = require("../RuntimeGlobals");
11
+ const RuntimeModule = require("../RuntimeModule");
12
+ const Template = require("../Template");
13
+ const compileBooleanMatcher = require("../util/compileBooleanMatcher");
14
+ const { chunkHasCss } = require("./CssModulesPlugin");
15
+
16
+ /** @typedef {import("../Chunk")} Chunk */
17
+
18
+ /**
19
+ * @typedef {Object} JsonpCompilationPluginHooks
20
+ * @property {SyncWaterfallHook<[string, Chunk]>} createStylesheet
21
+ */
22
+
23
+ /** @type {WeakMap<Compilation, JsonpCompilationPluginHooks>} */
24
+ const compilationHooksMap = new WeakMap();
25
+
26
+ class CssLoadingRuntimeModule extends RuntimeModule {
27
+ /**
28
+ * @param {Compilation} compilation the compilation
29
+ * @returns {JsonpCompilationPluginHooks} hooks
30
+ */
31
+ static getCompilationHooks(compilation) {
32
+ if (!(compilation instanceof Compilation)) {
33
+ throw new TypeError(
34
+ "The 'compilation' argument must be an instance of Compilation"
35
+ );
36
+ }
37
+ let hooks = compilationHooksMap.get(compilation);
38
+ if (hooks === undefined) {
39
+ hooks = {
40
+ createStylesheet: new SyncWaterfallHook(["source", "chunk"])
41
+ };
42
+ compilationHooksMap.set(compilation, hooks);
43
+ }
44
+ return hooks;
45
+ }
46
+
47
+ constructor(runtimeRequirements, runtimeOptions) {
48
+ super("css loading", 10);
49
+
50
+ this._runtimeRequirements = runtimeRequirements;
51
+ this.runtimeOptions = runtimeOptions;
52
+ }
53
+
54
+ /**
55
+ * @returns {string} runtime code
56
+ */
57
+ generate() {
58
+ const { compilation, chunk, _runtimeRequirements } = this;
59
+ const {
60
+ chunkGraph,
61
+ runtimeTemplate,
62
+ outputOptions: {
63
+ crossOriginLoading,
64
+ uniqueName,
65
+ chunkLoadTimeout: loadTimeout
66
+ }
67
+ } = compilation;
68
+ const fn = RuntimeGlobals.ensureChunkHandlers;
69
+ const conditionMap = chunkGraph.getChunkConditionMap(
70
+ chunk,
71
+ (chunk, chunkGraph) =>
72
+ !!chunkGraph.getChunkModulesIterableBySourceType(chunk, "css")
73
+ );
74
+ const hasCssMatcher = compileBooleanMatcher(conditionMap);
75
+
76
+ const withLoading =
77
+ _runtimeRequirements.has(RuntimeGlobals.ensureChunkHandlers) &&
78
+ hasCssMatcher !== false;
79
+ const withHmr = _runtimeRequirements.has(
80
+ RuntimeGlobals.hmrDownloadUpdateHandlers
81
+ );
82
+ const initialChunkIdsWithCss = new Set();
83
+ const initialChunkIdsWithoutCss = new Set();
84
+ for (const c of chunk.getAllInitialChunks()) {
85
+ (chunkHasCss(c, chunkGraph)
86
+ ? initialChunkIdsWithCss
87
+ : initialChunkIdsWithoutCss
88
+ ).add(c.id);
89
+ }
90
+
91
+ if (!withLoading && !withHmr && initialChunkIdsWithCss.size === 0) {
92
+ return null;
93
+ }
94
+
95
+ const { createStylesheet } =
96
+ CssLoadingRuntimeModule.getCompilationHooks(compilation);
97
+
98
+ const stateExpression = withHmr
99
+ ? `${RuntimeGlobals.hmrRuntimeStatePrefix}_css`
100
+ : undefined;
101
+
102
+ const code = Template.asString([
103
+ "link = document.createElement('link');",
104
+ uniqueName
105
+ ? 'link.setAttribute("data-webpack", uniqueName + ":" + key);'
106
+ : "",
107
+ "link.setAttribute(loadingAttribute, 1);",
108
+ 'link.rel = "stylesheet";',
109
+ "link.href = url;",
110
+ crossOriginLoading
111
+ ? Template.asString([
112
+ "if (link.src.indexOf(window.location.origin + '/') !== 0) {",
113
+ Template.indent(
114
+ `link.crossOrigin = ${JSON.stringify(crossOriginLoading)};`
115
+ ),
116
+ "}"
117
+ ])
118
+ : ""
119
+ ]);
120
+
121
+ const cc = str => str.charCodeAt(0);
122
+
123
+ return Template.asString([
124
+ "// object to store loaded and loading chunks",
125
+ "// undefined = chunk not loaded, null = chunk preloaded/prefetched",
126
+ "// [resolve, reject, Promise] = chunk loading, 0 = chunk loaded",
127
+ `var installedChunks = ${
128
+ stateExpression ? `${stateExpression} = ${stateExpression} || ` : ""
129
+ }{${Array.from(
130
+ initialChunkIdsWithoutCss,
131
+ id => `${JSON.stringify(id)}:0`
132
+ ).join(",")}};`,
133
+ "",
134
+ uniqueName
135
+ ? `var uniqueName = ${JSON.stringify(
136
+ runtimeTemplate.outputOptions.uniqueName
137
+ )};`
138
+ : "// data-webpack is not used as build has no uniqueName",
139
+ `var loadCssChunkData = ${runtimeTemplate.basicFunction(
140
+ "target, link, chunkId",
141
+ [
142
+ `var data, token = "", token2, exports = {}, exportsWithId = [], exportsWithDashes = [], ${
143
+ withHmr ? "moduleIds = [], " : ""
144
+ }i = 0, cc = 1;`,
145
+ "try { if(!link) link = loadStylesheet(chunkId); data = link.sheet.cssRules; data = data[data.length - 1].style; } catch(e) { data = getComputedStyle(document.head); }",
146
+ `data = data.getPropertyValue(${
147
+ uniqueName
148
+ ? runtimeTemplate.concatenation(
149
+ "--webpack-",
150
+ { expr: "uniqueName" },
151
+ "-",
152
+ { expr: "chunkId" }
153
+ )
154
+ : runtimeTemplate.concatenation("--webpack-", { expr: "chunkId" })
155
+ });`,
156
+ "if(!data) return [];",
157
+ "for(; cc; i++) {",
158
+ Template.indent([
159
+ "cc = data.charCodeAt(i);",
160
+ `if(cc == ${cc("(")}) { token2 = token; token = ""; }`,
161
+ `else if(cc == ${cc(
162
+ ")"
163
+ )}) { exports[token2.replace(/^_/, "")] = token.replace(/^_/, ""); token = ""; }`,
164
+ `else if(cc == ${cc("/")} || cc == ${cc(
165
+ "%"
166
+ )}) { token = token.replace(/^_/, ""); exports[token] = token; exportsWithId.push(token); if(cc == ${cc(
167
+ "%"
168
+ )}) exportsWithDashes.push(token); token = ""; }`,
169
+ `else if(!cc || cc == ${cc(
170
+ ","
171
+ )}) { token = token.replace(/^_/, ""); exportsWithId.forEach(${runtimeTemplate.expressionFunction(
172
+ `exports[x] = ${
173
+ uniqueName
174
+ ? runtimeTemplate.concatenation(
175
+ { expr: "uniqueName" },
176
+ "-",
177
+ { expr: "token" },
178
+ "-",
179
+ { expr: "exports[x]" }
180
+ )
181
+ : runtimeTemplate.concatenation({ expr: "token" }, "-", {
182
+ expr: "exports[x]"
183
+ })
184
+ }`,
185
+ "x"
186
+ )}); exportsWithDashes.forEach(${runtimeTemplate.expressionFunction(
187
+ `exports[x] = "--" + exports[x]`,
188
+ "x"
189
+ )}); ${
190
+ RuntimeGlobals.makeNamespaceObject
191
+ }(exports); target[token] = (${runtimeTemplate.basicFunction(
192
+ "exports, module",
193
+ `module.exports = exports;`
194
+ )}).bind(null, exports); ${
195
+ withHmr ? "moduleIds.push(token); " : ""
196
+ }token = ""; exports = {}; exportsWithId.length = 0; }`,
197
+ `else if(cc == ${cc("\\")}) { token += data[++i] }`,
198
+ `else { token += data[i]; }`
199
+ ]),
200
+ "}",
201
+ `${
202
+ withHmr ? `if(target == ${RuntimeGlobals.moduleFactories}) ` : ""
203
+ }installedChunks[chunkId] = 0;`,
204
+ withHmr ? "return moduleIds;" : ""
205
+ ]
206
+ )}`,
207
+ 'var loadingAttribute = "data-webpack-loading";',
208
+ `var loadStylesheet = ${runtimeTemplate.basicFunction(
209
+ "chunkId, url, done" + (withHmr ? ", hmr" : ""),
210
+ [
211
+ 'var link, needAttach, key = "chunk-" + chunkId;',
212
+ withHmr ? "if(!hmr) {" : "",
213
+ 'var links = document.getElementsByTagName("link");',
214
+ "for(var i = 0; i < links.length; i++) {",
215
+ Template.indent([
216
+ "var l = links[i];",
217
+ `if(l.rel == "stylesheet" && (${
218
+ withHmr
219
+ ? 'l.href.startsWith(url) || l.getAttribute("href").startsWith(url)'
220
+ : 'l.href == url || l.getAttribute("href") == url'
221
+ }${
222
+ uniqueName
223
+ ? ' || l.getAttribute("data-webpack") == uniqueName + ":" + key'
224
+ : ""
225
+ })) { link = l; break; }`
226
+ ]),
227
+ "}",
228
+ "if(!done) return link;",
229
+ withHmr ? "}" : "",
230
+ "if(!link) {",
231
+ Template.indent([
232
+ "needAttach = true;",
233
+ createStylesheet.call(code, this.chunk)
234
+ ]),
235
+ "}",
236
+ `var onLinkComplete = ${runtimeTemplate.basicFunction(
237
+ "prev, event",
238
+ Template.asString([
239
+ "link.onerror = link.onload = null;",
240
+ "link.removeAttribute(loadingAttribute);",
241
+ "clearTimeout(timeout);",
242
+ 'if(event && event.type != "load") link.parentNode.removeChild(link)',
243
+ "done(event);",
244
+ "if(prev) return prev(event);"
245
+ ])
246
+ )};`,
247
+ "if(link.getAttribute(loadingAttribute)) {",
248
+ Template.indent([
249
+ `var timeout = setTimeout(onLinkComplete.bind(null, undefined, { type: 'timeout', target: link }), ${loadTimeout});`,
250
+ "link.onerror = onLinkComplete.bind(null, link.onerror);",
251
+ "link.onload = onLinkComplete.bind(null, link.onload);"
252
+ ]),
253
+ "} else onLinkComplete(undefined, { type: 'load', target: link });", // We assume any existing stylesheet is render blocking
254
+ withHmr ? "hmr ? document.head.insertBefore(link, hmr) :" : "",
255
+ "needAttach && document.head.appendChild(link);",
256
+ "return link;"
257
+ ]
258
+ )};`,
259
+ initialChunkIdsWithCss.size > 2
260
+ ? `${JSON.stringify(
261
+ Array.from(initialChunkIdsWithCss)
262
+ )}.forEach(loadCssChunkData.bind(null, ${
263
+ RuntimeGlobals.moduleFactories
264
+ }, 0));`
265
+ : initialChunkIdsWithCss.size > 0
266
+ ? `${Array.from(
267
+ initialChunkIdsWithCss,
268
+ id =>
269
+ `loadCssChunkData(${
270
+ RuntimeGlobals.moduleFactories
271
+ }, 0, ${JSON.stringify(id)});`
272
+ ).join("")}`
273
+ : "// no initial css",
274
+ "",
275
+ withLoading
276
+ ? Template.asString([
277
+ `${fn}.css = ${runtimeTemplate.basicFunction("chunkId, promises", [
278
+ "// css chunk loading",
279
+ `var installedChunkData = ${RuntimeGlobals.hasOwnProperty}(installedChunks, chunkId) ? installedChunks[chunkId] : undefined;`,
280
+ 'if(installedChunkData !== 0) { // 0 means "already installed".',
281
+ Template.indent([
282
+ "",
283
+ '// a Promise means "currently loading".',
284
+ "if(installedChunkData) {",
285
+ Template.indent(["promises.push(installedChunkData[2]);"]),
286
+ "} else {",
287
+ Template.indent([
288
+ hasCssMatcher === true
289
+ ? "if(true) { // all chunks have CSS"
290
+ : `if(${hasCssMatcher("chunkId")}) {`,
291
+ Template.indent([
292
+ "// setup Promise in chunk cache",
293
+ `var promise = new Promise(${runtimeTemplate.expressionFunction(
294
+ `installedChunkData = installedChunks[chunkId] = [resolve, reject]`,
295
+ "resolve, reject"
296
+ )});`,
297
+ "promises.push(installedChunkData[2] = promise);",
298
+ "",
299
+ "// start chunk loading",
300
+ `var url = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkCssFilename}(chunkId);`,
301
+ "// create error before stack unwound to get useful stacktrace later",
302
+ "var error = new Error();",
303
+ `var loadingEnded = ${runtimeTemplate.basicFunction(
304
+ "event",
305
+ [
306
+ `if(${RuntimeGlobals.hasOwnProperty}(installedChunks, chunkId)) {`,
307
+ Template.indent([
308
+ "installedChunkData = installedChunks[chunkId];",
309
+ "if(installedChunkData !== 0) installedChunks[chunkId] = undefined;",
310
+ "if(installedChunkData) {",
311
+ Template.indent([
312
+ 'if(event.type !== "load") {',
313
+ Template.indent([
314
+ "var errorType = event && event.type;",
315
+ "var realSrc = event && event.target && event.target.src;",
316
+ "error.message = 'Loading css chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')';",
317
+ "error.name = 'ChunkLoadError';",
318
+ "error.type = errorType;",
319
+ "error.request = realSrc;",
320
+ "installedChunkData[1](error);"
321
+ ]),
322
+ "} else {",
323
+ Template.indent([
324
+ `loadCssChunkData(${RuntimeGlobals.moduleFactories}, link, chunkId);`,
325
+ "installedChunkData[0]();"
326
+ ]),
327
+ "}"
328
+ ]),
329
+ "}"
330
+ ]),
331
+ "}"
332
+ ]
333
+ )};`,
334
+ "var link = loadStylesheet(chunkId, url, loadingEnded);"
335
+ ]),
336
+ "} else installedChunks[chunkId] = 0;"
337
+ ]),
338
+ "}"
339
+ ]),
340
+ "}"
341
+ ])};`
342
+ ])
343
+ : "// no chunk loading",
344
+ "",
345
+ withHmr
346
+ ? Template.asString([
347
+ "var oldTags = [];",
348
+ "var newTags = [];",
349
+ `var applyHandler = ${runtimeTemplate.basicFunction("options", [
350
+ `return { dispose: ${runtimeTemplate.basicFunction(
351
+ "",
352
+ []
353
+ )}, apply: ${runtimeTemplate.basicFunction("", [
354
+ "var moduleIds = [];",
355
+ `newTags.forEach(${runtimeTemplate.expressionFunction(
356
+ "info[1].sheet.disabled = false",
357
+ "info"
358
+ )});`,
359
+ "while(oldTags.length) {",
360
+ Template.indent([
361
+ "var oldTag = oldTags.pop();",
362
+ "if(oldTag.parentNode) oldTag.parentNode.removeChild(oldTag);"
363
+ ]),
364
+ "}",
365
+ "while(newTags.length) {",
366
+ Template.indent([
367
+ `var info = newTags.pop();`,
368
+ `var chunkModuleIds = loadCssChunkData(${RuntimeGlobals.moduleFactories}, info[1], info[0]);`,
369
+ `chunkModuleIds.forEach(${runtimeTemplate.expressionFunction(
370
+ "moduleIds.push(id)",
371
+ "id"
372
+ )});`
373
+ ]),
374
+ "}",
375
+ "return moduleIds;"
376
+ ])} };`
377
+ ])}`,
378
+ `var cssTextKey = ${runtimeTemplate.returningFunction(
379
+ `Array.from(link.sheet.cssRules, ${runtimeTemplate.returningFunction(
380
+ "r.cssText",
381
+ "r"
382
+ )}).join()`,
383
+ "link"
384
+ )}`,
385
+ `${
386
+ RuntimeGlobals.hmrDownloadUpdateHandlers
387
+ }.css = ${runtimeTemplate.basicFunction(
388
+ "chunkIds, removedChunks, removedModules, promises, applyHandlers, updatedModulesList",
389
+ [
390
+ "applyHandlers.push(applyHandler);",
391
+ `chunkIds.forEach(${runtimeTemplate.basicFunction("chunkId", [
392
+ `var filename = ${RuntimeGlobals.getChunkCssFilename}(chunkId);`,
393
+ `var url = ${RuntimeGlobals.publicPath} + filename;`,
394
+ "var oldTag = loadStylesheet(chunkId, url);",
395
+ "if(!oldTag) return;",
396
+ `promises.push(new Promise(${runtimeTemplate.basicFunction(
397
+ "resolve, reject",
398
+ [
399
+ `var link = loadStylesheet(chunkId, url + (url.indexOf("?") < 0 ? "?" : "&") + "hmr=" + Date.now(), ${runtimeTemplate.basicFunction(
400
+ "event",
401
+ [
402
+ 'if(event.type !== "load") {',
403
+ Template.indent([
404
+ "var errorType = event && event.type;",
405
+ "var realSrc = event && event.target && event.target.src;",
406
+ "error.message = 'Loading css hot update chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')';",
407
+ "error.name = 'ChunkLoadError';",
408
+ "error.type = errorType;",
409
+ "error.request = realSrc;",
410
+ "reject(error);"
411
+ ]),
412
+ "} else {",
413
+ Template.indent([
414
+ "try { if(cssTextKey(oldTag) == cssTextKey(link)) { if(link.parentNode) link.parentNode.removeChild(link); return resolve(); } } catch(e) {}",
415
+ "var factories = {};",
416
+ "loadCssChunkData(factories, link, chunkId);",
417
+ `Object.keys(factories).forEach(${runtimeTemplate.expressionFunction(
418
+ "updatedModulesList.push(id)",
419
+ "id"
420
+ )})`,
421
+ "link.sheet.disabled = true;",
422
+ "oldTags.push(oldTag);",
423
+ "newTags.push([chunkId, link]);",
424
+ "resolve();"
425
+ ]),
426
+ "}"
427
+ ]
428
+ )}, oldTag);`
429
+ ]
430
+ )}));`
431
+ ])});`
432
+ ]
433
+ )}`
434
+ ])
435
+ : "// no hmr"
436
+ ]);
437
+ }
438
+ }
439
+
440
+ module.exports = CssLoadingRuntimeModule;