webpack 1.13.3 → 1.14.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 (212) hide show
  1. package/LICENSE +22 -22
  2. package/README.md +310 -310
  3. package/buildin/amd-define.js +1 -1
  4. package/buildin/amd-options.js +1 -1
  5. package/buildin/module.js +10 -10
  6. package/hot/dev-server.js +63 -63
  7. package/hot/log-apply-result.js +25 -25
  8. package/hot/only-dev-server.js +77 -77
  9. package/hot/poll.js +37 -37
  10. package/hot/signal.js +65 -65
  11. package/lib/APIPlugin.js +49 -49
  12. package/lib/AbstractPlugin.js +22 -22
  13. package/lib/AmdMainTemplatePlugin.js +43 -43
  14. package/lib/ArrayMap.js +50 -50
  15. package/lib/AsyncDependenciesBlock.js +39 -39
  16. package/lib/AutomaticPrefetchPlugin.js +35 -35
  17. package/lib/BannerPlugin.js +34 -34
  18. package/lib/BasicEvaluatedExpression.js +140 -140
  19. package/lib/CachePlugin.js +43 -43
  20. package/lib/CaseSensitiveModulesWarning.js +16 -16
  21. package/lib/Chunk.js +252 -252
  22. package/lib/ChunkRenderError.js +17 -17
  23. package/lib/ChunkTemplate.js +37 -37
  24. package/lib/CompatibilityPlugin.js +42 -42
  25. package/lib/Compilation.js +891 -891
  26. package/lib/Compiler.js +407 -407
  27. package/lib/ConcatSource.js +5 -5
  28. package/lib/ConstPlugin.js +54 -54
  29. package/lib/ContextModule.js +130 -130
  30. package/lib/ContextModuleFactory.js +132 -132
  31. package/lib/ContextReplacementPlugin.js +93 -93
  32. package/lib/CriticalDependenciesWarning.js +24 -24
  33. package/lib/DefinePlugin.js +126 -126
  34. package/lib/DelegatedModule.js +69 -69
  35. package/lib/DelegatedModuleFactoryPlugin.js +51 -51
  36. package/lib/DelegatedPlugin.js +21 -21
  37. package/lib/DependenciesBlock.js +57 -57
  38. package/lib/DependenciesBlockVariable.js +41 -41
  39. package/lib/Dependency.js +54 -54
  40. package/lib/DllEntryPlugin.js +32 -32
  41. package/lib/DllModule.js +55 -55
  42. package/lib/DllModuleFactory.js +18 -18
  43. package/lib/DllPlugin.js +30 -30
  44. package/lib/DllReferencePlugin.js +26 -26
  45. package/lib/EntryModuleNotFoundError.js +15 -15
  46. package/lib/EntryOptionPlugin.js +28 -28
  47. package/lib/EnvironmentPlugin.js +28 -28
  48. package/lib/EvalDevToolModulePlugin.js +17 -17
  49. package/lib/EvalDevToolModuleTemplatePlugin.js +29 -29
  50. package/lib/EvalSourceMapDevToolModuleTemplatePlugin.js +68 -68
  51. package/lib/EvalSourceMapDevToolPlugin.js +25 -25
  52. package/lib/ExtendedAPIPlugin.js +45 -45
  53. package/lib/ExternalModule.js +89 -89
  54. package/lib/ExternalModuleFactoryPlugin.js +86 -86
  55. package/lib/ExternalsPlugin.js +16 -16
  56. package/lib/FunctionModulePlugin.js +18 -18
  57. package/lib/FunctionModuleTemplatePlugin.js +39 -39
  58. package/lib/HotModuleReplacement.runtime.js +486 -486
  59. package/lib/HotModuleReplacementPlugin.js +223 -223
  60. package/lib/HotUpdateChunkTemplate.js +28 -28
  61. package/lib/IgnorePlugin.js +32 -32
  62. package/lib/JsonpChunkTemplatePlugin.js +26 -26
  63. package/lib/JsonpExportMainTemplatePlugin.js +28 -28
  64. package/lib/JsonpHotUpdateChunkTemplatePlugin.js +26 -26
  65. package/lib/JsonpMainTemplate.runtime.js +56 -56
  66. package/lib/JsonpMainTemplatePlugin.js +162 -162
  67. package/lib/JsonpTemplatePlugin.js +17 -17
  68. package/lib/LibManifestPlugin.js +49 -49
  69. package/lib/LibraryTemplatePlugin.js +77 -77
  70. package/lib/LoaderTargetPlugin.js +16 -16
  71. package/lib/MainTemplate.js +169 -169
  72. package/lib/MemoryOutputFileSystem.js +5 -5
  73. package/lib/Module.js +116 -116
  74. package/lib/ModuleFilenameHelpers.js +157 -157
  75. package/lib/ModuleNotFoundError.js +17 -17
  76. package/lib/ModuleParseError.js +26 -26
  77. package/lib/ModuleParserHelpers.js +21 -21
  78. package/lib/ModuleReason.js +9 -9
  79. package/lib/ModuleTemplate.js +23 -23
  80. package/lib/MovedToPluginWarningPlugin.js +17 -17
  81. package/lib/MultiCompiler.js +166 -166
  82. package/lib/MultiEntryPlugin.js +31 -31
  83. package/lib/MultiModule.js +71 -71
  84. package/lib/MultiModuleFactory.js +18 -18
  85. package/lib/NamedModulesPlugin.js +21 -21
  86. package/lib/NewWatchingPlugin.js +12 -12
  87. package/lib/NoErrorsPlugin.js +18 -18
  88. package/lib/NodeStuffPlugin.js +99 -99
  89. package/lib/NormalModule.js +322 -322
  90. package/lib/NormalModuleFactory.js +171 -171
  91. package/lib/NormalModuleReplacementPlugin.js +40 -40
  92. package/lib/NullFactory.js +10 -10
  93. package/lib/OldWatchingPlugin.js +14 -14
  94. package/lib/OptionsApply.js +10 -10
  95. package/lib/OriginalSource.js +5 -5
  96. package/lib/Parser.js +938 -938
  97. package/lib/PrefetchPlugin.js +25 -25
  98. package/lib/ProgressPlugin.js +80 -80
  99. package/lib/ProvidePlugin.js +48 -48
  100. package/lib/RawModule.js +65 -65
  101. package/lib/RawSource.js +5 -5
  102. package/lib/RecordIdsPlugin.js +130 -130
  103. package/lib/RequestShortener.js +56 -56
  104. package/lib/RequireJsStuffPlugin.js +38 -38
  105. package/lib/ResolverPlugin.js +34 -34
  106. package/lib/SetVarMainTemplatePlugin.js +36 -36
  107. package/lib/SingleEntryPlugin.js +24 -24
  108. package/lib/Source.js +5 -5
  109. package/lib/SourceMapDevToolModuleOptionsPlugin.js +33 -33
  110. package/lib/SourceMapDevToolPlugin.js +168 -168
  111. package/lib/SourceMapSource.js +5 -5
  112. package/lib/Stats.js +732 -732
  113. package/lib/Template.js +119 -119
  114. package/lib/TemplatedPathPlugin.js +111 -111
  115. package/lib/UmdMainTemplatePlugin.js +146 -146
  116. package/lib/UnsupportedFeatureWarning.js +14 -14
  117. package/lib/WarnCaseSensitiveModulesPlugin.js +27 -27
  118. package/lib/WatchIgnorePlugin.js +45 -45
  119. package/lib/WebpackOptionsApply.js +376 -376
  120. package/lib/WebpackOptionsDefaulter.js +112 -112
  121. package/lib/dependencies/AMDDefineDependency.js +63 -63
  122. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +240 -240
  123. package/lib/dependencies/AMDPlugin.js +115 -115
  124. package/lib/dependencies/AMDRequireArrayDependency.js +34 -34
  125. package/lib/dependencies/AMDRequireContextDependency.js +18 -18
  126. package/lib/dependencies/AMDRequireDependenciesBlock.js +25 -25
  127. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +145 -145
  128. package/lib/dependencies/AMDRequireDependency.js +64 -64
  129. package/lib/dependencies/AMDRequireItemDependency.js +17 -17
  130. package/lib/dependencies/CommonJsPlugin.js +93 -93
  131. package/lib/dependencies/CommonJsRequireContextDependency.js +18 -18
  132. package/lib/dependencies/CommonJsRequireDependency.js +17 -17
  133. package/lib/dependencies/CommonJsRequireDependencyParserPlugin.js +91 -91
  134. package/lib/dependencies/ConstDependency.js +29 -29
  135. package/lib/dependencies/ContextDependency.js +24 -24
  136. package/lib/dependencies/ContextDependencyHelpers.js +34 -34
  137. package/lib/dependencies/ContextDependencyTemplateAsId.js +22 -22
  138. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +31 -31
  139. package/lib/dependencies/ContextElementDependency.js +17 -17
  140. package/lib/dependencies/DelegatedSourceDependency.js +14 -14
  141. package/lib/dependencies/DepBlockHelpers.js +41 -41
  142. package/lib/dependencies/DllEntryDependency.js +17 -17
  143. package/lib/dependencies/LabeledExportsDependency.js +21 -21
  144. package/lib/dependencies/LabeledModuleDependency.js +36 -36
  145. package/lib/dependencies/LabeledModuleDependencyParserPlugin.js +76 -76
  146. package/lib/dependencies/LabeledModulesPlugin.js +26 -26
  147. package/lib/dependencies/LoaderDependency.js +14 -14
  148. package/lib/dependencies/LoaderPlugin.js +61 -61
  149. package/lib/dependencies/LocalModule.js +19 -19
  150. package/lib/dependencies/LocalModuleDependency.js +23 -23
  151. package/lib/dependencies/LocalModulesHelpers.js +43 -43
  152. package/lib/dependencies/ModuleDependency.js +20 -20
  153. package/lib/dependencies/ModuleDependencyTemplateAsId.js +22 -22
  154. package/lib/dependencies/ModuleDependencyTemplateAsRequireId.js +22 -22
  155. package/lib/dependencies/ModuleHotAcceptDependency.js +18 -18
  156. package/lib/dependencies/ModuleHotDeclineDependency.js +18 -18
  157. package/lib/dependencies/MultiEntryDependency.js +16 -16
  158. package/lib/dependencies/NullDependency.js +17 -17
  159. package/lib/dependencies/NullDependencyTemplate.js +8 -8
  160. package/lib/dependencies/PrefetchDependency.js +14 -14
  161. package/lib/dependencies/RequireContextDependency.js +17 -17
  162. package/lib/dependencies/RequireContextDependencyParserPlugin.js +33 -33
  163. package/lib/dependencies/RequireContextPlugin.js +64 -64
  164. package/lib/dependencies/RequireEnsureDependenciesBlock.js +20 -20
  165. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +66 -66
  166. package/lib/dependencies/RequireEnsureDependency.js +29 -29
  167. package/lib/dependencies/RequireEnsureItemDependency.js +16 -16
  168. package/lib/dependencies/RequireEnsurePlugin.js +38 -38
  169. package/lib/dependencies/RequireHeaderDependency.js +25 -25
  170. package/lib/dependencies/RequireIncludeDependency.js +25 -25
  171. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +18 -18
  172. package/lib/dependencies/RequireIncludePlugin.js +31 -31
  173. package/lib/dependencies/RequireResolveContextDependency.js +18 -18
  174. package/lib/dependencies/RequireResolveDependency.js +17 -17
  175. package/lib/dependencies/RequireResolveDependencyParserPlugin.js +69 -69
  176. package/lib/dependencies/RequireResolveHeaderDependency.js +25 -25
  177. package/lib/dependencies/SingleEntryDependency.js +14 -14
  178. package/lib/dependencies/TemplateArgumentDependency.js +26 -26
  179. package/lib/dependencies/WebpackMissingModule.js +23 -23
  180. package/lib/dependencies/getFunctionExpression.js +43 -43
  181. package/lib/node/NodeChunkTemplatePlugin.js +22 -22
  182. package/lib/node/NodeEnvironmentPlugin.js +25 -25
  183. package/lib/node/NodeHotUpdateChunkTemplatePlugin.js +24 -24
  184. package/lib/node/NodeMainTemplate.runtime.js +20 -20
  185. package/lib/node/NodeMainTemplateAsync.runtime.js +34 -34
  186. package/lib/node/NodeMainTemplatePlugin.js +164 -164
  187. package/lib/node/NodeOutputFileSystem.js +17 -17
  188. package/lib/node/NodeSourcePlugin.js +85 -75
  189. package/lib/node/NodeTargetPlugin.js +12 -12
  190. package/lib/node/NodeTemplatePlugin.js +21 -21
  191. package/lib/node/NodeWatchFileSystem.js +65 -65
  192. package/lib/node/OldNodeWatchFileSystem.js +265 -265
  193. package/lib/optimize/AggressiveMergingPlugin.js +115 -115
  194. package/lib/optimize/CommonsChunkPlugin.js +178 -178
  195. package/lib/optimize/DedupePlugin.js +227 -227
  196. package/lib/optimize/FlagIncludedChunksPlugin.js +24 -24
  197. package/lib/optimize/LimitChunkCountPlugin.js +55 -55
  198. package/lib/optimize/MergeDuplicateChunksPlugin.js +30 -30
  199. package/lib/optimize/MinChunkSizePlugin.js +61 -61
  200. package/lib/optimize/OccurenceOrderPlugin.js +5 -5
  201. package/lib/optimize/OccurrenceOrderPlugin.js +91 -91
  202. package/lib/optimize/RemoveEmptyChunksPlugin.js +19 -19
  203. package/lib/optimize/RemoveParentModulesPlugin.js +50 -50
  204. package/lib/optimize/UglifyJsPlugin.js +146 -146
  205. package/lib/removeAndDo.js +13 -13
  206. package/lib/web/WebEnvironmentPlugin.js +16 -16
  207. package/lib/webpack.js +113 -113
  208. package/lib/webpack.web.js +27 -27
  209. package/lib/webworker/WebWorkerChunkTemplatePlugin.js +26 -26
  210. package/lib/webworker/WebWorkerMainTemplatePlugin.js +78 -78
  211. package/lib/webworker/WebWorkerTemplatePlugin.js +15 -15
  212. package/package.json +4 -4
