webpack 2.3.1 → 2.4.1

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 (77) hide show
  1. package/README.md +139 -132
  2. package/lib/APIPlugin.js +0 -6
  3. package/lib/AsyncDependenciesBlock.js +1 -1
  4. package/lib/AutomaticPrefetchPlugin.js +2 -2
  5. package/lib/CachePlugin.js +2 -2
  6. package/lib/CaseSensitiveModulesWarning.js +6 -3
  7. package/lib/ChunkRenderError.js +3 -1
  8. package/lib/ChunkTemplate.js +2 -2
  9. package/lib/Compilation.js +3 -3
  10. package/lib/Compiler.js +1 -1
  11. package/lib/ContextModule.js +3 -2
  12. package/lib/ContextModuleFactory.js +5 -5
  13. package/lib/DelegatedModule.js +67 -63
  14. package/lib/DependenciesBlock.js +65 -59
  15. package/lib/Dependency.js +1 -0
  16. package/lib/EntryModuleNotFoundError.js +16 -10
  17. package/lib/ExtendedAPIPlugin.js +7 -2
  18. package/lib/ExternalModuleFactoryPlugin.js +26 -23
  19. package/lib/FlagDependencyUsagePlugin.js +63 -75
  20. package/lib/HotModuleReplacement.runtime.js +25 -27
  21. package/lib/HotModuleReplacementPlugin.js +3 -5
  22. package/lib/IgnorePlugin.js +48 -17
  23. package/lib/JsonpChunkTemplatePlugin.js +24 -24
  24. package/lib/JsonpMainTemplatePlugin.js +182 -182
  25. package/lib/LibManifestPlugin.js +51 -46
  26. package/lib/MainTemplate.js +17 -18
  27. package/lib/Module.js +158 -160
  28. package/lib/ModuleBuildError.js +4 -2
  29. package/lib/ModuleDependencyError.js +2 -1
  30. package/lib/ModuleDependencyWarning.js +2 -1
  31. package/lib/ModuleError.js +2 -2
  32. package/lib/ModuleFilenameHelpers.js +27 -27
  33. package/lib/ModuleNotFoundError.js +3 -1
  34. package/lib/ModuleParseError.js +6 -4
  35. package/lib/ModuleWarning.js +2 -2
  36. package/lib/MultiCompiler.js +3 -4
  37. package/lib/MultiStats.js +3 -3
  38. package/lib/MultiWatching.js +2 -2
  39. package/lib/NamedChunksPlugin.js +30 -0
  40. package/lib/NodeStuffPlugin.js +80 -79
  41. package/lib/NormalModule.js +6 -2
  42. package/lib/NormalModuleFactory.js +244 -240
  43. package/lib/Parser.js +1256 -1079
  44. package/lib/ProgressPlugin.js +1 -1
  45. package/lib/RecordIdsPlugin.js +5 -9
  46. package/lib/SetVarMainTemplatePlugin.js +1 -1
  47. package/lib/SourceMapDevToolPlugin.js +153 -157
  48. package/lib/Stats.js +32 -5
  49. package/lib/TemplatedPathPlugin.js +1 -0
  50. package/lib/UnsupportedFeatureWarning.js +2 -1
  51. package/lib/WebpackError.js +11 -0
  52. package/lib/WebpackOptionsApply.js +1 -1
  53. package/lib/WebpackOptionsValidationError.js +8 -4
  54. package/lib/dependencies/AMDDefineDependency.js +10 -6
  55. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +8 -1
  56. package/lib/dependencies/AMDPlugin.js +3 -3
  57. package/lib/dependencies/ContextDependencyHelpers.js +19 -16
  58. package/lib/dependencies/CriticalDependencyWarning.js +4 -1
  59. package/lib/dependencies/DepBlockHelpers.js +3 -3
  60. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +2 -2
  61. package/lib/dependencies/ImportContextDependency.js +2 -1
  62. package/lib/dependencies/ImportDependenciesBlock.js +2 -2
  63. package/lib/dependencies/ImportParserPlugin.js +16 -2
  64. package/lib/dependencies/RequireEnsureDependenciesBlock.js +11 -3
  65. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +42 -13
  66. package/lib/dependencies/RequireEnsureDependency.js +9 -2
  67. package/lib/optimize/AggressiveSplittingPlugin.js +10 -17
  68. package/lib/optimize/CommonsChunkPlugin.js +1 -1
  69. package/lib/performance/AssetsOverSizeLimitWarning.js +4 -1
  70. package/lib/performance/EntrypointsOverSizeLimitWarning.js +5 -1
  71. package/lib/performance/NoAsyncChunksWarning.js +5 -1
  72. package/lib/removeAndDo.js +6 -4
  73. package/lib/util/identifier.js +16 -0
  74. package/lib/webpack.js +2 -1
  75. package/package.json +4 -3
  76. package/schemas/ajv.absolutePath.js +1 -1
  77. package/schemas/webpackOptionsSchema.json +27 -0
