webpack/lib/WebpackOptionsApply.js

295 lines
11 KiB
JavaScript
Raw Normal View History

2013-01-31 01:49:25 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var OptionsApply = require("./OptionsApply");
var LoaderTargetPlugin = require("./LoaderTargetPlugin");
2013-01-31 01:49:25 +08:00
var FunctionModulePlugin = require("./FunctionModulePlugin");
var EvalDevToolModulePlugin = require("./EvalDevToolModulePlugin");
2013-03-26 23:54:41 +08:00
var SourceMapDevToolPlugin = require("./SourceMapDevToolPlugin");
2014-02-13 18:40:16 +08:00
var EvalSourceMapDevToolPlugin = require("./EvalSourceMapDevToolPlugin");
2013-01-31 01:49:25 +08:00
2015-05-17 00:27:59 +08:00
var EntryOptionPlugin = require("./EntryOptionPlugin");
2013-05-31 18:22:40 +08:00
var RecordIdsPlugin = require("./RecordIdsPlugin");
2013-01-31 01:49:25 +08:00
var APIPlugin = require("./APIPlugin");
var ConstPlugin = require("./ConstPlugin");
2013-02-14 04:24:00 +08:00
var RequireJsStuffPlugin = require("./RequireJsStuffPlugin");
var NodeStuffPlugin = require("./NodeStuffPlugin");
2013-01-31 01:49:25 +08:00
var CompatibilityPlugin = require("./CompatibilityPlugin");
2014-08-22 19:51:24 +08:00
var TemplatedPathPlugin = require("./TemplatedPathPlugin");
var WarnCaseSensitiveModulesPlugin = require("./WarnCaseSensitiveModulesPlugin");
var UseStrictPlugin = require("./UseStrictPlugin");
var LoaderPlugin = require("./dependencies/LoaderPlugin");
2013-01-31 01:49:25 +08:00
var CommonJsPlugin = require("./dependencies/CommonJsPlugin");
2015-01-13 00:45:30 +08:00
var HarmonyModulesPlugin = require("./dependencies/HarmonyModulesPlugin");
var SystemPlugin = require("./dependencies/SystemPlugin");
2016-12-03 18:36:10 +08:00
var ImportPlugin = require("./dependencies/ImportPlugin");
2013-01-31 01:49:25 +08:00
var AMDPlugin = require("./dependencies/AMDPlugin");
var RequireContextPlugin = require("./dependencies/RequireContextPlugin");
var RequireEnsurePlugin = require("./dependencies/RequireEnsurePlugin");
2013-02-16 00:08:14 +08:00
var RequireIncludePlugin = require("./dependencies/RequireIncludePlugin");
2013-01-31 01:49:25 +08:00
var EnsureChunkConditionsPlugin = require("./optimize/EnsureChunkConditionsPlugin");
2013-01-31 01:49:25 +08:00
var RemoveParentModulesPlugin = require("./optimize/RemoveParentModulesPlugin");
var RemoveEmptyChunksPlugin = require("./optimize/RemoveEmptyChunksPlugin");
var MergeDuplicateChunksPlugin = require("./optimize/MergeDuplicateChunksPlugin");
var FlagIncludedChunksPlugin = require("./optimize/FlagIncludedChunksPlugin");
var OccurrenceOrderPlugin = require("./optimize/OccurrenceOrderPlugin");
var FlagDependencyUsagePlugin = require("./FlagDependencyUsagePlugin");
var FlagDependencyExportsPlugin = require("./FlagDependencyExportsPlugin");
var SizeLimitsPlugin = require("./performance/SizeLimitsPlugin");
2013-01-31 01:49:25 +08:00
var ResolverFactory = require("enhanced-resolve").ResolverFactory;
2013-01-31 01:49:25 +08:00
function WebpackOptionsApply() {
OptionsApply.call(this);
}
module.exports = WebpackOptionsApply;
WebpackOptionsApply.prototype = Object.create(OptionsApply.prototype);
WebpackOptionsApply.prototype.constructor = WebpackOptionsApply;
2013-01-31 01:49:25 +08:00
WebpackOptionsApply.prototype.process = function(options, compiler) {
2016-01-19 08:52:28 +08:00
var ExternalsPlugin;
2013-01-31 01:49:25 +08:00
compiler.outputPath = options.output.path;
2013-05-31 18:22:40 +08:00
compiler.recordsInputPath = options.recordsInputPath || options.recordsPath;
compiler.recordsOutputPath = options.recordsOutputPath || options.recordsPath;
compiler.name = options.name;
compiler.dependencies = options.dependencies;
if(typeof options.target === "string") {
2016-01-19 08:52:28 +08:00
var JsonpTemplatePlugin;
var NodeSourcePlugin;
var NodeTargetPlugin;
var NodeTemplatePlugin;
switch(options.target) {
2015-07-13 06:20:09 +08:00
case "web":
2016-01-19 08:52:28 +08:00
JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
NodeSourcePlugin = require("./node/NodeSourcePlugin");
2015-07-13 06:20:09 +08:00
compiler.apply(
new JsonpTemplatePlugin(options.output),
new FunctionModulePlugin(options.output),
new NodeSourcePlugin(options.node),
new LoaderTargetPlugin("web")
);
break;
case "webworker":
var WebWorkerTemplatePlugin = require("./webworker/WebWorkerTemplatePlugin");
2016-01-19 08:52:28 +08:00
NodeSourcePlugin = require("./node/NodeSourcePlugin");
2015-07-13 06:20:09 +08:00
compiler.apply(
new WebWorkerTemplatePlugin(options.output),
new FunctionModulePlugin(options.output),
new NodeSourcePlugin(options.node),
new LoaderTargetPlugin("webworker")
);
break;
case "node":
case "async-node":
2016-01-19 08:52:28 +08:00
NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
NodeTargetPlugin = require("./node/NodeTargetPlugin");
2015-07-13 06:20:09 +08:00
compiler.apply(
2015-11-21 03:15:03 +08:00
new NodeTemplatePlugin({
asyncChunkLoading: options.target === "async-node"
}),
2015-07-13 06:20:09 +08:00
new FunctionModulePlugin(options.output),
new NodeTargetPlugin(),
new LoaderTargetPlugin("node")
);
break;
case "node-webkit":
2016-01-19 08:52:28 +08:00
JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
NodeTargetPlugin = require("./node/NodeTargetPlugin");
ExternalsPlugin = require("./ExternalsPlugin");
2015-07-13 06:20:09 +08:00
compiler.apply(
new JsonpTemplatePlugin(options.output),
new FunctionModulePlugin(options.output),
new NodeTargetPlugin(),
new ExternalsPlugin("commonjs", "nw.gui"),
new LoaderTargetPlugin("node-webkit")
);
break;
case "atom":
case "electron":
case "electron-main":
2016-01-19 08:52:28 +08:00
NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
NodeTargetPlugin = require("./node/NodeTargetPlugin");
ExternalsPlugin = require("./ExternalsPlugin");
2015-07-13 06:20:09 +08:00
compiler.apply(
2015-11-21 03:15:03 +08:00
new NodeTemplatePlugin({
asyncChunkLoading: true
}),
2015-07-13 06:20:09 +08:00
new FunctionModulePlugin(options.output),
new NodeTargetPlugin(),
new ExternalsPlugin("commonjs", [
"app",
"auto-updater",
"browser-window",
"content-tracing",
"dialog",
"electron",
2015-07-13 06:20:09 +08:00
"global-shortcut",
"ipc",
"ipc-main",
2015-07-13 06:20:09 +08:00
"menu",
"menu-item",
"power-monitor",
"power-save-blocker",
2015-07-13 06:20:09 +08:00
"protocol",
"session",
"web-contents",
2015-07-13 06:20:09 +08:00
"tray",
"clipboard",
"crash-reporter",
"native-image",
2015-07-13 06:20:09 +08:00
"screen",
"shell"
2015-07-13 06:20:09 +08:00
]),
new LoaderTargetPlugin(options.target)
);
break;
case "electron-renderer":
2016-01-19 08:52:28 +08:00
JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
NodeTargetPlugin = require("./node/NodeTargetPlugin");
ExternalsPlugin = require("./ExternalsPlugin");
compiler.apply(
new JsonpTemplatePlugin(options.output),
new FunctionModulePlugin(options.output),
new NodeTargetPlugin(),
new ExternalsPlugin("commonjs", [
"desktop-capturer",
"electron",
"ipc",
"ipc-renderer",
"remote",
"web-frame",
"clipboard",
"crash-reporter",
"native-image",
"screen",
"shell"
]),
new LoaderTargetPlugin(options.target)
);
break;
2015-07-13 06:20:09 +08:00
default:
throw new Error("Unsupported target '" + options.target + "'.");
}
} else if(options.target !== false) {
options.target(compiler);
} else {
2014-02-11 15:53:32 +08:00
throw new Error("Unsupported target '" + options.target + "'.");
}
2014-06-25 00:53:32 +08:00
if(options.output.library || options.output.libraryTarget !== "var") {
2013-07-11 05:20:07 +08:00
var LibraryTemplatePlugin = require("./LibraryTemplatePlugin");
compiler.apply(new LibraryTemplatePlugin(options.output.library, options.output.libraryTarget, options.output.umdNamedDefine, options.output.auxiliaryComment || ""));
2013-06-19 17:53:03 +08:00
}
2014-03-05 16:58:51 +08:00
if(options.externals) {
2016-01-19 08:52:28 +08:00
ExternalsPlugin = require("./ExternalsPlugin");
2014-03-05 16:58:51 +08:00
compiler.apply(new ExternalsPlugin(options.output.libraryTarget, options.externals));
}
var noSources;
2016-01-19 08:52:28 +08:00
var legacy;
var modern;
var comment;
2015-05-19 03:56:23 +08:00
if(options.devtool && (options.devtool.indexOf("sourcemap") >= 0 || options.devtool.indexOf("source-map") >= 0)) {
var hidden = options.devtool.indexOf("hidden") >= 0;
var inline = options.devtool.indexOf("inline") >= 0;
var evalWrapped = options.devtool.indexOf("eval") >= 0;
var cheap = options.devtool.indexOf("cheap") >= 0;
var moduleMaps = options.devtool.indexOf("module") >= 0;
noSources = options.devtool.indexOf("nosources") >= 0;
2016-01-19 08:52:28 +08:00
legacy = options.devtool.indexOf("@") >= 0;
modern = options.devtool.indexOf("#") >= 0;
comment = legacy && modern ? "\n/*\n//@ sourceMappingURL=[url]\n//# sourceMappingURL=[url]\n*/" :
legacy ? "\n/*\n//@ sourceMappingURL=[url]\n*/" :
modern ? "\n//# sourceMappingURL=[url]" :
null;
2015-07-13 06:20:09 +08:00
var Plugin = evalWrapped ? EvalSourceMapDevToolPlugin : SourceMapDevToolPlugin;
compiler.apply(new Plugin({
filename: inline ? null : options.output.sourceMapFilename,
moduleFilenameTemplate: options.output.devtoolModuleFilenameTemplate,
fallbackModuleFilenameTemplate: options.output.devtoolFallbackModuleFilenameTemplate,
append: hidden ? false : comment,
module: moduleMaps ? true : cheap ? false : true,
columns: cheap ? false : true,
lineToLine: options.output.devtoolLineToLine,
noSources: noSources,
}));
2015-05-19 03:56:23 +08:00
} else if(options.devtool && options.devtool.indexOf("eval") >= 0) {
2016-01-19 08:52:28 +08:00
legacy = options.devtool.indexOf("@") >= 0;
modern = options.devtool.indexOf("#") >= 0;
comment = legacy && modern ? "\n//@ sourceURL=[url]\n//# sourceURL=[url]" :
legacy ? "\n//@ sourceURL=[url]" :
modern ? "\n//# sourceURL=[url]" :
2015-05-19 03:56:23 +08:00
null;
compiler.apply(new EvalDevToolModulePlugin(comment, options.output.devtoolModuleFilenameTemplate));
}
2013-06-19 17:53:03 +08:00
2015-05-17 00:27:59 +08:00
compiler.apply(new EntryOptionPlugin());
compiler.applyPluginsBailResult("entry-option", options.context, options.entry);
2013-06-19 17:53:03 +08:00
2013-01-31 01:49:25 +08:00
compiler.apply(
new CompatibilityPlugin(),
new HarmonyModulesPlugin(options.module),
new AMDPlugin(options.module, options.amd || {}),
new CommonJsPlugin(options.module),
new LoaderPlugin(),
new NodeStuffPlugin(options.node),
2013-02-14 04:24:00 +08:00
new RequireJsStuffPlugin(),
2013-01-31 01:49:25 +08:00
new APIPlugin(),
new ConstPlugin(),
new UseStrictPlugin(),
2013-02-16 00:08:14 +08:00
new RequireIncludePlugin(),
2013-01-31 01:49:25 +08:00
new RequireEnsurePlugin(),
new RequireContextPlugin(options.resolve.modules, options.resolve.extensions),
2016-12-03 18:36:10 +08:00
new ImportPlugin(options.module),
new SystemPlugin(options.module)
2013-01-31 01:49:25 +08:00
);
compiler.apply(
new EnsureChunkConditionsPlugin(),
2013-01-31 01:49:25 +08:00
new RemoveParentModulesPlugin(),
new RemoveEmptyChunksPlugin(),
new MergeDuplicateChunksPlugin(),
new FlagIncludedChunksPlugin(),
new OccurrenceOrderPlugin(true),
new FlagDependencyExportsPlugin(),
new FlagDependencyUsagePlugin()
2013-01-31 01:49:25 +08:00
);
2013-05-08 19:28:54 +08:00
if(options.performance) {
compiler.apply(new SizeLimitsPlugin(options.performance));
}
2014-08-22 19:51:24 +08:00
compiler.apply(new TemplatedPathPlugin());
2013-05-31 18:22:40 +08:00
compiler.apply(new RecordIdsPlugin());
compiler.apply(new WarnCaseSensitiveModulesPlugin());
2016-12-05 06:47:19 +08:00
if(options.cache) {
2013-07-11 05:20:07 +08:00
var CachePlugin = require("./CachePlugin");
2014-06-25 00:53:32 +08:00
compiler.apply(new CachePlugin(typeof options.cache === "object" ? options.cache : null));
2013-07-11 05:20:07 +08:00
}
2013-01-31 01:49:25 +08:00
compiler.applyPlugins("after-plugins", compiler);
2016-12-05 06:47:19 +08:00
if(!compiler.inputFileSystem) throw new Error("No input filesystem provided");
compiler.resolvers.normal = ResolverFactory.createResolver(Object.assign({
2016-12-05 06:47:19 +08:00
fileSystem: compiler.inputFileSystem
}, options.resolve));
compiler.resolvers.context = ResolverFactory.createResolver(Object.assign({
2016-12-05 06:47:19 +08:00
fileSystem: compiler.inputFileSystem,
resolveToContext: true
}, options.resolve));
compiler.resolvers.loader = ResolverFactory.createResolver(Object.assign({
2016-12-05 06:47:19 +08:00
fileSystem: compiler.inputFileSystem
}, options.resolveLoader));
2013-01-31 01:49:25 +08:00
compiler.applyPlugins("after-resolvers", compiler);
return options;
2014-06-25 00:53:32 +08:00
};