webpack 4.1.0 → 4.4.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.
Files changed (118) hide show
  1. package/README.md +719 -721
  2. package/bin/webpack.js +69 -10
  3. package/lib/APIPlugin.js +84 -84
  4. package/lib/AmdMainTemplatePlugin.js +75 -77
  5. package/lib/AsyncDependencyToInitialChunkError.js +21 -23
  6. package/lib/BannerPlugin.js +101 -101
  7. package/lib/Chunk.js +477 -469
  8. package/lib/ChunkTemplate.js +51 -53
  9. package/lib/Compilation.js +1858 -1851
  10. package/lib/Compiler.js +493 -478
  11. package/lib/ConcurrentCompilationError.js +19 -0
  12. package/lib/ContextModule.js +696 -685
  13. package/lib/ContextModuleFactory.js +245 -243
  14. package/lib/DefinePlugin.js +197 -197
  15. package/lib/DelegatedModule.js +101 -101
  16. package/lib/DependenciesBlockVariable.js +51 -52
  17. package/lib/Dependency.js +53 -52
  18. package/lib/DllModule.js +54 -54
  19. package/lib/DllModuleFactory.js +29 -29
  20. package/lib/EnvironmentPlugin.js +65 -67
  21. package/lib/EvalDevToolModuleTemplatePlugin.js +60 -60
  22. package/lib/EvalSourceMapDevToolModuleTemplatePlugin.js +105 -105
  23. package/lib/ExportPropertyMainTemplatePlugin.js +40 -40
  24. package/lib/ExternalModule.js +159 -159
  25. package/lib/FunctionModuleTemplatePlugin.js +98 -98
  26. package/lib/HotModuleReplacement.runtime.js +631 -631
  27. package/lib/HotModuleReplacementPlugin.js +407 -406
  28. package/lib/HotUpdateChunkTemplate.js +78 -80
  29. package/lib/JavascriptGenerator.js +228 -229
  30. package/lib/JavascriptModulesPlugin.js +184 -158
  31. package/lib/JsonGenerator.js +42 -42
  32. package/lib/MainTemplate.js +406 -402
  33. package/lib/Module.js +343 -340
  34. package/lib/ModuleBuildError.js +42 -42
  35. package/lib/ModuleError.js +28 -28
  36. package/lib/ModuleFilenameHelpers.js +166 -166
  37. package/lib/ModuleTemplate.js +77 -79
  38. package/lib/ModuleWarning.js +30 -30
  39. package/lib/MultiCompiler.js +271 -259
  40. package/lib/MultiModule.js +78 -75
  41. package/lib/MultiModuleFactory.js +23 -23
  42. package/lib/MultiWatching.js +38 -37
  43. package/lib/NoModeWarning.js +23 -21
  44. package/lib/NormalModule.js +478 -470
  45. package/lib/NormalModuleFactory.js +483 -481
  46. package/lib/OptionsDefaulter.js +80 -86
  47. package/lib/Parser.js +2074 -2071
  48. package/lib/ProgressPlugin.js +231 -231
  49. package/lib/RawModule.js +54 -55
  50. package/lib/RecordIdsPlugin.js +160 -160
  51. package/lib/RemovedPluginError.js +13 -13
  52. package/lib/ResolverFactory.js +64 -67
  53. package/lib/RuntimeTemplate.js +267 -297
  54. package/lib/SetVarMainTemplatePlugin.js +57 -57
  55. package/lib/SourceMapDevToolPlugin.js +302 -308
  56. package/lib/Stats.js +1234 -1212
  57. package/lib/Template.js +205 -205
  58. package/lib/TemplatedPathPlugin.js +170 -143
  59. package/lib/UmdMainTemplatePlugin.js +264 -269
  60. package/lib/Watching.js +193 -193
  61. package/lib/WebAssemblyParser.js +50 -54
  62. package/lib/WebpackOptionsApply.js +401 -401
  63. package/lib/WebpackOptionsDefaulter.js +337 -317
  64. package/lib/WebpackOptionsValidationError.js +316 -319
  65. package/lib/debug/ProfilingPlugin.js +409 -405
  66. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +328 -311
  67. package/lib/dependencies/AMDRequireContextDependency.js +20 -20
  68. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +270 -241
  69. package/lib/dependencies/HarmonyAcceptImportDependency.js +23 -23
  70. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +620 -606
  71. package/lib/dependencies/HarmonyExportSpecifierDependency.js +53 -53
  72. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +214 -214
  73. package/lib/dependencies/HarmonyImportSpecifierDependency.js +154 -156
  74. package/lib/dependencies/ImportDependenciesBlock.js +17 -17
  75. package/lib/dependencies/ImportDependency.js +34 -34
  76. package/lib/dependencies/ImportEagerDependency.js +32 -32
  77. package/lib/dependencies/ImportParserPlugin.js +175 -179
  78. package/lib/dependencies/ImportWeakDependency.js +34 -34
  79. package/lib/dependencies/JsonExportsDependency.js +25 -25
  80. package/lib/dependencies/ModuleDependency.js +20 -20
  81. package/lib/dependencies/NullDependency.js +20 -20
  82. package/lib/dependencies/RequireContextDependency.js +22 -22
  83. package/lib/dependencies/RequireIncludeDependency.js +40 -40
  84. package/lib/dependencies/WebpackMissingModule.js +20 -22
  85. package/lib/node/NodeChunkTemplatePlugin.js +31 -31
  86. package/lib/node/NodeHotUpdateChunkTemplatePlugin.js +36 -36
  87. package/lib/node/NodeMainTemplatePlugin.js +320 -273
  88. package/lib/node/ReadFileCompileWasmMainTemplatePlugin.js +113 -115
  89. package/lib/optimize/AggressiveSplittingPlugin.js +281 -281
  90. package/lib/optimize/ConcatenatedModule.js +1364 -1366
  91. package/lib/optimize/RemoveParentModulesPlugin.js +114 -114
  92. package/lib/optimize/SplitChunksPlugin.js +519 -491
  93. package/lib/performance/SizeLimitsPlugin.js +105 -105
  94. package/lib/util/TrackingSet.js +35 -35
  95. package/lib/util/objectToMap.js +10 -10
  96. package/lib/wasm/WasmModuleTemplatePlugin.js +106 -106
  97. package/lib/web/JsonpChunkTemplatePlugin.js +47 -47
  98. package/lib/web/JsonpExportMainTemplatePlugin.js +47 -47
  99. package/lib/web/JsonpHotUpdateChunkTemplatePlugin.js +39 -39
  100. package/lib/web/JsonpMainTemplatePlugin.js +425 -403
  101. package/lib/webpack.js +182 -179
  102. package/lib/webworker/WebWorkerChunkTemplatePlugin.js +35 -35
  103. package/lib/webworker/WebWorkerHotUpdateChunkTemplatePlugin.js +40 -40
  104. package/lib/webworker/WebWorkerMainTemplatePlugin.js +177 -154
  105. package/package.json +9 -8
  106. package/schemas/WebpackOptions.json +1973 -1951
  107. package/schemas/ajv.absolutePath.js +55 -29
  108. package/schemas/plugins/BannerPlugin.json +85 -85
  109. package/schemas/plugins/DllPlugin.json +28 -28
  110. package/schemas/plugins/DllReferencePlugin.json +99 -99
  111. package/schemas/plugins/HashedModuleIdsPlugin.json +24 -24
  112. package/schemas/plugins/LoaderOptionsPlugin.json +26 -26
  113. package/schemas/plugins/SourceMapDevToolPlugin.json +187 -187
  114. package/schemas/plugins/WatchIgnorePlugin.json +16 -16
  115. package/schemas/plugins/debug/ProfilingPlugin.json +12 -12
  116. package/schemas/plugins/optimize/AggressiveSplittingPlugin.json +22 -22
  117. package/schemas/plugins/optimize/LimitChunkCountPlugin.json +15 -15
  118. package/schemas/plugins/optimize/MinChunkSizePlugin.json +13 -13