@@ -8,7 +8,7 @@ module.exports = function() {
8
8
  var hotApplyOnUpdate = true;
9
9
  var hotCurrentHash = $hash$; // eslint-disable-line no-unused-vars
10
10
  var hotCurrentModuleData = {};
11
- var hotMainModule = true; // eslint-disable-line no-unused-vars
11
+ var hotCurrentChildModule; // eslint-disable-line no-unused-vars
12
12
  var hotCurrentParents = []; // eslint-disable-line no-unused-vars
13
13
  var hotCurrentParentsTemp = []; // eslint-disable-line no-unused-vars
14
14
 
@@ -20,14 +20,16 @@ module.exports = function() {
20
20
  if(installedModules[request]) {
21
21
  if(installedModules[request].parents.indexOf(moduleId) < 0)
22
22
  installedModules[request].parents.push(moduleId);
23
- } else hotCurrentParents = [moduleId];
23
+ } else {
24
+ hotCurrentParents = [moduleId];
25
+ hotCurrentChildModule = request;
26
+ }
24
27
  if(me.children.indexOf(request) < 0)
25
28
  me.children.push(request);
26
29
  } else {
27
30
  console.warn("[HMR] unexpected require(" + request + ") from disposed module " + moduleId);
28
31
  hotCurrentParents = [];
29
32
  }
30
- hotMainModule = false;
31
33
  return $require$(request);
32
34
  };
33
35
  var ObjectFactory = function ObjectFactory(name) {
@@ -43,34 +45,31 @@ module.exports = function() {
43
45
  };
44
46
  };
