webpack 4.9.1 → 4.10.2

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 (279) hide show
  1. package/README.md +755 -755
  2. package/SECURITY.md +9 -9
  3. package/buildin/global.js +20 -20
  4. package/buildin/harmony-module.js +24 -24
  5. package/buildin/module.js +22 -22
  6. package/hot/dev-server.js +61 -61
  7. package/hot/log-apply-result.js +44 -44
  8. package/hot/log.js +45 -45
  9. package/hot/only-dev-server.js +105 -105
  10. package/hot/poll.js +37 -37
  11. package/hot/signal.js +62 -62
  12. package/lib/APIPlugin.js +84 -84
  13. package/lib/AmdMainTemplatePlugin.js +87 -75
  14. package/lib/AsyncDependenciesBlock.js +66 -66
  15. package/lib/AsyncDependencyToInitialChunkError.js +31 -21
  16. package/lib/AutomaticPrefetchPlugin.js +50 -50
  17. package/lib/BannerPlugin.js +3 -1
  18. package/lib/BasicEvaluatedExpression.js +211 -208
  19. package/lib/CachePlugin.js +102 -95
  20. package/lib/CaseSensitiveModulesWarning.js +71 -53
  21. package/lib/Chunk.js +750 -722
  22. package/lib/ChunkGroup.js +13 -5
  23. package/lib/ChunkRenderError.js +32 -32
  24. package/lib/CompatibilityPlugin.js +63 -63
  25. package/lib/Compilation.js +1947 -1905
  26. package/lib/Compiler.js +508 -496
  27. package/lib/ConcurrentCompilationError.js +19 -19
  28. package/lib/ConstPlugin.js +258 -242
  29. package/lib/ContextExclusionPlugin.js +17 -17
  30. package/lib/ContextModule.js +749 -710
  31. package/lib/ContextModuleFactory.js +256 -245
  32. package/lib/ContextReplacementPlugin.js +133 -126
  33. package/lib/DefinePlugin.js +206 -197
  34. package/lib/DelegatedModule.js +101 -101
  35. package/lib/DelegatedModuleFactoryPlugin.js +89 -89
  36. package/lib/DelegatedPlugin.js +39 -39
  37. package/lib/DependenciesBlock.js +89 -87
  38. package/lib/DependenciesBlockVariable.js +52 -51
  39. package/lib/Dependency.js +51 -51
  40. package/lib/DllEntryPlugin.js +51 -51
  41. package/lib/DllModule.js +54 -54
  42. package/lib/DllModuleFactory.js +29 -29
  43. package/lib/DllPlugin.js +44 -42
  44. package/lib/DllReferencePlugin.js +84 -84
  45. package/lib/DynamicEntryPlugin.js +73 -71
  46. package/lib/EntryOptionPlugin.js +33 -33
  47. package/lib/EnvironmentPlugin.js +65 -65
  48. package/lib/ErrorHelpers.js +60 -57
  49. package/lib/EvalDevToolModulePlugin.js +27 -27
  50. package/lib/EvalDevToolModuleTemplatePlugin.js +61 -61
  51. package/lib/EvalSourceMapDevToolPlugin.js +41 -40
  52. package/lib/ExportPropertyMainTemplatePlugin.js +53 -40
  53. package/lib/ExtendedAPIPlugin.js +84 -84
  54. package/lib/ExternalModule.js +159 -159
  55. package/lib/ExternalModuleFactoryPlugin.js +110 -110
  56. package/lib/ExternalsPlugin.js +23 -23
  57. package/lib/FlagDependencyExportsPlugin.js +146 -146
  58. package/lib/FlagDependencyUsagePlugin.js +110 -104
  59. package/lib/FlagInitialModulesAsUsedPlugin.js +36 -36
  60. package/lib/FunctionModulePlugin.js +19 -19
  61. package/lib/FunctionModuleTemplatePlugin.js +100 -98
  62. package/lib/GraphHelpers.js +64 -64
  63. package/lib/HarmonyLinkingError.js +18 -18
  64. package/lib/HashedModuleIdsPlugin.js +53 -53
  65. package/lib/HotModuleReplacement.runtime.js +7 -3
  66. package/lib/HotModuleReplacementPlugin.js +413 -406
  67. package/lib/HotUpdateChunk.js +16 -16
  68. package/lib/HotUpdateChunkTemplate.js +78 -78
  69. package/lib/IgnorePlugin.js +71 -71
  70. package/lib/JavascriptGenerator.js +229 -228
  71. package/lib/JavascriptModulesPlugin.js +179 -184
  72. package/lib/JsonGenerator.js +42 -42
  73. package/lib/JsonModulesPlugin.js +30 -30
  74. package/lib/JsonParser.js +27 -26
  75. package/lib/LibManifestPlugin.js +86 -86
  76. package/lib/LibraryTemplatePlugin.js +153 -119
  77. package/lib/LoaderOptionsPlugin.js +53 -52
  78. package/lib/LoaderTargetPlugin.js +24 -24
  79. package/lib/MainTemplate.js +34 -9
  80. package/lib/Module.js +381 -377
  81. package/lib/ModuleBuildError.js +42 -42
  82. package/lib/ModuleDependencyError.js +35 -25
  83. package/lib/ModuleDependencyWarning.js +25 -25
  84. package/lib/ModuleError.js +28 -28
  85. package/lib/ModuleFilenameHelpers.js +178 -166
  86. package/lib/ModuleParseError.js +44 -44
  87. package/lib/ModuleReason.js +40 -40
  88. package/lib/ModuleTemplate.js +84 -84
  89. package/lib/ModuleWarning.js +30 -30
  90. package/lib/MultiCompiler.js +283 -271
  91. package/lib/MultiEntryPlugin.js +58 -58
  92. package/lib/MultiModule.js +81 -78
  93. package/lib/MultiModuleFactory.js +23 -23
  94. package/lib/MultiStats.js +92 -92
  95. package/lib/MultiWatching.js +38 -38
  96. package/lib/NamedChunksPlugin.js +29 -29
  97. package/lib/NamedModulesPlugin.js +57 -57
  98. package/lib/NoEmitOnErrorsPlugin.js +20 -20
  99. package/lib/NoModeWarning.js +23 -23
  100. package/lib/NodeStuffPlugin.js +179 -178
  101. package/lib/NormalModule.js +497 -490
  102. package/lib/NormalModuleFactory.js +501 -483
  103. package/lib/NormalModuleReplacementPlugin.js +51 -51
  104. package/lib/OptionsDefaulter.js +84 -80
  105. package/lib/Parser.js +2164 -2086
  106. package/lib/ParserHelpers.js +103 -100
  107. package/lib/PrefetchPlugin.js +37 -37
  108. package/lib/ProgressPlugin.js +231 -231
  109. package/lib/ProvidePlugin.js +86 -86
  110. package/lib/RawModule.js +56 -54
  111. package/lib/RecordIdsPlugin.js +166 -162
  112. package/lib/RemovedPluginError.js +13 -13
  113. package/lib/RequestShortener.js +81 -74
  114. package/lib/RequireJsStuffPlugin.js +69 -69
  115. package/lib/ResolverFactory.js +64 -64
  116. package/lib/RuleSet.js +555 -534
  117. package/lib/RuntimeTemplate.js +320 -277
  118. package/lib/SetVarMainTemplatePlugin.js +69 -57
  119. package/lib/SingleEntryPlugin.js +44 -44
  120. package/lib/SizeFormatHelpers.js +24 -24
  121. package/lib/SourceMapDevToolModuleOptionsPlugin.js +49 -49
  122. package/lib/SourceMapDevToolPlugin.js +301 -300
  123. package/lib/Stats.js +1408 -1367
  124. package/lib/Template.js +4 -2
  125. package/lib/TemplatedPathPlugin.js +173 -170
  126. package/lib/UmdMainTemplatePlugin.js +304 -264
  127. package/lib/UseStrictPlugin.js +48 -48
  128. package/lib/WarnCaseSensitiveModulesPlugin.js +37 -36
  129. package/lib/WarnNoModeSetPlugin.js +17 -17
  130. package/lib/WatchIgnorePlugin.js +100 -100
  131. package/lib/Watching.js +194 -193
  132. package/lib/WebpackError.js +25 -19
  133. package/lib/WebpackOptionsApply.js +421 -405
  134. package/lib/WebpackOptionsDefaulter.js +347 -344
  135. package/lib/WebpackOptionsValidationError.js +345 -316
  136. package/lib/compareLocations.js +56 -56
  137. package/lib/dependencies/AMDDefineDependency.js +137 -137
  138. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +336 -327
  139. package/lib/dependencies/AMDPlugin.js +250 -250
  140. package/lib/dependencies/AMDRequireArrayDependency.js +49 -49
  141. package/lib/dependencies/AMDRequireContextDependency.js +20 -20
  142. package/lib/dependencies/AMDRequireDependenciesBlock.js +43 -43
  143. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +273 -270
  144. package/lib/dependencies/AMDRequireDependency.js +135 -135
  145. package/lib/dependencies/CommonJsPlugin.js +161 -161
  146. package/lib/dependencies/CommonJsRequireContextDependency.js +23 -23
  147. package/lib/dependencies/CommonJsRequireDependencyParserPlugin.js +130 -130
  148. package/lib/dependencies/ConstDependency.js +33 -33
  149. package/lib/dependencies/ContextDependency.js +68 -68
  150. package/lib/dependencies/ContextDependencyTemplateAsId.js +42 -42
  151. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +38 -38
  152. package/lib/dependencies/ContextElementDependency.js +21 -21
  153. package/lib/dependencies/DelegatedExportsDependency.js +33 -33
  154. package/lib/dependencies/DependencyReference.js +18 -18
  155. package/lib/dependencies/HarmonyAcceptDependency.js +45 -45
  156. package/lib/dependencies/HarmonyAcceptImportDependency.js +23 -23
  157. package/lib/dependencies/HarmonyCompatibilityDependency.js +31 -31
  158. package/lib/dependencies/HarmonyDetectionParserPlugin.js +92 -90
  159. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +139 -139
  160. package/lib/dependencies/HarmonyExportExpressionDependency.js +53 -53
  161. package/lib/dependencies/HarmonyExportHeaderDependency.js +30 -30
  162. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +620 -603
  163. package/lib/dependencies/HarmonyExportSpecifierDependency.js +54 -54
  164. package/lib/dependencies/HarmonyImportDependency.js +104 -94
  165. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +217 -214
  166. package/lib/dependencies/HarmonyImportSideEffectDependency.js +31 -31
  167. package/lib/dependencies/HarmonyImportSpecifierDependency.js +166 -156
  168. package/lib/dependencies/HarmonyInitDependency.js +60 -60
  169. package/lib/dependencies/HarmonyModulesPlugin.js +146 -146
  170. package/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +26 -26
  171. package/lib/dependencies/ImportContextDependency.js +23 -23
  172. package/lib/dependencies/ImportDependenciesBlock.js +18 -18
  173. package/lib/dependencies/ImportDependency.js +34 -34
  174. package/lib/dependencies/ImportEagerDependency.js +32 -32
  175. package/lib/dependencies/ImportParserPlugin.js +233 -232
  176. package/lib/dependencies/ImportPlugin.js +82 -82
  177. package/lib/dependencies/ImportWeakDependency.js +34 -34
  178. package/lib/dependencies/JsonExportsDependency.js +26 -26
  179. package/lib/dependencies/LoaderPlugin.js +98 -93
  180. package/lib/dependencies/LocalModuleDependency.js +28 -28
  181. package/lib/dependencies/LocalModulesHelpers.js +52 -45
  182. package/lib/dependencies/ModuleDependency.js +20 -20
  183. package/lib/dependencies/ModuleDependencyTemplateAsId.js +17 -17
  184. package/lib/dependencies/ModuleDependencyTemplateAsRequireId.js +17 -17
  185. package/lib/dependencies/MultiEntryDependency.js +20 -20
  186. package/lib/dependencies/NullDependency.js +20 -20
  187. package/lib/dependencies/RequireContextDependency.js +22 -22
  188. package/lib/dependencies/RequireContextDependencyParserPlugin.js +56 -56
  189. package/lib/dependencies/RequireContextPlugin.js +143 -141
  190. package/lib/dependencies/RequireEnsureDependenciesBlock.js +33 -33
  191. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +116 -112
  192. package/lib/dependencies/RequireEnsureDependency.js +58 -58
  193. package/lib/dependencies/RequireEnsurePlugin.js +74 -74
  194. package/lib/dependencies/RequireHeaderDependency.js +26 -26
  195. package/lib/dependencies/RequireIncludeDependency.js +39 -39
  196. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +23 -23
  197. package/lib/dependencies/RequireIncludePlugin.js +61 -61
  198. package/lib/dependencies/RequireResolveContextDependency.js +23 -23
  199. package/lib/dependencies/RequireResolveDependencyParserPlugin.js +85 -85
  200. package/lib/dependencies/RequireResolveHeaderDependency.js +26 -26
  201. package/lib/dependencies/SingleEntryDependency.js +18 -18
  202. package/lib/dependencies/SystemPlugin.js +125 -125
  203. package/lib/dependencies/UnsupportedDependency.js +27 -27
  204. package/lib/dependencies/WebAssemblyImportDependency.js +48 -44
  205. package/lib/dependencies/WebpackMissingModule.js +20 -20
  206. package/lib/dependencies/getFunctionExpression.js +52 -52
  207. package/lib/formatLocation.js +61 -53
  208. package/lib/node/NodeChunkTemplatePlugin.js +31 -31
  209. package/lib/node/NodeEnvironmentPlugin.js +28 -28
  210. package/lib/node/NodeHotUpdateChunkTemplatePlugin.js +36 -36
  211. package/lib/node/NodeMainTemplate.runtime.js +27 -27
  212. package/lib/node/NodeMainTemplateAsync.runtime.js +44 -44
  213. package/lib/node/NodeMainTemplatePlugin.js +323 -320
  214. package/lib/node/NodeSourcePlugin.js +144 -140
  215. package/lib/node/NodeTargetPlugin.js +18 -18
  216. package/lib/node/NodeTemplatePlugin.js +31 -31
  217. package/lib/node/NodeWatchFileSystem.js +99 -82
  218. package/lib/node/ReadFileCompileWasmTemplatePlugin.js +52 -52
  219. package/lib/optimize/AggressiveMergingPlugin.js +87 -87
  220. package/lib/optimize/AggressiveSplittingPlugin.js +287 -281
  221. package/lib/optimize/ChunkModuleIdRangePlugin.js +68 -68
  222. package/lib/optimize/ConcatenatedModule.js +1420 -1413
  223. package/lib/optimize/EnsureChunkConditionsPlugin.js +70 -70
  224. package/lib/optimize/FlagIncludedChunksPlugin.js +99 -99
  225. package/lib/optimize/LimitChunkCountPlugin.js +66 -66
  226. package/lib/optimize/MergeDuplicateChunksPlugin.js +78 -75
  227. package/lib/optimize/MinChunkSizePlugin.js +77 -77
  228. package/lib/optimize/ModuleConcatenationPlugin.js +470 -457
  229. package/lib/optimize/OccurrenceOrderPlugin.js +133 -126
  230. package/lib/optimize/RemoveParentModulesPlugin.js +127 -117
  231. package/lib/optimize/RuntimeChunkPlugin.js +41 -41
  232. package/lib/optimize/SideEffectsFlagPlugin.js +158 -156
  233. package/lib/optimize/SplitChunksPlugin.js +709 -696
  234. package/lib/performance/AssetsOverSizeLimitWarning.js +30 -30
  235. package/lib/performance/EntrypointsOverSizeLimitWarning.js +31 -31
  236. package/lib/performance/NoAsyncChunksWarning.js +21 -21
  237. package/lib/performance/SizeLimitsPlugin.js +105 -105
  238. package/lib/util/Semaphore.js +41 -41
  239. package/lib/util/SortableSet.js +5 -2
  240. package/lib/util/StackedSetMap.js +12 -5
  241. package/lib/util/TrackingSet.js +35 -35
  242. package/lib/util/cachedMerge.js +35 -35
  243. package/lib/util/createHash.js +77 -77
  244. package/lib/util/identifier.js +76 -76
  245. package/lib/validateSchema.js +67 -67
  246. package/lib/wasm/UnsupportedWebAssemblyFeatureError.js +18 -18
  247. package/lib/wasm/WasmMainTemplatePlugin.js +310 -304
  248. package/lib/wasm/WebAssemblyGenerator.js +143 -19
  249. package/lib/wasm/WebAssemblyJavascriptGenerator.js +90 -107
  250. package/lib/wasm/WebAssemblyModulesPlugin.js +80 -80
  251. package/lib/wasm/WebAssemblyParser.js +28 -5
  252. package/lib/wasm/WebAssemblyUtils.js +48 -0
  253. package/lib/web/FetchCompileWasmTemplatePlugin.js +25 -25
  254. package/lib/web/JsonpChunkTemplatePlugin.js +47 -47
  255. package/lib/web/JsonpExportMainTemplatePlugin.js +47 -47
  256. package/lib/web/JsonpHotUpdateChunkTemplatePlugin.js +39 -39
  257. package/lib/web/JsonpMainTemplate.runtime.js +65 -64
  258. package/lib/web/JsonpMainTemplatePlugin.js +576 -574
  259. package/lib/web/JsonpTemplatePlugin.js +23 -23
  260. package/lib/webpack.js +183 -182
  261. package/lib/webpack.web.js +31 -31
  262. package/lib/webworker/WebWorkerChunkTemplatePlugin.js +35 -35
  263. package/lib/webworker/WebWorkerHotUpdateChunkTemplatePlugin.js +40 -40
  264. package/lib/webworker/WebWorkerMainTemplate.runtime.js +65 -64
  265. package/lib/webworker/WebWorkerMainTemplatePlugin.js +179 -179
  266. package/lib/webworker/WebWorkerTemplatePlugin.js +25 -25
  267. package/package.json +9 -8
  268. package/schemas/WebpackOptions.json +1988 -1988
  269. package/schemas/ajv.absolutePath.js +55 -55
  270. package/schemas/plugins/DllPlugin.json +32 -32
  271. package/schemas/plugins/DllReferencePlugin.json +99 -99
  272. package/schemas/plugins/HashedModuleIdsPlugin.json +24 -24
  273. package/schemas/plugins/LoaderOptionsPlugin.json +26 -26
  274. package/schemas/plugins/SourceMapDevToolPlugin.json +187 -187
  275. package/schemas/plugins/WatchIgnorePlugin.json +16 -16
  276. package/schemas/plugins/debug/ProfilingPlugin.json +12 -12
  277. package/schemas/plugins/optimize/AggressiveSplittingPlugin.json +22 -22
  278. package/schemas/plugins/optimize/LimitChunkCountPlugin.json +15 -15
  279. package/schemas/plugins/optimize/MinChunkSizePlugin.json +13 -13
