webpack/lib/webpack.js

123 lines
3.7 KiB
JavaScript
Raw Normal View History

2012-03-12 04:50:55 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2013-01-31 01:49:25 +08:00
var Compiler = require("./Compiler");
var MultiCompiler = require("./MultiCompiler");
2013-01-31 01:49:25 +08:00
var NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
var WebpackOptionsApply = require("./WebpackOptionsApply");
var WebpackOptionsDefaulter = require("./WebpackOptionsDefaulter");
2016-11-03 22:02:14 +08:00
var validateSchema = require("./validateSchema");
var WebpackOptionsValidationError = require("./WebpackOptionsValidationError");
2016-11-02 06:38:54 +08:00
var webpackOptionsSchema = require("../schemas/webpackOptionsSchema.json");
2013-01-31 01:49:25 +08:00
function webpack(options, callback) {
2016-11-03 22:02:14 +08:00
var webpackOptionsValidationErrors = validateSchema(webpackOptionsSchema, options);
if(webpackOptionsValidationErrors.length) {
throw new WebpackOptionsValidationError(webpackOptionsValidationErrors);
}
var compiler;
if(Array.isArray(options)) {
compiler = new MultiCompiler(options.map(function(options) {
return webpack(options);
}));
} else if(typeof options === "object") {
new WebpackOptionsDefaulter().process(options);
2013-01-31 01:49:25 +08:00
compiler = new Compiler();
2016-12-14 19:03:24 +08:00
compiler.context = options.context;
compiler.options = options;
new NodeEnvironmentPlugin().apply(compiler);
2016-12-14 19:03:24 +08:00
if(options.plugins && Array.isArray(options.plugins)) {
compiler.apply.apply(compiler, options.plugins);
}
compiler.applyPlugins("environment");
compiler.applyPlugins("after-environment");
2016-12-05 06:47:19 +08:00
compiler.options = new WebpackOptionsApply().process(options, compiler);
} else {
throw new Error("Invalid argument: options");
}
2013-01-31 01:49:25 +08:00
if(callback) {
if(typeof callback !== "function") throw new Error("Invalid argument: callback");
if(options.watch === true || (Array.isArray(options) &&
options.some(function(o) {
return o.watch;
}))) {
var watchOptions = (!Array.isArray(options) ? options : options[0]).watchOptions || {};
return compiler.watch(watchOptions, callback);
2012-03-10 20:11:23 +08:00
}
compiler.run(callback);
2012-10-20 21:08:12 +08:00
}
2013-01-31 01:49:25 +08:00
return compiler;
2012-05-07 15:01:28 +08:00
}
exports = module.exports = webpack;
2012-05-07 15:01:28 +08:00
2013-01-31 01:49:25 +08:00
webpack.WebpackOptionsDefaulter = WebpackOptionsDefaulter;
webpack.WebpackOptionsApply = WebpackOptionsApply;
webpack.Compiler = Compiler;
webpack.MultiCompiler = MultiCompiler;
2013-01-31 01:49:25 +08:00
webpack.NodeEnvironmentPlugin = NodeEnvironmentPlugin;
2016-11-03 22:02:14 +08:00
webpack.validate = validateSchema.bind(this, webpackOptionsSchema);
webpack.validateSchema = validateSchema;
webpack.WebpackOptionsValidationError = WebpackOptionsValidationError;
function exportPlugins(exports, path, plugins) {
plugins.forEach(function(name) {
Object.defineProperty(exports, name, {
configurable: false,
enumerable: true,
get: function() {
return require(path + "/" + name);
}
});
});
}
exportPlugins(exports, ".", [
"DefinePlugin",
"NormalModuleReplacementPlugin",
"ContextReplacementPlugin",
"IgnorePlugin",
"WatchIgnorePlugin",
"BannerPlugin",
"PrefetchPlugin",
"AutomaticPrefetchPlugin",
"ProvidePlugin",
"HotModuleReplacementPlugin",
2014-06-24 06:41:10 +08:00
"SourceMapDevToolPlugin",
2015-03-02 12:49:43 +08:00
"EvalSourceMapDevToolPlugin",
2014-06-24 06:41:10 +08:00
"EvalDevToolModulePlugin",
"CachePlugin",
2014-08-25 15:50:26 +08:00
"ExtendedAPIPlugin",
2014-06-24 06:41:10 +08:00
"ExternalsPlugin",
"JsonpTemplatePlugin",
"LibraryTemplatePlugin",
"LoaderTargetPlugin",
"MemoryOutputFileSystem",
"ProgressPlugin",
"SetVarMainTemplatePlugin",
"UmdMainTemplatePlugin",
"NoErrorsPlugin",
"NoEmitOnErrorsPlugin",
"NewWatchingPlugin",
"EnvironmentPlugin",
"DllPlugin",
"DllReferencePlugin",
"LoaderOptionsPlugin",
2015-11-30 03:16:01 +08:00
"NamedModulesPlugin",
2016-08-17 18:52:02 +08:00
"HashedModuleIdsPlugin",
"ModuleFilenameHelpers"
]);
exportPlugins(exports.optimize = {}, "./optimize", [
2014-02-05 19:37:37 +08:00
"AggressiveMergingPlugin",
"AggressiveSplittingPlugin",
"CommonsChunkPlugin",
"ChunkModuleIdRangePlugin",
"DedupePlugin",
"LimitChunkCountPlugin",
"MinChunkSizePlugin",
"OccurrenceOrderPlugin",
"UglifyJsPlugin"
]);
2016-09-09 03:41:03 +08:00
exportPlugins(exports.dependencies = {}, "./dependencies", []);