45
47
  for(var name in $require$) {
46
- if(Object.prototype.hasOwnProperty.call($require$, name)) {
48
+ if(Object.prototype.hasOwnProperty.call($require$, name) && name !== "e") {
47
49
  Object.defineProperty(fn, name, ObjectFactory(name));
48
50
  }
49
51
  }
50
- Object.defineProperty(fn, "e", {
51
- enumerable: true,
52
- value: function(chunkId) {
53
- if(hotStatus === "ready")
54
- hotSetStatus("prepare");
55
- hotChunksLoading++;
56
- return $require$.e(chunkId).then(finishChunkLoading, function(err) {
57
- finishChunkLoading();
58
- throw err;
59
- });
52
+ fn.e = function(chunkId) {
53
+ if(hotStatus === "ready")
54
+ hotSetStatus("prepare");
55
+ hotChunksLoading++;
56
+ return $require$.e(chunkId).then(finishChunkLoading, function(err) {
57
+ finishChunkLoading();
58
+ throw err;
59
+ });
60
60
 
61
- function finishChunkLoading() {
62
- hotChunksLoading--;
63
- if(hotStatus === "prepare") {
64
- if(!hotWaitingFilesMap[chunkId]) {
65
- hotEnsureUpdateChunk(chunkId);
66
- }
67
- if(hotChunksLoading === 0 && hotWaitingFiles === 0) {
68
- hotUpdateDownloaded();
69
- }
61
+ function finishChunkLoading() {
62
+ hotChunksLoading--;
63
+ if(hotStatus === "prepare") {
64
+ if(!hotWaitingFilesMap[chunkId]) {
65
+ hotEnsureUpdateChunk(chunkId);
66
+ }
67
+ if(hotChunksLoading === 0 && hotWaitingFiles === 0) {
68
+ hotUpdateDownloaded();
70
69
  }
71
70
  }
72
71
  }
73
- });
72
+ };
74
73
  return fn;
75
74
  }
76
75
 
@@ -82,7 +81,7 @@ module.exports = function() {
82
81
  _selfAccepted: false,
83
82
  _selfDeclined: false,
84
83
  _disposeHandlers: [],
85
- _main: hotMainModule,
84
+ _main: hotCurrentChildModule !== moduleId,
86
85
 
87
86
  // Module API
88
87
  active: true,
@@ -135,7 +134,7 @@ module.exports = function() {
135
134
  //inherit from previous dispose call
136
135
  data: hotCurrentModuleData[moduleId]
137
136
  };
138
- hotMainModule = true;
137
+ hotCurrentChildModule = undefined;
139
138
  return hot;
140
139
  }
141
140
 
@@ -173,7 +172,6 @@ module.exports = function() {
173
172
  hotSetStatus("idle");
174
173
  return null;
175
174
  }
176
-
177
175
  hotRequestedFilesMap = {};
178
176
  hotWaitingFilesMap = {};
179
177
  hotAvailableFilesMap = update.c;
@@ -109,10 +109,8 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
109
109
  c: {}
110
110
  };
111
111
  Object.keys(records.chunkHashs).forEach(function(chunkId) {
112
- chunkId = +chunkId;
113
- var currentChunk = this.chunks.filter(function(chunk) {
114
- return chunk.id === chunkId;
115
- })[0];
112
+ chunkId = isNaN(+chunkId) ? chunkId : +chunkId;
113
+ var currentChunk = this.chunks.find(chunk => chunk.id === chunkId);
116
114
  if(currentChunk) {
117
115
  var newModules = currentChunk.modules.filter(function(module) {
118
116
  return module.hotUpdate;
@@ -171,7 +169,7 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
171
169
  hotInitCode
172
170
  .replace(/\$require\$/g, this.requireFn)
173
171
  .replace(/\$hash\$/g, JSON.stringify(hash))
174
- .replace(/\/\*foreachInstalledChunks\*\//g, chunk.chunks.length > 0 ? "for(var chunkId in installedChunks)" : "var chunkId = " + chunk.id + ";")
172
+ .replace(/\/\*foreachInstalledChunks\*\//g, chunk.chunks.length > 0 ? "for(var chunkId in installedChunks)" : "var chunkId = " + JSON.stringify(chunk.id) + ";")
175
173
  ]);
176
174
  });
177
175
 
@@ -8,29 +8,60 @@ class IgnorePlugin {
8
8
  constructor(resourceRegExp, contextRegExp) {
9
9
  this.resourceRegExp = resourceRegExp;
10
10
  this.contextRegExp = contextRegExp;
11
+
12
+ this.checkIgnore = this.checkIgnore.bind(this);
13
+ }
14
+
15
+ /*
16
+ * Only returns true if a "resourceRegExp" exists
17
+ * and the resource given matches the regexp.
18
+ */
19
+ checkResouce(resource) {
20
+ if(!this.resourceRegExp) {
21
+ return false;
22
+ }
23
+ return this.resourceRegExp.test(resource);
24
+ }
25
+
26
+ /*
27
+ * Returns true if contextRegExp does not exist
28
+ * or if context matches the given regexp.
29
+ */
30
+ checkContext(context) {
31
+ if(!this.contextRegExp) {
32
+ return true;
33
+ }
34
+ return this.contextRegExp.test(context);
35
+ }
36
+
37
+ /*
38
+ * Returns true if result should be ignored.
39
+ * false if it shouldn't.
40
+ *
41
+ * Not that if "contextRegExp" is given, both the "resourceRegExp"
42
+ * and "contextRegExp" have to match.
43
+ */
44
+ checkResult(result) {
45
+ if(!result) {
46
+ return true;
47
+ }
48
+ return this.checkResouce(result.request) && this.checkContext(result.context);
49
+ }
50
+
51
+ checkIgnore(result, callback) {
52
+ // check if result is ignored
53
+ if(this.checkResult(result)) {
54
+ return callback();
55
+ }
56
+ return callback(null, result);
11
57
  }
12
58
 
13
59
  apply(compiler) {
14
- const resourceRegExp = this.resourceRegExp;
15
- const contextRegExp = this.contextRegExp;
16
60
  compiler.plugin("normal-module-factory", (nmf) => {
17
- nmf.plugin("before-resolve", (result, callback) => {
18
- if(!result) return callback();
19
- if(resourceRegExp.test(result.request) &&
20
- (!contextRegExp || contextRegExp.test(result.context))) {
21
- return callback();
22
- }
23
- return callback(null, result);
24
- });
61
+ nmf.plugin("before-resolve", this.checkIgnore);
25
62
  });
26
63
  compiler.plugin("context-module-factory", (cmf) => {
27
- cmf.plugin("before-resolve", (result, callback) => {
28
- if(!result) return callback();
29
- if(resourceRegExp.test(result.request)) {
30
- return callback();
31
- }
32
- return callback(null, result);
33
- });
64
+ cmf.plugin("before-resolve", this.checkIgnore);
34
65
  });
35
66
  }
36
67
  }
@@ -2,30 +2,30 @@
2
2
  MIT License http://www.opensource.org/licenses/mit-license.php
3
3
  Author Tobias Koppers @sokra
4
4
  */
5
- var ConcatSource = require("webpack-sources").ConcatSource;
5
+ "use strict";
6
6
 
7
- function JsonpChunkTemplatePlugin() {}
8
- module.exports = JsonpChunkTemplatePlugin;
7
+ const ConcatSource = require("webpack-sources").ConcatSource;
9
8
 
10
- JsonpChunkTemplatePlugin.prototype.apply = function(chunkTemplate) {
11
- chunkTemplate.plugin("render", function(modules, chunk) {
12
- var jsonpFunction = this.outputOptions.jsonpFunction;
13
- var source = new ConcatSource();
14
- source.add(jsonpFunction + "(" + JSON.stringify(chunk.ids) + ",");
15
- source.add(modules);
16
- var entries = [chunk.entryModule].filter(Boolean).map(function(m) {
17
- return m.id;
9
+ class JsonpChunkTemplatePlugin {
10
+ apply(chunkTemplate) {
11
+ chunkTemplate.plugin("render", function(modules, chunk) {
12
+ const jsonpFunction = this.outputOptions.jsonpFunction;
13
+ const source = new ConcatSource();
14
+ source.add(`${jsonpFunction}(${JSON.stringify(chunk.ids)},`);
15
+ source.add(modules);
16
+ const entries = [chunk.entryModule].filter(Boolean).map(m => m.id);
17
+ if(entries.length > 0) {
18
+ source.add(`,${JSON.stringify(entries)}`);
19
+ }
20
+ source.add(")");
21
+ return source;
22
+ });
23
+ chunkTemplate.plugin("hash", function(hash) {
24
+ hash.update("JsonpChunkTemplatePlugin");
25
+ hash.update("3");
26
+ hash.update(`${this.outputOptions.jsonpFunction}`);
27
+ hash.update(`${this.outputOptions.library}`);
18
28
  });
19
- if(entries.length > 0) {
20
- source.add("," + JSON.stringify(entries));
21
- }
22
- source.add(")");
23
- return source;
24
- });
25
- chunkTemplate.plugin("hash", function(hash) {
26
- hash.update("JsonpChunkTemplatePlugin");
27
- hash.update("3");
28
- hash.update(this.outputOptions.jsonpFunction + "");
29
- hash.update(this.outputOptions.library + "");
30
- });
31
- };
29
+ }
30
+ }
31
+ module.exports = JsonpChunkTemplatePlugin;
@@ -2,207 +2,207 @@
2
2
  MIT License http://www.opensource.org/licenses/mit-license.php
3
3
  Author Tobias Koppers @sokra
4
4
  */
5
- var Template = require("./Template");
5
+ "use strict";
6
6
 
7
- function JsonpMainTemplatePlugin() {}
8
- module.exports = JsonpMainTemplatePlugin;
7
+ const Template = require("./Template");
9
8
 
10
- JsonpMainTemplatePlugin.prototype.constructor = JsonpMainTemplatePlugin;
11
- JsonpMainTemplatePlugin.prototype.apply = function(mainTemplate) {
12
- mainTemplate.plugin("local-vars", function(source, chunk) {
13
- if(chunk.chunks.length > 0) {
14
- return this.asString([
15
- source,
16
- "",
17
- "// objects to store loaded and loading chunks",
18
- "var installedChunks = {",
19
- this.indent(
20
- chunk.ids.map(function(id) {
21
- return id + ": 0";
22
- }).join(",\n")
23
- ),
24
- "};"
25
- ]);
26
- }
27
- return source;
28
- });
29
- mainTemplate.plugin("jsonp-script", function(_, chunk, hash) {
30
- var chunkFilename = this.outputOptions.chunkFilename;
31
- var chunkMaps = chunk.getChunkMaps();
32
- var crossOriginLoading = this.outputOptions.crossOriginLoading;
33
- var chunkLoadTimeout = this.outputOptions.chunkLoadTimeout || 120000;
34
- return this.asString([
35
- "var script = document.createElement('script');",
36
- "script.type = 'text/javascript';",
37
- "script.charset = 'utf-8';",
38
- "script.async = true;",
39
- "script.timeout = " + chunkLoadTimeout + ";",
40
- crossOriginLoading ? "script.crossOrigin = '" + crossOriginLoading + "';" : "",
41
- "if (" + this.requireFn + ".nc) {",
42
- this.indent("script.setAttribute(\"nonce\", " + this.requireFn + ".nc);"),
43
- "}",
44
- "script.src = " + this.requireFn + ".p + " +
45
- this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
46
- hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
47
- hashWithLength: function(length) {
48
- return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
49
- }.bind(this),
9
+ class JsonpMainTemplatePlugin {
10
+
11
+ apply(mainTemplate) {
12
+ mainTemplate.plugin("local-vars", function(source, chunk) {
13
+ if(chunk.chunks.length > 0) {
14
+ return this.asString([
15
+ source,
16
+ "",
17
+ "// objects to store loaded and loading chunks",
18
+ "var installedChunks = {",
19
+ this.indent(
20
+ chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
21
+ ),
22
+ "};"
23
+ ]);
24
+ }
25
+ return source;
26
+ });
27
+ mainTemplate.plugin("jsonp-script", function(_, chunk, hash) {
28
+ const chunkFilename = this.outputOptions.chunkFilename;
29
+ const chunkMaps = chunk.getChunkMaps();
30
+ const crossOriginLoading = this.outputOptions.crossOriginLoading;
31
+ const chunkLoadTimeout = this.outputOptions.chunkLoadTimeout || 120000;
32
+ const scriptSrcPath = this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
33
+ hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
34
+ hashWithLength: length => `" + ${this.renderCurrentHashCode(hash, length)} + "`,
50
35
  chunk: {
51
36
  id: "\" + chunkId + \"",
52
- hash: "\" + " + JSON.stringify(chunkMaps.hash) + "[chunkId] + \"",
53
- hashWithLength: function(length) {
54
- var shortChunkHashMap = {};
55
- Object.keys(chunkMaps.hash).forEach(function(chunkId) {
37
+ hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
38
+ hashWithLength(length) {
39
+ const shortChunkHashMap = Object.create(null);
40
+ Object.keys(chunkMaps.hash).forEach(chunkId => {
56
41
  if(typeof chunkMaps.hash[chunkId] === "string")
57
42
  shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(0, length);
58
43
  });
59
- return "\" + " + JSON.stringify(shortChunkHashMap) + "[chunkId] + \"";
44
+ return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
60
45
  },
61
- name: "\" + (" + JSON.stringify(chunkMaps.name) + "[chunkId]||chunkId) + \""
46
+ name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "`
62
47
  }
63
- }) + ";",
64
- "var timeout = setTimeout(onScriptComplete, " + chunkLoadTimeout + ");",
65
- "script.onerror = script.onload = onScriptComplete;",
66
- "function onScriptComplete() {",
67
- this.indent([
68
- "// avoid mem leaks in IE.",
69
- "script.onerror = script.onload = null;",
70
- "clearTimeout(timeout);",
71
- "var chunk = installedChunks[chunkId];",
72
- "if(chunk !== 0) {",
48
+ });
49
+ return this.asString([
50
+ "var script = document.createElement('script');",
51
+ "script.type = 'text/javascript';",
52
+ "script.charset = 'utf-8';",
53
+ "script.async = true;",
54
+ `script.timeout = ${chunkLoadTimeout};`,
55
+ crossOriginLoading ? `script.crossOrigin = ${JSON.stringify(crossOriginLoading)};` : "",
56
+ `if (${this.requireFn}.nc) {`,
57
+ this.indent(`script.setAttribute("nonce", ${this.requireFn}.nc);`),
58
+ "}",
59
+ `script.src = ${this.requireFn}.p + ${scriptSrcPath};`,
60
+ `var timeout = setTimeout(onScriptComplete, ${chunkLoadTimeout});`,
61
+ "script.onerror = script.onload = onScriptComplete;",
62
+ "function onScriptComplete() {",
73
63
  this.indent([
74
- "if(chunk) chunk[1](new Error('Loading chunk ' + chunkId + ' failed.'));",
75
- "installedChunks[chunkId] = undefined;"
64
+ "// avoid mem leaks in IE.",
65
+ "script.onerror = script.onload = null;",
66
+ "clearTimeout(timeout);",
67
+ "var chunk = installedChunks[chunkId];",
68
+ "if(chunk !== 0) {",
69
+ this.indent([
70
+ "if(chunk) {",
71
+ this.indent("chunk[1](new Error('Loading chunk ' + chunkId + ' failed.'));"),
72
+ "}",
73
+ "installedChunks[chunkId] = undefined;"
74
+ ]),
75
+ "}"
76
76
  ]),
77
- "}"
78
- ]),
79
- "};",
80
- ]);
81
- });
82
- mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
83
- return this.asString([
84
- "if(installedChunks[chunkId] === 0)",
85
- this.indent([
86
- "return Promise.resolve();"
87
- ]),
88
- "",
89
- "// a Promise means \"currently loading\".",
90
- "if(installedChunks[chunkId]) {",
91
- this.indent([
92
- "return installedChunks[chunkId][2];"
93
- ]),
94
- "}",
95
- "",
96
- "// setup Promise in chunk cache",
97
- "var promise = new Promise(function(resolve, reject) {",
98
- this.indent([
99
- "installedChunks[chunkId] = [resolve, reject];"
100
- ]),
101
- "});",
102
- "installedChunks[chunkId][2] = promise;",
103
- "",
104
- "// start chunk loading",
105
- "var head = document.getElementsByTagName('head')[0];",
106
- this.applyPluginsWaterfall("jsonp-script", "", chunk, hash),
107
- "head.appendChild(script);",
108
- "",
109
- "return promise;"
110
- ]);
111
- });
112
- mainTemplate.plugin("require-extensions", function(source, chunk) {
113
- if(chunk.chunks.length === 0) return source;
114
- return this.asString([
115
- source,
116
- "",
117
- "// on error function for async loading",
118
- this.requireFn + ".oe = function(err) { console.error(err); throw err; };"
119
- ]);
120
- });
121
- mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
122
- if(chunk.chunks.length > 0) {
123
- var jsonpFunction = this.outputOptions.jsonpFunction;
77
+ "};",
78
+ ]);
79
+ });
80
+ mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
124
81
  return this.asString([
125
- source,
82
+ "if(installedChunks[chunkId] === 0) {",
83
+ this.indent([
84
+ "return Promise.resolve();"
85
+ ]),
86
+ "}",
126
87
  "",
127
- "// install a JSONP callback for chunk loading",
128
- "var parentJsonpFunction = window[" + JSON.stringify(jsonpFunction) + "];",
129
- "window[" + JSON.stringify(jsonpFunction) + "] = function webpackJsonpCallback(chunkIds, moreModules, executeModules) {",
88
+ "// a Promise means \"currently loading\".",
89
+ "if(installedChunks[chunkId]) {",
130
90
  this.indent([
131
- "// add \"moreModules\" to the modules object,",
132
- "// then flag all \"chunkIds\" as loaded and fire callback",
133
- "var moduleId, chunkId, i = 0, resolves = [], result;",
134
- "for(;i < chunkIds.length; i++) {",
135
- this.indent([
136
- "chunkId = chunkIds[i];",
137
- "if(installedChunks[chunkId])",
138
- this.indent("resolves.push(installedChunks[chunkId][0]);"),
139
- "installedChunks[chunkId] = 0;"
140
- ]),
141
- "}",
142
- "for(moduleId in moreModules) {",
91
+ "return installedChunks[chunkId][2];"
92
+ ]),
93
+ "}",
94
+ "",
95
+ "// setup Promise in chunk cache",
96
+ "var promise = new Promise(function(resolve, reject) {",
97
+ this.indent([
98
+ "installedChunks[chunkId] = [resolve, reject];"
99
+ ]),
100
+ "});",
101
+ "installedChunks[chunkId][2] = promise;",
102
+ "",
103
+ "// start chunk loading",
104
+ "var head = document.getElementsByTagName('head')[0];",
105
+ this.applyPluginsWaterfall("jsonp-script", "", chunk, hash),
106
+ "head.appendChild(script);",
107
+ "",
108
+ "return promise;"
109
+ ]);
110
+ });
111
+ mainTemplate.plugin("require-extensions", function(source, chunk) {
112
+ if(chunk.chunks.length === 0) return source;
113
+
114
+ return this.asString([
115
+ source,
116
+ "",
117
+ "// on error function for async loading",
118
+ `${this.requireFn}.oe = function(err) { console.error(err); throw err; };`
119
+ ]);
120
+ });
121
+ mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
122
+ if(chunk.chunks.length > 0) {
123
+ var jsonpFunction = this.outputOptions.jsonpFunction;
124
+ return this.asString([
125
+ source,
126
+ "",
127
+ "// install a JSONP callback for chunk loading",
128
+ `var parentJsonpFunction = window[${JSON.stringify(jsonpFunction)}];`,
129
+ `window[${JSON.stringify(jsonpFunction)}] = function webpackJsonpCallback(chunkIds, moreModules, executeModules) {`,
143
130
  this.indent([
144
- "if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {",
145
- this.indent(this.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")),
146
- "}"
147
- ]),
148
- "}",
149
- "if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules, executeModules);",
150
- "while(resolves.length)",
151
- this.indent("resolves.shift()();"),
152
- this.entryPointInChildren(chunk) ? [
153
- "if(executeModules) {",
131
+ "// add \"moreModules\" to the modules object,",
132
+ "// then flag all \"chunkIds\" as loaded and fire callback",
133
+ "var moduleId, chunkId, i = 0, resolves = [], result;",
134
+ "for(;i < chunkIds.length; i++) {",
154
135
  this.indent([
155
- "for(i=0; i < executeModules.length; i++) {",
156
- this.indent("result = " + this.requireFn + "(" + this.requireFn + ".s = executeModules[i]);"),
136
+ "chunkId = chunkIds[i];",
137
+ "if(installedChunks[chunkId]) {",
138
+ this.indent("resolves.push(installedChunks[chunkId][0]);"),
139
+ "}",
140
+ "installedChunks[chunkId] = 0;"
141
+ ]),
142
+ "}",
143
+ "for(moduleId in moreModules) {",
144
+ this.indent([
145
+ "if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {",
146
+ this.indent(this.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")),
157
147
  "}"
158
148
  ]),
159
149
  "}",
160
- "return result;",
161
- ] : ""
162
- ]),
163
- "};"
164
- ]);
165
- }
166
- return source;
167
- });
168
- mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) {
169
- var hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
170
- var hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
171
- var hotUpdateFunction = this.outputOptions.hotUpdateFunction;
172
- var currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), {
173
- hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
174
- hashWithLength: function(length) {
175
- return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
176
- }.bind(this),
177
- chunk: {
178
- id: "\" + chunkId + \""
150
+ "if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules, executeModules);",
151
+ "while(resolves.length) {",
152
+ this.indent("resolves.shift()();"),
153
+ "}",
154
+ this.entryPointInChildren(chunk) ? [
155
+ "if(executeModules) {",
156
+ this.indent([
157
+ "for(i=0; i < executeModules.length; i++) {",
158
+ this.indent(`result = ${this.requireFn}(${this.requireFn}.s = executeModules[i]);`),
159
+ "}"
160
+ ]),
161
+ "}",
162
+ "return result;",
163
+ ] : ""
164
+ ]),
165
+ "};"
166
+ ]);
179
167
  }
