webpack/lib/WebpackOptionsApply.js

680 lines
24 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
*/
2018-07-30 23:08:51 +08:00
2017-02-21 21:49:15 +08:00
"use strict";
2013-01-31 01:49:25 +08:00
2017-02-21 21:49:15 +08:00
const OptionsApply = require("./OptionsApply");
2013-01-31 01:49:25 +08:00
const JavascriptModulesPlugin = require("./JavascriptModulesPlugin");
const JsonModulesPlugin = require("./JsonModulesPlugin");
2017-02-21 21:49:15 +08:00
const EvalDevToolModulePlugin = require("./EvalDevToolModulePlugin");
const EvalSourceMapDevToolPlugin = require("./EvalSourceMapDevToolPlugin");
2018-07-30 23:08:51 +08:00
const FunctionModulePlugin = require("./FunctionModulePlugin");
const LoaderTargetPlugin = require("./LoaderTargetPlugin");
const SourceMapDevToolPlugin = require("./SourceMapDevToolPlugin");
2013-01-31 01:49:25 +08:00
2017-02-21 21:49:15 +08:00
const EntryOptionPlugin = require("./EntryOptionPlugin");
const RecordIdsPlugin = require("./RecordIdsPlugin");
2013-01-31 01:49:25 +08:00
2018-11-17 01:18:44 +08:00
const RuntimePlugin = require("./RuntimePlugin");
2017-02-21 21:49:15 +08:00
const APIPlugin = require("./APIPlugin");
const CommonJsStuffPlugin = require("./CommonJsStuffPlugin");
2018-07-30 23:08:51 +08:00
const CompatibilityPlugin = require("./CompatibilityPlugin");
2017-02-21 21:49:15 +08:00
const ConstPlugin = require("./ConstPlugin");
const ExportsInfoApiPlugin = require("./ExportsInfoApiPlugin");
2017-02-21 21:49:15 +08:00
const TemplatedPathPlugin = require("./TemplatedPathPlugin");
const UseStrictPlugin = require("./UseStrictPlugin");
2018-07-30 23:08:51 +08:00
const WarnCaseSensitiveModulesPlugin = require("./WarnCaseSensitiveModulesPlugin");
2013-01-31 01:49:25 +08:00
2018-10-23 13:22:44 +08:00
const ResolverCachePlugin = require("./cache/ResolverCachePlugin");
2017-02-21 21:49:15 +08:00
const CommonJsPlugin = require("./dependencies/CommonJsPlugin");
const HarmonyModulesPlugin = require("./dependencies/HarmonyModulesPlugin");
const ImportPlugin = require("./dependencies/ImportPlugin");
2018-07-30 23:08:51 +08:00
const LoaderPlugin = require("./dependencies/LoaderPlugin");
2017-02-21 21:49:15 +08:00
const RequireContextPlugin = require("./dependencies/RequireContextPlugin");
const RequireEnsurePlugin = require("./dependencies/RequireEnsurePlugin");
const RequireIncludePlugin = require("./dependencies/RequireIncludePlugin");
2018-07-30 23:08:51 +08:00
const SystemPlugin = require("./dependencies/SystemPlugin");
2013-01-31 01:49:25 +08:00
2018-09-05 20:22:10 +08:00
const OccurrenceChunkIdsPlugin = require("./ids/OccurrenceChunkIdsPlugin");
const DefaultStatsFactoryPlugin = require("./stats/DefaultStatsFactoryPlugin");
const DefaultStatsPresetPlugin = require("./stats/DefaultStatsPresetPlugin");
const DefaultStatsPrinterPlugin = require("./stats/DefaultStatsPrinterPlugin");
const { cachedCleverMerge } = require("./util/cleverMerge");
2018-09-19 19:05:22 +08:00
/** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
/** @typedef {import("./Compiler")} Compiler */
2017-02-21 21:49:15 +08:00
class WebpackOptionsApply extends OptionsApply {
constructor() {
super();
}
2018-09-19 19:05:22 +08:00
/**
* @param {WebpackOptions} options options object
* @param {Compiler} compiler compiler object
* @returns {WebpackOptions} options object
*/
2017-02-21 21:49:15 +08:00
process(options, compiler) {
compiler.outputPath = options.output.path;
compiler.recordsInputPath = options.recordsInputPath || options.recordsPath;
2018-02-25 09:00:20 +08:00
compiler.recordsOutputPath =
options.recordsOutputPath || options.recordsPath;
2017-02-21 21:49:15 +08:00
compiler.name = options.name;
2018-02-25 09:00:20 +08:00
if (typeof options.target === "string") {
switch (options.target) {
case "web": {
const JsonpTemplatePlugin = require("./web/JsonpTemplatePlugin");
const FetchCompileWasmPlugin = require("./web/FetchCompileWasmPlugin");
const FetchCompileAsyncWasmPlugin = require("./web/FetchCompileAsyncWasmPlugin");
const NodeSourcePlugin = require("./node/NodeSourcePlugin");
2018-04-04 14:53:58 +08:00
new JsonpTemplatePlugin().apply(compiler);
new FetchCompileWasmPlugin({
mangleImports: options.optimization.mangleWasmImports
}).apply(compiler);
new FetchCompileAsyncWasmPlugin().apply(compiler);
2018-04-04 14:53:58 +08:00
new FunctionModulePlugin().apply(compiler);
new NodeSourcePlugin(options.node).apply(compiler);
new LoaderTargetPlugin(options.target).apply(compiler);
2017-02-21 21:49:15 +08:00
break;
}
2018-02-25 09:00:20 +08:00
case "webworker": {
const WebWorkerTemplatePlugin = require("./webworker/WebWorkerTemplatePlugin");
const FetchCompileWasmPlugin = require("./web/FetchCompileWasmPlugin");
const FetchCompileAsyncWasmPlugin = require("./web/FetchCompileAsyncWasmPlugin");
const NodeSourcePlugin = require("./node/NodeSourcePlugin");
const StartupChunkDependenciesPlugin = require("./runtime/StartupChunkDependenciesPlugin");
2018-02-25 09:00:20 +08:00
new WebWorkerTemplatePlugin().apply(compiler);
new FetchCompileWasmPlugin({
mangleImports: options.optimization.mangleWasmImports
}).apply(compiler);
new FetchCompileAsyncWasmPlugin().apply(compiler);
2018-04-04 14:53:58 +08:00
new FunctionModulePlugin().apply(compiler);
2018-02-25 09:00:20 +08:00
new NodeSourcePlugin(options.node).apply(compiler);
new LoaderTargetPlugin(options.target).apply(compiler);
new StartupChunkDependenciesPlugin({
asyncChunkLoading: true
}).apply(compiler);
2018-02-25 09:00:20 +08:00
break;
}
2017-02-21 21:49:15 +08:00
case "node":
case "async-node": {
const NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
const ReadFileCompileWasmPlugin = require("./node/ReadFileCompileWasmPlugin");
const ReadFileCompileAsyncWasmPlugin = require("./node/ReadFileCompileAsyncWasmPlugin");
const NodeTargetPlugin = require("./node/NodeTargetPlugin");
const StartupChunkDependenciesPlugin = require("./runtime/StartupChunkDependenciesPlugin");
new NodeTemplatePlugin({
asyncChunkLoading: options.target === "async-node"
}).apply(compiler);
new ReadFileCompileWasmPlugin({
mangleImports: options.optimization.mangleWasmImports
}).apply(compiler);
new ReadFileCompileAsyncWasmPlugin().apply(compiler);
2018-04-04 14:53:58 +08:00
new FunctionModulePlugin().apply(compiler);
new NodeTargetPlugin().apply(compiler);
new LoaderTargetPlugin("node").apply(compiler);
new StartupChunkDependenciesPlugin({
asyncChunkLoading: options.target === "async-node"
}).apply(compiler);
2017-02-21 21:49:15 +08:00
break;
}
case "node-webkit": {
const JsonpTemplatePlugin = require("./web/JsonpTemplatePlugin");
const NodeTargetPlugin = require("./node/NodeTargetPlugin");
const ExternalsPlugin = require("./ExternalsPlugin");
const StartupChunkDependenciesPlugin = require("./runtime/StartupChunkDependenciesPlugin");
2018-04-04 14:53:58 +08:00
new JsonpTemplatePlugin().apply(compiler);
new FunctionModulePlugin().apply(compiler);
new NodeTargetPlugin().apply(compiler);
new ExternalsPlugin("commonjs", "nw.gui").apply(compiler);
new LoaderTargetPlugin(options.target).apply(compiler);
new StartupChunkDependenciesPlugin({
asyncChunkLoading: true
}).apply(compiler);
2017-02-21 21:49:15 +08:00
break;
}
case "electron-main": {
const NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
const NodeTargetPlugin = require("./node/NodeTargetPlugin");
const ExternalsPlugin = require("./ExternalsPlugin");
const StartupChunkDependenciesPlugin = require("./runtime/StartupChunkDependenciesPlugin");
new NodeTemplatePlugin({
asyncChunkLoading: true
}).apply(compiler);
2018-04-04 14:53:58 +08:00
new FunctionModulePlugin().apply(compiler);
new NodeTargetPlugin().apply(compiler);
new ExternalsPlugin("commonjs", [
"app",
"auto-updater",
"browser-window",
"clipboard",
"content-tracing",
"crash-reporter",
"dialog",
"electron",
"global-shortcut",
"ipc",
"ipc-main",
"menu",
"menu-item",
"native-image",
"original-fs",
"power-monitor",
"power-save-blocker",
"protocol",
"screen",
"session",
"shell",
"tray",
"web-contents"
]).apply(compiler);
new LoaderTargetPlugin(options.target).apply(compiler);
new StartupChunkDependenciesPlugin({
asyncChunkLoading: true
}).apply(compiler);
2017-02-21 21:49:15 +08:00
break;
}
2017-02-21 21:49:15 +08:00
case "electron-renderer":
2019-06-05 05:23:25 +08:00
case "electron-preload": {
const FetchCompileWasmPlugin = require("./web/FetchCompileWasmPlugin");
const FetchCompileAsyncWasmPlugin = require("./web/FetchCompileAsyncWasmPlugin");
const NodeTargetPlugin = require("./node/NodeTargetPlugin");
const ExternalsPlugin = require("./ExternalsPlugin");
if (options.target === "electron-renderer") {
2019-06-05 05:23:25 +08:00
const JsonpTemplatePlugin = require("./web/JsonpTemplatePlugin");
new JsonpTemplatePlugin().apply(compiler);
} else if (options.target === "electron-preload") {
2019-06-05 05:23:25 +08:00
const NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
new NodeTemplatePlugin({
asyncChunkLoading: true
}).apply(compiler);
}
new FetchCompileWasmPlugin({
mangleImports: options.optimization.mangleWasmImports
}).apply(compiler);
new FetchCompileAsyncWasmPlugin().apply(compiler);
2018-04-04 14:53:58 +08:00
new FunctionModulePlugin().apply(compiler);
new NodeTargetPlugin().apply(compiler);
new ExternalsPlugin("commonjs", [
"clipboard",
"crash-reporter",
"desktop-capturer",
"electron",
"ipc",
"ipc-renderer",
"native-image",
"original-fs",
"remote",
"screen",
"shell",
"web-frame"
]).apply(compiler);
new LoaderTargetPlugin(options.target).apply(compiler);
2017-02-21 21:49:15 +08:00
break;
}
2017-02-21 21:49:15 +08:00
default:
throw new Error("Unsupported target '" + options.target + "'.");
}
2018-09-19 19:05:22 +08:00
}
// @ts-ignore This is always true, which is good this way
else if (options.target !== false) {
2017-02-21 21:49:15 +08:00
options.target(compiler);
} else {
throw new Error("Unsupported target '" + options.target + "'.");
}
2018-02-25 09:00:20 +08:00
if (options.output.library || options.output.libraryTarget !== "var") {
const LibraryTemplatePlugin = require("./LibraryTemplatePlugin");
2018-02-25 09:00:20 +08:00
new LibraryTemplatePlugin(
options.output.library,
options.output.libraryTarget,
options.output.umdNamedDefine,
options.output.auxiliaryComment || "",
options.output.libraryExport
).apply(compiler);
2017-02-21 21:49:15 +08:00
}
2018-02-25 09:00:20 +08:00
if (options.externals) {
const ExternalsPlugin = require("./ExternalsPlugin");
2018-02-25 09:00:20 +08:00
new ExternalsPlugin(
options.output.libraryTarget,
options.externals
).apply(compiler);
2017-02-21 21:49:15 +08:00
}
2018-02-25 09:00:20 +08:00
if (
options.devtool &&
(options.devtool.includes("sourcemap") ||
options.devtool.includes("source-map"))
) {
const hidden = options.devtool.includes("hidden");
const inline = options.devtool.includes("inline");
const evalWrapped = options.devtool.includes("eval");
const cheap = options.devtool.includes("cheap");
const moduleMaps = options.devtool.includes("module");
const noSources = options.devtool.includes("nosources");
const legacy = options.devtool.includes("@");
const modern = options.devtool.includes("#");
const comment =
2018-02-25 09:00:20 +08:00
legacy && modern
? "\n/*\n//@ source" +
2018-03-26 22:56:10 +08:00
"MappingURL=[url]\n//# source" +
"MappingURL=[url]\n*/"
2018-02-25 09:00:20 +08:00
: legacy
2019-02-05 17:06:32 +08:00
? "\n/*\n//@ source" + "MappingURL=[url]\n*/"
: modern
? "\n//# source" + "MappingURL=[url]"
: null;
const Plugin = evalWrapped
2018-02-25 09:00:20 +08:00
? EvalSourceMapDevToolPlugin
: SourceMapDevToolPlugin;
new Plugin({
2017-02-21 21:49:15 +08:00
filename: inline ? null : options.output.sourceMapFilename,
moduleFilenameTemplate: options.output.devtoolModuleFilenameTemplate,
2018-02-25 09:00:20 +08:00
fallbackModuleFilenameTemplate:
options.output.devtoolFallbackModuleFilenameTemplate,
2017-02-21 21:49:15 +08:00
append: hidden ? false : comment,
module: moduleMaps ? true : cheap ? false : true,
columns: cheap ? false : true,
noSources: noSources,
namespace: options.output.devtoolNamespace
}).apply(compiler);
2018-02-25 09:00:20 +08:00
} else if (options.devtool && options.devtool.includes("eval")) {
const legacy = options.devtool.includes("@");
const modern = options.devtool.includes("#");
const comment =
2018-02-25 09:00:20 +08:00
legacy && modern
? "\n//@ sourceURL=[url]\n//# sourceURL=[url]"
: legacy
2019-02-05 17:06:32 +08:00
? "\n//@ sourceURL=[url]"
: modern
? "\n//# sourceURL=[url]"
: null;
new EvalDevToolModulePlugin({
sourceUrlComment: comment,
moduleFilenameTemplate: options.output.devtoolModuleFilenameTemplate,
namespace: options.output.devtoolNamespace
}).apply(compiler);
2017-02-21 21:49:15 +08:00
}
2013-06-19 17:53:03 +08:00
new JavascriptModulesPlugin().apply(compiler);
new JsonModulesPlugin().apply(compiler);
if (options.experiments.asset) {
const AssetModulesPlugin = require("./asset/AssetModulesPlugin");
new AssetModulesPlugin().apply(compiler);
}
if (options.experiments.syncWebAssembly) {
const WebAssemblyModulesPlugin = require("./wasm/WebAssemblyModulesPlugin");
new WebAssemblyModulesPlugin({
mangleImports: options.optimization.mangleWasmImports
}).apply(compiler);
}
if (options.experiments.asyncWebAssembly) {
const AsyncWebAssemblyModulesPlugin = require("./wasm-async/AsyncWebAssemblyModulesPlugin");
new AsyncWebAssemblyModulesPlugin({
mangleImports: options.optimization.mangleWasmImports
}).apply(compiler);
}
new EntryOptionPlugin().apply(compiler);
2017-11-27 22:27:37 +08:00
compiler.hooks.entryOption.call(options.context, options.entry);
2013-06-19 17:53:03 +08:00
2018-11-17 01:18:44 +08:00
new RuntimePlugin().apply(compiler);
if (options.experiments.importAwait || options.experiments.importAsync) {
const InferAsyncModulesPlugin = require("./async-modules/InferAsyncModulesPlugin");
new InferAsyncModulesPlugin({
errorOnMissingAwait: !options.experiments.importAsync
}).apply(compiler);
}
2019-06-05 17:15:25 +08:00
new CompatibilityPlugin().apply(compiler);
new HarmonyModulesPlugin({
module: options.module,
topLevelAwait: options.experiments.topLevelAwait,
importAwait: options.experiments.importAwait
}).apply(compiler);
2019-03-11 00:13:53 +08:00
if (options.amd !== false) {
const AMDPlugin = require("./dependencies/AMDPlugin");
const RequireJsStuffPlugin = require("./RequireJsStuffPlugin");
new AMDPlugin(options.module, options.amd || {}).apply(compiler);
new RequireJsStuffPlugin().apply(compiler);
}
new CommonJsPlugin(options.module).apply(compiler);
new LoaderPlugin().apply(compiler);
if (options.node !== false) {
const NodeStuffPlugin = require("./NodeStuffPlugin");
new NodeStuffPlugin(options.node).apply(compiler);
}
new CommonJsStuffPlugin().apply(compiler);
new APIPlugin().apply(compiler);
new ExportsInfoApiPlugin().apply(compiler);
new ConstPlugin().apply(compiler);
new UseStrictPlugin().apply(compiler);
new RequireIncludePlugin().apply(compiler);
new RequireEnsurePlugin().apply(compiler);
2018-02-25 09:00:20 +08:00
new RequireContextPlugin(
options.resolve.modules,
options.resolve.extensions,
options.resolve.mainFiles
).apply(compiler);
new ImportPlugin(options.module).apply(compiler);
new SystemPlugin(options.module).apply(compiler);
2013-01-31 01:49:25 +08:00
new DefaultStatsFactoryPlugin().apply(compiler);
new DefaultStatsPresetPlugin().apply(compiler);
new DefaultStatsPrinterPlugin().apply(compiler);
if (typeof options.mode !== "string") {
const WarnNoModeSetPlugin = require("./WarnNoModeSetPlugin");
new WarnNoModeSetPlugin().apply(compiler);
}
const EnsureChunkConditionsPlugin = require("./optimize/EnsureChunkConditionsPlugin");
new EnsureChunkConditionsPlugin().apply(compiler);
if (options.optimization.removeAvailableModules) {
const RemoveParentModulesPlugin = require("./optimize/RemoveParentModulesPlugin");
new RemoveParentModulesPlugin().apply(compiler);
}
if (options.optimization.removeEmptyChunks) {
const RemoveEmptyChunksPlugin = require("./optimize/RemoveEmptyChunksPlugin");
new RemoveEmptyChunksPlugin().apply(compiler);
}
if (options.optimization.mergeDuplicateChunks) {
2019-03-12 05:18:50 +08:00
const MergeDuplicateChunksPlugin = require("./optimize/MergeDuplicateChunksPlugin");
new MergeDuplicateChunksPlugin().apply(compiler);
}
if (options.optimization.flagIncludedChunks) {
2019-03-12 05:18:50 +08:00
const FlagIncludedChunksPlugin = require("./optimize/FlagIncludedChunksPlugin");
new FlagIncludedChunksPlugin().apply(compiler);
}
if (options.optimization.sideEffects) {
2019-03-12 05:18:50 +08:00
const SideEffectsFlagPlugin = require("./optimize/SideEffectsFlagPlugin");
new SideEffectsFlagPlugin().apply(compiler);
}
if (options.optimization.providedExports) {
2019-03-12 05:18:50 +08:00
const FlagDependencyExportsPlugin = require("./FlagDependencyExportsPlugin");
new FlagDependencyExportsPlugin().apply(compiler);
}
if (options.optimization.usedExports) {
2019-03-12 05:18:50 +08:00
const FlagDependencyUsagePlugin = require("./FlagDependencyUsagePlugin");
new FlagDependencyUsagePlugin().apply(compiler);
}
2019-01-28 17:40:32 +08:00
if (options.optimization.mangleExports) {
2019-05-10 03:34:28 +08:00
const MangleExportsPlugin = require("./optimize/MangleExportsPlugin");
2019-01-28 17:40:32 +08:00
new MangleExportsPlugin().apply(compiler);
}
if (options.optimization.concatenateModules) {
2019-03-12 05:18:50 +08:00
const ModuleConcatenationPlugin = require("./optimize/ModuleConcatenationPlugin");
new ModuleConcatenationPlugin().apply(compiler);
}
if (options.optimization.splitChunks) {
2019-03-12 05:18:50 +08:00
const SplitChunksPlugin = require("./optimize/SplitChunksPlugin");
new SplitChunksPlugin(options.optimization.splitChunks).apply(compiler);
}
if (options.optimization.runtimeChunk) {
2019-03-12 05:18:50 +08:00
const RuntimeChunkPlugin = require("./optimize/RuntimeChunkPlugin");
new RuntimeChunkPlugin(options.optimization.runtimeChunk).apply(compiler);
}
if (options.optimization.noEmitOnErrors) {
2019-03-12 05:18:50 +08:00
const NoEmitOnErrorsPlugin = require("./NoEmitOnErrorsPlugin");
new NoEmitOnErrorsPlugin().apply(compiler);
}
if (options.optimization.checkWasmTypes) {
2019-03-12 05:18:50 +08:00
const WasmFinalizeExportsPlugin = require("./wasm/WasmFinalizeExportsPlugin");
new WasmFinalizeExportsPlugin().apply(compiler);
}
2018-07-15 03:48:49 +08:00
const moduleIds = options.optimization.moduleIds;
if (moduleIds) {
switch (moduleIds) {
case "natural": {
const NaturalModuleIdsPlugin = require("./ids/NaturalModuleIdsPlugin");
2018-07-23 02:25:56 +08:00
new NaturalModuleIdsPlugin().apply(compiler);
break;
}
case "named": {
const NamedModuleIdsPlugin = require("./ids/NamedModuleIdsPlugin");
new NamedModuleIdsPlugin().apply(compiler);
break;
}
case "hashed": {
const WarnDeprecatedOptionPlugin = require("./WarnDeprecatedOptionPlugin");
const HashedModuleIdsPlugin = require("./ids/HashedModuleIdsPlugin");
new WarnDeprecatedOptionPlugin(
"optimization.moduleIds",
"hashed",
"deterministic"
).apply(compiler);
new HashedModuleIdsPlugin().apply(compiler);
break;
}
case "deterministic": {
const DeterministicModuleIdsPlugin = require("./ids/DeterministicModuleIdsPlugin");
new DeterministicModuleIdsPlugin().apply(compiler);
break;
}
case "size": {
const OccurrenceModuleIdsPlugin = require("./ids/OccurrenceModuleIdsPlugin");
new OccurrenceModuleIdsPlugin({
prioritiseInitial: true
}).apply(compiler);
break;
}
default:
throw new Error(
`webpack bug: moduleIds: ${moduleIds} is not implemented`
);
}
2018-06-07 20:22:35 +08:00
}
2018-07-15 03:48:49 +08:00
const chunkIds = options.optimization.chunkIds;
if (chunkIds) {
switch (chunkIds) {
case "natural": {
const NaturalChunkIdsPlugin = require("./ids/NaturalChunkIdsPlugin");
2018-09-05 20:22:10 +08:00
new NaturalChunkIdsPlugin().apply(compiler);
break;
}
case "named": {
const NamedChunkIdsPlugin = require("./ids/NamedChunkIdsPlugin");
2018-09-05 20:22:10 +08:00
new NamedChunkIdsPlugin().apply(compiler);
break;
}
case "deterministic": {
const DeterministicChunkIdsPlugin = require("./ids/DeterministicChunkIdsPlugin");
new DeterministicChunkIdsPlugin().apply(compiler);
break;
}
case "size":
2018-09-05 20:22:10 +08:00
new OccurrenceChunkIdsPlugin({
prioritiseInitial: true
}).apply(compiler);
break;
case "total-size":
2018-09-05 20:22:10 +08:00
new OccurrenceChunkIdsPlugin({
prioritiseInitial: false
}).apply(compiler);
break;
default:
throw new Error(
`webpack bug: chunkIds: ${chunkIds} is not implemented`
);
}
}
2018-02-25 09:00:20 +08:00
if (options.optimization.nodeEnv) {
2019-03-12 05:18:50 +08:00
const DefinePlugin = require("./DefinePlugin");
new DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(options.optimization.nodeEnv)
}).apply(compiler);
}
2018-02-25 09:00:20 +08:00
if (options.optimization.minimize) {
for (const minimizer of options.optimization.minimizer) {
2018-09-19 19:05:22 +08:00
if (typeof minimizer === "function") {
minimizer.call(compiler, compiler);
2018-09-19 19:05:22 +08:00
} else {
minimizer.apply(compiler);
}
2017-12-13 23:05:21 +08:00
}
}
2013-05-08 19:28:54 +08:00
2018-02-25 09:00:20 +08:00
if (options.performance) {
2019-03-12 05:18:50 +08:00
const SizeLimitsPlugin = require("./performance/SizeLimitsPlugin");
new SizeLimitsPlugin(options.performance).apply(compiler);
2017-02-21 21:49:15 +08:00
}
new TemplatedPathPlugin().apply(compiler);
2014-08-22 19:51:24 +08:00
new RecordIdsPlugin({
portableIds: options.optimization.portableRecords
}).apply(compiler);
2013-05-31 18:22:40 +08:00
new WarnCaseSensitiveModulesPlugin().apply(compiler);
2018-10-09 20:30:59 +08:00
if (options.cache && typeof options.cache === "object") {
const cacheOptions = options.cache;
const AddManagedPathsPlugin = require("./cache/AddManagedPathsPlugin");
new AddManagedPathsPlugin(
cacheOptions.managedPaths,
cacheOptions.immutablePaths
).apply(compiler);
switch (cacheOptions.type) {
2018-10-09 20:30:59 +08:00
case "memory": {
const MemoryCachePlugin = require("./cache/MemoryCachePlugin");
new MemoryCachePlugin().apply(compiler);
break;
}
case "filesystem": {
2019-08-09 20:46:42 +08:00
const AddBuildDependenciesPlugin = require("./cache/AddBuildDependenciesPlugin");
for (const key in cacheOptions.buildDependencies) {
2019-08-09 20:46:42 +08:00
const list = cacheOptions.buildDependencies[key];
new AddBuildDependenciesPlugin(list).apply(compiler);
}
const MemoryCachePlugin = require("./cache/MemoryCachePlugin");
new MemoryCachePlugin().apply(compiler);
switch (cacheOptions.store) {
case "instant": {
const InstantFileCachePlugin = require("./cache/InstantFileCachePlugin");
const SeparateFilesCacheStrategy = require("./cache/SeparateFilesCacheStrategy");
new InstantFileCachePlugin(
new SeparateFilesCacheStrategy({
fs: compiler.intermediateFileSystem,
cacheLocation: cacheOptions.cacheLocation,
version: cacheOptions.version,
hashAlgorithm: cacheOptions.hashAlgorithm,
logger: compiler.getInfrastructureLogger(
"webpack.cache.SeparateFilesCacheStrategy"
)
})
).apply(compiler);
break;
}
case "background": {
const BackgroundFileCachePlugin = require("./cache/BackgroundFileCachePlugin");
const SeparateFilesCacheStrategy = require("./cache/SeparateFilesCacheStrategy");
new BackgroundFileCachePlugin(
new SeparateFilesCacheStrategy({
fs: compiler.intermediateFileSystem,
cacheLocation: cacheOptions.cacheLocation,
version: cacheOptions.version,
hashAlgorithm: cacheOptions.hashAlgorithm,
logger: compiler.getInfrastructureLogger(
"webpack.cache.SeparateFilesCacheStrategy"
)
})
).apply(compiler);
break;
}
case "idle": {
const IdleFileCachePlugin = require("./cache/IdleFileCachePlugin");
const SeparateFilesCacheStrategy = require("./cache/SeparateFilesCacheStrategy");
new IdleFileCachePlugin(
new SeparateFilesCacheStrategy({
fs: compiler.intermediateFileSystem,
cacheLocation: cacheOptions.cacheLocation,
version: cacheOptions.version,
hashAlgorithm: cacheOptions.hashAlgorithm,
logger: compiler.getInfrastructureLogger(
"webpack.cache.SeparateFilesCacheStrategy"
)
}),
cacheOptions.idleTimeout,
cacheOptions.idleTimeoutForInitialStore
).apply(compiler);
break;
}
case "pack": {
const IdleFileCachePlugin = require("./cache/IdleFileCachePlugin");
const PackFileCacheStrategy = require("./cache/PackFileCacheStrategy");
new IdleFileCachePlugin(
new PackFileCacheStrategy({
fs: compiler.intermediateFileSystem,
context: options.context,
cacheLocation: cacheOptions.cacheLocation,
version: cacheOptions.version,
logger: compiler.getInfrastructureLogger(
"webpack.cache.PackFileCacheStrategy"
),
managedPaths: cacheOptions.managedPaths,
immutablePaths: cacheOptions.immutablePaths
}),
cacheOptions.idleTimeout,
cacheOptions.idleTimeoutForInitialStore
).apply(compiler);
break;
}
}
2018-10-09 20:30:59 +08:00
break;
}
default:
// @ts-ignore never is expected here
throw new Error(`Unknown cache type ${options.cache.type}`);
}
2017-02-21 21:49:15 +08:00
}
2018-10-23 13:22:44 +08:00
new ResolverCachePlugin().apply(compiler);
2017-02-21 21:49:15 +08:00
2017-11-27 22:27:37 +08:00
compiler.hooks.afterPlugins.call(compiler);
if (!compiler.inputFileSystem) {
2018-02-25 09:00:20 +08:00
throw new Error("No input filesystem provided");
}
2018-02-25 09:00:20 +08:00
compiler.resolverFactory.hooks.resolveOptions
.for("normal")
.tap("WebpackOptionsApply", resolveOptions => {
return {
fileSystem: compiler.inputFileSystem,
...cachedCleverMerge(options.resolve, resolveOptions)
};
2018-02-25 09:00:20 +08:00
});
compiler.resolverFactory.hooks.resolveOptions
.for("context")
.tap("WebpackOptionsApply", resolveOptions => {
return {
fileSystem: compiler.inputFileSystem,
resolveToContext: true,
...cachedCleverMerge(options.resolve, resolveOptions)
};
2018-02-25 09:00:20 +08:00
});
compiler.resolverFactory.hooks.resolveOptions
.for("loader")
.tap("WebpackOptionsApply", resolveOptions => {
return {
fileSystem: compiler.inputFileSystem,
...cachedCleverMerge(options.resolveLoader, resolveOptions)
};
2018-02-25 09:00:20 +08:00
});
2017-11-27 22:27:37 +08:00
compiler.hooks.afterResolvers.call(compiler);
2017-02-21 21:49:15 +08:00
return options;
2013-07-11 05:20:07 +08:00
}
2017-02-21 21:49:15 +08:00
}
2013-01-31 01:49:25 +08:00
2017-02-21 21:49:15 +08:00
module.exports = WebpackOptionsApply;