@@ -1,403 +1,425 @@
1
- /*
2
- MIT License http://www.opensource.org/licenses/mit-license.php
3
- Author Tobias Koppers @sokra
4
- */
5
- "use strict";
6
-
7
- const Template = require("../Template");
8
- const SyncWaterfallHook = require("tapable").SyncWaterfallHook;
9
-
10
- class JsonpMainTemplatePlugin {
11
- apply(mainTemplate) {
12
- const needChunkOnDemandLoadingCode = chunk => {
13
- for (const chunkGroup of chunk.groupsIterable) {
14
- if (chunkGroup.getNumberOfChildren() > 0) return true;
15
- }
16
- return false;
17
- };
18
- const needChunkLoadingCode = chunk => {
19
- for (const chunkGroup of chunk.groupsIterable) {
20
- if (chunkGroup.chunks.length > 1) return true;
21
- if (chunkGroup.getNumberOfChildren() > 0) return true;
22
- }
23
- return false;
24
- };
25
- const needEntryDeferringCode = chunk => {
26
- for (const chunkGroup of chunk.groupsIterable) {
27
- if (chunkGroup.chunks.length > 1) return true;
28
- }
29
- return false;
30
- };
31
- // TODO refactor this
32
- if (!mainTemplate.hooks.jsonpScript) {
33
- mainTemplate.hooks.jsonpScript = new SyncWaterfallHook([
34
- "source",
35
- "chunk",
36
- "hash"
37
- ]);
38
- }
39
-
40
- mainTemplate.hooks.localVars.tap(
41
- "JsonpMainTemplatePlugin",
42
- (source, chunk) => {
43
- if (needChunkLoadingCode(chunk)) {
44
- return Template.asString([
45
- source,
46
- "",
47
- "// object to store loaded and loading chunks",
48
- "var installedChunks = {",
49
- Template.indent(
50
- chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
51
- ),
52
- "};",
53
- "",
54
- needEntryDeferringCode(chunk) ? "var deferredModules = [];" : ""
55
- ]);
56
- }
57
- return source;
58
- }
59
- );
60
- mainTemplate.hooks.jsonpScript.tap(
61
- "JsonpMainTemplatePlugin",
62
- (_, chunk, hash) => {
63
- const chunkFilename = mainTemplate.outputOptions.chunkFilename;
64
- const chunkMaps = chunk.getChunkMaps();
65
- const crossOriginLoading =
66
- mainTemplate.outputOptions.crossOriginLoading;
67
- const chunkLoadTimeout = mainTemplate.outputOptions.chunkLoadTimeout;
68
- const jsonpScriptType = mainTemplate.outputOptions.jsonpScriptType;
69
- const scriptSrcPath = mainTemplate.getAssetPath(
70
- JSON.stringify(chunkFilename),
71
- {
72
- hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
73
- hashWithLength: length =>
74
- `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
75
- chunk: {
76
- id: '" + chunkId + "',
77
- hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
78
- hashWithLength(length) {
79
- const shortChunkHashMap = Object.create(null);
80
- for (const chunkId of Object.keys(chunkMaps.hash)) {
81
- if (typeof chunkMaps.hash[chunkId] === "string")
82
- shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(
83
- 0,
84
- length
85
- );
86
- }
87
- return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
88
- },
89
- name: `" + (${JSON.stringify(
90
- chunkMaps.name
91
- )}[chunkId]||chunkId) + "`
92
- }
93
- }
94
- );
95
- return Template.asString([
96
- "var script = document.createElement('script');",
97
- jsonpScriptType
98
- ? `script.type = ${JSON.stringify(jsonpScriptType)};`
99
- : "",
100
- "script.charset = 'utf-8';",
101
- `script.timeout = ${chunkLoadTimeout};`,
102
- crossOriginLoading
103
- ? `script.crossOrigin = ${JSON.stringify(crossOriginLoading)};`
104
- : "",
105
- `if (${mainTemplate.requireFn}.nc) {`,
106
- Template.indent(
107
- `script.setAttribute("nonce", ${mainTemplate.requireFn}.nc);`
108
- ),
109
- "}",
110
- `script.src = ${mainTemplate.requireFn}.p + ${scriptSrcPath};`,
111
- "var timeout = setTimeout(function(){",
112
- Template.indent([
113
- "onScriptComplete({ type: 'timeout', target: script });"
114
- ]),
115
- `}, ${chunkLoadTimeout});`,
116
- "script.onerror = script.onload = onScriptComplete;",
117
- "function onScriptComplete(event) {",
118
- Template.indent([
119
- "// avoid mem leaks in IE.",
120
- "script.onerror = script.onload = null;",
121
- "clearTimeout(timeout);",
122
- "var chunk = installedChunks[chunkId];",
123
- "if(chunk !== 0) {",
124
- Template.indent([
125
- "if(chunk) {",
126
- Template.indent([
127
- "var errorType = event && (event.type === 'load' ? 'missing' : event.type);",
128
- "var realSrc = event && event.target && event.target.src;",
129
- "var error = new Error('Loading chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')');",
130
- "error.type = errorType;",
131
- "error.request = realSrc;",
132
- "chunk[1](error);"
133
- ]),
134
- "}",
135
- "installedChunks[chunkId] = undefined;"
136
- ]),
137
- "}"
138
- ]),
139
- "};"
140
- ]);
141
- }
142
- );
143
- mainTemplate.hooks.requireEnsure.tap(
144
- "JsonpMainTemplatePlugin",
145
- (source, chunk, hash) => {
146
- return Template.asString([
147
- source,
148
- "",
149
- "// JSONP chunk loading for javascript",
150
- "",
151
- "var installedChunkData = installedChunks[chunkId];",
152
- 'if(installedChunkData !== 0) { // 0 means "already installed".',
153
- Template.indent([
154
- "",
155
- '// a Promise means "currently loading".',
156
- "if(installedChunkData) {",
157
- Template.indent(["promises.push(installedChunkData[2]);"]),
158
- "} else {",
159
- Template.indent([
160
- "// setup Promise in chunk cache",
161
- "var promise = new Promise(function(resolve, reject) {",
162
- Template.indent([
163
- "installedChunkData = installedChunks[chunkId] = [resolve, reject];"
164
- ]),
165
- "});",
166
- "promises.push(installedChunkData[2] = promise);",
167
- "",
168
- "// start chunk loading",
169
- "var head = document.getElementsByTagName('head')[0];",
170
- mainTemplate.hooks.jsonpScript.call("", chunk, hash),
171
- "head.appendChild(script);"
172
- ]),
173
- "}"
174
- ]),
175
- "}"
176
- ]);
177
- }
178
- );
179
- mainTemplate.hooks.requireExtensions.tap(
180
- "JsonpMainTemplatePlugin",
181
- (source, chunk) => {
182
- if (!needChunkOnDemandLoadingCode(chunk)) return source;
183
-
184
- return Template.asString([
185
- source,
186
- "",
187
- "// on error function for async loading",
188
- `${
189
- mainTemplate.requireFn
190
- }.oe = function(err) { console.error(err); throw err; };`
191
- ]);
192
- }
193
- );
194
- mainTemplate.hooks.bootstrap.tap(
195
- "JsonpMainTemplatePlugin",
196
- (source, chunk, hash) => {
197
- if (needChunkLoadingCode(chunk)) {
198
- const withDefer = needEntryDeferringCode(chunk);
199
- return Template.asString([
200
- source,
201
- "",
202
- "// install a JSONP callback for chunk loading",
203
- "function webpackJsonpCallback(data) {",
204
- Template.indent([
205
- "var chunkIds = data[0];",
206
- "var moreModules = data[1]",
207
- withDefer ? "var executeModules = data[2];" : "",
208
- '// add "moreModules" to the modules object,',
209
- '// then flag all "chunkIds" as loaded and fire callback',
210
- "var moduleId, chunkId, i = 0, resolves = [];",
211
- "for(;i < chunkIds.length; i++) {",
212
- Template.indent([
213
- "chunkId = chunkIds[i];",
214
- "if(installedChunks[chunkId]) {",
215
- Template.indent("resolves.push(installedChunks[chunkId][0]);"),
216
- "}",
217
- "installedChunks[chunkId] = 0;"
218
- ]),
219
- "}",
220
- "for(moduleId in moreModules) {",
221
- Template.indent([
222
- "if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {",
223
- Template.indent(
224
- mainTemplate.renderAddModule(
225
- hash,
226
- chunk,
227
- "moduleId",
228
- "moreModules[moduleId]"
229
- )
230
- ),
231
- "}"
232
- ]),
233
- "}",
234
- "if(parentJsonpFunction) parentJsonpFunction(data);",
235
- "while(resolves.length) {",
236
- Template.indent("resolves.shift()();"),
237
- "}",
238
- withDefer
239
- ? Template.asString([
240
- "",
241
- "// add entry modules from loaded chunk to deferred list",
242
- "deferredModules.push.apply(deferredModules, executeModules || []);",
243
- "",
244
- "// run deferred modules when all chunks ready",
245
- "return checkDeferredModules();"
246
- ])
247
- : ""
248
- ]),
249
- "};",
250
- withDefer
251
- ? Template.asString([
252
- "function checkDeferredModules() {",
253
- Template.indent([
254
- "var result;",
255
- "for(var i = 0; i < deferredModules.length; i++) {",
256
- Template.indent([
257
- "var deferredModule = deferredModules[i];",
258
- "var fullfilled = true;",
259
- "for(var j = 1; j < deferredModule.length; j++) {",
260
- Template.indent([
261
- "var depId = deferredModule[j];",
262
- "if(installedChunks[depId] !== 0) fullfilled = false;"
263
- ]),
264
- "}",
265
- "if(fullfilled) {",
266
- Template.indent([
267
- "deferredModules.splice(i--, 1);",
268
- "result = " +
269
- mainTemplate.requireFn +
270
- "(" +
271
- mainTemplate.requireFn +
272
- ".s = deferredModule[0]);"
273
- ]),
274
- "}"
275
- ]),
276
- "}",
277
- "return result;"
278
- ]),
279
- "}"
280
- ])
281
- : ""
282
- ]);
283
- }
284
- return source;
285
- }
286
- );
287
- mainTemplate.hooks.beforeStartup.tap(
288
- "JsonpMainTemplatePlugin",
289
- (source, chunk, hash) => {
290
- if (needChunkLoadingCode(chunk)) {
291
- var jsonpFunction = mainTemplate.outputOptions.jsonpFunction;
292
- var globalObject = mainTemplate.outputOptions.globalObject;
293
- return Template.asString([
294
- `var jsonpArray = ${globalObject}[${JSON.stringify(
295
- jsonpFunction
296
- )}] = ${globalObject}[${JSON.stringify(jsonpFunction)}] || [];`,
297
- "var oldJsonpFunction = jsonpArray.push.bind(jsonpArray);",
298
- "jsonpArray.push = webpackJsonpCallback;",
299
- "jsonpArray = jsonpArray.slice();",
300
- "for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);",
301
- "var parentJsonpFunction = oldJsonpFunction;",
302
- "",
303
- source
304
- ]);
305
- }
306
- return source;
307
- }
308
- );
309
- mainTemplate.hooks.startup.tap(
310
- "JsonpMainTemplatePlugin",
311
- (source, chunk, hash) => {
312
- if (needEntryDeferringCode(chunk)) {
313
- if (chunk.hasEntryModule()) {
314
- const entries = [chunk.entryModule].filter(Boolean).map(m =>
315
- [m.id].concat(
316
- Array.from(chunk.groupsIterable)[0]
317
- .chunks.filter(c => c !== chunk)
318
- .map(c => c.id)
319
- )
320
- );
321
- return Template.asString([
322
- "// add entry module to deferred list",
323
- `deferredModules.push(${entries
324
- .map(e => JSON.stringify(e))
325
- .join(", ")});`,
326
- "// run deferred modules when ready",
327
- "return checkDeferredModules();"
328
- ]);
329
- } else {
330
- return Template.asString([
331
- "// run deferred modules from other chunks",
332
- "checkDeferredModules();"
333
- ]);
334
- }
335
- }
336
- return source;
337
- }
338
- );
339
- mainTemplate.hooks.hotBootstrap.tap(
340
- "JsonpMainTemplatePlugin",
341
- (source, chunk, hash) => {
342
- const globalObject = mainTemplate.outputOptions.globalObject;
343
- const hotUpdateChunkFilename =
344
- mainTemplate.outputOptions.hotUpdateChunkFilename;
345
- const hotUpdateMainFilename =
346
- mainTemplate.outputOptions.hotUpdateMainFilename;
347
- const crossOriginLoading =
348
- mainTemplate.outputOptions.crossOriginLoading;
349
- const hotUpdateFunction = mainTemplate.outputOptions.hotUpdateFunction;
350
- const currentHotUpdateChunkFilename = mainTemplate.getAssetPath(
351
- JSON.stringify(hotUpdateChunkFilename),
352
- {
353
- hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
354
- hashWithLength: length =>
355
- `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
356
- chunk: {
357
- id: '" + chunkId + "'
358
- }
359
- }
360
- );
361
- const currentHotUpdateMainFilename = mainTemplate.getAssetPath(
362
- JSON.stringify(hotUpdateMainFilename),
363
- {
364
- hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
365
- hashWithLength: length =>
366
- `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`
367
- }
368
- );
369
- const runtimeSource = Template.getFunctionContent(
370
- require("./JsonpMainTemplate.runtime.js")
371
- )
372
- .replace(/\/\/\$semicolon/g, ";")
373
- .replace(/\$require\$/g, mainTemplate.requireFn)
374
- .replace(
375
- /\$crossOriginLoading\$/g,
376
- crossOriginLoading
377
- ? `script.crossOrigin = ${JSON.stringify(crossOriginLoading)}`
378
- : ""
379
- )
380
- .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
381
- .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
382
- .replace(/\$hash\$/g, JSON.stringify(hash));
383
- return `${source}
384
- function hotDisposeChunk(chunkId) {
385
- delete installedChunks[chunkId];
386
- }
387
- var parentHotUpdateCallback = ${globalObject}[${JSON.stringify(
388
- hotUpdateFunction
389
- )}];
390
- ${globalObject}[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
391
- }
392
- );
393
- mainTemplate.hooks.hash.tap("JsonpMainTemplatePlugin", hash => {
394
- hash.update("jsonp");
395
- hash.update("5");
396
- hash.update(`${mainTemplate.outputOptions.globalObject}`);
397
- hash.update(`${mainTemplate.outputOptions.chunkFilename}`);
398
- hash.update(`${mainTemplate.outputOptions.jsonpFunction}`);
399
- hash.update(`${mainTemplate.outputOptions.hotUpdateFunction}`);
400
- });
401
- }
402
- }
403
- module.exports = JsonpMainTemplatePlugin;
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+ "use strict";
6
+
7
+ const { SyncWaterfallHook } = require("tapable");
8
+ const Template = require("../Template");
9
+
10
+ class JsonpMainTemplatePlugin {
11
+ apply(mainTemplate) {
12
+ const needChunkOnDemandLoadingCode = chunk => {
13
+ for (const chunkGroup of chunk.groupsIterable) {
14
+ if (chunkGroup.getNumberOfChildren() > 0) return true;
15
+ }
16
+ return false;
17
+ };
18
+ const needChunkLoadingCode = chunk => {
19
+ for (const chunkGroup of chunk.groupsIterable) {
20
+ if (chunkGroup.chunks.length > 1) return true;
21
+ if (chunkGroup.getNumberOfChildren() > 0) return true;
22
+ }
23
+ return false;
24
+ };
25
+ const needEntryDeferringCode = chunk => {
26
+ for (const chunkGroup of chunk.groupsIterable) {
27
+ if (chunkGroup.chunks.length > 1) return true;
28
+ }
29
+ return false;
30
+ };
31
+ // TODO refactor this
32
+ if (!mainTemplate.hooks.jsonpScript) {
33
+ mainTemplate.hooks.jsonpScript = new SyncWaterfallHook([
34
+ "source",
35
+ "chunk",
36
+ "hash"
37
+ ]);
38
+ }
39
+
40
+ mainTemplate.hooks.localVars.tap(
41
+ "JsonpMainTemplatePlugin",
42
+ (source, chunk) => {
43
+ if (needChunkLoadingCode(chunk)) {
44
+ return Template.asString([
45
+ source,
46
+ "",
47
+ "// object to store loaded and loading chunks",
48
+ "var installedChunks = {",
49
+ Template.indent(
50
+ chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
51
+ ),
52
+ "};",
53
+ "",
54
+ needEntryDeferringCode(chunk) ? "var deferredModules = [];" : ""
55
+ ]);
56
+ }
57
+ return source;
58
+ }
59
+ );
60
+ mainTemplate.hooks.jsonpScript.tap(
61
+ "JsonpMainTemplatePlugin",
62
+ (_, chunk, hash) => {
63
+ const chunkFilename = mainTemplate.outputOptions.chunkFilename;
64
+ const chunkMaps = chunk.getChunkMaps();
65
+ const crossOriginLoading =
66
+ mainTemplate.outputOptions.crossOriginLoading;
67
+ const chunkLoadTimeout = mainTemplate.outputOptions.chunkLoadTimeout;
68
+ const jsonpScriptType = mainTemplate.outputOptions.jsonpScriptType;
69
+ const scriptSrcPath = mainTemplate.getAssetPath(
70
+ JSON.stringify(chunkFilename),
71
+ {
72
+ hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
73
+ hashWithLength: length =>
74
+ `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
75
+ chunk: {
76
+ id: '" + chunkId + "',
77
+ hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
78
+ hashWithLength(length) {
79
+ const shortChunkHashMap = Object.create(null);
80
+ for (const chunkId of Object.keys(chunkMaps.hash)) {
81
+ if (typeof chunkMaps.hash[chunkId] === "string")
82
+ shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(
83
+ 0,
84
+ length
85
+ );
86
+ }
87
+ return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
88
+ },
89
+ name: `" + (${JSON.stringify(
90
+ chunkMaps.name
91
+ )}[chunkId]||chunkId) + "`,
92
+ contentHash: {
93
+ javascript: `" + ${JSON.stringify(
94
+ chunkMaps.contentHash.javascript
95
+ )}[chunkId] + "`
96
+ },
97
+ contentHashWithLength: {
98
+ javascript: length => {
99
+ const shortContentHashMap = {};
100
+ const contentHash = chunkMaps.contentHash.javascript;
101
+ for (const chunkId of Object.keys(contentHash)) {
102
+ if (typeof contentHash[chunkId] === "string") {
103
+ shortContentHashMap[chunkId] = contentHash[
104
+ chunkId
105
+ ].substr(0, length);
106
+ }
107
+ }
108
+ return `" + ${JSON.stringify(
109
+ shortContentHashMap
110
+ )}[chunkId] + "`;
111
+ }
112
+ }
113
+ },
114
+ contentHashType: "javascript"
115
+ }
116
+ );
117
+ return Template.asString([
118
+ "var script = document.createElement('script');",
119
+ jsonpScriptType
120
+ ? `script.type = ${JSON.stringify(jsonpScriptType)};`
121
+ : "",
122
+ "script.charset = 'utf-8';",
123
+ `script.timeout = ${chunkLoadTimeout / 1000};`,
124
+ crossOriginLoading
125
+ ? `script.crossOrigin = ${JSON.stringify(crossOriginLoading)};`
126
+ : "",
127
+ `if (${mainTemplate.requireFn}.nc) {`,
128
+ Template.indent(
129
+ `script.setAttribute("nonce", ${mainTemplate.requireFn}.nc);`
130
+ ),
131
+ "}",
132
+ `script.src = ${mainTemplate.requireFn}.p + ${scriptSrcPath};`,
133
+ "var timeout = setTimeout(function(){",
134
+ Template.indent([
135
+ "onScriptComplete({ type: 'timeout', target: script });"
136
+ ]),
137
+ `}, ${chunkLoadTimeout});`,
138
+ "script.onerror = script.onload = onScriptComplete;",
139
+ "function onScriptComplete(event) {",
140
+ Template.indent([
141
+ "// avoid mem leaks in IE.",
142
+ "script.onerror = script.onload = null;",
143
+ "clearTimeout(timeout);",
144
+ "var chunk = installedChunks[chunkId];",
145
+ "if(chunk !== 0) {",
146
+ Template.indent([
147
+ "if(chunk) {",
148
+ Template.indent([
149
+ "var errorType = event && (event.type === 'load' ? 'missing' : event.type);",
150
+ "var realSrc = event && event.target && event.target.src;",
151
+ "var error = new Error('Loading chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')');",
152
+ "error.type = errorType;",
153
+ "error.request = realSrc;",
154
+ "chunk[1](error);"
155
+ ]),
156
+ "}",
157
+ "installedChunks[chunkId] = undefined;"
158
+ ]),
159
+ "}"
160
+ ]),
161
+ "};"
162
+ ]);
163
+ }
164
+ );
165
+ mainTemplate.hooks.requireEnsure.tap(
166
+ "JsonpMainTemplatePlugin",
167
+ (source, chunk, hash) => {
168
+ return Template.asString([
169
+ source,
170
+ "",
171
+ "// JSONP chunk loading for javascript",
172
+ "",
173
+ "var installedChunkData = installedChunks[chunkId];",
174
+ 'if(installedChunkData !== 0) { // 0 means "already installed".',
175
+ Template.indent([
176
+ "",
177
+ '// a Promise means "currently loading".',
178
+ "if(installedChunkData) {",
179
+ Template.indent(["promises.push(installedChunkData[2]);"]),
180
+ "} else {",
181
+ Template.indent([
182
+ "// setup Promise in chunk cache",
183
+ "var promise = new Promise(function(resolve, reject) {",
184
+ Template.indent([
185
+ "installedChunkData = installedChunks[chunkId] = [resolve, reject];"
186
+ ]),
187
+ "});",
188
+ "promises.push(installedChunkData[2] = promise);",
189
+ "",
190
+ "// start chunk loading",
191
+ "var head = document.getElementsByTagName('head')[0];",
192
+ mainTemplate.hooks.jsonpScript.call("", chunk, hash),
193
+ "head.appendChild(script);"
194
+ ]),
195
+ "}"
196
+ ]),
197
+ "}"
198
+ ]);
199
+ }
200
+ );
201
+ mainTemplate.hooks.requireExtensions.tap(
202
+ "JsonpMainTemplatePlugin",
203
+ (source, chunk) => {
204
+ if (!needChunkOnDemandLoadingCode(chunk)) return source;
205
+
206
+ return Template.asString([
207
+ source,
208
+ "",
209
+ "// on error function for async loading",
210
+ `${
211
+ mainTemplate.requireFn
212
+ }.oe = function(err) { console.error(err); throw err; };`
213
+ ]);
214
+ }
215
+ );
216
+ mainTemplate.hooks.bootstrap.tap(
217
+ "JsonpMainTemplatePlugin",
218
+ (source, chunk, hash) => {
219
+ if (needChunkLoadingCode(chunk)) {
220
+ const withDefer = needEntryDeferringCode(chunk);
221
+ return Template.asString([
222
+ source,
223
+ "",
224
+ "// install a JSONP callback for chunk loading",
225
+ "function webpackJsonpCallback(data) {",
226
+ Template.indent([
227
+ "var chunkIds = data[0];",
228
+ "var moreModules = data[1];",
229
+ withDefer ? "var executeModules = data[2];" : "",
230
+ '// add "moreModules" to the modules object,',
231
+ '// then flag all "chunkIds" as loaded and fire callback',
232
+ "var moduleId, chunkId, i = 0, resolves = [];",
233
+ "for(;i < chunkIds.length; i++) {",
234
+ Template.indent([
235
+ "chunkId = chunkIds[i];",
236
+ "if(installedChunks[chunkId]) {",
237
+ Template.indent("resolves.push(installedChunks[chunkId][0]);"),
238
+ "}",
239
+ "installedChunks[chunkId] = 0;"
240
+ ]),
241
+ "}",
242
+ "for(moduleId in moreModules) {",
243
+ Template.indent([
244
+ "if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {",
245
+ Template.indent(
246
+ mainTemplate.renderAddModule(
247
+ hash,
248
+ chunk,
249
+ "moduleId",
250
+ "moreModules[moduleId]"
251
+ )
252
+ ),
253
+ "}"
254
+ ]),
255
+ "}",
256
+ "if(parentJsonpFunction) parentJsonpFunction(data);",
257
+ "while(resolves.length) {",
258
+ Template.indent("resolves.shift()();"),
259
+ "}",
260
+ withDefer
261
+ ? Template.asString([
262
+ "",
263
+ "// add entry modules from loaded chunk to deferred list",
264
+ "deferredModules.push.apply(deferredModules, executeModules || []);",
265
+ "",
266
+ "// run deferred modules when all chunks ready",
267
+ "return checkDeferredModules();"
268
+ ])
269
+ : ""
270
+ ]),
271
+ "};",
272
+ withDefer
273
+ ? Template.asString([
274
+ "function checkDeferredModules() {",
275
+ Template.indent([
276
+ "var result;",
277
+ "for(var i = 0; i < deferredModules.length; i++) {",
278
+ Template.indent([
279
+ "var deferredModule = deferredModules[i];",
280
+ "var fulfilled = true;",
281
+ "for(var j = 1; j < deferredModule.length; j++) {",
282
+ Template.indent([
283
+ "var depId = deferredModule[j];",
284
+ "if(installedChunks[depId] !== 0) fulfilled = false;"
285
+ ]),
286
+ "}",
287
+ "if(fulfilled) {",
288
+ Template.indent([
289
+ "deferredModules.splice(i--, 1);",
290
+ "result = " +
291
+ mainTemplate.requireFn +
292
+ "(" +
293
+ mainTemplate.requireFn +
294
+ ".s = deferredModule[0]);"
295
+ ]),
296
+ "}"
297
+ ]),
298
+ "}",
299
+ "return result;"
300
+ ]),
301
+ "}"
302
+ ])
303
+ : ""
304
+ ]);
305
+ }
306
+ return source;
307
+ }
308
+ );
309
+ mainTemplate.hooks.beforeStartup.tap(
310
+ "JsonpMainTemplatePlugin",
311
+ (source, chunk, hash) => {
312
+ if (needChunkLoadingCode(chunk)) {
313
+ var jsonpFunction = mainTemplate.outputOptions.jsonpFunction;
314
+ var globalObject = mainTemplate.outputOptions.globalObject;
315
+ return Template.asString([
316
+ `var jsonpArray = ${globalObject}[${JSON.stringify(
317
+ jsonpFunction
318
+ )}] = ${globalObject}[${JSON.stringify(jsonpFunction)}] || [];`,
319
+ "var oldJsonpFunction = jsonpArray.push.bind(jsonpArray);",
320
+ "jsonpArray.push = webpackJsonpCallback;",
321
+ "jsonpArray = jsonpArray.slice();",
322
+ "for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);",
323
+ "var parentJsonpFunction = oldJsonpFunction;",
324
+ "",
325
+ source
326
+ ]);
327
+ }
328
+ return source;
329
+ }
330
+ );
331
+ mainTemplate.hooks.startup.tap(
332
+ "JsonpMainTemplatePlugin",
333
+ (source, chunk, hash) => {
334
+ if (needEntryDeferringCode(chunk)) {
335
+ if (chunk.hasEntryModule()) {
336
+ const entries = [chunk.entryModule].filter(Boolean).map(m =>
337
+ [m.id].concat(
338
+ Array.from(chunk.groupsIterable)[0]
339
+ .chunks.filter(c => c !== chunk)
340
+ .map(c => c.id)
341
+ )
342
+ );
343
+ return Template.asString([
344
+ "// add entry module to deferred list",
345
+ `deferredModules.push(${entries
346
+ .map(e => JSON.stringify(e))
347
+ .join(", ")});`,
348
+ "// run deferred modules when ready",
349
+ "return checkDeferredModules();"
350
+ ]);
351
+ } else {
352
+ return Template.asString([
353
+ "// run deferred modules from other chunks",
354
+ "checkDeferredModules();"
355
+ ]);
356
+ }
357
+ }
358
+ return source;
359
+ }
360
+ );
361
+ mainTemplate.hooks.hotBootstrap.tap(
362
+ "JsonpMainTemplatePlugin",
363
+ (source, chunk, hash) => {
364
+ const globalObject = mainTemplate.outputOptions.globalObject;
365
+ const hotUpdateChunkFilename =
366
+ mainTemplate.outputOptions.hotUpdateChunkFilename;
367
+ const hotUpdateMainFilename =
368
+ mainTemplate.outputOptions.hotUpdateMainFilename;
369
+ const crossOriginLoading =
370
+ mainTemplate.outputOptions.crossOriginLoading;
371
+ const hotUpdateFunction = mainTemplate.outputOptions.hotUpdateFunction;
372
+ const currentHotUpdateChunkFilename = mainTemplate.getAssetPath(
373
+ JSON.stringify(hotUpdateChunkFilename),
374
+ {
375
+ hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
376
+ hashWithLength: length =>
377
+ `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
378
+ chunk: {
379
+ id: '" + chunkId + "'
380
+ }
381
+ }
382
+ );
383
+ const currentHotUpdateMainFilename = mainTemplate.getAssetPath(
384
+ JSON.stringify(hotUpdateMainFilename),
385
+ {
386
+ hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
387
+ hashWithLength: length =>
388
+ `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`
389
+ }
390
+ );
391
+ const runtimeSource = Template.getFunctionContent(
392
+ require("./JsonpMainTemplate.runtime.js")
393
+ )
394
+ .replace(/\/\/\$semicolon/g, ";")
395
+ .replace(/\$require\$/g, mainTemplate.requireFn)
396
+ .replace(
397
+ /\$crossOriginLoading\$/g,
398
+ crossOriginLoading
399
+ ? `script.crossOrigin = ${JSON.stringify(crossOriginLoading)}`
400
+ : ""
401
+ )
402
+ .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
403
+ .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
404
+ .replace(/\$hash\$/g, JSON.stringify(hash));
405
+ return `${source}
406
+ function hotDisposeChunk(chunkId) {
407
+ delete installedChunks[chunkId];
408
+ }
409
+ var parentHotUpdateCallback = ${globalObject}[${JSON.stringify(
410
+ hotUpdateFunction
411
+ )}];
412
+ ${globalObject}[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
413
+ }
414
+ );
415
+ mainTemplate.hooks.hash.tap("JsonpMainTemplatePlugin", hash => {
416
+ hash.update("jsonp");
417
+ hash.update("5");
418
+ hash.update(`${mainTemplate.outputOptions.globalObject}`);
419
+ hash.update(`${mainTemplate.outputOptions.chunkFilename}`);
420
+ hash.update(`${mainTemplate.outputOptions.jsonpFunction}`);
421
+ hash.update(`${mainTemplate.outputOptions.hotUpdateFunction}`);
422
+ });
423
+ }
424
+ }
425
+ module.exports = JsonpMainTemplatePlugin;