168
+ return source;
180
169
  });
181
- var currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), {
182
- hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
183
- hashWithLength: function(length) {
184
- return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
185
- }.bind(this)
170
+ mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) {
171
+ const hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
172
+ const hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
173
+ const hotUpdateFunction = this.outputOptions.hotUpdateFunction;
174
+ const currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), {
175
+ hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
176
+ hashWithLength: length => `" + ${this.renderCurrentHashCode(hash, length)} + "`,
177
+ chunk: {
178
+ id: "\" + chunkId + \""
179
+ }
180
+ });
181
+ const currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), {
182
+ hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
183
+ hashWithLength: length => `" + ${this.renderCurrentHashCode(hash, length)} + "`
184
+ });
185
+ const runtimeSource = Template.getFunctionContent(require("./JsonpMainTemplate.runtime.js"))
186
+ .replace(/\/\/\$semicolon/g, ";")
187
+ .replace(/\$require\$/g, this.requireFn)
188
+ .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
189
+ .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
190
+ .replace(/\$hash\$/g, JSON.stringify(hash));
191
+ return `${source}
192
+ function hotDisposeChunk(chunkId) {
193
+ delete installedChunks[chunkId];
194
+ }
195
+ var parentHotUpdateCallback = this[${JSON.stringify(hotUpdateFunction)}];
196
+ this[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
186
197
  });
187
-
188
- return source + "\n" +
189
- "function hotDisposeChunk(chunkId) {\n" +
190
- "\tdelete installedChunks[chunkId];\n" +
191
- "}\n" +
192
- "var parentHotUpdateCallback = this[" + JSON.stringify(hotUpdateFunction) + "];\n" +
193
- "this[" + JSON.stringify(hotUpdateFunction) + "] = " + Template.getFunctionContent(require("./JsonpMainTemplate.runtime.js"))
194
- .replace(/\/\/\$semicolon/g, ";")
195
- .replace(/\$require\$/g, this.requireFn)
196
- .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
197
- .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
198
- .replace(/\$hash\$/g, JSON.stringify(hash));
199
- });
200
- mainTemplate.plugin("hash", function(hash) {
201
- hash.update("jsonp");
202
- hash.update("4");
203
- hash.update(this.outputOptions.filename + "");
204
- hash.update(this.outputOptions.chunkFilename + "");
205
- hash.update(this.outputOptions.jsonpFunction + "");
206
- hash.update(this.outputOptions.hotUpdateFunction + "");
207
- });
208
- };
198
+ mainTemplate.plugin("hash", function(hash) {
199
+ hash.update("jsonp");
200
+ hash.update("4");
201
+ hash.update(`${this.outputOptions.filename}`);
202
+ hash.update(`${this.outputOptions.chunkFilename}`);
203
+ hash.update(`${this.outputOptions.jsonpFunction}`);
204
+ hash.update(`${this.outputOptions.hotUpdateFunction}`);
205
+ });
206
+ }
207
+ }
208
+ module.exports = JsonpMainTemplatePlugin;