@@ -1,891 +1,891 @@
1
- /*
2
- MIT License http://www.opensource.org/licenses/mit-license.php
3
- Author Tobias Koppers @sokra
4
- */
5
- var async = require("async");
6
-
7
- var Tapable = require("tapable");
8
- var EntryModuleNotFoundError = require("./EntryModuleNotFoundError");
9
- var ModuleNotFoundError = require("./ModuleNotFoundError");
10
- var CriticalDependenciesWarning = require("./CriticalDependenciesWarning");
11
- var Module = require("./Module");
12
- var ArrayMap = require("./ArrayMap");
13
- var Chunk = require("./Chunk");
14
- var Stats = require("./Stats");
15
- var MainTemplate = require("./MainTemplate");
16
- var ChunkTemplate = require("./ChunkTemplate");
17
- var HotUpdateChunkTemplate = require("./HotUpdateChunkTemplate");
18
- var ModuleTemplate = require("./ModuleTemplate");
19
- var Dependency = require("./Dependency");
20
- var ChunkRenderError = require("./ChunkRenderError");
21
- var CachedSource = require("webpack-core/lib/CachedSource");
22
-
23
- function Compilation(compiler) {
24
- Tapable.call(this);
25
- this.compiler = compiler;
26
- this.resolvers = compiler.resolvers;
27
- this.inputFileSystem = compiler.inputFileSystem;
28
-
29
- var options = this.options = compiler.options;
30
- this.outputOptions = options && options.output;
31
- this.bail = options && options.bail;
32
- this.profile = options && options.profile;
33
-
34
- this.mainTemplate = new MainTemplate(this.outputOptions);
35
- this.chunkTemplate = new ChunkTemplate(this.outputOptions, this.mainTemplate);
36
- this.hotUpdateChunkTemplate = new HotUpdateChunkTemplate(this.outputOptions);
37
- this.moduleTemplate = new ModuleTemplate(this.outputOptions);
38
-
39
- this.entries = [];
40
- this.preparedChunks = [];
41
- this.chunks = [];
42
- this.namedChunks = {};
43
- this.modules = [];
44
- this._modules = {};
45
- this.cache = null;
46
- this.records = null;
47
- this.nextFreeModuleId = 1;
48
- this.nextFreeChunkId = 0;
49
- this.nextFreeModuleIndex = 0;
50
- this.nextFreeModuleIndex2 = 0;
51
- this.additionalChunkAssets = [];
52
- this.assets = {};
53
- this.errors = [];
54
- this.warnings = [];
55
- this.children = [];
56
- this.dependencyFactories = new ArrayMap();
57
- this.dependencyTemplates = new ArrayMap();
58
- }
59
- module.exports = Compilation;
60
-
61
- Compilation.prototype = Object.create(Tapable.prototype);
62
- Compilation.prototype.constructor = Compilation;
63
-
64
- Compilation.prototype.templatesPlugin = function(name, fn) {
65
- this.mainTemplate.plugin(name, fn);
66
- this.chunkTemplate.plugin(name, fn);
67
- };
68
-
69
- Compilation.prototype.addModule = function(module, cacheGroup) {
70
- cacheGroup = cacheGroup || "m";
71
- var identifier = module.identifier();
72
- if(this._modules[identifier]) {
73
- return false;
74
- }
75
- if(this.cache && this.cache[cacheGroup + identifier]) {
76
- var cacheModule = this.cache[cacheGroup + identifier];
77
-
78
- var rebuild = true;
79
- if(!cacheModule.error && cacheModule.cacheable && this.fileTimestamps && this.contextTimestamps) {
80
- rebuild = cacheModule.needRebuild(this.fileTimestamps, this.contextTimestamps);
81
- }
82
-
83
- if(!rebuild) {
84
- cacheModule.disconnect();
85
- this._modules[identifier] = cacheModule;
86
- this.modules.push(cacheModule);
87
- cacheModule.errors.forEach(function(err) {
88
- this.errors.push(err);
89
- }, this);
90
- cacheModule.warnings.forEach(function(err) {
91
- this.warnings.push(err);
92
- }, this);
93
- return cacheModule;
94
- } else {
95
- module.lastId = cacheModule.id;
96
- }
97
- }
98
- this._modules[identifier] = module;
99
- if(this.cache) {
100
- this.cache[cacheGroup + identifier] = module;
101
- }
102
- this.modules.push(module);
103
- return true;
104
- };
105
-
106
- Compilation.prototype.getModule = function(module) {
107
- var identifier = module.identifier();
108
- return this._modules[identifier];
109
- };
110
-
111
- Compilation.prototype.findModule = function(identifier) {
112
- return this._modules[identifier];
113
- };
114
-
115
- Compilation.prototype.buildModule = function(module, thisCallback) {
116
- this.applyPlugins("build-module", module);
117
- if(module.building) return module.building.push(thisCallback);
118
- var building = module.building = [thisCallback];
119
-
120
- function callback(err) {
121
- module.building = undefined;
122
- building.forEach(function(cb) {
123
- cb(err);
124
- });
125
- }
126
- module.build(this.options, this, this.resolvers.normal, this.inputFileSystem, function(err) {
127
- module.errors.forEach(function(err) {
128
- this.errors.push(err);
129
- }, this);
130
- module.warnings.forEach(function(err) {
131
- this.warnings.push(err);
132
- }, this);
133
- module.dependencies.sort(Dependency.compare);
134
- if(err) {
135
- module.error = err;
136
- this.applyPlugins("failed-module", module);
137
- return callback(err);
138
- }
139
- this.applyPlugins("succeed-module", module);
140
- return callback();
141
- }.bind(this));
142
- };
143
-
144
- Compilation.prototype.processModuleDependencies = function(module, callback) {
145
- var dependencies = [];
146
-
147
- function addDependency(dep) {
148
- for(var i = 0; i < dependencies.length; i++) {
149
- if(dep.isEqualResource(dependencies[i][0])) {
150
- return dependencies[i].push(dep);
151
- }
152
- }
153
- dependencies.push([dep]);
154
- }
155
-
156
- function addDependenciesBlock(block) {
157
- if(block.dependencies) {
158
- block.dependencies.forEach(addDependency);
159
- }
160
- if(block.blocks) {
161
- block.blocks.forEach(addDependenciesBlock);
162
- }
163
- if(block.variables) {
164
- block.variables.forEach(function(v) {
165
- v.dependencies.forEach(addDependency);
166
- });
167
- }
168
- }
169
- addDependenciesBlock(module);
170
- this.addModuleDependencies(module, dependencies, this.bail, null, true, callback);
171
- };
172
-
173
- Compilation.prototype.addModuleDependencies = function(module, dependencies, bail, cacheGroup, recursive, callback) {
174
- var _this = this;
175
- var start = _this.profile && +new Date();
176
-
177
- var factories = [];
178
- for(var i = 0; i < dependencies.length; i++) {
179
- var factory = _this.dependencyFactories.get(dependencies[i][0].constructor);
180
- if(!factory) {
181
- return callback(new Error("No module factory available for dependency type: " + dependencies[i][0].constructor.name));
182
- }
183
- factories[i] = [factory, dependencies[i]];
184
- }
185
- async.forEach(factories, function(item, callback) {
186
- var dependencies = item[1];
187
- var criticalDependencies = dependencies.filter(function(d) {
188
- return !!d.critical;
189
- });
190
- if(criticalDependencies.length > 0) {
191
- _this.warnings.push(new CriticalDependenciesWarning(module, criticalDependencies));
192
- }
193
-
194
- var errorAndCallback = function errorAndCallback(err) {
195
- err.dependencies = dependencies;
196
- err.origin = module;
197
- module.dependenciesErrors.push(err);
198
- _this.errors.push(err);
199
- if(bail) {
200
- callback(err);
201
- } else {
202
- callback();
203
- }
204
- };
205
- var warningAndCallback = function warningAndCallback(err) {
206
- err.dependencies = dependencies;
207
- err.origin = module;
208
- module.dependenciesWarnings.push(err);
209
- _this.warnings.push(err);
210
- callback();
211
- };
212
-
213
- var factory = item[0];
214
- factory.create(module.context, dependencies[0], function(err, dependantModule) {
215
- function isOptional() {
216
- return dependencies.filter(function(d) {
217
- return !d.optional;
218
- }).length === 0;
219
- }
220
-
221
- function errorOrWarningAndCallback(err) {
222
- if(isOptional()) {
223
- return warningAndCallback(err);
224
- } else {
225
- return errorAndCallback(err);
226
- }
227
- }
228
- if(err) {
229
- return errorOrWarningAndCallback(new ModuleNotFoundError(module, err));
230
- }
231
- if(!dependantModule) {
232
- return process.nextTick(callback);
233
- }
234
- if(_this.profile) {
235
- if(!dependantModule.profile) {
236
- dependantModule.profile = {};
237
- }
238
- var afterFactory = +new Date();
239
- dependantModule.profile.factory = afterFactory - start;
240
- }
241
-
242
- dependantModule.issuer = module.identifier();
243
- var newModule = _this.addModule(dependantModule, cacheGroup);
244
-
245
- if(!newModule) { // from cache
246
- dependantModule = _this.getModule(dependantModule);
247
-
248
- if(dependantModule.optional) {
249
- dependantModule.optional = isOptional();
250
- }
251
-
252
- if(dependantModule.id === 0) {
253
- return errorOrWarningAndCallback(
254
- new ModuleNotFoundError(module, new Error("a dependency to an entry point is not allowed"))
255
- );
256
- }
257
-
258
- dependencies.forEach(function(dep) {
259
- dep.module = dependantModule;
260
- dependantModule.addReason(module, dep);
261
- });
262
-
263
- if(_this.profile) {
264
- if(!module.profile) {
265
- module.profile = {};
266
- }
267
- var time = +new Date() - start;
268
- if(!module.profile.dependencies || time > module.profile.dependencies) {
269
- module.profile.dependencies = time;
270
- }
271
- }
272
-
273
- return process.nextTick(callback);
274
- }
275
-
276
- if(newModule instanceof Module) {
277
- if(_this.profile) {
278
- newModule.profile = dependantModule.profile;
279
- }
280
-
281
- newModule.optional = isOptional();
282
- newModule.issuer = dependantModule.issuer;
283
- dependantModule = newModule;
284
-
285
- dependencies.forEach(function(dep) {
286
- dep.module = dependantModule;
287
- dependantModule.addReason(module, dep);
288
- });
289
-
290
- if(_this.profile) {
291
- var afterBuilding = +new Date();
292
- module.profile.building = afterBuilding - afterFactory;
293
- }
294
-
295
- if(recursive) {
296
- return process.nextTick(_this.processModuleDependencies.bind(_this, dependantModule, callback));
297
- } else {
298
- return process.nextTick(callback);
299
- }
300
- }
301
-
302
- dependantModule.optional = isOptional();
303
-
304
- dependencies.forEach(function(dep) {
305
- dep.module = dependantModule;
306
- dependantModule.addReason(module, dep);
307
- });
308
-
309
- _this.buildModule(dependantModule, function(err) {
310
- if(err) {
311
- return errorOrWarningAndCallback(err);
312
- }
313
-
314
- if(_this.profile) {
315
- var afterBuilding = +new Date();
316
- dependantModule.profile.building = afterBuilding - afterFactory;
317
- }
318
-
319
- if(recursive) {
320
- _this.processModuleDependencies(dependantModule, callback);
321
- } else {
322
- return callback();
323
- }
324
- });
325
-
326
- });
327
- }, function(err) {
328
- if(err) {
329
- return callback(err);
330
- }
331
-
332
- return callback();
333
- });
334
- };
335
-
336
- Compilation.prototype._addModuleChain = function process(context, dependency, onModule, callback) {
337
- var start = this.profile && +new Date();
338
-
339
- var errorAndCallback = this.bail ? function errorAndCallback(err) {
340
- callback(err);
341
- } : function errorAndCallback(err) {
342
- err.dependencies = [dependency];
343
- this.errors.push(err);
344
- callback();
345
- }.bind(this);
346
-
347
- if(typeof dependency !== "object" || dependency === null || !dependency.constructor) {
348
- throw new Error("Parameter 'dependency' must be a Dependency");
349
- }
350
-
351
- var moduleFactory = this.dependencyFactories.get(dependency.constructor);
352
- if(!moduleFactory) {
353
- throw new Error("No dependency factory available for this dependency type: " + dependency.constructor.name);
354
- }
355
-
356
- moduleFactory.create(context, dependency, function(err, module) {
357
- if(err) {
358
- return errorAndCallback(new EntryModuleNotFoundError(err));
359
- }
360
-
361
- if(this.profile) {
362
- if(!module.profile) {
363
- module.profile = {};
364
- }
365
- var afterFactory = +new Date();
366
- module.profile.factory = afterFactory - start;
367
- }
368
-
369
- var result = this.addModule(module);
370
- if(!result) {
371
- module = this.getModule(module);
372
-
373
- onModule(module);
374
-
375
- if(this.profile) {
376
- var afterBuilding = +new Date();
377
- module.profile.building = afterBuilding - afterFactory;
378
- }
379
-
380
- return callback(null, module);
381
- }
382
-
383
- if(result instanceof Module) {
384
- if(this.profile) {
385
- result.profile = module.profile;
386
- }
387
-
388
- module = result;
389
-
390
- onModule(module);
391
-
392
- moduleReady.call(this);
393
- return;
394
- }
395
-
396
- onModule(module);
397
-
398
- this.buildModule(module, function(err) {
399
- if(err) {
400
- return errorAndCallback(err);
401
- }
402
-
403
- if(this.profile) {
404
- var afterBuilding = +new Date();
405
- module.profile.building = afterBuilding - afterFactory;
406
- }
407
-
408
- moduleReady.call(this);
409
- }.bind(this));
410
-
411
- function moduleReady() {
412
- this.processModuleDependencies(module, function(err) {
413
- if(err) {
414
- return callback(err);
415
- }
416
-
417
- return callback(null, module);
418
- });
419
- }
420
- }.bind(this));
421
- };
422
-
423
- Compilation.prototype.addEntry = function process(context, entry, name, callback) {
424
- this._addModuleChain(context, entry, function(module) {
425
-
426
- entry.module = module;
427
- this.entries.push(module);
428
- module.issuer = null;
429
- module.id = 0;
430
-
431
- }.bind(this), function(err, module) {
432
- if(err) {
433
- return callback(err);
434
- }
435
-
436
- if(module) {
437
- if(module.reasons.length > 0) {
438
- return callback(new Error("module cannot be added as entry point, because it's already in the bundle"));
439
- }
440
- this.preparedChunks.push({
441
- name: name,
442
- module: module
443
- });
444
- }
445
- return callback();
446
- }.bind(this));
447
- };
448
-
449
- Compilation.prototype.prefetch = function process(context, dependency, callback) {
450
- this._addModuleChain(context, dependency, function(module) {
451
-
452
- module.prefetched = true;
453
- module.issuer = null;
454
-
455
- }, callback);
456
- };
457
-
458
- Compilation.prototype.rebuildModule = function(module, thisCallback) {
459
- if(module.variables.length || module.blocks.length)
460
- throw new Error("Cannot rebuild a complex module with variables or blocks");
461
- if(module.rebuilding) {
462
- return module.rebuilding.push(thisCallback);
463
- }
464
- var rebuilding = module.rebuilding = [thisCallback];
465
-
466
- function callback(err) {
467
- module.rebuilding = undefined;
468
- rebuilding.forEach(function(cb) {
469
- cb(err);
470
- });
471
- }
472
- var deps = module.dependencies.slice();
473
- this.buildModule(module, function(err) {
474
- if(err) return callback(err);
475
-
476
- this.processModuleDependencies(module, function(err) {
477
- if(err) return callback(err);
478
- deps.forEach(function(d) {
479
- if(d.module && d.module.removeReason(module, d)) {
480
- module.chunks.forEach(function(chunk) {
481
- if(!d.module.hasReasonForChunk(chunk)) {
482
- if(d.module.removeChunk(chunk)) {
483
- this.removeChunkFromDependencies(d.module, chunk);
484
- }
485
- }
486
- }, this);
487
- }
488
- }, this);
489
- callback();
490
- }.bind(this));
491
-
492
- }.bind(this));
493
- };
494
-
495
- Compilation.prototype.seal = function seal(callback) {
496
- this.applyPlugins("seal");
497
- this.preparedChunks.sort(function(a, b) {
498
- if(a.name < b.name) return -1;
499
- if(a.name > b.name) return 1;
500
- return 0;
501
- });
502
- this.preparedChunks.forEach(function(preparedChunk) {
503
- var module = preparedChunk.module;
504
- var chunk = this.addChunk(preparedChunk.name, module);
505
- chunk.initial = chunk.entry = true;
506
- chunk.addModule(module);
507
- module.addChunk(chunk);
508
- if(typeof module.index !== "number") {
509
- module.index = this.nextFreeModuleIndex++;
510
- }
511
- this.processDependenciesBlockForChunk(module, chunk);
512
- if(typeof module.index2 !== "number") {
513
- module.index2 = this.nextFreeModuleIndex2++;
514
- }
515
- }, this);
516
- this.sortModules(this.modules);
517
- this.applyPlugins("optimize");
518
-
519
- this.applyPlugins("optimize-modules", this.modules);
520
- this.applyPlugins("after-optimize-modules", this.modules);
521
-
522
- this.applyPlugins("optimize-chunks", this.chunks);
523
- this.applyPlugins("after-optimize-chunks", this.chunks);
524
-
525
- this.applyPluginsAsync("optimize-tree", this.chunks, this.modules, function(err) {
526
- if(err) {
527
- return callback(err);
528
- }
529
-
530
- this.applyPlugins("after-optimize-tree", this.chunks, this.modules);
531
-
532
- var shouldRecord = this.applyPluginsBailResult("should-record") !== false;
533
-
534
- this.applyPlugins("revive-modules", this.modules, this.records);
535
- this.applyPlugins("optimize-module-order", this.modules);
536
- this.applyPlugins("before-module-ids", this.modules);
537
- this.applyModuleIds();
538
- this.applyPlugins("optimize-module-ids", this.modules);
539
- this.applyPlugins("after-optimize-module-ids", this.modules);
540
- if(shouldRecord)
541
- this.applyPlugins("record-modules", this.modules, this.records);
542
-
543
- this.applyPlugins("revive-chunks", this.chunks, this.records);
544
- this.applyPlugins("optimize-chunk-order", this.chunks);
545
- this.applyPlugins("before-chunk-ids", this.chunks);
546
- this.applyChunkIds();
547
- this.applyPlugins("optimize-chunk-ids", this.chunks);
548
- this.applyPlugins("after-optimize-chunk-ids", this.chunks);
549
- if(shouldRecord)
550
- this.applyPlugins("record-chunks", this.chunks, this.records);
551
-
552
- this.sortItems();
553
- this.applyPlugins("before-hash");
554
- this.createHash();
555
- this.applyPlugins("after-hash");
556
- this.applyPlugins("before-chunk-assets");
557
- this.createChunkAssets();
558
- this.applyPlugins("additional-chunk-assets", this.chunks);
559
- this.summarizeDependencies();
560
- if(shouldRecord)
561
- this.applyPlugins("record", this, this.records);
562
-
563
- this.applyPluginsAsync("additional-assets", function(err) {
564
- if(err) {
565
- return callback(err);
566
- }
567
- this.applyPluginsAsync("optimize-chunk-assets", this.chunks, function(err) {
568
- if(err) {
569
- return callback(err);
570
- }
571
- this.applyPlugins("after-optimize-chunk-assets", this.chunks);
572
- this.applyPluginsAsync("optimize-assets", this.assets, function(err) {
573
- if(err) {
574
- return callback(err);
575
- }
576
- this.applyPlugins("after-optimize-assets", this.assets);
577
- return callback();
578
- }.bind(this));
579
- }.bind(this));
580
- }.bind(this));
581
- }.bind(this));
582
- };
583
-
584
- Compilation.prototype.sortModules = function sortModules(modules) {
585
- modules.sort(function(a, b) {
586
- if(a.index < b.index) return -1;
587
- if(a.index > b.index) return 1;
588
- return 0;
589
- });
590
- };
591
-
592
- Compilation.prototype.addChunk = function addChunk(name, module, loc) {
593
- var chunk;
594
- if(name) {
595
- if(Object.prototype.hasOwnProperty.call(this.namedChunks, name)) {
596
- chunk = this.namedChunks[name];
597
- if(module) {
598
- chunk.addOrigin(module, loc);
599
- }
600
- return chunk;
601
- }
602
- }
603
- chunk = new Chunk(name, module, loc);
604
- this.chunks.push(chunk);
605
- if(name) {
606
- this.namedChunks[name] = chunk;
607
- }
608
- return chunk;
609
- };
610
-
611
- Compilation.prototype.processDependenciesBlockForChunk = function processDependenciesBlockForChunk(block, chunk) {
612
- if(block.variables) {
613
- block.variables.forEach(function(v) {
614
- v.dependencies.forEach(iteratorDependency, this);
615
- }, this);
616
- }
617
- if(block.dependencies) {
618
- block.dependencies.forEach(iteratorDependency, this);
619
- }
620
- if(block.blocks) {
621
- block.blocks.forEach(function(b) {
622
- var c;
623
- if(!b.chunks) {
624
- c = this.addChunk(b.chunkName, b.module, b.loc);
625
- b.chunks = [c];
626
- c.addBlock(b);
627
- } else {
628
- c = b.chunks[0];
629
- }
630
- chunk.addChunk(c);
631
- c.addParent(chunk);
632
- this.processDependenciesBlockForChunk(b, c);
633
- }, this);
634
- }
635
-
636
- function iteratorDependency(d) {
637
- if(!d.module) {
638
- return;
639
- }
640
- if(typeof d.module.index !== "number") {
641
- d.module.index = this.nextFreeModuleIndex++;
642
- }
643
- if(d.weak) {
644
- return;
645
- }
646
- if(d.module.error) {
647
- d.module = null;
648
- return;
649
- }
650
- if(chunk.addModule(d.module)) {
651
- d.module.addChunk(chunk);
652
- this.processDependenciesBlockForChunk(d.module, chunk);
653
- }
654
- if(typeof d.module.index2 !== "number") {
655
- d.module.index2 = this.nextFreeModuleIndex2++;
656
- }
657
- }
658
- };
659
-
660
- Compilation.prototype.removeChunkFromDependencies = function removeChunkFromDependencies(block, chunk) {
661
- block.blocks.forEach(function(b) {
662
- b.chunks.forEach(function(c) {
663
- chunk.removeChunk(c);
664
- c.removeParent(chunk);
665
- this.removeChunkFromDependencies(b, c);
666
- }, this);
667
- }, this);
668
-
669
- function iteratorDependency(d) {
670
- if(!d.module) {
671
- return;
672
- }
673
- if(!d.module.hasReasonForChunk(chunk)) {
674
- if(d.module.removeChunk(chunk)) {
675
- this.removeChunkFromDependencies(d.module, chunk);
676
- }
677
- }
678
- }
679
- block.dependencies.forEach(iteratorDependency, this);
680
- block.variables.forEach(function(v) {
681
- v.dependencies.forEach(iteratorDependency, this);
682
- }, this);
683
-
684
- };
685
-
686
- Compilation.prototype.applyModuleIds = function applyModuleIds() {
687
- this.modules.forEach(function(module) {
688
- if(module.id === null) {
689
- module.id = this.nextFreeModuleId++;
690
- }
691
- }, this);
692
- };
693
-
694
- Compilation.prototype.applyChunkIds = function applyChunkIds() {
695
- this.chunks.forEach(function(chunk) {
696
- if(chunk.id === null) {
697
- chunk.id = this.nextFreeChunkId++;
698
- }
699
- if(!chunk.ids) {
700
- chunk.ids = [chunk.id];
701
- }
702
- }, this);
703
- };
704
-
705
- Compilation.prototype.sortItems = function sortItems() {
706
- function byId(a, b) {
707
- return a.id - b.id;
708
- }
709
- this.chunks.sort(byId);
710
- this.modules.sort(byId);
711
- this.modules.forEach(function(module) {
712
- module.chunks.sort(byId);
713
- module.reasons.sort(function(a, b) {
714
- return byId(a.module, b.module);
715
- });
716
- });
717
- this.chunks.forEach(function(chunk) {
718
- chunk.modules.sort(byId);
719
- });
720
- };
721
-
722
- Compilation.prototype.summarizeDependencies = function summarizeDependencies() {
723
- function filterDups(array) {
724
- var newArray = [];
725
- for(var i = 0; i < array.length; i++) {
726
- if(i === 0 || array[i - 1] !== array[i])
727
- newArray.push(array[i]);
728
- }
729
- return newArray;
730
- }
731
- this.fileDependencies = [];
732
- this.contextDependencies = [];
733
- this.missingDependencies = [];
734
- this.children.forEach(function(child) {
735
- this.fileDependencies = this.fileDependencies.concat(child.fileDependencies);
736
- this.contextDependencies = this.contextDependencies.concat(child.contextDependencies);
737
- this.missingDependencies = this.missingDependencies.concat(child.missingDependencies);
738
- }.bind(this));
739
- this.modules.forEach(function(module) {
740
- if(module.fileDependencies) {
741
- module.fileDependencies.forEach(function(item) {
742
- this.fileDependencies.push(item);
743
- }, this);
744
- }
745
- if(module.contextDependencies) {
746
- module.contextDependencies.forEach(function(item) {
747
- this.contextDependencies.push(item);
748
- }, this);
749
- }
750
- }, this);
751
- this.errors.forEach(function(error) {
752
- if(Array.isArray(error.missing)) {
753
- error.missing.forEach(function(item) {
754
- this.missingDependencies.push(item);
755
- }, this);
756
- }
757
- }, this);
758
- this.fileDependencies.sort();
759
- this.fileDependencies = filterDups(this.fileDependencies);
760
- this.contextDependencies.sort();
761
- this.contextDependencies = filterDups(this.contextDependencies);
762
- this.missingDependencies.sort();
763
- this.missingDependencies = filterDups(this.missingDependencies);
764
- };
765
-
766
- Compilation.prototype.createHash = function createHash() {
767
- var outputOptions = this.outputOptions;
768
- var hashFunction = outputOptions.hashFunction;
769
- var hashDigest = outputOptions.hashDigest;
770
- var hashDigestLength = outputOptions.hashDigestLength;
771
- var hash = require("crypto").createHash(hashFunction);
772
- this.mainTemplate.updateHash(hash);
773
- this.chunkTemplate.updateHash(hash);
774
- this.moduleTemplate.updateHash(hash);
775
- var i, chunk;
776
- var chunks = this.chunks.slice();
777
- chunks.sort(function(a, b) {
778
- if(a.entry && !b.entry) return 1;
779
- if(!a.entry && b.entry) return -1;
780
- return 0;
781
- });
782
- for(i = 0; i < chunks.length; i++) {
783
- chunk = chunks[i];
784
- var chunkHash = require("crypto").createHash(hashFunction);
785
- chunk.updateHash(chunkHash);
786
- if(chunk.entry) {
787
- this.mainTemplate.updateHashForChunk(chunkHash, chunk);
788
- } else {
789
- this.chunkTemplate.updateHashForChunk(chunkHash);
790
- }
791
- this.applyPlugins("chunk-hash", chunk, chunkHash);
792
- chunk.hash = chunkHash.digest(hashDigest);
793
- hash.update(chunk.hash);
794
- chunk.renderedHash = chunk.hash.substr(0, hashDigestLength);
795
- }
796
- this.fullHash = hash.digest(hashDigest);
797
- this.hash = this.fullHash.substr(0, hashDigestLength);
798
- };
799
-
800
- Compilation.prototype.modifyHash = function modifyHash(update) {
801
- var outputOptions = this.outputOptions;
802
- var hashFunction = outputOptions.hashFunction;
803
- var hashDigest = outputOptions.hashDigest;
804
- var hashDigestLength = outputOptions.hashDigestLength;
805
- var hash = require("crypto").createHash(hashFunction);
806
- hash.update(this.fullHash);
807
- hash.update(update);
808
- this.fullHash = hash.digest(hashDigest);
809
- this.hash = this.fullHash.substr(0, hashDigestLength);
810
- };
811
-
812
- Compilation.prototype.createChunkAssets = function createChunkAssets() {
813
- var outputOptions = this.outputOptions;
814
- var filename = outputOptions.filename || "bundle.js";
815
- var chunkFilename = outputOptions.chunkFilename || "[id]." + filename;
816
- var namedChunkFilename = outputOptions.namedChunkFilename || null;
817
- for(var i = 0; i < this.modules.length; i++) {
818
- var module = this.modules[i];
819
- if(module.assets) {
820
- Object.keys(module.assets).forEach(function(name) {
821
- var file = this.getPath(name);
822
- this.assets[file] = module.assets[name];
823
- this.applyPlugins("module-asset", module, file);
824
- }, this);
825
- }
826
- }
827
- for(i = 0; i < this.chunks.length; i++) {
828
- var chunk = this.chunks[i];
829
- chunk.files = [];
830
- var chunkHash = chunk.hash;
831
- var source;
832
- var file;
833
- var filenameTemplate = chunk.filenameTemplate ? chunk.filenameTemplate :
834
- chunk.initial ? filename :
835
- chunkFilename;
836
- try {
837
- var useChunkHash = !chunk.entry || (this.mainTemplate.useChunkHash && this.mainTemplate.useChunkHash(chunk));
838
- var usedHash = useChunkHash ? chunkHash : this.fullHash;
839
- if(this.cache && this.cache["c" + chunk.id] && this.cache["c" + chunk.id].hash === usedHash) {
840
- source = this.cache["c" + chunk.id].source;
841
- } else {
842
- if(chunk.entry) {
843
- source = this.mainTemplate.render(this.hash, chunk, this.moduleTemplate, this.dependencyTemplates);
844
- } else {
845
- source = this.chunkTemplate.render(chunk, this.moduleTemplate, this.dependencyTemplates);
846
- }
847
- if(this.cache) {
848
- this.cache["c" + chunk.id] = {
849
- hash: usedHash,
850
- source: source = (source instanceof CachedSource ? source : new CachedSource(source))
851
- };
852
- }
853
- }
854
- this.assets[
855
- file = this.getPath(filenameTemplate, {
856
- noChunkHash: !useChunkHash,
857
- chunk: chunk
858
- })
859
- ] = source;
860
- chunk.files.push(file);
861
- this.applyPlugins("chunk-asset", chunk, file);
862
- file = undefined;
863
- if(chunk.id !== 0 && namedChunkFilename && chunk.name) {
864
- this.assets[
865
- file = this.getPath(namedChunkFilename, {
866
- noChunkHash: !useChunkHash,
867
- chunk: chunk
868
- })
869
- ] = source;
870
- chunk.files.push(file);
871
- this.applyPlugins("chunk-asset", chunk, file);
872
- }
873
- } catch(err) {
874
- this.errors.push(new ChunkRenderError(chunk, file || filenameTemplate, err));
875
- }
876
- }
877
- };
878
-
879
- Compilation.prototype.getPath = function(filename, data) {
880
- data = data || {};
881
- data.hash = data.hash || this.hash;
882
- return this.mainTemplate.applyPluginsWaterfall("asset-path", filename, data);
883
- };
884
-
885
- Compilation.prototype.getStats = function() {
886
- return new Stats(this);
887
- };
888
-
889
- Compilation.prototype.createChildCompiler = function(name, outputOptions) {
890
- return this.compiler.createChildCompiler(this, name, outputOptions);
891
- };
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+ var async = require("async");
6
+
7
+ var Tapable = require("tapable");
8
+ var EntryModuleNotFoundError = require("./EntryModuleNotFoundError");
9
+ var ModuleNotFoundError = require("./ModuleNotFoundError");
10
+ var CriticalDependenciesWarning = require("./CriticalDependenciesWarning");
11
+ var Module = require("./Module");
12
+ var ArrayMap = require("./ArrayMap");
13
+ var Chunk = require("./Chunk");
14
+ var Stats = require("./Stats");
15
+ var MainTemplate = require("./MainTemplate");
16
+ var ChunkTemplate = require("./ChunkTemplate");
17
+ var HotUpdateChunkTemplate = require("./HotUpdateChunkTemplate");
18
+ var ModuleTemplate = require("./ModuleTemplate");
19
+ var Dependency = require("./Dependency");
20
+ var ChunkRenderError = require("./ChunkRenderError");
21
+ var CachedSource = require("webpack-core/lib/CachedSource");
22
+
23
+ function Compilation(compiler) {
24
+ Tapable.call(this);
25
+ this.compiler = compiler;
26
+ this.resolvers = compiler.resolvers;
27
+ this.inputFileSystem = compiler.inputFileSystem;
28
+
29
+ var options = this.options = compiler.options;
30
+ this.outputOptions = options && options.output;
31
+ this.bail = options && options.bail;
32
+ this.profile = options && options.profile;
33
+
34
+ this.mainTemplate = new MainTemplate(this.outputOptions);
35
+ this.chunkTemplate = new ChunkTemplate(this.outputOptions, this.mainTemplate);
36
+ this.hotUpdateChunkTemplate = new HotUpdateChunkTemplate(this.outputOptions);
37
+ this.moduleTemplate = new ModuleTemplate(this.outputOptions);
38
+
39
+ this.entries = [];
40
+ this.preparedChunks = [];
41
+ this.chunks = [];
42
+ this.namedChunks = {};
43
+ this.modules = [];
44
+ this._modules = {};
45
+ this.cache = null;
46
+ this.records = null;
47
+ this.nextFreeModuleId = 1;
48
+ this.nextFreeChunkId = 0;
49
+ this.nextFreeModuleIndex = 0;
50
+ this.nextFreeModuleIndex2 = 0;
51
+ this.additionalChunkAssets = [];
52
+ this.assets = {};
53
+ this.errors = [];
54
+ this.warnings = [];
55
+ this.children = [];
56
+ this.dependencyFactories = new ArrayMap();
57
+ this.dependencyTemplates = new ArrayMap();
58
+ }
59
+ module.exports = Compilation;
60
+
61
+ Compilation.prototype = Object.create(Tapable.prototype);
62
+ Compilation.prototype.constructor = Compilation;
63
+
64
+ Compilation.prototype.templatesPlugin = function(name, fn) {
65
+ this.mainTemplate.plugin(name, fn);
66
+ this.chunkTemplate.plugin(name, fn);
67
+ };
68
+
69
+ Compilation.prototype.addModule = function(module, cacheGroup) {
70
+ cacheGroup = cacheGroup || "m";
71
+ var identifier = module.identifier();
72
+ if(this._modules[identifier]) {
73
+ return false;
74
+ }
75
+ if(this.cache && this.cache[cacheGroup + identifier]) {
76
+ var cacheModule = this.cache[cacheGroup + identifier];
77
+
78
+ var rebuild = true;
79
+ if(!cacheModule.error && cacheModule.cacheable && this.fileTimestamps && this.contextTimestamps) {
80
+ rebuild = cacheModule.needRebuild(this.fileTimestamps, this.contextTimestamps);
81
+ }
82
+
83
+ if(!rebuild) {
84
+ cacheModule.disconnect();
85
+ this._modules[identifier] = cacheModule;
86
+ this.modules.push(cacheModule);
87
+ cacheModule.errors.forEach(function(err) {
88
+ this.errors.push(err);
89
+ }, this);
90
+ cacheModule.warnings.forEach(function(err) {
91
+ this.warnings.push(err);
92
+ }, this);
93
+ return cacheModule;
94
+ } else {
95
+ module.lastId = cacheModule.id;
96
+ }
97
+ }
98
+ this._modules[identifier] = module;
99
+ if(this.cache) {
100
+ this.cache[cacheGroup + identifier] = module;
101
+ }
102
+ this.modules.push(module);
103
+ return true;
104
+ };
105
+
106
+ Compilation.prototype.getModule = function(module) {
107
+ var identifier = module.identifier();
108
+ return this._modules[identifier];
109
+ };
110
+
111
+ Compilation.prototype.findModule = function(identifier) {
112
+ return this._modules[identifier];
113
+ };
114
+
115
+ Compilation.prototype.buildModule = function(module, thisCallback) {
116
+ this.applyPlugins("build-module", module);
117
+ if(module.building) return module.building.push(thisCallback);
118
+ var building = module.building = [thisCallback];
119
+
120
+ function callback(err) {
121
+ module.building = undefined;
122
+ building.forEach(function(cb) {
123
+ cb(err);
124
+ });
125
+ }
126
+ module.build(this.options, this, this.resolvers.normal, this.inputFileSystem, function(err) {
127
+ module.errors.forEach(function(err) {
128
+ this.errors.push(err);
129
+ }, this);
130
+ module.warnings.forEach(function(err) {
131
+ this.warnings.push(err);
132
+ }, this);
133
+ module.dependencies.sort(Dependency.compare);
134
+ if(err) {
135
+ module.error = err;
136
+ this.applyPlugins("failed-module", module);
137
+ return callback(err);
138
+ }
139
+ this.applyPlugins("succeed-module", module);
140
+ return callback();
141
+ }.bind(this));
142
+ };
143
+
144
+ Compilation.prototype.processModuleDependencies = function(module, callback) {
145
+ var dependencies = [];
146
+
147
+ function addDependency(dep) {
148
+ for(var i = 0; i < dependencies.length; i++) {
149
+ if(dep.isEqualResource(dependencies[i][0])) {
150
+ return dependencies[i].push(dep);
151
+ }
152
+ }
153
+ dependencies.push([dep]);
154
+ }
155
+
156
+ function addDependenciesBlock(block) {
157
+ if(block.dependencies) {
158
+ block.dependencies.forEach(addDependency);
159
+ }
160
+ if(block.blocks) {
161
+ block.blocks.forEach(addDependenciesBlock);
162
+ }
163
+ if(block.variables) {
164
+ block.variables.forEach(function(v) {
165
+ v.dependencies.forEach(addDependency);
166
+ });
167
+ }
168
+ }
169
+ addDependenciesBlock(module);
170
+ this.addModuleDependencies(module, dependencies, this.bail, null, true, callback);
171
+ };
172
+
173
+ Compilation.prototype.addModuleDependencies = function(module, dependencies, bail, cacheGroup, recursive, callback) {
174
+ var _this = this;
175
+ var start = _this.profile && +new Date();
176
+
177
+ var factories = [];
178
+ for(var i = 0; i < dependencies.length; i++) {
179
+ var factory = _this.dependencyFactories.get(dependencies[i][0].constructor);
180
+ if(!factory) {
181
+ return callback(new Error("No module factory available for dependency type: " + dependencies[i][0].constructor.name));
182
+ }
183
+ factories[i] = [factory, dependencies[i]];
184
+ }
185
+ async.forEach(factories, function(item, callback) {
186
+ var dependencies = item[1];
187
+ var criticalDependencies = dependencies.filter(function(d) {
188
+ return !!d.critical;
189
+ });
190
+ if(criticalDependencies.length > 0) {
191
+ _this.warnings.push(new CriticalDependenciesWarning(module, criticalDependencies));
192
+ }
193
+
194
+ var errorAndCallback = function errorAndCallback(err) {
195
+ err.dependencies = dependencies;
196
+ err.origin = module;
197
+ module.dependenciesErrors.push(err);
198
+ _this.errors.push(err);
199
+ if(bail) {
200
+ callback(err);
201
+ } else {
202
+ callback();
203
+ }
204
+ };
205
+ var warningAndCallback = function warningAndCallback(err) {
206
+ err.dependencies = dependencies;
207
+ err.origin = module;
208
+ module.dependenciesWarnings.push(err);
209
+ _this.warnings.push(err);
210
+ callback();
211
+ };
212
+
213
+ var factory = item[0];
214
+ factory.create(module.context, dependencies[0], function(err, dependantModule) {
215
+ function isOptional() {
216
+ return dependencies.filter(function(d) {
217
+ return !d.optional;
218
+ }).length === 0;
219
+ }
220
+
221
+ function errorOrWarningAndCallback(err) {
222
+ if(isOptional()) {
223
+ return warningAndCallback(err);
224
+ } else {
225
+ return errorAndCallback(err);
226
+ }
227
+ }
228
+ if(err) {
229
+ return errorOrWarningAndCallback(new ModuleNotFoundError(module, err));
230
+ }
231
+ if(!dependantModule) {
232
+ return process.nextTick(callback);
233
+ }
234
+ if(_this.profile) {
235
+ if(!dependantModule.profile) {
236
+ dependantModule.profile = {};
237
+ }
238
+ var afterFactory = +new Date();
239
+ dependantModule.profile.factory = afterFactory - start;
240
+ }
241
+
242
+ dependantModule.issuer = module.identifier();
243
+ var newModule = _this.addModule(dependantModule, cacheGroup);
244
+
245
+ if(!newModule) { // from cache
246
+ dependantModule = _this.getModule(dependantModule);
247
+
248
+ if(dependantModule.optional) {
249
+ dependantModule.optional = isOptional();
250
+ }
251
+
252
+ if(dependantModule.id === 0) {
253
+ return errorOrWarningAndCallback(
254
+ new ModuleNotFoundError(module, new Error("a dependency to an entry point is not allowed"))
255
+ );
256
+ }
257
+
258
+ dependencies.forEach(function(dep) {
259
+ dep.module = dependantModule;
260
+ dependantModule.addReason(module, dep);
261
+ });
262
+
263
+ if(_this.profile) {
264
+ if(!module.profile) {
265
+ module.profile = {};
266
+ }
267
+ var time = +new Date() - start;
268
+ if(!module.profile.dependencies || time > module.profile.dependencies) {
269
+ module.profile.dependencies = time;
270
+ }
271
+ }
272
+
273
+ return process.nextTick(callback);
274
+ }
275
+
276
+ if(newModule instanceof Module) {
277
+ if(_this.profile) {
278
+ newModule.profile = dependantModule.profile;
279
+ }
280
+
281
+ newModule.optional = isOptional();
282
+ newModule.issuer = dependantModule.issuer;
283
+ dependantModule = newModule;
284
+
285
+ dependencies.forEach(function(dep) {
286
+ dep.module = dependantModule;
287
+ dependantModule.addReason(module, dep);
288
+ });
289
+
290
+ if(_this.profile) {
291
+ var afterBuilding = +new Date();
292
+ module.profile.building = afterBuilding - afterFactory;
293
+ }
294
+
295
+ if(recursive) {
296
+ return process.nextTick(_this.processModuleDependencies.bind(_this, dependantModule, callback));
297
+ } else {
298
+ return process.nextTick(callback);
299
+ }
300
+ }
301
+
302
+ dependantModule.optional = isOptional();
303
+
304
+ dependencies.forEach(function(dep) {
305
+ dep.module = dependantModule;
306
+ dependantModule.addReason(module, dep);
307
+ });
308
+
309
+ _this.buildModule(dependantModule, function(err) {
310
+ if(err) {
311
+ return errorOrWarningAndCallback(err);
312
+ }
313
+
314
+ if(_this.profile) {
315
+ var afterBuilding = +new Date();
316
+ dependantModule.profile.building = afterBuilding - afterFactory;
317
+ }
318
+
319
+ if(recursive) {
320
+ _this.processModuleDependencies(dependantModule, callback);
321
+ } else {
322
+ return callback();
323
+ }
324
+ });
325
+
326
+ });
327
+ }, function(err) {
328
+ if(err) {
329
+ return callback(err);
330
+ }
331
+
332
+ return callback();
333
+ });
334
+ };
335
+
336
+ Compilation.prototype._addModuleChain = function process(context, dependency, onModule, callback) {
337
+ var start = this.profile && +new Date();
338
+
339
+ var errorAndCallback = this.bail ? function errorAndCallback(err) {
340
+ callback(err);
341
+ } : function errorAndCallback(err) {
342
+ err.dependencies = [dependency];
343
+ this.errors.push(err);
344
+ callback();
345
+ }.bind(this);
346
+
347
+ if(typeof dependency !== "object" || dependency === null || !dependency.constructor) {
348
+ throw new Error("Parameter 'dependency' must be a Dependency");
349
+ }
350
+
351
+ var moduleFactory = this.dependencyFactories.get(dependency.constructor);
352
+ if(!moduleFactory) {
353
+ throw new Error("No dependency factory available for this dependency type: " + dependency.constructor.name);
354
+ }
355
+
356
+ moduleFactory.create(context, dependency, function(err, module) {
357
+ if(err) {
358
+ return errorAndCallback(new EntryModuleNotFoundError(err));
359
+ }
360
+
361
+ if(this.profile) {
362
+ if(!module.profile) {
363
+ module.profile = {};
364
+ }
365
+ var afterFactory = +new Date();
366
+ module.profile.factory = afterFactory - start;
367
+ }
368
+
369
+ var result = this.addModule(module);
370
+ if(!result) {
371
+ module = this.getModule(module);
372
+
373
+ onModule(module);
374
+
375
+ if(this.profile) {
376
+ var afterBuilding = +new Date();
377
+ module.profile.building = afterBuilding - afterFactory;
378
+ }
379
+
380
+ return callback(null, module);
381
+ }
382
+
383
+ if(result instanceof Module) {
384
+ if(this.profile) {
385
+ result.profile = module.profile;
386
+ }
387
+
388
+ module = result;
389
+
390
+ onModule(module);
391
+
392
+ moduleReady.call(this);
393
+ return;
394
+ }
395
+
396
+ onModule(module);
397
+
398
+ this.buildModule(module, function(err) {
399
+ if(err) {
400
+ return errorAndCallback(err);
401
+ }
402
+
403
+ if(this.profile) {
404
+ var afterBuilding = +new Date();
405
+ module.profile.building = afterBuilding - afterFactory;
406
+ }
407
+
408
+ moduleReady.call(this);
409
+ }.bind(this));
410
+
411
+ function moduleReady() {
412
+ this.processModuleDependencies(module, function(err) {
413
+ if(err) {
414
+ return callback(err);
415
+ }
416
+
417
+ return callback(null, module);
418
+ });
419
+ }
420
+ }.bind(this));
421
+ };
422
+
423
+ Compilation.prototype.addEntry = function process(context, entry, name, callback) {
424
+ this._addModuleChain(context, entry, function(module) {
425
+
426
+ entry.module = module;
427
+ this.entries.push(module);
428
+ module.issuer = null;
429
+ module.id = 0;
430
+
431
+ }.bind(this), function(err, module) {
432
+ if(err) {
433
+ return callback(err);
434
+ }
435
+
436
+ if(module) {
437
+ if(module.reasons.length > 0) {
438
+ return callback(new Error("module cannot be added as entry point, because it's already in the bundle"));
439
+ }
440
+ this.preparedChunks.push({
441
+ name: name,
442
+ module: module
443
+ });
444
+ }
445
+ return callback();
446
+ }.bind(this));
447
+ };
448
+
449
+ Compilation.prototype.prefetch = function process(context, dependency, callback) {
450
+ this._addModuleChain(context, dependency, function(module) {
451
+
452
+ module.prefetched = true;
453
+ module.issuer = null;
454
+
455
+ }, callback);
456
+ };
457
+
458
+ Compilation.prototype.rebuildModule = function(module, thisCallback) {
459
+ if(module.variables.length || module.blocks.length)
460
+ throw new Error("Cannot rebuild a complex module with variables or blocks");
461
+ if(module.rebuilding) {
462
+ return module.rebuilding.push(thisCallback);
463
+ }
464
+ var rebuilding = module.rebuilding = [thisCallback];
465
+
466
+ function callback(err) {
467
+ module.rebuilding = undefined;
468
+ rebuilding.forEach(function(cb) {
469
+ cb(err);
470
+ });
471
+ }
472
+ var deps = module.dependencies.slice();
473
+ this.buildModule(module, function(err) {
474
+ if(err) return callback(err);
475
+
476
+ this.processModuleDependencies(module, function(err) {
477
+ if(err) return callback(err);
478
+ deps.forEach(function(d) {
479
+ if(d.module && d.module.removeReason(module, d)) {
480
+ module.chunks.forEach(function(chunk) {
481
+ if(!d.module.hasReasonForChunk(chunk)) {
482
+ if(d.module.removeChunk(chunk)) {
483
+ this.removeChunkFromDependencies(d.module, chunk);
484
+ }
485
+ }
486
+ }, this);
487
+ }
488
+ }, this);
489
+ callback();
490
+ }.bind(this));
491
+
492
+ }.bind(this));
493
+ };
494
+
495
+ Compilation.prototype.seal = function seal(callback) {
496
+ this.applyPlugins("seal");
497
+ this.preparedChunks.sort(function(a, b) {
498
+ if(a.name < b.name) return -1;
499
+ if(a.name > b.name) return 1;
500
+ return 0;
501
+ });
502
+ this.preparedChunks.forEach(function(preparedChunk) {
503
+ var module = preparedChunk.module;
504
+ var chunk = this.addChunk(preparedChunk.name, module);
505
+ chunk.initial = chunk.entry = true;
506
+ chunk.addModule(module);
507
+ module.addChunk(chunk);
508
+ if(typeof module.index !== "number") {
509
+ module.index = this.nextFreeModuleIndex++;
510
+ }
511
+ this.processDependenciesBlockForChunk(module, chunk);
512
+ if(typeof module.index2 !== "number") {
513
+ module.index2 = this.nextFreeModuleIndex2++;
514
+ }
515
+ }, this);
516
+ this.sortModules(this.modules);
517
+ this.applyPlugins("optimize");
518
+
519
+ this.applyPlugins("optimize-modules", this.modules);
520
+ this.applyPlugins("after-optimize-modules", this.modules);
521
+
522
+ this.applyPlugins("optimize-chunks", this.chunks);
523
+ this.applyPlugins("after-optimize-chunks", this.chunks);
524
+
525
+ this.applyPluginsAsync("optimize-tree", this.chunks, this.modules, function(err) {
526
+ if(err) {
527
+ return callback(err);
528
+ }
529
+
530
+ this.applyPlugins("after-optimize-tree", this.chunks, this.modules);
531
+
532
+ var shouldRecord = this.applyPluginsBailResult("should-record") !== false;
533
+
534
+ this.applyPlugins("revive-modules", this.modules, this.records);
535
+ this.applyPlugins("optimize-module-order", this.modules);
536
+ this.applyPlugins("before-module-ids", this.modules);
537
+ this.applyModuleIds();
538
+ this.applyPlugins("optimize-module-ids", this.modules);
539
+ this.applyPlugins("after-optimize-module-ids", this.modules);
540
+ if(shouldRecord)
541
+ this.applyPlugins("record-modules", this.modules, this.records);
542
+
543
+ this.applyPlugins("revive-chunks", this.chunks, this.records);
544
+ this.applyPlugins("optimize-chunk-order", this.chunks);
545
+ this.applyPlugins("before-chunk-ids", this.chunks);
546
+ this.applyChunkIds();
547
+ this.applyPlugins("optimize-chunk-ids", this.chunks);
548
+ this.applyPlugins("after-optimize-chunk-ids", this.chunks);
549
+ if(shouldRecord)
550
+ this.applyPlugins("record-chunks", this.chunks, this.records);
551
+
552
+ this.sortItems();
553
+ this.applyPlugins("before-hash");
554
+ this.createHash();
555
+ this.applyPlugins("after-hash");
556
+ this.applyPlugins("before-chunk-assets");
557
+ this.createChunkAssets();
558
+ this.applyPlugins("additional-chunk-assets", this.chunks);
559
+ this.summarizeDependencies();
560
+ if(shouldRecord)
561
+ this.applyPlugins("record", this, this.records);
562
+
563
+ this.applyPluginsAsync("additional-assets", function(err) {
564
+ if(err) {
565
+ return callback(err);
566
+ }
567
+ this.applyPluginsAsync("optimize-chunk-assets", this.chunks, function(err) {
568
+ if(err) {
569
+ return callback(err);
570
+ }
571
+ this.applyPlugins("after-optimize-chunk-assets", this.chunks);
572
+ this.applyPluginsAsync("optimize-assets", this.assets, function(err) {
573
+ if(err) {
574
+ return callback(err);
575
+ }
576
+ this.applyPlugins("after-optimize-assets", this.assets);
577
+ return callback();
578
+ }.bind(this));
579
+ }.bind(this));
580
+ }.bind(this));
581
+ }.bind(this));
582
+ };
583
+
584
+ Compilation.prototype.sortModules = function sortModules(modules) {
585
+ modules.sort(function(a, b) {
586
+ if(a.index < b.index) return -1;
587
+ if(a.index > b.index) return 1;
588
+ return 0;
589
+ });
590
+ };
591
+
592
+ Compilation.prototype.addChunk = function addChunk(name, module, loc) {
593
+ var chunk;
594
+ if(name) {
595
+ if(Object.prototype.hasOwnProperty.call(this.namedChunks, name)) {
596
+ chunk = this.namedChunks[name];
597
+ if(module) {
598
+ chunk.addOrigin(module, loc);
599
+ }
600
+ return chunk;
601
+ }
602
+ }
603
+ chunk = new Chunk(name, module, loc);
604
+ this.chunks.push(chunk);
605
+ if(name) {
606
+ this.namedChunks[name] = chunk;
607
+ }
608
+ return chunk;
609
+ };
610
+
611
+ Compilation.prototype.processDependenciesBlockForChunk = function processDependenciesBlockForChunk(block, chunk) {
612
+ if(block.variables) {
613
+ block.variables.forEach(function(v) {
614
+ v.dependencies.forEach(iteratorDependency, this);
615
+ }, this);
616
+ }
617
+ if(block.dependencies) {
618
+ block.dependencies.forEach(iteratorDependency, this);
619
+ }
620
+ if(block.blocks) {
621
+ block.blocks.forEach(function(b) {
622
+ var c;
623
+ if(!b.chunks) {
624
+ c = this.addChunk(b.chunkName, b.module, b.loc);
625
+ b.chunks = [c];
626
+ c.addBlock(b);
627
+ } else {
628
+ c = b.chunks[0];
629
+ }
630
+ chunk.addChunk(c);
631
+ c.addParent(chunk);
632
+ this.processDependenciesBlockForChunk(b, c);
633
+ }, this);
634
+ }
635
+
636
+ function iteratorDependency(d) {
637
+ if(!d.module) {
638
+ return;
639
+ }
640
+ if(typeof d.module.index !== "number") {
641
+ d.module.index = this.nextFreeModuleIndex++;
642
+ }
643
+ if(d.weak) {
644
+ return;
645
+ }
646
+ if(d.module.error) {
647
+ d.module = null;
648
+ return;
649
+ }
650
+ if(chunk.addModule(d.module)) {
651
+ d.module.addChunk(chunk);
652
+ this.processDependenciesBlockForChunk(d.module, chunk);
653
+ }
654
+ if(typeof d.module.index2 !== "number") {
655
+ d.module.index2 = this.nextFreeModuleIndex2++;
656
+ }
657
+ }
658
+ };
659
+
660
+ Compilation.prototype.removeChunkFromDependencies = function removeChunkFromDependencies(block, chunk) {
661
+ block.blocks.forEach(function(b) {
662
+ b.chunks.forEach(function(c) {
663
+ chunk.removeChunk(c);
664
+ c.removeParent(chunk);
665
+ this.removeChunkFromDependencies(b, c);
666
+ }, this);
667
+ }, this);
668
+
669
+ function iteratorDependency(d) {
670
+ if(!d.module) {
671
+ return;
672
+ }
673
+ if(!d.module.hasReasonForChunk(chunk)) {
674
+ if(d.module.removeChunk(chunk)) {
675
+ this.removeChunkFromDependencies(d.module, chunk);
676
+ }
677
+ }
678
+ }
679
+ block.dependencies.forEach(iteratorDependency, this);
680
+ block.variables.forEach(function(v) {
681
+ v.dependencies.forEach(iteratorDependency, this);
682
+ }, this);
683
+
684
+ };
685
+
686
+ Compilation.prototype.applyModuleIds = function applyModuleIds() {
687
+ this.modules.forEach(function(module) {
688
+ if(module.id === null) {
689
+ module.id = this.nextFreeModuleId++;
690
+ }
691
+ }, this);
692
+ };
693
+
694
+ Compilation.prototype.applyChunkIds = function applyChunkIds() {
695
+ this.chunks.forEach(function(chunk) {
696
+ if(chunk.id === null) {
697
+ chunk.id = this.nextFreeChunkId++;
698
+ }
699
+ if(!chunk.ids) {
700
+ chunk.ids = [chunk.id];
701
+ }
702
+ }, this);
703
+ };
704
+
705
+ Compilation.prototype.sortItems = function sortItems() {
706
+ function byId(a, b) {
707
+ return a.id - b.id;
708
+ }
709
+ this.chunks.sort(byId);
710
+ this.modules.sort(byId);
711
+ this.modules.forEach(function(module) {
712
+ module.chunks.sort(byId);
713
+ module.reasons.sort(function(a, b) {
714
+ return byId(a.module, b.module);
715
+ });
716
+ });
717
+ this.chunks.forEach(function(chunk) {
718
+ chunk.modules.sort(byId);
719
+ });
720
+ };
721
+
722
+ Compilation.prototype.summarizeDependencies = function summarizeDependencies() {
723
+ function filterDups(array) {
724
+ var newArray = [];
725
+ for(var i = 0; i < array.length; i++) {
726
+ if(i === 0 || array[i - 1] !== array[i])
727
+ newArray.push(array[i]);
728
+ }
729
+ return newArray;
730
+ }
731
+ this.fileDependencies = [];
732
+ this.contextDependencies = [];
733
+ this.missingDependencies = [];
734
+ this.children.forEach(function(child) {
735
+ this.fileDependencies = this.fileDependencies.concat(child.fileDependencies);
736
+ this.contextDependencies = this.contextDependencies.concat(child.contextDependencies);
737
+ this.missingDependencies = this.missingDependencies.concat(child.missingDependencies);
738
+ }.bind(this));
739
+ this.modules.forEach(function(module) {
740
+ if(module.fileDependencies) {
741
+ module.fileDependencies.forEach(function(item) {
742
+ this.fileDependencies.push(item);
743
+ }, this);
744
+ }
745
+ if(module.contextDependencies) {
746
+ module.contextDependencies.forEach(function(item) {
747
+ this.contextDependencies.push(item);
748
+ }, this);
749
+ }
750
+ }, this);
751
+ this.errors.forEach(function(error) {
752
+ if(Array.isArray(error.missing)) {
753
+ error.missing.forEach(function(item) {
754
+ this.missingDependencies.push(item);
755
+ }, this);
756
+ }
757
+ }, this);
758
+ this.fileDependencies.sort();
759
+ this.fileDependencies = filterDups(this.fileDependencies);
760
+ this.contextDependencies.sort();
761
+ this.contextDependencies = filterDups(this.contextDependencies);
762
+ this.missingDependencies.sort();
763
+ this.missingDependencies = filterDups(this.missingDependencies);
764
+ };
765
+
766
+ Compilation.prototype.createHash = function createHash() {
767
+ var outputOptions = this.outputOptions;
768
+ var hashFunction = outputOptions.hashFunction;
769
+ var hashDigest = outputOptions.hashDigest;
770
+ var hashDigestLength = outputOptions.hashDigestLength;
771
+ var hash = require("crypto").createHash(hashFunction);
772
+ this.mainTemplate.updateHash(hash);
773
+ this.chunkTemplate.updateHash(hash);
774
+ this.moduleTemplate.updateHash(hash);
775
+ var i, chunk;
776
+ var chunks = this.chunks.slice();
777
+ chunks.sort(function(a, b) {
778
+ if(a.entry && !b.entry) return 1;
779
+ if(!a.entry && b.entry) return -1;
780
+ return 0;
781
+ });
782
+ for(i = 0; i < chunks.length; i++) {
783
+ chunk = chunks[i];
784
+ var chunkHash = require("crypto").createHash(hashFunction);
785
+ chunk.updateHash(chunkHash);
786
+ if(chunk.entry) {
787
+ this.mainTemplate.updateHashForChunk(chunkHash, chunk);
788
+ } else {
789
+ this.chunkTemplate.updateHashForChunk(chunkHash);
790
+ }
791
+ this.applyPlugins("chunk-hash", chunk, chunkHash);
792
+ chunk.hash = chunkHash.digest(hashDigest);
793
+ hash.update(chunk.hash);
794
+ chunk.renderedHash = chunk.hash.substr(0, hashDigestLength);
795
+ }
796
+ this.fullHash = hash.digest(hashDigest);
797
+ this.hash = this.fullHash.substr(0, hashDigestLength);
798
+ };
799
+
800
+ Compilation.prototype.modifyHash = function modifyHash(update) {
801
+ var outputOptions = this.outputOptions;
802
+ var hashFunction = outputOptions.hashFunction;
803
+ var hashDigest = outputOptions.hashDigest;
804
+ var hashDigestLength = outputOptions.hashDigestLength;
805
+ var hash = require("crypto").createHash(hashFunction);
806
+ hash.update(this.fullHash);
807
+ hash.update(update);
808
+ this.fullHash = hash.digest(hashDigest);
809
+ this.hash = this.fullHash.substr(0, hashDigestLength);
810
+ };
811
+
812
+ Compilation.prototype.createChunkAssets = function createChunkAssets() {
813
+ var outputOptions = this.outputOptions;
814
+ var filename = outputOptions.filename || "bundle.js";
815
+ var chunkFilename = outputOptions.chunkFilename || "[id]." + filename;
816
+ var namedChunkFilename = outputOptions.namedChunkFilename || null;
817
+ for(var i = 0; i < this.modules.length; i++) {
818
+ var module = this.modules[i];
819
+ if(module.assets) {
820
+ Object.keys(module.assets).forEach(function(name) {
821
+ var file = this.getPath(name);
822
+ this.assets[file] = module.assets[name];
823
+ this.applyPlugins("module-asset", module, file);
824
+ }, this);
825
+ }
826
+ }
827
+ for(i = 0; i < this.chunks.length; i++) {
828
+ var chunk = this.chunks[i];
829
+ chunk.files = [];
830
+ var chunkHash = chunk.hash;
831
+ var source;
832
+ var file;
833
+ var filenameTemplate = chunk.filenameTemplate ? chunk.filenameTemplate :
834
+ chunk.initial ? filename :
835
+ chunkFilename;
836
+ try {
837
+ var useChunkHash = !chunk.entry || (this.mainTemplate.useChunkHash && this.mainTemplate.useChunkHash(chunk));
838
+ var usedHash = useChunkHash ? chunkHash : this.fullHash;
839
+ if(this.cache && this.cache["c" + chunk.id] && this.cache["c" + chunk.id].hash === usedHash) {
840
+ source = this.cache["c" + chunk.id].source;
841
+ } else {
842
+ if(chunk.entry) {
843
+ source = this.mainTemplate.render(this.hash, chunk, this.moduleTemplate, this.dependencyTemplates);
844
+ } else {
845
+ source = this.chunkTemplate.render(chunk, this.moduleTemplate, this.dependencyTemplates);
846
+ }
847
+ if(this.cache) {
848
+ this.cache["c" + chunk.id] = {
849
+ hash: usedHash,
850
+ source: source = (source instanceof CachedSource ? source : new CachedSource(source))
851
+ };
852
+ }
853
+ }
854
+ this.assets[
855
+ file = this.getPath(filenameTemplate, {
856
+ noChunkHash: !useChunkHash,
857
+ chunk: chunk
858
+ })
859
+ ] = source;
860
+ chunk.files.push(file);
861
+ this.applyPlugins("chunk-asset", chunk, file);
862
+ file = undefined;
863
+ if(chunk.id !== 0 && namedChunkFilename && chunk.name) {
864
+ this.assets[
865
+ file = this.getPath(namedChunkFilename, {
866
+ noChunkHash: !useChunkHash,
867
+ chunk: chunk
868
+ })
869
+ ] = source;
870
+ chunk.files.push(file);
871
+ this.applyPlugins("chunk-asset", chunk, file);
872
+ }
873
+ } catch(err) {
874
+ this.errors.push(new ChunkRenderError(chunk, file || filenameTemplate, err));
875
+ }
876
+ }
877
+ };
878
+
879
+ Compilation.prototype.getPath = function(filename, data) {
880
+ data = data || {};
881
+ data.hash = data.hash || this.hash;
882
+ return this.mainTemplate.applyPluginsWaterfall("asset-path", filename, data);
883
+ };
884
+
885
+ Compilation.prototype.getStats = function() {
886
+ return new Stats(this);
887
+ };
888
+
889
+ Compilation.prototype.createChildCompiler = function(name, outputOptions) {
890
+ return this.compiler.createChildCompiler(this, name, outputOptions);
891
+ };