@@ -1,574 +1,576 @@
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
- if (!mainTemplate.hooks.linkPreload) {
40
- mainTemplate.hooks.linkPreload = new SyncWaterfallHook([
41
- "source",
42
- "chunk",
43
- "hash"
44
- ]);
45
- }
46
- if (!mainTemplate.hooks.linkPrefetch) {
47
- mainTemplate.hooks.linkPrefetch = new SyncWaterfallHook([
48
- "source",
49
- "chunk",
50
- "hash"
51
- ]);
52
- }
53
-
54
- const getScriptSrcPath = (hash, chunk, chunkIdExpression) => {
55
- const chunkFilename = mainTemplate.outputOptions.chunkFilename;
56
- const chunkMaps = chunk.getChunkMaps();
57
- return mainTemplate.getAssetPath(JSON.stringify(chunkFilename), {
58
- hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
59
- hashWithLength: length =>
60
- `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
61
- chunk: {
62
- id: `" + ${chunkIdExpression} + "`,
63
- hash: `" + ${JSON.stringify(
64
- chunkMaps.hash
65
- )}[${chunkIdExpression}] + "`,
66
- hashWithLength(length) {
67
- const shortChunkHashMap = Object.create(null);
68
- for (const chunkId of Object.keys(chunkMaps.hash)) {
69
- if (typeof chunkMaps.hash[chunkId] === "string")
70
- shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(
71
- 0,
72
- length
73
- );
74
- }
75
- return `" + ${JSON.stringify(
76
- shortChunkHashMap
77
- )}[${chunkIdExpression}] + "`;
78
- },
79
- name: `" + (${JSON.stringify(
80
- chunkMaps.name
81
- )}[${chunkIdExpression}]||${chunkIdExpression}) + "`,
82
- contentHash: {
83
- javascript: `" + ${JSON.stringify(
84
- chunkMaps.contentHash.javascript
85
- )}[${chunkIdExpression}] + "`
86
- },
87
- contentHashWithLength: {
88
- javascript: length => {
89
- const shortContentHashMap = {};
90
- const contentHash = chunkMaps.contentHash.javascript;
91
- for (const chunkId of Object.keys(contentHash)) {
92
- if (typeof contentHash[chunkId] === "string") {
93
- shortContentHashMap[chunkId] = contentHash[chunkId].substr(
94
- 0,
95
- length
96
- );
97
- }
98
- }
99
- return `" + ${JSON.stringify(
100
- shortContentHashMap
101
- )}[${chunkIdExpression}] + "`;
102
- }
103
- }
104
- },
105
- contentHashType: "javascript"
106
- });
107
- };
108
- mainTemplate.hooks.localVars.tap(
109
- "JsonpMainTemplatePlugin",
110
- (source, chunk, hash) => {
111
- const extraCode = [];
112
- if (needChunkLoadingCode(chunk)) {
113
- extraCode.push(
114
- "",
115
- "// object to store loaded and loading chunks",
116
- "// undefined = chunk not loaded, null = chunk preloaded/prefetched",
117
- "// Promise = chunk loading, 0 = chunk loaded",
118
- "var installedChunks = {",
119
- Template.indent(
120
- chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
121
- ),
122
- "};",
123
- "",
124
- needEntryDeferringCode(chunk) ? "var deferredModules = [];" : ""
125
- );
126
- }
127
- if (needChunkOnDemandLoadingCode(chunk)) {
128
- extraCode.push(
129
- "",
130
- "// script path function",
131
- "function jsonpScriptSrc(chunkId) {",
132
- Template.indent([
133
- `return ${mainTemplate.requireFn}.p + ${getScriptSrcPath(
134
- hash,
135
- chunk,
136
- "chunkId"
137
- )}`
138
- ]),
139
- "}"
140
- );
141
- }
142
- if (extraCode.length === 0) return source;
143
- return Template.asString([source, ...extraCode]);
144
- }
145
- );
146
-
147
- mainTemplate.hooks.jsonpScript.tap(
148
- "JsonpMainTemplatePlugin",
149
- (_, chunk, hash) => {
150
- const crossOriginLoading =
151
- mainTemplate.outputOptions.crossOriginLoading;
152
- const chunkLoadTimeout = mainTemplate.outputOptions.chunkLoadTimeout;
153
- const jsonpScriptType = mainTemplate.outputOptions.jsonpScriptType;
154
-
155
- return Template.asString([
156
- "var script = document.createElement('script');",
157
- jsonpScriptType
158
- ? `script.type = ${JSON.stringify(jsonpScriptType)};`
159
- : "",
160
- "script.charset = 'utf-8';",
161
- `script.timeout = ${chunkLoadTimeout / 1000};`,
162
- crossOriginLoading
163
- ? `script.crossOrigin = ${JSON.stringify(crossOriginLoading)};`
164
- : "",
165
- `if (${mainTemplate.requireFn}.nc) {`,
166
- Template.indent(
167
- `script.setAttribute("nonce", ${mainTemplate.requireFn}.nc);`
168
- ),
169
- "}",
170
- "script.src = jsonpScriptSrc(chunkId);",
171
- "var timeout = setTimeout(function(){",
172
- Template.indent([
173
- "onScriptComplete({ type: 'timeout', target: script });"
174
- ]),
175
- `}, ${chunkLoadTimeout});`,
176
- "script.onerror = script.onload = onScriptComplete;",
177
- "function onScriptComplete(event) {",
178
- Template.indent([
179
- "// avoid mem leaks in IE.",
180
- "script.onerror = script.onload = null;",
181
- "clearTimeout(timeout);",
182
- "var chunk = installedChunks[chunkId];",
183
- "if(chunk !== 0) {",
184
- Template.indent([
185
- "if(chunk) {",
186
- Template.indent([
187
- "var errorType = event && (event.type === 'load' ? 'missing' : event.type);",
188
- "var realSrc = event && event.target && event.target.src;",
189
- "var error = new Error('Loading chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')');",
190
- "error.type = errorType;",
191
- "error.request = realSrc;",
192
- "chunk[1](error);"
193
- ]),
194
- "}",
195
- "installedChunks[chunkId] = undefined;"
196
- ]),
197
- "}"
198
- ]),
199
- "};"
200
- ]);
201
- }
202
- );
203
- mainTemplate.hooks.linkPreload.tap(
204
- "JsonpMainTemplatePlugin",
205
- (_, chunk, hash) => {
206
- const crossOriginLoading =
207
- mainTemplate.outputOptions.crossOriginLoading;
208
- const jsonpScriptType = mainTemplate.outputOptions.jsonpScriptType;
209
-
210
- return Template.asString([
211
- "var link = document.createElement('link');",
212
- jsonpScriptType
213
- ? `link.type = ${JSON.stringify(jsonpScriptType)};`
214
- : "",
215
- "link.charset = 'utf-8';",
216
- crossOriginLoading
217
- ? `link.crossOrigin = ${JSON.stringify(crossOriginLoading)};`
218
- : "",
219
- `if (${mainTemplate.requireFn}.nc) {`,
220
- Template.indent(
221
- `link.setAttribute("nonce", ${mainTemplate.requireFn}.nc);`
222
- ),
223
- "}",
224
- 'link.rel = "preload";',
225
- 'link.as = "script";',
226
- "link.href = jsonpScriptSrc(chunkId);"
227
- ]);
228
- }
229
- );
230
- mainTemplate.hooks.linkPrefetch.tap(
231
- "JsonpMainTemplatePlugin",
232
- (_, chunk, hash) => {
233
- return Template.asString([
234
- "var link = document.createElement('link');",
235
- 'link.rel = "prefetch";',
236
- "link.href = jsonpScriptSrc(chunkId);"
237
- ]);
238
- }
239
- );
240
- mainTemplate.hooks.requireEnsure.tap(
241
- "JsonpMainTemplatePlugin load",
242
- (source, chunk, hash) => {
243
- return Template.asString([
244
- source,
245
- "",
246
- "// JSONP chunk loading for javascript",
247
- "",
248
- "var installedChunkData = installedChunks[chunkId];",
249
- 'if(installedChunkData !== 0) { // 0 means "already installed".',
250
- Template.indent([
251
- "",
252
- '// a Promise means "currently loading".',
253
- "if(installedChunkData) {",
254
- Template.indent(["promises.push(installedChunkData[2]);"]),
255
- "} else {",
256
- Template.indent([
257
- "// setup Promise in chunk cache",
258
- "var promise = new Promise(function(resolve, reject) {",
259
- Template.indent([
260
- "installedChunkData = installedChunks[chunkId] = [resolve, reject];"
261
- ]),
262
- "});",
263
- "promises.push(installedChunkData[2] = promise);",
264
- "",
265
- "// start chunk loading",
266
- "var head = document.getElementsByTagName('head')[0];",
267
- mainTemplate.hooks.jsonpScript.call("", chunk, hash),
268
- "head.appendChild(script);"
269
- ]),
270
- "}"
271
- ]),
272
- "}"
273
- ]);
274
- }
275
- );
276
- mainTemplate.hooks.requireEnsure.tap(
277
- {
278
- name: "JsonpMainTemplatePlugin preload",
279
- stage: 10
280
- },
281
- (source, chunk, hash) => {
282
- const chunkMap = chunk.getChildIdsByOrdersMap().preload;
283
- if (!chunkMap || Object.keys(chunkMap).length === 0) return source;
284
- return Template.asString([
285
- source,
286
- "",
287
- "// chunk preloadng for javascript",
288
- "",
289
- `var chunkPreloadMap = ${JSON.stringify(chunkMap, null, "\t")}`,
290
- "",
291
- "var chunkPreloadData = chunkPreloadMap[chunkId];",
292
- "if(chunkPreloadData) {",
293
- Template.indent([
294
- "var head = document.getElementsByTagName('head')[0];",
295
- "chunkPreloadData.forEach(function(chunkId) {",
296
- Template.indent([
297
- "if(installedChunks[chunkId] === undefined) {",
298
- Template.indent([
299
- "installedChunks[chunkId] = null;",
300
- mainTemplate.hooks.linkPreload.call("", chunk, hash),
301
- "head.appendChild(link);"
302
- ]),
303
- "}"
304
- ]),
305
- "});"
306
- ]),
307
- "}"
308
- ]);
309
- }
310
- );
311
- mainTemplate.hooks.requireEnsure.tap(
312
- {
313
- name: "JsonpMainTemplatePlugin prefetch",
314
- stage: 20
315
- },
316
- (source, chunk, hash) => {
317
- const chunkMap = chunk.getChildIdsByOrdersMap().prefetch;
318
- if (!chunkMap || Object.keys(chunkMap).length === 0) return source;
319
- return Template.asString([
320
- source,
321
- "",
322
- "// chunk prefetching for javascript",
323
- "",
324
- `var chunkPrefetchMap = ${JSON.stringify(chunkMap, null, "\t")}`,
325
- "",
326
- "var chunkPrefetchData = chunkPrefetchMap[chunkId];",
327
- "if(chunkPrefetchData) {",
328
- Template.indent([
329
- "Promise.all(promises).then(function() {",
330
- Template.indent([
331
- "var head = document.getElementsByTagName('head')[0];",
332
- "chunkPrefetchData.forEach(function(chunkId) {",
333
- Template.indent([
334
- "if(installedChunks[chunkId] === undefined) {",
335
- Template.indent([
336
- "installedChunks[chunkId] = null;",
337
- mainTemplate.hooks.linkPrefetch.call("", chunk, hash),
338
- "head.appendChild(link);"
339
- ]),
340
- "}"
341
- ]),
342
- "});"
343
- ]),
344
- "})"
345
- ]),
346
- "}"
347
- ]);
348
- }
349
- );
350
- mainTemplate.hooks.requireExtensions.tap(
351
- "JsonpMainTemplatePlugin",
352
- (source, chunk) => {
353
- if (!needChunkOnDemandLoadingCode(chunk)) return source;
354
-
355
- return Template.asString([
356
- source,
357
- "",
358
- "// on error function for async loading",
359
- `${
360
- mainTemplate.requireFn
361
- }.oe = function(err) { console.error(err); throw err; };`
362
- ]);
363
- }
364
- );
365
- mainTemplate.hooks.bootstrap.tap(
366
- "JsonpMainTemplatePlugin",
367
- (source, chunk, hash) => {
368
- if (needChunkLoadingCode(chunk)) {
369
- const withDefer = needEntryDeferringCode(chunk);
370
- return Template.asString([
371
- source,
372
- "",
373
- "// install a JSONP callback for chunk loading",
374
- "function webpackJsonpCallback(data) {",
375
- Template.indent([
376
- "var chunkIds = data[0];",
377
- "var moreModules = data[1];",
378
- withDefer ? "var executeModules = data[2];" : "",
379
- '// add "moreModules" to the modules object,',
380
- '// then flag all "chunkIds" as loaded and fire callback',
381
- "var moduleId, chunkId, i = 0, resolves = [];",
382
- "for(;i < chunkIds.length; i++) {",
383
- Template.indent([
384
- "chunkId = chunkIds[i];",
385
- "if(installedChunks[chunkId]) {",
386
- Template.indent("resolves.push(installedChunks[chunkId][0]);"),
387
- "}",
388
- "installedChunks[chunkId] = 0;"
389
- ]),
390
- "}",
391
- "for(moduleId in moreModules) {",
392
- Template.indent([
393
- "if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {",
394
- Template.indent(
395
- mainTemplate.renderAddModule(
396
- hash,
397
- chunk,
398
- "moduleId",
399
- "moreModules[moduleId]"
400
- )
401
- ),
402
- "}"
403
- ]),
404
- "}",
405
- "if(parentJsonpFunction) parentJsonpFunction(data);",
406
- "while(resolves.length) {",
407
- Template.indent("resolves.shift()();"),
408
- "}",
409
- withDefer
410
- ? Template.asString([
411
- "",
412
- "// add entry modules from loaded chunk to deferred list",
413
- "deferredModules.push.apply(deferredModules, executeModules || []);",
414
- "",
415
- "// run deferred modules when all chunks ready",
416
- "return checkDeferredModules();"
417
- ])
418
- : ""
419
- ]),
420
- "};",
421
- withDefer
422
- ? Template.asString([
423
- "function checkDeferredModules() {",
424
- Template.indent([
425
- "var result;",
426
- "for(var i = 0; i < deferredModules.length; i++) {",
427
- Template.indent([
428
- "var deferredModule = deferredModules[i];",
429
- "var fulfilled = true;",
430
- "for(var j = 1; j < deferredModule.length; j++) {",
431
- Template.indent([
432
- "var depId = deferredModule[j];",
433
- "if(installedChunks[depId] !== 0) fulfilled = false;"
434
- ]),
435
- "}",
436
- "if(fulfilled) {",
437
- Template.indent([
438
- "deferredModules.splice(i--, 1);",
439
- "result = " +
440
- mainTemplate.requireFn +
441
- "(" +
442
- mainTemplate.requireFn +
443
- ".s = deferredModule[0]);"
444
- ]),
445
- "}"
446
- ]),
447
- "}",
448
- "return result;"
449
- ]),
450
- "}"
451
- ])
452
- : ""
453
- ]);
454
- }
455
- return source;
456
- }
457
- );
458
- mainTemplate.hooks.beforeStartup.tap(
459
- "JsonpMainTemplatePlugin",
460
- (source, chunk, hash) => {
461
- if (needChunkLoadingCode(chunk)) {
462
- var jsonpFunction = mainTemplate.outputOptions.jsonpFunction;
463
- var globalObject = mainTemplate.outputOptions.globalObject;
464
- return Template.asString([
465
- `var jsonpArray = ${globalObject}[${JSON.stringify(
466
- jsonpFunction
467
- )}] = ${globalObject}[${JSON.stringify(jsonpFunction)}] || [];`,
468
- "var oldJsonpFunction = jsonpArray.push.bind(jsonpArray);",
469
- "jsonpArray.push = webpackJsonpCallback;",
470
- "jsonpArray = jsonpArray.slice();",
471
- "for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);",
472
- "var parentJsonpFunction = oldJsonpFunction;",
473
- "",
474
- source
475
- ]);
476
- }
477
- return source;
478
- }
479
- );
480
- mainTemplate.hooks.startup.tap(
481
- "JsonpMainTemplatePlugin",
482
- (source, chunk, hash) => {
483
- if (needEntryDeferringCode(chunk)) {
484
- if (chunk.hasEntryModule()) {
485
- const entries = [chunk.entryModule].filter(Boolean).map(m =>
486
- [m.id].concat(
487
- Array.from(chunk.groupsIterable)[0]
488
- .chunks.filter(c => c !== chunk)
489
- .map(c => c.id)
490
- )
491
- );
492
- return Template.asString([
493
- "// add entry module to deferred list",
494
- `deferredModules.push(${entries
495
- .map(e => JSON.stringify(e))
496
- .join(", ")});`,
497
- "// run deferred modules when ready",
498
- "return checkDeferredModules();"
499
- ]);
500
- } else {
501
- return Template.asString([
502
- "// run deferred modules from other chunks",
503
- "checkDeferredModules();"
504
- ]);
505
- }
506
- }
507
- return source;
508
- }
509
- );
510
- mainTemplate.hooks.hotBootstrap.tap(
511
- "JsonpMainTemplatePlugin",
512
- (source, chunk, hash) => {
513
- const globalObject = mainTemplate.outputOptions.globalObject;
514
- const hotUpdateChunkFilename =
515
- mainTemplate.outputOptions.hotUpdateChunkFilename;
516
- const hotUpdateMainFilename =
517
- mainTemplate.outputOptions.hotUpdateMainFilename;
518
- const crossOriginLoading =
519
- mainTemplate.outputOptions.crossOriginLoading;
520
- const hotUpdateFunction = mainTemplate.outputOptions.hotUpdateFunction;
521
- const currentHotUpdateChunkFilename = mainTemplate.getAssetPath(
522
- JSON.stringify(hotUpdateChunkFilename),
523
- {
524
- hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
525
- hashWithLength: length =>
526
- `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
527
- chunk: {
528
- id: '" + chunkId + "'
529
- }
530
- }
531
- );
532
- const currentHotUpdateMainFilename = mainTemplate.getAssetPath(
533
- JSON.stringify(hotUpdateMainFilename),
534
- {
535
- hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
536
- hashWithLength: length =>
537
- `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`
538
- }
539
- );
540
- const runtimeSource = Template.getFunctionContent(
541
- require("./JsonpMainTemplate.runtime.js")
542
- )
543
- .replace(/\/\/\$semicolon/g, ";")
544
- .replace(/\$require\$/g, mainTemplate.requireFn)
545
- .replace(
546
- /\$crossOriginLoading\$/g,
547
- crossOriginLoading
548
- ? `script.crossOrigin = ${JSON.stringify(crossOriginLoading)}`
549
- : ""
550
- )
551
- .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
552
- .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
553
- .replace(/\$hash\$/g, JSON.stringify(hash));
554
- return `${source}
555
- function hotDisposeChunk(chunkId) {
556
- delete installedChunks[chunkId];
557
- }
558
- var parentHotUpdateCallback = ${globalObject}[${JSON.stringify(
559
- hotUpdateFunction
560
- )}];
561
- ${globalObject}[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
562
- }
563
- );
564
- mainTemplate.hooks.hash.tap("JsonpMainTemplatePlugin", hash => {
565
- hash.update("jsonp");
566
- hash.update("5");
567
- hash.update(`${mainTemplate.outputOptions.globalObject}`);
568
- hash.update(`${mainTemplate.outputOptions.chunkFilename}`);
569
- hash.update(`${mainTemplate.outputOptions.jsonpFunction}`);
570
- hash.update(`${mainTemplate.outputOptions.hotUpdateFunction}`);
571
- });
572
- }
573
- }
574
- 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
+ if (!mainTemplate.hooks.linkPreload) {
40
+ mainTemplate.hooks.linkPreload = new SyncWaterfallHook([
41
+ "source",
42
+ "chunk",
43
+ "hash"
44
+ ]);
45
+ }
46
+ if (!mainTemplate.hooks.linkPrefetch) {
47
+ mainTemplate.hooks.linkPrefetch = new SyncWaterfallHook([
48
+ "source",
49
+ "chunk",
50
+ "hash"
51
+ ]);
52
+ }
53
+
54
+ const getScriptSrcPath = (hash, chunk, chunkIdExpression) => {
55
+ const chunkFilename = mainTemplate.outputOptions.chunkFilename;
56
+ const chunkMaps = chunk.getChunkMaps();
57
+ return mainTemplate.getAssetPath(JSON.stringify(chunkFilename), {
58
+ hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
59
+ hashWithLength: length =>
60
+ `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
61
+ chunk: {
62
+ id: `" + ${chunkIdExpression} + "`,
63
+ hash: `" + ${JSON.stringify(
64
+ chunkMaps.hash
65
+ )}[${chunkIdExpression}] + "`,
66
+ hashWithLength(length) {
67
+ const shortChunkHashMap = Object.create(null);
68
+ for (const chunkId of Object.keys(chunkMaps.hash)) {
69
+ if (typeof chunkMaps.hash[chunkId] === "string") {
70
+ shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(
71
+ 0,
72
+ length
73
+ );
74
+ }
75
+ }
76
+ return `" + ${JSON.stringify(
77
+ shortChunkHashMap
78
+ )}[${chunkIdExpression}] + "`;
79
+ },
80
+ name: `" + (${JSON.stringify(
81
+ chunkMaps.name
82
+ )}[${chunkIdExpression}]||${chunkIdExpression}) + "`,
83
+ contentHash: {
84
+ javascript: `" + ${JSON.stringify(
85
+ chunkMaps.contentHash.javascript
86
+ )}[${chunkIdExpression}] + "`
87
+ },
88
+ contentHashWithLength: {
89
+ javascript: length => {
90
+ const shortContentHashMap = {};
91
+ const contentHash = chunkMaps.contentHash.javascript;
92
+ for (const chunkId of Object.keys(contentHash)) {
93
+ if (typeof contentHash[chunkId] === "string") {
94
+ shortContentHashMap[chunkId] = contentHash[chunkId].substr(
95
+ 0,
96
+ length
97
+ );
98
+ }
99
+ }
100
+ return `" + ${JSON.stringify(
101
+ shortContentHashMap
102
+ )}[${chunkIdExpression}] + "`;
103
+ }
104
+ }
105
+ },
106
+ contentHashType: "javascript"
107
+ });
108
+ };
109
+ mainTemplate.hooks.localVars.tap(
110
+ "JsonpMainTemplatePlugin",
111
+ (source, chunk, hash) => {
112
+ const extraCode = [];
113
+ if (needChunkLoadingCode(chunk)) {
114
+ extraCode.push(
115
+ "",
116
+ "// object to store loaded and loading chunks",
117
+ "// undefined = chunk not loaded, null = chunk preloaded/prefetched",
118
+ "// Promise = chunk loading, 0 = chunk loaded",
119
+ "var installedChunks = {",
120
+ Template.indent(
121
+ chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
122
+ ),
123
+ "};",
124
+ "",
125
+ needEntryDeferringCode(chunk) ? "var deferredModules = [];" : ""
126
+ );
127
+ }
128
+ if (needChunkOnDemandLoadingCode(chunk)) {
129
+ extraCode.push(
130
+ "",
131
+ "// script path function",
132
+ "function jsonpScriptSrc(chunkId) {",
133
+ Template.indent([
134
+ `return ${mainTemplate.requireFn}.p + ${getScriptSrcPath(
135
+ hash,
136
+ chunk,
137
+ "chunkId"
138
+ )}`
139
+ ]),
140
+ "}"
141
+ );
142
+ }
143
+ if (extraCode.length === 0) return source;
144
+ return Template.asString([source, ...extraCode]);
145
+ }
146
+ );
147
+
148
+ mainTemplate.hooks.jsonpScript.tap(
149
+ "JsonpMainTemplatePlugin",
150
+ (_, chunk, hash) => {
151
+ const crossOriginLoading =
152
+ mainTemplate.outputOptions.crossOriginLoading;
153
+ const chunkLoadTimeout = mainTemplate.outputOptions.chunkLoadTimeout;
154
+ const jsonpScriptType = mainTemplate.outputOptions.jsonpScriptType;
155
+
156
+ return Template.asString([
157
+ "var script = document.createElement('script');",
158
+ "var onScriptComplete;",
159
+ jsonpScriptType
160
+ ? `script.type = ${JSON.stringify(jsonpScriptType)};`
161
+ : "",
162
+ "script.charset = 'utf-8';",
163
+ `script.timeout = ${chunkLoadTimeout / 1000};`,
164
+ crossOriginLoading
165
+ ? `script.crossOrigin = ${JSON.stringify(crossOriginLoading)};`
166
+ : "",
167
+ `if (${mainTemplate.requireFn}.nc) {`,
168
+ Template.indent(
169
+ `script.setAttribute("nonce", ${mainTemplate.requireFn}.nc);`
170
+ ),
171
+ "}",
172
+ "script.src = jsonpScriptSrc(chunkId);",
173
+ "onScriptComplete = function (event) {",
174
+ Template.indent([
175
+ "// avoid mem leaks in IE.",
176
+ "script.onerror = script.onload = null;",
177
+ "clearTimeout(timeout);",
178
+ "var chunk = installedChunks[chunkId];",
179
+ "if(chunk !== 0) {",
180
+ Template.indent([
181
+ "if(chunk) {",
182
+ Template.indent([
183
+ "var errorType = event && (event.type === 'load' ? 'missing' : event.type);",
184
+ "var realSrc = event && event.target && event.target.src;",
185
+ "var error = new Error('Loading chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')');",
186
+ "error.type = errorType;",
187
+ "error.request = realSrc;",
188
+ "chunk[1](error);"
189
+ ]),
190
+ "}",
191
+ "installedChunks[chunkId] = undefined;"
192
+ ]),
193
+ "}"
194
+ ]),
195
+ "};",
196
+ "var timeout = setTimeout(function(){",
197
+ Template.indent([
198
+ "onScriptComplete({ type: 'timeout', target: script });"
199
+ ]),
200
+ `}, ${chunkLoadTimeout});`,
201
+ "script.onerror = script.onload = onScriptComplete;"
202
+ ]);
203
+ }
204
+ );
205
+ mainTemplate.hooks.linkPreload.tap(
206
+ "JsonpMainTemplatePlugin",
207
+ (_, chunk, hash) => {
208
+ const crossOriginLoading =
209
+ mainTemplate.outputOptions.crossOriginLoading;
210
+ const jsonpScriptType = mainTemplate.outputOptions.jsonpScriptType;
211
+
212
+ return Template.asString([
213
+ "var link = document.createElement('link');",
214
+ jsonpScriptType
215
+ ? `link.type = ${JSON.stringify(jsonpScriptType)};`
216
+ : "",
217
+ "link.charset = 'utf-8';",
218
+ crossOriginLoading
219
+ ? `link.crossOrigin = ${JSON.stringify(crossOriginLoading)};`
220
+ : "",
221
+ `if (${mainTemplate.requireFn}.nc) {`,
222
+ Template.indent(
223
+ `link.setAttribute("nonce", ${mainTemplate.requireFn}.nc);`
224
+ ),
225
+ "}",
226
+ 'link.rel = "preload";',
227
+ 'link.as = "script";',
228
+ "link.href = jsonpScriptSrc(chunkId);"
229
+ ]);
230
+ }
231
+ );
232
+ mainTemplate.hooks.linkPrefetch.tap(
233
+ "JsonpMainTemplatePlugin",
234
+ (_, chunk, hash) => {
235
+ return Template.asString([
236
+ "var link = document.createElement('link');",
237
+ 'link.rel = "prefetch";',
238
+ "link.href = jsonpScriptSrc(chunkId);"
239
+ ]);
240
+ }
241
+ );
242
+ mainTemplate.hooks.requireEnsure.tap(
243
+ "JsonpMainTemplatePlugin load",
244
+ (source, chunk, hash) => {
245
+ return Template.asString([
246
+ source,
247
+ "",
248
+ "// JSONP chunk loading for javascript",
249
+ "",
250
+ "var installedChunkData = installedChunks[chunkId];",
251
+ 'if(installedChunkData !== 0) { // 0 means "already installed".',
252
+ Template.indent([
253
+ "",
254
+ '// a Promise means "currently loading".',
255
+ "if(installedChunkData) {",
256
+ Template.indent(["promises.push(installedChunkData[2]);"]),
257
+ "} else {",
258
+ Template.indent([
259
+ "// setup Promise in chunk cache",
260
+ "var promise = new Promise(function(resolve, reject) {",
261
+ Template.indent([
262
+ "installedChunkData = installedChunks[chunkId] = [resolve, reject];"
263
+ ]),
264
+ "});",
265
+ "promises.push(installedChunkData[2] = promise);",
266
+ "",
267
+ "// start chunk loading",
268
+ "var head = document.getElementsByTagName('head')[0];",
269
+ mainTemplate.hooks.jsonpScript.call("", chunk, hash),
270
+ "head.appendChild(script);"
271
+ ]),
272
+ "}"
273
+ ]),
274
+ "}"
275
+ ]);
276
+ }
277
+ );
278
+ mainTemplate.hooks.requireEnsure.tap(
279
+ {
280
+ name: "JsonpMainTemplatePlugin preload",
281
+ stage: 10
282
+ },
283
+ (source, chunk, hash) => {
284
+ const chunkMap = chunk.getChildIdsByOrdersMap().preload;
285
+ if (!chunkMap || Object.keys(chunkMap).length === 0) return source;
286
+ return Template.asString([
287
+ source,
288
+ "",
289
+ "// chunk preloadng for javascript",
290
+ "",
291
+ `var chunkPreloadMap = ${JSON.stringify(chunkMap, null, "\t")}`,
292
+ "",
293
+ "var chunkPreloadData = chunkPreloadMap[chunkId];",
294
+ "if(chunkPreloadData) {",
295
+ Template.indent([
296
+ "var head = document.getElementsByTagName('head')[0];",
297
+ "chunkPreloadData.forEach(function(chunkId) {",
298
+ Template.indent([
299
+ "if(installedChunks[chunkId] === undefined) {",
300
+ Template.indent([
301
+ "installedChunks[chunkId] = null;",
302
+ mainTemplate.hooks.linkPreload.call("", chunk, hash),
303
+ "head.appendChild(link);"
304
+ ]),
305
+ "}"
306
+ ]),
307
+ "});"
308
+ ]),
309
+ "}"
310
+ ]);
311
+ }
312
+ );
313
+ mainTemplate.hooks.requireEnsure.tap(
314
+ {
315
+ name: "JsonpMainTemplatePlugin prefetch",
316
+ stage: 20
317
+ },
318
+ (source, chunk, hash) => {
319
+ const chunkMap = chunk.getChildIdsByOrdersMap().prefetch;
320
+ if (!chunkMap || Object.keys(chunkMap).length === 0) return source;
321
+ return Template.asString([
322
+ source,
323
+ "",
324
+ "// chunk prefetching for javascript",
325
+ "",
326
+ `var chunkPrefetchMap = ${JSON.stringify(chunkMap, null, "\t")}`,
327
+ "",
328
+ "var chunkPrefetchData = chunkPrefetchMap[chunkId];",
329
+ "if(chunkPrefetchData) {",
330
+ Template.indent([
331
+ "Promise.all(promises).then(function() {",
332
+ Template.indent([
333
+ "var head = document.getElementsByTagName('head')[0];",
334
+ "chunkPrefetchData.forEach(function(chunkId) {",
335
+ Template.indent([
336
+ "if(installedChunks[chunkId] === undefined) {",
337
+ Template.indent([
338
+ "installedChunks[chunkId] = null;",
339
+ mainTemplate.hooks.linkPrefetch.call("", chunk, hash),
340
+ "head.appendChild(link);"
341
+ ]),
342
+ "}"
343
+ ]),
344
+ "});"
345
+ ]),
346
+ "})"
347
+ ]),
348
+ "}"
349
+ ]);
350
+ }
351
+ );
352
+ mainTemplate.hooks.requireExtensions.tap(
353
+ "JsonpMainTemplatePlugin",
354
+ (source, chunk) => {
355
+ if (!needChunkOnDemandLoadingCode(chunk)) return source;
356
+
357
+ return Template.asString([
358
+ source,
359
+ "",
360
+ "// on error function for async loading",
361
+ `${
362
+ mainTemplate.requireFn
363
+ }.oe = function(err) { console.error(err); throw err; };`
364
+ ]);
365
+ }
366
+ );
367
+ mainTemplate.hooks.bootstrap.tap(
368
+ "JsonpMainTemplatePlugin",
369
+ (source, chunk, hash) => {
370
+ if (needChunkLoadingCode(chunk)) {
371
+ const withDefer = needEntryDeferringCode(chunk);
372
+ return Template.asString([
373
+ source,
374
+ "",
375
+ "// install a JSONP callback for chunk loading",
376
+ "function webpackJsonpCallback(data) {",
377
+ Template.indent([
378
+ "var chunkIds = data[0];",
379
+ "var moreModules = data[1];",
380
+ withDefer ? "var executeModules = data[2];" : "",
381
+ '// add "moreModules" to the modules object,',
382
+ '// then flag all "chunkIds" as loaded and fire callback',
383
+ "var moduleId, chunkId, i = 0, resolves = [];",
384
+ "for(;i < chunkIds.length; i++) {",
385
+ Template.indent([
386
+ "chunkId = chunkIds[i];",
387
+ "if(installedChunks[chunkId]) {",
388
+ Template.indent("resolves.push(installedChunks[chunkId][0]);"),
389
+ "}",
390
+ "installedChunks[chunkId] = 0;"
391
+ ]),
392
+ "}",
393
+ "for(moduleId in moreModules) {",
394
+ Template.indent([
395
+ "if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {",
396
+ Template.indent(
397
+ mainTemplate.renderAddModule(
398
+ hash,
399
+ chunk,
400
+ "moduleId",
401
+ "moreModules[moduleId]"
402
+ )
403
+ ),
404
+ "}"
405
+ ]),
406
+ "}",
407
+ "if(parentJsonpFunction) parentJsonpFunction(data);",
408
+ "while(resolves.length) {",
409
+ Template.indent("resolves.shift()();"),
410
+ "}",
411
+ withDefer
412
+ ? Template.asString([
413
+ "",
414
+ "// add entry modules from loaded chunk to deferred list",
415
+ "deferredModules.push.apply(deferredModules, executeModules || []);",
416
+ "",
417
+ "// run deferred modules when all chunks ready",
418
+ "return checkDeferredModules();"
419
+ ])
420
+ : ""
421
+ ]),
422
+ "};",
423
+ withDefer
424
+ ? Template.asString([
425
+ "function checkDeferredModules() {",
426
+ Template.indent([
427
+ "var result;",
428
+ "for(var i = 0; i < deferredModules.length; i++) {",
429
+ Template.indent([
430
+ "var deferredModule = deferredModules[i];",
431
+ "var fulfilled = true;",
432
+ "for(var j = 1; j < deferredModule.length; j++) {",
433
+ Template.indent([
434
+ "var depId = deferredModule[j];",
435
+ "if(installedChunks[depId] !== 0) fulfilled = false;"
436
+ ]),
437
+ "}",
438
+ "if(fulfilled) {",
439
+ Template.indent([
440
+ "deferredModules.splice(i--, 1);",
441
+ "result = " +
442
+ mainTemplate.requireFn +
443
+ "(" +
444
+ mainTemplate.requireFn +
445
+ ".s = deferredModule[0]);"
446
+ ]),
447
+ "}"
448
+ ]),
449
+ "}",
450
+ "return result;"
451
+ ]),
452
+ "}"
453
+ ])
454
+ : ""
455
+ ]);
456
+ }
457
+ return source;
458
+ }
459
+ );
460
+ mainTemplate.hooks.beforeStartup.tap(
461
+ "JsonpMainTemplatePlugin",
462
+ (source, chunk, hash) => {
463
+ if (needChunkLoadingCode(chunk)) {
464
+ var jsonpFunction = mainTemplate.outputOptions.jsonpFunction;
465
+ var globalObject = mainTemplate.outputOptions.globalObject;
466
+ return Template.asString([
467
+ `var jsonpArray = ${globalObject}[${JSON.stringify(
468
+ jsonpFunction
469
+ )}] = ${globalObject}[${JSON.stringify(jsonpFunction)}] || [];`,
470
+ "var oldJsonpFunction = jsonpArray.push.bind(jsonpArray);",
471
+ "jsonpArray.push = webpackJsonpCallback;",
472
+ "jsonpArray = jsonpArray.slice();",
473
+ "for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);",
474
+ "var parentJsonpFunction = oldJsonpFunction;",
475
+ "",
476
+ source
477
+ ]);
478
+ }
479
+ return source;
480
+ }
481
+ );
482
+ mainTemplate.hooks.startup.tap(
483
+ "JsonpMainTemplatePlugin",
484
+ (source, chunk, hash) => {
485
+ if (needEntryDeferringCode(chunk)) {
486
+ if (chunk.hasEntryModule()) {
487
+ const entries = [chunk.entryModule].filter(Boolean).map(m =>
488
+ [m.id].concat(
489
+ Array.from(chunk.groupsIterable)[0]
490
+ .chunks.filter(c => c !== chunk)
491
+ .map(c => c.id)
492
+ )
493
+ );
494
+ return Template.asString([
495
+ "// add entry module to deferred list",
496
+ `deferredModules.push(${entries
497
+ .map(e => JSON.stringify(e))
498
+ .join(", ")});`,
499
+ "// run deferred modules when ready",
500
+ "return checkDeferredModules();"
501
+ ]);
502
+ } else {
503
+ return Template.asString([
504
+ "// run deferred modules from other chunks",
505
+ "checkDeferredModules();"
506
+ ]);
507
+ }
508
+ }
509
+ return source;
510
+ }
511
+ );
512
+ mainTemplate.hooks.hotBootstrap.tap(
513
+ "JsonpMainTemplatePlugin",
514
+ (source, chunk, hash) => {
515
+ const globalObject = mainTemplate.outputOptions.globalObject;
516
+ const hotUpdateChunkFilename =
517
+ mainTemplate.outputOptions.hotUpdateChunkFilename;
518
+ const hotUpdateMainFilename =
519
+ mainTemplate.outputOptions.hotUpdateMainFilename;
520
+ const crossOriginLoading =
521
+ mainTemplate.outputOptions.crossOriginLoading;
522
+ const hotUpdateFunction = mainTemplate.outputOptions.hotUpdateFunction;
523
+ const currentHotUpdateChunkFilename = mainTemplate.getAssetPath(
524
+ JSON.stringify(hotUpdateChunkFilename),
525
+ {
526
+ hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
527
+ hashWithLength: length =>
528
+ `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
529
+ chunk: {
530
+ id: '" + chunkId + "'
531
+ }
532
+ }
533
+ );
534
+ const currentHotUpdateMainFilename = mainTemplate.getAssetPath(
535
+ JSON.stringify(hotUpdateMainFilename),
536
+ {
537
+ hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
538
+ hashWithLength: length =>
539
+ `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`
540
+ }
541
+ );
542
+ const runtimeSource = Template.getFunctionContent(
543
+ require("./JsonpMainTemplate.runtime.js")
544
+ )
545
+ .replace(/\/\/\$semicolon/g, ";")
546
+ .replace(/\$require\$/g, mainTemplate.requireFn)
547
+ .replace(
548
+ /\$crossOriginLoading\$/g,
549
+ crossOriginLoading
550
+ ? `script.crossOrigin = ${JSON.stringify(crossOriginLoading)}`
551
+ : ""
552
+ )
553
+ .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
554
+ .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
555
+ .replace(/\$hash\$/g, JSON.stringify(hash));
556
+ return `${source}
557
+ function hotDisposeChunk(chunkId) {
558
+ delete installedChunks[chunkId];
559
+ }
560
+ var parentHotUpdateCallback = ${globalObject}[${JSON.stringify(
561
+ hotUpdateFunction
562
+ )}];
563
+ ${globalObject}[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
564
+ }
565
+ );
566
+ mainTemplate.hooks.hash.tap("JsonpMainTemplatePlugin", hash => {
567
+ hash.update("jsonp");
568
+ hash.update("5");
569
+ hash.update(`${mainTemplate.outputOptions.globalObject}`);
570
+ hash.update(`${mainTemplate.outputOptions.chunkFilename}`);
571
+ hash.update(`${mainTemplate.outputOptions.jsonpFunction}`);
572
+ hash.update(`${mainTemplate.outputOptions.hotUpdateFunction}`);
573
+ });
574
+ }
575
+ }
576
+ module.exports = JsonpMainTemplatePlugin;