2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
var async = require("async");
|
|
|
|
|
|
|
|
var Tapable = require("tapable");
|
|
|
|
var EntryModuleNotFoundError = require("./EntryModuleNotFoundError");
|
|
|
|
var ModuleNotFoundError = require("./ModuleNotFoundError");
|
2013-02-13 21:42:34 +08:00
|
|
|
var CriticalDependenciesWarning = require("./CriticalDependenciesWarning");
|
2013-01-31 01:49:25 +08:00
|
|
|
var Module = require("./Module");
|
|
|
|
var ArrayMap = require("./ArrayMap");
|
|
|
|
var Chunk = require("./Chunk");
|
|
|
|
var Stats = require("./Stats");
|
2014-06-03 03:23:53 +08:00
|
|
|
var MainTemplate = require("./MainTemplate");
|
2014-06-03 05:40:50 +08:00
|
|
|
var ChunkTemplate = require("./ChunkTemplate");
|
2014-06-03 06:14:46 +08:00
|
|
|
var HotUpdateChunkTemplate = require("./HotUpdateChunkTemplate");
|
2014-06-03 14:45:26 +08:00
|
|
|
var ModuleTemplate = require("./ModuleTemplate");
|
2015-05-11 00:43:47 +08:00
|
|
|
var Dependency = require("./Dependency");
|
2015-06-27 17:34:17 +08:00
|
|
|
var ChunkRenderError = require("./ChunkRenderError");
|
2015-12-30 00:44:55 +08:00
|
|
|
var CachedSource = require("webpack-sources").CachedSource;
|
2013-01-31 01:49:25 +08:00
|
|
|
|
|
|
|
function Compilation(compiler) {
|
|
|
|
Tapable.call(this);
|
|
|
|
this.compiler = compiler;
|
|
|
|
this.resolvers = compiler.resolvers;
|
|
|
|
this.inputFileSystem = compiler.inputFileSystem;
|
2014-06-03 03:23:53 +08:00
|
|
|
|
2013-02-13 21:42:34 +08:00
|
|
|
var options = this.options = compiler.options;
|
2013-01-31 01:49:25 +08:00
|
|
|
this.outputOptions = options && options.output;
|
|
|
|
this.bail = options && options.bail;
|
2013-05-08 20:47:13 +08:00
|
|
|
this.profile = options && options.profile;
|
2014-06-03 03:23:53 +08:00
|
|
|
|
|
|
|
this.mainTemplate = new MainTemplate(this.outputOptions);
|
2014-12-22 23:10:23 +08:00
|
|
|
this.chunkTemplate = new ChunkTemplate(this.outputOptions, this.mainTemplate);
|
2014-06-03 06:14:46 +08:00
|
|
|
this.hotUpdateChunkTemplate = new HotUpdateChunkTemplate(this.outputOptions);
|
2014-06-03 14:45:26 +08:00
|
|
|
this.moduleTemplate = new ModuleTemplate(this.outputOptions);
|
2014-06-03 03:23:53 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
this.entries = [];
|
2013-05-22 16:12:53 +08:00
|
|
|
this.preparedChunks = [];
|
2013-01-31 01:49:25 +08:00
|
|
|
this.chunks = [];
|
|
|
|
this.namedChunks = {};
|
|
|
|
this.modules = [];
|
|
|
|
this._modules = {};
|
|
|
|
this.cache = null;
|
2013-05-31 18:22:40 +08:00
|
|
|
this.records = null;
|
2015-06-13 17:45:28 +08:00
|
|
|
this.nextFreeModuleId = 0;
|
2014-02-11 20:27:41 +08:00
|
|
|
this.nextFreeChunkId = 0;
|
2015-05-11 00:43:47 +08:00
|
|
|
this.nextFreeModuleIndex = 0;
|
|
|
|
this.nextFreeModuleIndex2 = 0;
|
2013-07-01 19:59:02 +08:00
|
|
|
this.additionalChunkAssets = [];
|
2013-01-31 01:49:25 +08:00
|
|
|
this.assets = {};
|
|
|
|
this.errors = [];
|
|
|
|
this.warnings = [];
|
|
|
|
this.children = [];
|
|
|
|
this.dependencyFactories = new ArrayMap();
|
|
|
|
this.dependencyTemplates = new ArrayMap();
|
|
|
|
}
|
|
|
|
module.exports = Compilation;
|
|
|
|
|
|
|
|
Compilation.prototype = Object.create(Tapable.prototype);
|
2015-08-18 19:35:57 +08:00
|
|
|
Compilation.prototype.constructor = Compilation;
|
|
|
|
|
2014-12-22 23:10:23 +08:00
|
|
|
Compilation.prototype.templatesPlugin = function(name, fn) {
|
|
|
|
this.mainTemplate.plugin(name, fn);
|
|
|
|
this.chunkTemplate.plugin(name, fn);
|
|
|
|
};
|
|
|
|
|
2013-10-28 23:21:29 +08:00
|
|
|
Compilation.prototype.addModule = function(module, cacheGroup) {
|
|
|
|
cacheGroup = cacheGroup || "m";
|
2013-01-31 01:49:25 +08:00
|
|
|
var identifier = module.identifier();
|
2014-06-04 03:03:21 +08:00
|
|
|
if(this._modules[identifier]) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-28 23:21:29 +08:00
|
|
|
if(this.cache && this.cache[cacheGroup + identifier]) {
|
|
|
|
var cacheModule = this.cache[cacheGroup + identifier];
|
2013-05-08 19:28:54 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
var rebuild = true;
|
|
|
|
if(!cacheModule.error && cacheModule.cacheable && this.fileTimestamps && this.contextTimestamps) {
|
|
|
|
rebuild = cacheModule.needRebuild(this.fileTimestamps, this.contextTimestamps);
|
|
|
|
}
|
2013-02-01 01:00:22 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
if(!rebuild) {
|
|
|
|
cacheModule.disconnect();
|
|
|
|
this._modules[identifier] = cacheModule;
|
|
|
|
this.modules.push(cacheModule);
|
2013-03-05 18:05:13 +08:00
|
|
|
cacheModule.errors.forEach(function(err) {
|
|
|
|
this.errors.push(err);
|
|
|
|
}, this);
|
|
|
|
cacheModule.warnings.forEach(function(err) {
|
|
|
|
this.warnings.push(err);
|
|
|
|
}, this);
|
2013-01-31 01:49:25 +08:00
|
|
|
return cacheModule;
|
2013-05-08 19:28:54 +08:00
|
|
|
} else {
|
|
|
|
module.lastId = cacheModule.id;
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this._modules[identifier] = module;
|
2014-06-04 03:03:21 +08:00
|
|
|
if(this.cache) {
|
|
|
|
this.cache[cacheGroup + identifier] = module;
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
this.modules.push(module);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
Compilation.prototype.getModule = function(module) {
|
|
|
|
var identifier = module.identifier();
|
|
|
|
return this._modules[identifier];
|
|
|
|
};
|
|
|
|
|
2013-03-26 23:54:41 +08:00
|
|
|
Compilation.prototype.findModule = function(identifier) {
|
|
|
|
return this._modules[identifier];
|
|
|
|
};
|
|
|
|
|
2013-10-29 21:14:16 +08:00
|
|
|
Compilation.prototype.buildModule = function(module, thisCallback) {
|
2013-01-31 01:49:25 +08:00
|
|
|
this.applyPlugins("build-module", module);
|
2014-07-29 06:13:25 +08:00
|
|
|
if(module.building) return module.building.push(thisCallback);
|
2013-10-29 21:14:16 +08:00
|
|
|
var building = module.building = [thisCallback];
|
2015-07-08 20:15:21 +08:00
|
|
|
|
2013-10-29 21:14:16 +08:00
|
|
|
function callback(err) {
|
|
|
|
module.building = undefined;
|
|
|
|
building.forEach(function(cb) {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
module.build(this.options, this, this.resolvers.normal, this.inputFileSystem, function(err) {
|
2013-02-19 19:48:17 +08:00
|
|
|
module.errors.forEach(function(err) {
|
|
|
|
this.errors.push(err);
|
|
|
|
}, this);
|
|
|
|
module.warnings.forEach(function(err) {
|
|
|
|
this.warnings.push(err);
|
|
|
|
}, this);
|
2015-05-11 00:43:47 +08:00
|
|
|
module.dependencies.sort(Dependency.compare);
|
2013-01-31 01:49:25 +08:00
|
|
|
if(err) {
|
2013-05-18 20:42:11 +08:00
|
|
|
module.error = err;
|
2013-01-31 01:49:25 +08:00
|
|
|
this.applyPlugins("failed-module", module);
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
this.applyPlugins("succeed-module", module);
|
|
|
|
return callback();
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
Compilation.prototype.processModuleDependencies = function(module, callback) {
|
|
|
|
var dependencies = [];
|
2015-07-08 20:15:21 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
function addDependency(dep) {
|
|
|
|
for(var i = 0; i < dependencies.length; i++) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(dep.isEqualResource(dependencies[i][0])) {
|
2013-01-31 01:49:25 +08:00
|
|
|
return dependencies[i].push(dep);
|
2014-06-04 03:03:21 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
dependencies.push([dep]);
|
|
|
|
}
|
2015-07-08 20:15:21 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
function addDependenciesBlock(block) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(block.dependencies) {
|
|
|
|
block.dependencies.forEach(addDependency);
|
|
|
|
}
|
|
|
|
if(block.blocks) {
|
|
|
|
block.blocks.forEach(addDependenciesBlock);
|
|
|
|
}
|
|
|
|
if(block.variables) {
|
|
|
|
block.variables.forEach(function(v) {
|
|
|
|
v.dependencies.forEach(addDependency);
|
|
|
|
});
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
addDependenciesBlock(module);
|
2013-10-28 23:21:29 +08:00
|
|
|
this.addModuleDependencies(module, dependencies, this.bail, null, true, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
Compilation.prototype.addModuleDependencies = function(module, dependencies, bail, cacheGroup, recursive, callback) {
|
2015-11-11 06:31:03 +08:00
|
|
|
var _this = this;
|
|
|
|
var start = _this.profile && +new Date();
|
2015-02-05 06:21:22 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
var factories = [];
|
|
|
|
for(var i = 0; i < dependencies.length; i++) {
|
2015-11-11 06:31:03 +08:00
|
|
|
var factory = _this.dependencyFactories.get(dependencies[i][0].constructor);
|
2014-06-04 03:03:21 +08:00
|
|
|
if(!factory) {
|
2015-10-18 16:53:38 +08:00
|
|
|
return callback(new Error("No module factory available for dependency type: " + dependencies[i][0].constructor.name));
|
2014-06-04 03:03:21 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
factories[i] = [factory, dependencies[i]];
|
|
|
|
}
|
|
|
|
async.forEach(factories, function(item, callback) {
|
|
|
|
var dependencies = item[1];
|
2014-06-04 03:03:21 +08:00
|
|
|
var criticalDependencies = dependencies.filter(function(d) {
|
|
|
|
return !!d.critical;
|
|
|
|
});
|
2013-02-13 21:42:34 +08:00
|
|
|
if(criticalDependencies.length > 0) {
|
2015-11-11 06:31:03 +08:00
|
|
|
_this.warnings.push(new CriticalDependenciesWarning(module, criticalDependencies));
|
2013-02-13 21:42:34 +08:00
|
|
|
}
|
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
var errorAndCallback = function errorAndCallback(err) {
|
|
|
|
err.dependencies = dependencies;
|
|
|
|
err.origin = module;
|
2013-05-18 20:42:11 +08:00
|
|
|
module.dependenciesErrors.push(err);
|
2015-11-11 06:31:03 +08:00
|
|
|
_this.errors.push(err);
|
2014-06-04 03:03:21 +08:00
|
|
|
if(bail) {
|
2013-10-28 23:21:29 +08:00
|
|
|
callback(err);
|
2014-06-04 03:03:21 +08:00
|
|
|
} else {
|
2013-10-28 23:21:29 +08:00
|
|
|
callback();
|
2014-06-04 03:03:21 +08:00
|
|
|
}
|
2015-11-11 06:31:03 +08:00
|
|
|
};
|
2013-01-31 01:49:25 +08:00
|
|
|
var warningAndCallback = function warningAndCallback(err) {
|
|
|
|
err.dependencies = dependencies;
|
|
|
|
err.origin = module;
|
2013-05-18 20:42:11 +08:00
|
|
|
module.dependenciesWarnings.push(err);
|
2015-11-11 06:31:03 +08:00
|
|
|
_this.warnings.push(err);
|
2013-01-31 01:49:25 +08:00
|
|
|
callback();
|
2015-11-11 06:31:03 +08:00
|
|
|
};
|
2013-01-31 01:49:25 +08:00
|
|
|
|
|
|
|
var factory = item[0];
|
|
|
|
factory.create(module.context, dependencies[0], function(err, dependantModule) {
|
|
|
|
function isOptional() {
|
2014-06-04 03:03:21 +08:00
|
|
|
return dependencies.filter(function(d) {
|
|
|
|
return !d.optional;
|
2014-06-25 00:53:32 +08:00
|
|
|
}).length === 0;
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
2015-07-08 20:15:21 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
function errorOrWarningAndCallback(err) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(isOptional()) {
|
2013-01-31 01:49:25 +08:00
|
|
|
return warningAndCallback(err);
|
2014-06-04 03:03:21 +08:00
|
|
|
} else {
|
2013-02-13 21:42:34 +08:00
|
|
|
return errorAndCallback(err);
|
2014-06-04 03:03:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(err) {
|
|
|
|
return errorOrWarningAndCallback(new ModuleNotFoundError(module, err));
|
|
|
|
}
|
|
|
|
if(!dependantModule) {
|
2015-04-04 05:39:58 +08:00
|
|
|
return process.nextTick(callback);
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
2015-11-11 06:31:03 +08:00
|
|
|
if(_this.profile) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(!dependantModule.profile) {
|
|
|
|
dependantModule.profile = {};
|
|
|
|
}
|
2013-05-08 20:47:13 +08:00
|
|
|
var afterFactory = +new Date();
|
|
|
|
dependantModule.profile.factory = afterFactory - start;
|
|
|
|
}
|
2013-05-08 19:28:54 +08:00
|
|
|
|
2013-05-08 20:47:13 +08:00
|
|
|
dependantModule.issuer = module.identifier();
|
2015-11-11 06:31:03 +08:00
|
|
|
var newModule = _this.addModule(dependantModule, cacheGroup);
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2014-02-04 01:12:19 +08:00
|
|
|
if(!newModule) { // from cache
|
2015-11-11 06:31:03 +08:00
|
|
|
dependantModule = _this.getModule(dependantModule);
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2014-07-03 06:00:06 +08:00
|
|
|
if(dependantModule.optional) {
|
|
|
|
dependantModule.optional = isOptional();
|
|
|
|
}
|
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
dependencies.forEach(function(dep) {
|
|
|
|
dep.module = dependantModule;
|
|
|
|
dependantModule.addReason(module, dep);
|
|
|
|
});
|
|
|
|
|
2015-11-11 06:31:03 +08:00
|
|
|
if(_this.profile) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(!module.profile) {
|
|
|
|
module.profile = {};
|
|
|
|
}
|
2013-05-08 20:47:13 +08:00
|
|
|
var time = +new Date() - start;
|
2014-06-04 03:03:21 +08:00
|
|
|
if(!module.profile.dependencies || time > module.profile.dependencies) {
|
2013-05-08 20:47:13 +08:00
|
|
|
module.profile.dependencies = time;
|
2014-06-04 03:03:21 +08:00
|
|
|
}
|
2013-05-08 20:47:13 +08:00
|
|
|
}
|
|
|
|
|
2015-04-04 05:39:58 +08:00
|
|
|
return process.nextTick(callback);
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
|
2014-02-04 01:12:19 +08:00
|
|
|
if(newModule instanceof Module) {
|
2015-11-11 06:31:03 +08:00
|
|
|
if(_this.profile) {
|
2013-05-08 20:47:13 +08:00
|
|
|
newModule.profile = dependantModule.profile;
|
2014-06-04 03:03:21 +08:00
|
|
|
}
|
2013-05-08 20:47:13 +08:00
|
|
|
|
2014-07-03 06:00:06 +08:00
|
|
|
newModule.optional = isOptional();
|
2013-05-08 20:47:13 +08:00
|
|
|
newModule.issuer = dependantModule.issuer;
|
2013-01-31 01:49:25 +08:00
|
|
|
dependantModule = newModule;
|
2013-05-08 19:28:54 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
dependencies.forEach(function(dep) {
|
|
|
|
dep.module = dependantModule;
|
|
|
|
dependantModule.addReason(module, dep);
|
|
|
|
});
|
|
|
|
|
2015-11-11 06:31:03 +08:00
|
|
|
if(_this.profile) {
|
2015-02-05 06:21:22 +08:00
|
|
|
var afterBuilding = +new Date();
|
|
|
|
module.profile.building = afterBuilding - afterFactory;
|
|
|
|
}
|
|
|
|
|
2014-06-04 03:03:21 +08:00
|
|
|
if(recursive) {
|
2015-11-11 06:31:03 +08:00
|
|
|
return process.nextTick(_this.processModuleDependencies.bind(_this, dependantModule, callback));
|
2014-06-04 03:03:21 +08:00
|
|
|
} else {
|
2015-04-04 05:39:58 +08:00
|
|
|
return process.nextTick(callback);
|
2014-06-04 03:03:21 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
2013-05-08 19:28:54 +08:00
|
|
|
|
2014-07-03 06:00:06 +08:00
|
|
|
dependantModule.optional = isOptional();
|
|
|
|
|
2013-05-18 20:42:11 +08:00
|
|
|
dependencies.forEach(function(dep) {
|
|
|
|
dep.module = dependantModule;
|
|
|
|
dependantModule.addReason(module, dep);
|
|
|
|
});
|
|
|
|
|
2015-11-11 06:31:03 +08:00
|
|
|
_this.buildModule(dependantModule, function(err) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(err) {
|
|
|
|
return errorOrWarningAndCallback(err);
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2015-11-11 06:31:03 +08:00
|
|
|
if(_this.profile) {
|
2013-05-08 20:47:13 +08:00
|
|
|
var afterBuilding = +new Date();
|
|
|
|
dependantModule.profile.building = afterBuilding - afterFactory;
|
|
|
|
}
|
|
|
|
|
2014-06-04 03:03:21 +08:00
|
|
|
if(recursive) {
|
2015-11-11 06:31:03 +08:00
|
|
|
_this.processModuleDependencies(dependantModule, callback);
|
2014-06-04 03:03:21 +08:00
|
|
|
} else {
|
2013-10-28 23:21:29 +08:00
|
|
|
return callback();
|
2014-06-04 03:03:21 +08:00
|
|
|
}
|
2015-11-11 06:31:03 +08:00
|
|
|
});
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2015-11-11 06:31:03 +08:00
|
|
|
});
|
|
|
|
}, function(err) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(err) {
|
2014-07-07 22:11:40 +08:00
|
|
|
return callback(err);
|
2014-06-04 03:03:21 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
|
|
|
return callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-05-13 19:34:00 +08:00
|
|
|
Compilation.prototype._addModuleChain = function process(context, dependency, onModule, callback) {
|
2015-02-05 06:21:22 +08:00
|
|
|
var start = this.profile && +new Date();
|
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
var errorAndCallback = this.bail ? function errorAndCallback(err) {
|
|
|
|
callback(err);
|
|
|
|
} : function errorAndCallback(err) {
|
2013-05-13 19:34:00 +08:00
|
|
|
err.dependencies = [dependency];
|
2013-01-31 01:49:25 +08:00
|
|
|
this.errors.push(err);
|
|
|
|
callback();
|
|
|
|
}.bind(this);
|
|
|
|
|
2015-10-18 16:53:38 +08:00
|
|
|
if(typeof dependency !== "object" || dependency === null || !dependency.constructor) {
|
2013-05-13 19:34:00 +08:00
|
|
|
throw new Error("Parameter 'dependency' must be a Dependency");
|
2014-06-04 03:03:21 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2015-10-18 16:53:38 +08:00
|
|
|
var moduleFactory = this.dependencyFactories.get(dependency.constructor);
|
2014-06-04 03:03:21 +08:00
|
|
|
if(!moduleFactory) {
|
2015-10-18 16:53:38 +08:00
|
|
|
throw new Error("No dependency factory available for this dependency type: " + dependency.constructor.name);
|
2014-06-04 03:03:21 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2013-05-13 19:34:00 +08:00
|
|
|
moduleFactory.create(context, dependency, function(err, module) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(err) {
|
|
|
|
return errorAndCallback(new EntryModuleNotFoundError(err));
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2013-05-13 19:34:00 +08:00
|
|
|
if(this.profile) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(!module.profile) {
|
|
|
|
module.profile = {};
|
|
|
|
}
|
2013-05-13 19:34:00 +08:00
|
|
|
var afterFactory = +new Date();
|
|
|
|
module.profile.factory = afterFactory - start;
|
|
|
|
}
|
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
var result = this.addModule(module);
|
|
|
|
if(!result) {
|
2013-05-13 19:34:00 +08:00
|
|
|
module = this.getModule(module);
|
|
|
|
|
|
|
|
onModule(module);
|
|
|
|
|
2015-02-05 06:21:22 +08:00
|
|
|
if(this.profile) {
|
|
|
|
var afterBuilding = +new Date();
|
|
|
|
module.profile.building = afterBuilding - afterFactory;
|
|
|
|
}
|
|
|
|
|
2013-05-13 19:34:00 +08:00
|
|
|
return callback(null, module);
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
2013-05-08 19:28:54 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
if(result instanceof Module) {
|
2013-05-13 19:34:00 +08:00
|
|
|
if(this.profile) {
|
|
|
|
result.profile = module.profile;
|
|
|
|
}
|
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
module = result;
|
2015-05-11 00:43:47 +08:00
|
|
|
|
|
|
|
onModule(module);
|
|
|
|
|
|
|
|
moduleReady.call(this);
|
|
|
|
return;
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
|
2013-05-13 19:34:00 +08:00
|
|
|
onModule(module);
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2015-05-11 00:43:47 +08:00
|
|
|
this.buildModule(module, function(err) {
|
|
|
|
if(err) {
|
|
|
|
return errorAndCallback(err);
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2015-05-11 00:43:47 +08:00
|
|
|
if(this.profile) {
|
|
|
|
var afterBuilding = +new Date();
|
|
|
|
module.profile.building = afterBuilding - afterFactory;
|
|
|
|
}
|
2013-05-13 19:34:00 +08:00
|
|
|
|
2015-05-11 00:43:47 +08:00
|
|
|
moduleReady.call(this);
|
|
|
|
}.bind(this));
|
2013-05-08 19:28:54 +08:00
|
|
|
|
2013-05-13 19:34:00 +08:00
|
|
|
function moduleReady() {
|
2013-01-31 01:49:25 +08:00
|
|
|
this.processModuleDependencies(module, function(err) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2013-05-13 19:34:00 +08:00
|
|
|
return callback(null, module);
|
2015-04-24 05:55:50 +08:00
|
|
|
});
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
2013-05-13 19:34:00 +08:00
|
|
|
Compilation.prototype.addEntry = function process(context, entry, name, callback) {
|
|
|
|
this._addModuleChain(context, entry, function(module) {
|
|
|
|
|
2014-05-27 05:53:13 +08:00
|
|
|
entry.module = module;
|
2013-05-13 19:34:00 +08:00
|
|
|
this.entries.push(module);
|
2015-02-05 06:21:04 +08:00
|
|
|
module.issuer = null;
|
2015-06-13 17:45:28 +08:00
|
|
|
module.entry = true;
|
2013-05-13 19:34:00 +08:00
|
|
|
|
|
|
|
}.bind(this), function(err, module) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2013-05-13 19:34:00 +08:00
|
|
|
|
|
|
|
if(module) {
|
2013-05-22 16:12:53 +08:00
|
|
|
this.preparedChunks.push({
|
|
|
|
name: name,
|
|
|
|
module: module
|
|
|
|
});
|
2013-05-13 19:34:00 +08:00
|
|
|
}
|
|
|
|
return callback();
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
Compilation.prototype.prefetch = function process(context, dependency, callback) {
|
|
|
|
this._addModuleChain(context, dependency, function(module) {
|
|
|
|
|
|
|
|
module.prefetched = true;
|
2015-02-05 06:21:04 +08:00
|
|
|
module.issuer = null;
|
2013-05-13 19:34:00 +08:00
|
|
|
|
|
|
|
}, callback);
|
|
|
|
};
|
|
|
|
|
2014-07-29 06:13:25 +08:00
|
|
|
Compilation.prototype.rebuildModule = function(module, thisCallback) {
|
2014-07-19 20:32:48 +08:00
|
|
|
if(module.variables.length || module.blocks.length)
|
|
|
|
throw new Error("Cannot rebuild a complex module with variables or blocks");
|
2014-07-29 06:13:25 +08:00
|
|
|
if(module.rebuilding) {
|
|
|
|
return module.rebuilding.push(thisCallback);
|
|
|
|
}
|
|
|
|
var rebuilding = module.rebuilding = [thisCallback];
|
2015-07-08 20:15:21 +08:00
|
|
|
|
2014-07-29 06:13:25 +08:00
|
|
|
function callback(err) {
|
|
|
|
module.rebuilding = undefined;
|
|
|
|
rebuilding.forEach(function(cb) {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
2014-07-19 20:32:48 +08:00
|
|
|
var deps = module.dependencies.slice();
|
|
|
|
this.buildModule(module, function(err) {
|
|
|
|
if(err) return callback(err);
|
|
|
|
|
|
|
|
this.processModuleDependencies(module, function(err) {
|
|
|
|
if(err) return callback(err);
|
|
|
|
deps.forEach(function(d) {
|
|
|
|
if(d.module && d.module.removeReason(module, d)) {
|
|
|
|
module.chunks.forEach(function(chunk) {
|
|
|
|
if(!d.module.hasReasonForChunk(chunk)) {
|
|
|
|
if(d.module.removeChunk(chunk)) {
|
|
|
|
this.removeChunkFromDependencies(d.module, chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
callback();
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
Compilation.prototype.seal = function seal(callback) {
|
|
|
|
this.applyPlugins("seal");
|
2015-06-13 17:52:29 +08:00
|
|
|
this.preparedChunks.sort(function(a, b) {
|
|
|
|
if(a.name < b.name) return -1;
|
|
|
|
if(a.name > b.name) return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
2013-05-22 16:12:53 +08:00
|
|
|
this.preparedChunks.forEach(function(preparedChunk) {
|
|
|
|
var module = preparedChunk.module;
|
2014-01-23 22:31:40 +08:00
|
|
|
var chunk = this.addChunk(preparedChunk.name, module);
|
2013-12-04 06:11:14 +08:00
|
|
|
chunk.initial = chunk.entry = true;
|
2013-05-22 16:12:53 +08:00
|
|
|
chunk.addModule(module);
|
|
|
|
module.addChunk(chunk);
|
2015-06-13 17:45:28 +08:00
|
|
|
chunk.entryModule = module;
|
2015-05-26 02:46:23 +08:00
|
|
|
if(typeof module.index !== "number") {
|
|
|
|
module.index = this.nextFreeModuleIndex++;
|
|
|
|
}
|
2013-05-22 16:12:53 +08:00
|
|
|
this.processDependenciesBlockForChunk(module, chunk);
|
2015-05-26 02:46:23 +08:00
|
|
|
if(typeof module.index2 !== "number") {
|
|
|
|
module.index2 = this.nextFreeModuleIndex2++;
|
|
|
|
}
|
2013-05-22 16:12:53 +08:00
|
|
|
}, this);
|
2015-05-11 00:43:47 +08:00
|
|
|
this.sortModules(this.modules);
|
2013-01-31 01:49:25 +08:00
|
|
|
this.applyPlugins("optimize");
|
2013-05-31 18:22:40 +08:00
|
|
|
|
2015-07-07 06:11:13 +08:00
|
|
|
while(this.applyPluginsBailResult("optimize-modules-basic", this.modules) ||
|
|
|
|
this.applyPluginsBailResult("optimize-modules", this.modules) ||
|
|
|
|
this.applyPluginsBailResult("optimize-modules-advanced", this.modules)); // eslint-disable-line no-extra-semi
|
2013-01-31 01:49:25 +08:00
|
|
|
this.applyPlugins("after-optimize-modules", this.modules);
|
2013-05-31 18:22:40 +08:00
|
|
|
|
2015-07-07 06:11:13 +08:00
|
|
|
while(this.applyPluginsBailResult("optimize-chunks-basic", this.chunks) ||
|
|
|
|
this.applyPluginsBailResult("optimize-chunks", this.chunks) ||
|
|
|
|
this.applyPluginsBailResult("optimize-chunks-advanced", this.chunks)); // eslint-disable-line no-extra-semi
|
2013-01-31 01:49:25 +08:00
|
|
|
this.applyPlugins("after-optimize-chunks", this.chunks);
|
2013-05-31 18:22:40 +08:00
|
|
|
|
2014-04-18 18:56:19 +08:00
|
|
|
this.applyPluginsAsync("optimize-tree", this.chunks, this.modules, function(err) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2014-01-29 17:13:28 +08:00
|
|
|
|
2015-04-21 01:39:02 +08:00
|
|
|
this.applyPlugins("after-optimize-tree", this.chunks, this.modules);
|
|
|
|
|
2014-09-03 20:16:17 +08:00
|
|
|
var shouldRecord = this.applyPluginsBailResult("should-record") !== false;
|
|
|
|
|
2014-01-29 17:13:28 +08:00
|
|
|
this.applyPlugins("revive-modules", this.modules, this.records);
|
|
|
|
this.applyPlugins("optimize-module-order", this.modules);
|
2015-06-28 04:47:51 +08:00
|
|
|
this.applyPlugins("before-module-ids", this.modules);
|
2014-01-29 17:13:28 +08:00
|
|
|
this.applyModuleIds();
|
|
|
|
this.applyPlugins("optimize-module-ids", this.modules);
|
|
|
|
this.applyPlugins("after-optimize-module-ids", this.modules);
|
2014-09-03 20:16:17 +08:00
|
|
|
if(shouldRecord)
|
|
|
|
this.applyPlugins("record-modules", this.modules, this.records);
|
2014-01-29 17:13:28 +08:00
|
|
|
|
|
|
|
this.applyPlugins("revive-chunks", this.chunks, this.records);
|
|
|
|
this.applyPlugins("optimize-chunk-order", this.chunks);
|
2015-06-28 04:47:51 +08:00
|
|
|
this.applyPlugins("before-chunk-ids", this.chunks);
|
2014-01-29 17:13:28 +08:00
|
|
|
this.applyChunkIds();
|
|
|
|
this.applyPlugins("optimize-chunk-ids", this.chunks);
|
|
|
|
this.applyPlugins("after-optimize-chunk-ids", this.chunks);
|
2014-09-03 20:16:17 +08:00
|
|
|
if(shouldRecord)
|
|
|
|
this.applyPlugins("record-chunks", this.chunks, this.records);
|
2014-01-29 17:13:28 +08:00
|
|
|
|
|
|
|
this.sortItems();
|
|
|
|
this.applyPlugins("before-hash");
|
|
|
|
this.createHash();
|
|
|
|
this.applyPlugins("after-hash");
|
2015-01-31 23:19:36 +08:00
|
|
|
this.applyPlugins("before-module-assets");
|
|
|
|
this.createModuleAssets();
|
2015-01-30 07:46:52 +08:00
|
|
|
if(this.applyPluginsBailResult("should-generate-chunk-assets") !== false) {
|
|
|
|
this.applyPlugins("before-chunk-assets");
|
|
|
|
this.createChunkAssets();
|
|
|
|
}
|
2014-01-29 17:13:28 +08:00
|
|
|
this.applyPlugins("additional-chunk-assets", this.chunks);
|
|
|
|
this.summarizeDependencies();
|
2014-09-03 20:16:17 +08:00
|
|
|
if(shouldRecord)
|
|
|
|
this.applyPlugins("record", this, this.records);
|
2014-01-29 17:13:28 +08:00
|
|
|
|
2014-04-18 18:56:19 +08:00
|
|
|
this.applyPluginsAsync("additional-assets", function(err) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2014-04-18 18:56:19 +08:00
|
|
|
this.applyPluginsAsync("optimize-chunk-assets", this.chunks, function(err) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2014-04-18 18:56:19 +08:00
|
|
|
this.applyPlugins("after-optimize-chunk-assets", this.chunks);
|
|
|
|
this.applyPluginsAsync("optimize-assets", this.assets, function(err) {
|
2014-06-04 03:03:21 +08:00
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2014-04-18 18:56:19 +08:00
|
|
|
this.applyPlugins("after-optimize-assets", this.assets);
|
|
|
|
return callback();
|
|
|
|
}.bind(this));
|
2014-01-29 17:13:28 +08:00
|
|
|
}.bind(this));
|
2013-01-31 01:49:25 +08:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
2015-05-11 00:43:47 +08:00
|
|
|
Compilation.prototype.sortModules = function sortModules(modules) {
|
|
|
|
modules.sort(function(a, b) {
|
|
|
|
if(a.index < b.index) return -1;
|
|
|
|
if(a.index > b.index) return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-01-23 22:31:40 +08:00
|
|
|
Compilation.prototype.addChunk = function addChunk(name, module, loc) {
|
2014-06-04 03:03:21 +08:00
|
|
|
var chunk;
|
2013-01-31 01:49:25 +08:00
|
|
|
if(name) {
|
2014-01-24 20:32:58 +08:00
|
|
|
if(Object.prototype.hasOwnProperty.call(this.namedChunks, name)) {
|
2014-06-04 03:03:21 +08:00
|
|
|
chunk = this.namedChunks[name];
|
|
|
|
if(module) {
|
|
|
|
chunk.addOrigin(module, loc);
|
|
|
|
}
|
2014-01-24 20:32:58 +08:00
|
|
|
return chunk;
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
2014-06-04 03:03:21 +08:00
|
|
|
chunk = new Chunk(name, module, loc);
|
2013-01-31 01:49:25 +08:00
|
|
|
this.chunks.push(chunk);
|
|
|
|
if(name) {
|
|
|
|
this.namedChunks[name] = chunk;
|
|
|
|
}
|
|
|
|
return chunk;
|
|
|
|
};
|
|
|
|
|
|
|
|
Compilation.prototype.processDependenciesBlockForChunk = function processDependenciesBlockForChunk(block, chunk) {
|
2015-05-11 00:43:47 +08:00
|
|
|
if(block.variables) {
|
|
|
|
block.variables.forEach(function(v) {
|
|
|
|
v.dependencies.forEach(iteratorDependency, this);
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
if(block.dependencies) {
|
|
|
|
block.dependencies.forEach(iteratorDependency, this);
|
|
|
|
}
|
2014-11-02 19:17:05 +08:00
|
|
|
if(block.blocks) {
|
|
|
|
block.blocks.forEach(function(b) {
|
|
|
|
var c;
|
2015-01-02 04:58:27 +08:00
|
|
|
if(!b.chunks) {
|
2014-11-02 19:17:05 +08:00
|
|
|
c = this.addChunk(b.chunkName, b.module, b.loc);
|
2015-01-02 04:58:27 +08:00
|
|
|
b.chunks = [c];
|
2014-11-02 19:17:05 +08:00
|
|
|
c.addBlock(b);
|
|
|
|
} else {
|
2015-01-02 04:58:27 +08:00
|
|
|
c = b.chunks[0];
|
2014-11-02 19:17:05 +08:00
|
|
|
}
|
|
|
|
chunk.addChunk(c);
|
|
|
|
c.addParent(chunk);
|
|
|
|
this.processDependenciesBlockForChunk(b, c);
|
|
|
|
}, this);
|
|
|
|
}
|
2015-07-08 20:15:21 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
function iteratorDependency(d) {
|
2015-05-11 00:43:47 +08:00
|
|
|
if(!d.module) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(typeof d.module.index !== "number") {
|
|
|
|
d.module.index = this.nextFreeModuleIndex++;
|
|
|
|
}
|
|
|
|
if(d.weak) {
|
2014-06-04 03:03:21 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
if(d.module.error) {
|
|
|
|
d.module = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(chunk.addModule(d.module)) {
|
|
|
|
d.module.addChunk(chunk);
|
|
|
|
this.processDependenciesBlockForChunk(d.module, chunk);
|
|
|
|
}
|
2015-05-11 00:43:47 +08:00
|
|
|
if(typeof d.module.index2 !== "number") {
|
|
|
|
d.module.index2 = this.nextFreeModuleIndex2++;
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-19 20:32:48 +08:00
|
|
|
Compilation.prototype.removeChunkFromDependencies = function removeChunkFromDependencies(block, chunk) {
|
|
|
|
block.blocks.forEach(function(b) {
|
2015-01-02 04:58:27 +08:00
|
|
|
b.chunks.forEach(function(c) {
|
|
|
|
chunk.removeChunk(c);
|
|
|
|
c.removeParent(chunk);
|
|
|
|
this.removeChunkFromDependencies(b, c);
|
|
|
|
}, this);
|
2014-07-19 20:32:48 +08:00
|
|
|
}, this);
|
2015-07-08 20:15:21 +08:00
|
|
|
|
2014-07-19 20:32:48 +08:00
|
|
|
function iteratorDependency(d) {
|
|
|
|
if(!d.module) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!d.module.hasReasonForChunk(chunk)) {
|
|
|
|
if(d.module.removeChunk(chunk)) {
|
|
|
|
this.removeChunkFromDependencies(d.module, chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
block.dependencies.forEach(iteratorDependency, this);
|
|
|
|
block.variables.forEach(function(v) {
|
|
|
|
v.dependencies.forEach(iteratorDependency, this);
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
Compilation.prototype.applyModuleIds = function applyModuleIds() {
|
|
|
|
this.modules.forEach(function(module) {
|
2013-05-08 19:28:54 +08:00
|
|
|
if(module.id === null) {
|
2013-05-31 18:22:40 +08:00
|
|
|
module.id = this.nextFreeModuleId++;
|
2013-05-08 19:28:54 +08:00
|
|
|
}
|
2013-05-31 18:22:40 +08:00
|
|
|
}, this);
|
2013-01-31 01:49:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Compilation.prototype.applyChunkIds = function applyChunkIds() {
|
|
|
|
this.chunks.forEach(function(chunk) {
|
2013-05-09 05:01:25 +08:00
|
|
|
if(chunk.id === null) {
|
2014-06-04 03:03:21 +08:00
|
|
|
chunk.id = this.nextFreeChunkId++;
|
2013-05-09 05:01:25 +08:00
|
|
|
}
|
2014-06-04 03:03:21 +08:00
|
|
|
if(!chunk.ids) {
|
2013-02-24 09:05:55 +08:00
|
|
|
chunk.ids = [chunk.id];
|
2014-06-04 03:03:21 +08:00
|
|
|
}
|
2013-05-09 05:01:25 +08:00
|
|
|
}, this);
|
2013-01-31 01:49:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Compilation.prototype.sortItems = function sortItems() {
|
|
|
|
function byId(a, b) {
|
|
|
|
return a.id - b.id;
|
|
|
|
}
|
|
|
|
this.chunks.sort(byId);
|
|
|
|
this.modules.sort(byId);
|
|
|
|
this.modules.forEach(function(module) {
|
|
|
|
module.chunks.sort(byId);
|
2014-02-04 01:12:19 +08:00
|
|
|
module.reasons.sort(function(a, b) {
|
2015-04-24 05:55:50 +08:00
|
|
|
return byId(a.module, b.module);
|
2014-02-04 01:12:19 +08:00
|
|
|
});
|
2013-01-31 01:49:25 +08:00
|
|
|
});
|
|
|
|
this.chunks.forEach(function(chunk) {
|
|
|
|
chunk.modules.sort(byId);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Compilation.prototype.summarizeDependencies = function summarizeDependencies() {
|
|
|
|
function filterDups(array) {
|
|
|
|
var newArray = [];
|
|
|
|
for(var i = 0; i < array.length; i++) {
|
2015-04-24 05:55:50 +08:00
|
|
|
if(i === 0 || array[i - 1] !== array[i])
|
2013-01-31 01:49:25 +08:00
|
|
|
newArray.push(array[i]);
|
|
|
|
}
|
|
|
|
return newArray;
|
|
|
|
}
|
|
|
|
this.fileDependencies = [];
|
|
|
|
this.contextDependencies = [];
|
2015-01-18 04:55:44 +08:00
|
|
|
this.missingDependencies = [];
|
2013-01-31 01:49:25 +08:00
|
|
|
this.children.forEach(function(child) {
|
|
|
|
this.fileDependencies = this.fileDependencies.concat(child.fileDependencies);
|
|
|
|
this.contextDependencies = this.contextDependencies.concat(child.contextDependencies);
|
2015-01-18 04:55:44 +08:00
|
|
|
this.missingDependencies = this.missingDependencies.concat(child.missingDependencies);
|
2013-01-31 01:49:25 +08:00
|
|
|
}.bind(this));
|
|
|
|
this.modules.forEach(function(module) {
|
|
|
|
if(module.fileDependencies) {
|
|
|
|
module.fileDependencies.forEach(function(item) {
|
|
|
|
this.fileDependencies.push(item);
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
if(module.contextDependencies) {
|
|
|
|
module.contextDependencies.forEach(function(item) {
|
|
|
|
this.contextDependencies.push(item);
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
}, this);
|
2015-01-18 04:55:44 +08:00
|
|
|
this.errors.forEach(function(error) {
|
|
|
|
if(Array.isArray(error.missing)) {
|
|
|
|
error.missing.forEach(function(item) {
|
|
|
|
this.missingDependencies.push(item);
|
2015-04-24 05:55:50 +08:00
|
|
|
}, this);
|
2015-01-18 04:55:44 +08:00
|
|
|
}
|
|
|
|
}, this);
|
2013-01-31 01:49:25 +08:00
|
|
|
this.fileDependencies.sort();
|
|
|
|
this.fileDependencies = filterDups(this.fileDependencies);
|
|
|
|
this.contextDependencies.sort();
|
|
|
|
this.contextDependencies = filterDups(this.contextDependencies);
|
2015-01-18 04:55:44 +08:00
|
|
|
this.missingDependencies.sort();
|
|
|
|
this.missingDependencies = filterDups(this.missingDependencies);
|
2013-01-31 01:49:25 +08:00
|
|
|
};
|
|
|
|
|
2013-07-01 19:59:02 +08:00
|
|
|
Compilation.prototype.createHash = function createHash() {
|
|
|
|
var outputOptions = this.outputOptions;
|
2013-05-21 17:08:08 +08:00
|
|
|
var hashFunction = outputOptions.hashFunction;
|
|
|
|
var hashDigest = outputOptions.hashDigest;
|
|
|
|
var hashDigestLength = outputOptions.hashDigestLength;
|
2013-07-11 05:20:07 +08:00
|
|
|
var hash = require("crypto").createHash(hashFunction);
|
2013-01-31 01:49:25 +08:00
|
|
|
this.mainTemplate.updateHash(hash);
|
|
|
|
this.chunkTemplate.updateHash(hash);
|
|
|
|
this.moduleTemplate.updateHash(hash);
|
|
|
|
var i, chunk;
|
2015-07-23 04:34:25 +08:00
|
|
|
var chunks = this.chunks.slice();
|
|
|
|
chunks.sort(function(a, b) {
|
|
|
|
if(a.entry && !b.entry) return 1;
|
|
|
|
if(!a.entry && b.entry) return -1;
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
for(i = 0; i < chunks.length; i++) {
|
|
|
|
chunk = chunks[i];
|
2013-07-11 05:20:07 +08:00
|
|
|
var chunkHash = require("crypto").createHash(hashFunction);
|
2013-05-21 07:46:14 +08:00
|
|
|
chunk.updateHash(chunkHash);
|
2015-06-25 05:17:12 +08:00
|
|
|
if(chunk.entry) {
|
|
|
|
this.mainTemplate.updateHashForChunk(chunkHash, chunk);
|
|
|
|
} else {
|
|
|
|
this.chunkTemplate.updateHashForChunk(chunkHash);
|
|
|
|
}
|
2015-02-09 15:43:46 +08:00
|
|
|
this.applyPlugins("chunk-hash", chunk, chunkHash);
|
2013-05-21 17:08:08 +08:00
|
|
|
chunk.hash = chunkHash.digest(hashDigest);
|
2013-05-21 07:46:14 +08:00
|
|
|
hash.update(chunk.hash);
|
2013-05-21 17:08:08 +08:00
|
|
|
chunk.renderedHash = chunk.hash.substr(0, hashDigestLength);
|
2013-02-13 20:00:01 +08:00
|
|
|
}
|
2013-07-01 19:59:02 +08:00
|
|
|
this.fullHash = hash.digest(hashDigest);
|
|
|
|
this.hash = this.fullHash.substr(0, hashDigestLength);
|
|
|
|
};
|
|
|
|
|
|
|
|
Compilation.prototype.modifyHash = function modifyHash(update) {
|
|
|
|
var outputOptions = this.outputOptions;
|
|
|
|
var hashFunction = outputOptions.hashFunction;
|
|
|
|
var hashDigest = outputOptions.hashDigest;
|
|
|
|
var hashDigestLength = outputOptions.hashDigestLength;
|
2013-07-11 05:20:07 +08:00
|
|
|
var hash = require("crypto").createHash(hashFunction);
|
2013-07-01 19:59:02 +08:00
|
|
|
hash.update(this.fullHash);
|
|
|
|
hash.update(update);
|
|
|
|
this.fullHash = hash.digest(hashDigest);
|
|
|
|
this.hash = this.fullHash.substr(0, hashDigestLength);
|
|
|
|
};
|
|
|
|
|
2015-03-06 05:30:24 +08:00
|
|
|
Compilation.prototype.createModuleAssets = function createModuleAssets() {
|
2013-07-04 18:11:17 +08:00
|
|
|
for(var i = 0; i < this.modules.length; i++) {
|
2013-01-31 01:49:25 +08:00
|
|
|
var module = this.modules[i];
|
|
|
|
if(module.assets) {
|
|
|
|
Object.keys(module.assets).forEach(function(name) {
|
2014-08-22 19:51:24 +08:00
|
|
|
var file = this.getPath(name);
|
2013-03-26 23:54:41 +08:00
|
|
|
this.assets[file] = module.assets[name];
|
|
|
|
this.applyPlugins("module-asset", module, file);
|
2013-01-31 01:49:25 +08:00
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
}
|
2015-01-31 23:19:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Compilation.prototype.createChunkAssets = function createChunkAssets() {
|
|
|
|
var outputOptions = this.outputOptions;
|
2016-01-07 06:02:25 +08:00
|
|
|
var filename = outputOptions.filename;
|
|
|
|
var chunkFilename = outputOptions.chunkFilename;
|
2013-07-04 18:11:17 +08:00
|
|
|
for(var i = 0; i < this.chunks.length; i++) {
|
|
|
|
var chunk = this.chunks[i];
|
2013-01-31 01:49:25 +08:00
|
|
|
chunk.files = [];
|
2013-05-21 07:46:14 +08:00
|
|
|
var chunkHash = chunk.hash;
|
2013-01-31 01:49:25 +08:00
|
|
|
var source;
|
|
|
|
var file;
|
2015-07-08 20:15:21 +08:00
|
|
|
var filenameTemplate = chunk.filenameTemplate ? chunk.filenameTemplate :
|
2013-12-04 06:11:14 +08:00
|
|
|
chunk.initial ? filename :
|
2013-12-03 18:19:30 +08:00
|
|
|
chunkFilename;
|
2015-06-27 17:34:17 +08:00
|
|
|
try {
|
|
|
|
var useChunkHash = !chunk.entry || (this.mainTemplate.useChunkHash && this.mainTemplate.useChunkHash(chunk));
|
|
|
|
var usedHash = useChunkHash ? chunkHash : this.fullHash;
|
|
|
|
if(this.cache && this.cache["c" + chunk.id] && this.cache["c" + chunk.id].hash === usedHash) {
|
|
|
|
source = this.cache["c" + chunk.id].source;
|
2013-05-08 19:28:54 +08:00
|
|
|
} else {
|
2015-06-27 17:34:17 +08:00
|
|
|
if(chunk.entry) {
|
|
|
|
source = this.mainTemplate.render(this.hash, chunk, this.moduleTemplate, this.dependencyTemplates);
|
|
|
|
} else {
|
|
|
|
source = this.chunkTemplate.render(chunk, this.moduleTemplate, this.dependencyTemplates);
|
|
|
|
}
|
|
|
|
if(this.cache) {
|
|
|
|
this.cache["c" + chunk.id] = {
|
|
|
|
hash: usedHash,
|
|
|
|
source: source = (source instanceof CachedSource ? source : new CachedSource(source))
|
|
|
|
};
|
|
|
|
}
|
2013-05-08 19:28:54 +08:00
|
|
|
}
|
2015-07-01 06:44:17 +08:00
|
|
|
file = this.getPath(filenameTemplate, {
|
|
|
|
noChunkHash: !useChunkHash,
|
|
|
|
chunk: chunk
|
|
|
|
});
|
|
|
|
if(this.assets[file])
|
|
|
|
throw new Error("Conflict: Multiple assets emit to the same filename '" + file + "'");
|
|
|
|
this.assets[file] = source;
|
2013-12-03 18:19:30 +08:00
|
|
|
chunk.files.push(file);
|
|
|
|
this.applyPlugins("chunk-asset", chunk, file);
|
2015-06-27 17:34:17 +08:00
|
|
|
} catch(err) {
|
|
|
|
this.errors.push(new ChunkRenderError(chunk, file || filenameTemplate, err));
|
2013-12-03 18:19:30 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-08 20:15:21 +08:00
|
|
|
Compilation.prototype.getPath = function(filename, data) {
|
2014-08-22 19:51:24 +08:00
|
|
|
data = data || {};
|
|
|
|
data.hash = data.hash || this.hash;
|
|
|
|
return this.mainTemplate.applyPluginsWaterfall("asset-path", filename, data);
|
|
|
|
};
|
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
Compilation.prototype.getStats = function() {
|
|
|
|
return new Stats(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
Compilation.prototype.createChildCompiler = function(name, outputOptions) {
|
|
|
|
return this.compiler.createChildCompiler(this, name, outputOptions);
|
|
|
|
};
|