webpack/lib/WebpackOptionsApply.js

325 lines
13 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 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");
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 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");
2013-01-31 01:49:25 +08:00
var UnsafeCachePlugin = require("enhanced-resolve/lib/UnsafeCachePlugin");
2013-01-31 01:49:25 +08:00
var ModulesInDirectoriesPlugin = require("enhanced-resolve/lib/ModulesInDirectoriesPlugin");
2013-02-04 17:47:00 +08:00
var ModulesInRootPlugin = require("enhanced-resolve/lib/ModulesInRootPlugin");
2013-01-31 01:49:25 +08:00
var ModuleTemplatesPlugin = require("enhanced-resolve/lib/ModuleTemplatesPlugin");
var ModuleAsFilePlugin = require("enhanced-resolve/lib/ModuleAsFilePlugin");
var ModuleAsDirectoryPlugin = require("enhanced-resolve/lib/ModuleAsDirectoryPlugin");
var ModuleAliasPlugin = require("enhanced-resolve/lib/ModuleAliasPlugin");
var DirectoryDefaultFilePlugin = require("enhanced-resolve/lib/DirectoryDefaultFilePlugin");
var DirectoryDescriptionFilePlugin = require("enhanced-resolve/lib/DirectoryDescriptionFilePlugin");
var DirectoryDescriptionFileFieldAliasPlugin = require("enhanced-resolve/lib/DirectoryDescriptionFileFieldAliasPlugin");
2013-01-31 01:49:25 +08:00
var FileAppendPlugin = require("enhanced-resolve/lib/FileAppendPlugin");
var DirectoryResultPlugin = require("enhanced-resolve/lib/DirectoryResultPlugin");
2015-01-18 04:57:28 +08:00
var ResultSymlinkPlugin = require("enhanced-resolve/lib/ResultSymlinkPlugin");
2013-01-31 01:49:25 +08:00
function WebpackOptionsApply() {
OptionsApply.call(this);
}
module.exports = WebpackOptionsApply;
WebpackOptionsApply.prototype = Object.create(OptionsApply.prototype);
WebpackOptionsApply.prototype.process = function(options, compiler) {
compiler.context = options.context;
2013-01-31 01:49:25 +08:00
if(options.plugins && Array.isArray(options.plugins)) {
compiler.apply.apply(compiler, options.plugins);
}
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;
if(typeof options.target === "string") {
switch(options.target) {
2015-07-13 06:20:09 +08:00
case "web":
var JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
var NodeSourcePlugin = require("./node/NodeSourcePlugin");
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");
var NodeSourcePlugin = require("./node/NodeSourcePlugin");
compiler.apply(
new WebWorkerTemplatePlugin(options.output),
new FunctionModulePlugin(options.output),
new NodeSourcePlugin(options.node),
new LoaderTargetPlugin("webworker")
);
break;
case "node":
case "async-node":
var NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
var NodeTargetPlugin = require("./node/NodeTargetPlugin");
compiler.apply(
new NodeTemplatePlugin(options.output, options.target === "async-node"),
new FunctionModulePlugin(options.output),
new NodeTargetPlugin(),
new LoaderTargetPlugin("node")
);
break;
case "node-webkit":
var JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
var NodeTargetPlugin = require("./node/NodeTargetPlugin");
var ExternalsPlugin = require("./ExternalsPlugin");
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":
2015-07-13 06:20:09 +08:00
var NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
var NodeTargetPlugin = require("./node/NodeTargetPlugin");
var ExternalsPlugin = require("./ExternalsPlugin");
compiler.apply(
new NodeTemplatePlugin(options.output, true),
new FunctionModulePlugin(options.output),
new NodeTargetPlugin(),
new ExternalsPlugin("commonjs", [
"app",
"auto-updater",
"browser-window",
"content-tracing",
"dialog",
"global-shortcut",
"ipc",
"menu",
"menu-item",
"power-monitor",
"protocol",
"tray",
"remote",
"web-view",
"web-frame",
2015-07-13 06:20:09 +08:00
"clipboard",
"crash-reporter",
"screen",
"shell",
"native-image"
2015-07-13 06:20:09 +08:00
]),
new LoaderTargetPlugin(options.target)
);
break;
case "electron-renderer":
var JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
var NodeTargetPlugin = require("./node/NodeTargetPlugin");
var ExternalsPlugin = require("./ExternalsPlugin");
compiler.apply(
new JsonpTemplatePlugin(options.output),
new FunctionModulePlugin(options.output),
new NodeTargetPlugin(),
new ExternalsPlugin("commonjs", [
"app",
"auto-updater",
"browser-window",
"content-tracing",
"dialog",
"global-shortcut",
"ipc",
"menu",
"menu-item",
"power-monitor",
"protocol",
"tray",
"remote",
"web-view",
"clipboard",
"crash-reporter",
"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));
2013-06-19 17:53:03 +08:00
}
2014-03-05 16:58:51 +08:00
if(options.externals) {
var ExternalsPlugin = require("./ExternalsPlugin");
compiler.apply(new ExternalsPlugin(options.output.libraryTarget, options.externals));
}
2013-06-19 17:53:03 +08:00
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;
var legacy = options.devtool.indexOf("@") >= 0;
var modern = options.devtool.indexOf("#") >= 0;
var 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
}));
2015-05-19 03:56:23 +08:00
} else if(options.devtool && options.devtool.indexOf("eval") >= 0) {
var legacy = options.devtool.indexOf("@") >= 0;
var modern = options.devtool.indexOf("#") >= 0;
var comment = legacy && modern ? "//@ sourceURL=[url]\n//# sourceURL=[url]" :
legacy ? "//@ sourceURL=[url]" :
modern ? "//# sourceURL=[url]" :
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 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(),
2013-02-16 00:08:14 +08:00
new RequireIncludePlugin(),
2013-01-31 01:49:25 +08:00
new RequireEnsurePlugin(),
new RequireContextPlugin(options.resolve.modulesDirectories, options.resolve.extensions),
new AMDPlugin(options.module, options.amd || {}),
2015-01-13 00:45:30 +08:00
new CommonJsPlugin(options.module),
new HarmonyModulesPlugin(options.module),
new SystemPlugin(options.module)
2013-01-31 01:49:25 +08:00
);
compiler.apply(
new RemoveParentModulesPlugin(),
new RemoveEmptyChunksPlugin(),
new MergeDuplicateChunksPlugin(),
new FlagIncludedChunksPlugin(),
new OccurrenceOrderPlugin(true),
new FlagDependencyUsagePlugin()
2013-01-31 01:49:25 +08:00
);
2013-05-08 19:28:54 +08:00
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());
2013-07-11 05:20:07 +08:00
if(options.cache === undefined ? options.watch : options.cache) {
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);
compiler.resolvers.normal.apply(
new UnsafeCachePlugin(options.resolve.unsafeCache),
2014-02-11 19:24:41 +08:00
options.resolve.packageAlias ? new DirectoryDescriptionFileFieldAliasPlugin("package.json", options.resolve.packageAlias) : function() {},
2013-01-31 01:49:25 +08:00
new ModuleAliasPlugin(options.resolve.alias),
2013-02-11 03:37:30 +08:00
makeRootPlugin("module", options.resolve.root),
2013-02-04 20:59:43 +08:00
new ModulesInDirectoriesPlugin("module", options.resolve.modulesDirectories),
2013-02-11 03:37:30 +08:00
makeRootPlugin("module", options.resolve.fallback),
2013-01-31 01:49:25 +08:00
new ModuleAsFilePlugin("module"),
new ModuleAsDirectoryPlugin("module"),
2013-10-13 04:08:04 +08:00
new DirectoryDescriptionFilePlugin("package.json", options.resolve.packageMains),
2013-01-31 01:49:25 +08:00
new DirectoryDefaultFilePlugin(["index"]),
2015-01-18 04:57:28 +08:00
new FileAppendPlugin(options.resolve.extensions),
new ResultSymlinkPlugin()
2013-01-31 01:49:25 +08:00
);
compiler.resolvers.context.apply(
new UnsafeCachePlugin(options.resolve.unsafeCache),
2013-01-31 01:49:25 +08:00
new ModuleAliasPlugin(options.resolve.alias),
2013-02-11 03:37:30 +08:00
makeRootPlugin("module", options.resolve.root),
2013-02-04 20:59:43 +08:00
new ModulesInDirectoriesPlugin("module", options.resolve.modulesDirectories),
2013-02-11 03:37:30 +08:00
makeRootPlugin("module", options.resolve.fallback),
2013-01-31 01:49:25 +08:00
new ModuleAsFilePlugin("module"),
new ModuleAsDirectoryPlugin("module"),
2015-01-18 04:57:28 +08:00
new DirectoryResultPlugin(),
new ResultSymlinkPlugin()
2013-01-31 01:49:25 +08:00
);
compiler.resolvers.loader.apply(
new UnsafeCachePlugin(options.resolve.unsafeCache),
2013-01-31 01:49:25 +08:00
new ModuleAliasPlugin(options.resolveLoader.alias),
2013-02-11 03:37:30 +08:00
makeRootPlugin("loader-module", options.resolveLoader.root),
2013-02-04 20:59:43 +08:00
new ModulesInDirectoriesPlugin("loader-module", options.resolveLoader.modulesDirectories),
2013-02-11 03:37:30 +08:00
makeRootPlugin("loader-module", options.resolveLoader.fallback),
2013-01-31 01:49:25 +08:00
new ModuleTemplatesPlugin("loader-module", options.resolveLoader.moduleTemplates, "module"),
new ModuleAsFilePlugin("module"),
new ModuleAsDirectoryPlugin("module"),
2013-10-13 04:08:04 +08:00
new DirectoryDescriptionFilePlugin("package.json", options.resolveLoader.packageMains),
2013-01-31 01:49:25 +08:00
new DirectoryDefaultFilePlugin(["index"]),
2015-01-18 04:57:28 +08:00
new FileAppendPlugin(options.resolveLoader.extensions),
new ResultSymlinkPlugin()
2013-01-31 01:49:25 +08:00
);
compiler.applyPlugins("after-resolvers", compiler);
return options;
2014-06-25 00:53:32 +08:00
};
2013-02-04 17:47:00 +08:00
2013-02-11 03:37:30 +08:00
function makeRootPlugin(name, root) {
2013-02-04 17:47:00 +08:00
if(typeof root === "string")
2013-02-11 03:37:30 +08:00
return new ModulesInRootPlugin(name, root);
2013-02-04 17:47:00 +08:00
else if(Array.isArray(root)) {
return function() {
root.forEach(function(root) {
2013-02-11 18:10:15 +08:00
this.apply(new ModulesInRootPlugin(name, root));
2013-02-04 17:47:00 +08:00
}, this);
2015-04-24 05:55:50 +08:00
};
2013-02-04 17:47:00 +08:00
}
return function() {};
}