[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const OptionsApply = require("./OptionsApply");
|
---|
| 9 |
|
---|
| 10 | const AssetModulesPlugin = require("./asset/AssetModulesPlugin");
|
---|
| 11 | const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
|
---|
| 12 | const JsonModulesPlugin = require("./json/JsonModulesPlugin");
|
---|
| 13 |
|
---|
| 14 | const ChunkPrefetchPreloadPlugin = require("./prefetch/ChunkPrefetchPreloadPlugin");
|
---|
| 15 |
|
---|
| 16 | const EntryOptionPlugin = require("./EntryOptionPlugin");
|
---|
| 17 | const RecordIdsPlugin = require("./RecordIdsPlugin");
|
---|
| 18 |
|
---|
| 19 | const RuntimePlugin = require("./RuntimePlugin");
|
---|
| 20 |
|
---|
| 21 | const APIPlugin = require("./APIPlugin");
|
---|
| 22 | const CompatibilityPlugin = require("./CompatibilityPlugin");
|
---|
| 23 | const ConstPlugin = require("./ConstPlugin");
|
---|
| 24 | const ExportsInfoApiPlugin = require("./ExportsInfoApiPlugin");
|
---|
| 25 | const WebpackIsIncludedPlugin = require("./WebpackIsIncludedPlugin");
|
---|
| 26 |
|
---|
| 27 | const TemplatedPathPlugin = require("./TemplatedPathPlugin");
|
---|
| 28 | const UseStrictPlugin = require("./UseStrictPlugin");
|
---|
| 29 | const WarnCaseSensitiveModulesPlugin = require("./WarnCaseSensitiveModulesPlugin");
|
---|
| 30 |
|
---|
| 31 | const DataUriPlugin = require("./schemes/DataUriPlugin");
|
---|
| 32 | const FileUriPlugin = require("./schemes/FileUriPlugin");
|
---|
| 33 |
|
---|
| 34 | const ResolverCachePlugin = require("./cache/ResolverCachePlugin");
|
---|
| 35 |
|
---|
| 36 | const CommonJsPlugin = require("./dependencies/CommonJsPlugin");
|
---|
| 37 | const HarmonyModulesPlugin = require("./dependencies/HarmonyModulesPlugin");
|
---|
| 38 | const ImportMetaPlugin = require("./dependencies/ImportMetaPlugin");
|
---|
| 39 | const ImportPlugin = require("./dependencies/ImportPlugin");
|
---|
| 40 | const LoaderPlugin = require("./dependencies/LoaderPlugin");
|
---|
| 41 | const RequireContextPlugin = require("./dependencies/RequireContextPlugin");
|
---|
| 42 | const RequireEnsurePlugin = require("./dependencies/RequireEnsurePlugin");
|
---|
| 43 | const RequireIncludePlugin = require("./dependencies/RequireIncludePlugin");
|
---|
| 44 | const SystemPlugin = require("./dependencies/SystemPlugin");
|
---|
| 45 | const URLPlugin = require("./dependencies/URLPlugin");
|
---|
| 46 | const WorkerPlugin = require("./dependencies/WorkerPlugin");
|
---|
| 47 |
|
---|
| 48 | const InferAsyncModulesPlugin = require("./async-modules/InferAsyncModulesPlugin");
|
---|
| 49 |
|
---|
| 50 | const JavascriptMetaInfoPlugin = require("./JavascriptMetaInfoPlugin");
|
---|
| 51 | const DefaultStatsFactoryPlugin = require("./stats/DefaultStatsFactoryPlugin");
|
---|
| 52 | const DefaultStatsPresetPlugin = require("./stats/DefaultStatsPresetPlugin");
|
---|
| 53 | const DefaultStatsPrinterPlugin = require("./stats/DefaultStatsPrinterPlugin");
|
---|
| 54 |
|
---|
| 55 | const { cleverMerge } = require("./util/cleverMerge");
|
---|
| 56 |
|
---|
| 57 | /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
|
---|
| 58 | /** @typedef {import("./Compiler")} Compiler */
|
---|
| 59 |
|
---|
| 60 | class WebpackOptionsApply extends OptionsApply {
|
---|
| 61 | constructor() {
|
---|
| 62 | super();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /**
|
---|
| 66 | * @param {WebpackOptions} options options object
|
---|
| 67 | * @param {Compiler} compiler compiler object
|
---|
| 68 | * @returns {WebpackOptions} options object
|
---|
| 69 | */
|
---|
| 70 | process(options, compiler) {
|
---|
| 71 | compiler.outputPath = options.output.path;
|
---|
| 72 | compiler.recordsInputPath = options.recordsInputPath || null;
|
---|
| 73 | compiler.recordsOutputPath = options.recordsOutputPath || null;
|
---|
| 74 | compiler.name = options.name;
|
---|
| 75 |
|
---|
| 76 | if (options.externals) {
|
---|
| 77 | //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 78 | const ExternalsPlugin = require("./ExternalsPlugin");
|
---|
| 79 | new ExternalsPlugin(options.externalsType, options.externals).apply(
|
---|
| 80 | compiler
|
---|
| 81 | );
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | if (options.externalsPresets.node) {
|
---|
| 85 | const NodeTargetPlugin = require("./node/NodeTargetPlugin");
|
---|
| 86 | new NodeTargetPlugin().apply(compiler);
|
---|
| 87 | }
|
---|
| 88 | if (options.externalsPresets.electronMain) {
|
---|
| 89 | //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 90 | const ElectronTargetPlugin = require("./electron/ElectronTargetPlugin");
|
---|
| 91 | new ElectronTargetPlugin("main").apply(compiler);
|
---|
| 92 | }
|
---|
| 93 | if (options.externalsPresets.electronPreload) {
|
---|
| 94 | //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 95 | const ElectronTargetPlugin = require("./electron/ElectronTargetPlugin");
|
---|
| 96 | new ElectronTargetPlugin("preload").apply(compiler);
|
---|
| 97 | }
|
---|
| 98 | if (options.externalsPresets.electronRenderer) {
|
---|
| 99 | //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 100 | const ElectronTargetPlugin = require("./electron/ElectronTargetPlugin");
|
---|
| 101 | new ElectronTargetPlugin("renderer").apply(compiler);
|
---|
| 102 | }
|
---|
| 103 | if (
|
---|
| 104 | options.externalsPresets.electron &&
|
---|
| 105 | !options.externalsPresets.electronMain &&
|
---|
| 106 | !options.externalsPresets.electronPreload &&
|
---|
| 107 | !options.externalsPresets.electronRenderer
|
---|
| 108 | ) {
|
---|
| 109 | //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 110 | const ElectronTargetPlugin = require("./electron/ElectronTargetPlugin");
|
---|
| 111 | new ElectronTargetPlugin().apply(compiler);
|
---|
| 112 | }
|
---|
| 113 | if (options.externalsPresets.nwjs) {
|
---|
| 114 | //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 115 | const ExternalsPlugin = require("./ExternalsPlugin");
|
---|
| 116 | new ExternalsPlugin("node-commonjs", "nw.gui").apply(compiler);
|
---|
| 117 | }
|
---|
| 118 | if (options.externalsPresets.webAsync) {
|
---|
| 119 | //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 120 | const ExternalsPlugin = require("./ExternalsPlugin");
|
---|
| 121 | new ExternalsPlugin("import", /^(https?:\/\/|std:)/).apply(compiler);
|
---|
| 122 | } else if (options.externalsPresets.web) {
|
---|
| 123 | //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 124 | const ExternalsPlugin = require("./ExternalsPlugin");
|
---|
| 125 | new ExternalsPlugin("module", /^(https?:\/\/|std:)/).apply(compiler);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | new ChunkPrefetchPreloadPlugin().apply(compiler);
|
---|
| 129 |
|
---|
| 130 | if (typeof options.output.chunkFormat === "string") {
|
---|
| 131 | switch (options.output.chunkFormat) {
|
---|
| 132 | case "array-push": {
|
---|
| 133 | const ArrayPushCallbackChunkFormatPlugin = require("./javascript/ArrayPushCallbackChunkFormatPlugin");
|
---|
| 134 | new ArrayPushCallbackChunkFormatPlugin().apply(compiler);
|
---|
| 135 | break;
|
---|
| 136 | }
|
---|
| 137 | case "commonjs": {
|
---|
| 138 | const CommonJsChunkFormatPlugin = require("./javascript/CommonJsChunkFormatPlugin");
|
---|
| 139 | new CommonJsChunkFormatPlugin().apply(compiler);
|
---|
| 140 | break;
|
---|
| 141 | }
|
---|
| 142 | case "module": {
|
---|
| 143 | const ModuleChunkFormatPlugin = require("./esm/ModuleChunkFormatPlugin");
|
---|
| 144 | new ModuleChunkFormatPlugin().apply(compiler);
|
---|
| 145 | break;
|
---|
| 146 | }
|
---|
| 147 | default:
|
---|
| 148 | throw new Error(
|
---|
| 149 | "Unsupported chunk format '" + options.output.chunkFormat + "'."
|
---|
| 150 | );
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | if (options.output.enabledChunkLoadingTypes.length > 0) {
|
---|
| 155 | for (const type of options.output.enabledChunkLoadingTypes) {
|
---|
| 156 | const EnableChunkLoadingPlugin = require("./javascript/EnableChunkLoadingPlugin");
|
---|
| 157 | new EnableChunkLoadingPlugin(type).apply(compiler);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | if (options.output.enabledWasmLoadingTypes.length > 0) {
|
---|
| 162 | for (const type of options.output.enabledWasmLoadingTypes) {
|
---|
| 163 | const EnableWasmLoadingPlugin = require("./wasm/EnableWasmLoadingPlugin");
|
---|
| 164 | new EnableWasmLoadingPlugin(type).apply(compiler);
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | if (options.output.enabledLibraryTypes.length > 0) {
|
---|
| 169 | for (const type of options.output.enabledLibraryTypes) {
|
---|
| 170 | const EnableLibraryPlugin = require("./library/EnableLibraryPlugin");
|
---|
| 171 | new EnableLibraryPlugin(type).apply(compiler);
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | if (options.output.pathinfo) {
|
---|
| 176 | const ModuleInfoHeaderPlugin = require("./ModuleInfoHeaderPlugin");
|
---|
| 177 | new ModuleInfoHeaderPlugin(options.output.pathinfo !== true).apply(
|
---|
| 178 | compiler
|
---|
| 179 | );
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | if (options.output.clean) {
|
---|
| 183 | const CleanPlugin = require("./CleanPlugin");
|
---|
| 184 | new CleanPlugin(
|
---|
| 185 | options.output.clean === true ? {} : options.output.clean
|
---|
| 186 | ).apply(compiler);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | if (options.devtool) {
|
---|
| 190 | if (options.devtool.includes("source-map")) {
|
---|
| 191 | const hidden = options.devtool.includes("hidden");
|
---|
| 192 | const inline = options.devtool.includes("inline");
|
---|
| 193 | const evalWrapped = options.devtool.includes("eval");
|
---|
| 194 | const cheap = options.devtool.includes("cheap");
|
---|
| 195 | const moduleMaps = options.devtool.includes("module");
|
---|
| 196 | const noSources = options.devtool.includes("nosources");
|
---|
| 197 | const Plugin = evalWrapped
|
---|
| 198 | ? require("./EvalSourceMapDevToolPlugin")
|
---|
| 199 | : require("./SourceMapDevToolPlugin");
|
---|
| 200 | new Plugin({
|
---|
| 201 | filename: inline ? null : options.output.sourceMapFilename,
|
---|
| 202 | moduleFilenameTemplate: options.output.devtoolModuleFilenameTemplate,
|
---|
| 203 | fallbackModuleFilenameTemplate:
|
---|
| 204 | options.output.devtoolFallbackModuleFilenameTemplate,
|
---|
| 205 | append: hidden ? false : undefined,
|
---|
| 206 | module: moduleMaps ? true : cheap ? false : true,
|
---|
| 207 | columns: cheap ? false : true,
|
---|
| 208 | noSources: noSources,
|
---|
| 209 | namespace: options.output.devtoolNamespace
|
---|
| 210 | }).apply(compiler);
|
---|
| 211 | } else if (options.devtool.includes("eval")) {
|
---|
| 212 | const EvalDevToolModulePlugin = require("./EvalDevToolModulePlugin");
|
---|
| 213 | new EvalDevToolModulePlugin({
|
---|
| 214 | moduleFilenameTemplate: options.output.devtoolModuleFilenameTemplate,
|
---|
| 215 | namespace: options.output.devtoolNamespace
|
---|
| 216 | }).apply(compiler);
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | new JavascriptModulesPlugin().apply(compiler);
|
---|
| 221 | new JsonModulesPlugin().apply(compiler);
|
---|
| 222 | new AssetModulesPlugin().apply(compiler);
|
---|
| 223 |
|
---|
| 224 | if (!options.experiments.outputModule) {
|
---|
| 225 | if (options.output.module) {
|
---|
| 226 | throw new Error(
|
---|
| 227 | "'output.module: true' is only allowed when 'experiments.outputModule' is enabled"
|
---|
| 228 | );
|
---|
| 229 | }
|
---|
| 230 | if (options.output.enabledLibraryTypes.includes("module")) {
|
---|
| 231 | throw new Error(
|
---|
| 232 | "library type \"module\" is only allowed when 'experiments.outputModule' is enabled"
|
---|
| 233 | );
|
---|
| 234 | }
|
---|
| 235 | if (options.externalsType === "module") {
|
---|
| 236 | throw new Error(
|
---|
| 237 | "'externalsType: \"module\"' is only allowed when 'experiments.outputModule' is enabled"
|
---|
| 238 | );
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | if (options.experiments.syncWebAssembly) {
|
---|
| 243 | const WebAssemblyModulesPlugin = require("./wasm-sync/WebAssemblyModulesPlugin");
|
---|
| 244 | new WebAssemblyModulesPlugin({
|
---|
| 245 | mangleImports: options.optimization.mangleWasmImports
|
---|
| 246 | }).apply(compiler);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | if (options.experiments.asyncWebAssembly) {
|
---|
| 250 | const AsyncWebAssemblyModulesPlugin = require("./wasm-async/AsyncWebAssemblyModulesPlugin");
|
---|
| 251 | new AsyncWebAssemblyModulesPlugin({
|
---|
| 252 | mangleImports: options.optimization.mangleWasmImports
|
---|
| 253 | }).apply(compiler);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | if (options.experiments.lazyCompilation) {
|
---|
| 257 | const LazyCompilationPlugin = require("./hmr/LazyCompilationPlugin");
|
---|
| 258 | const lazyOptions =
|
---|
| 259 | typeof options.experiments.lazyCompilation === "object"
|
---|
| 260 | ? options.experiments.lazyCompilation
|
---|
| 261 | : null;
|
---|
| 262 | new LazyCompilationPlugin({
|
---|
| 263 | backend:
|
---|
| 264 | (lazyOptions && lazyOptions.backend) ||
|
---|
| 265 | require("./hmr/lazyCompilationBackend"),
|
---|
| 266 | client:
|
---|
| 267 | (lazyOptions && lazyOptions.client) ||
|
---|
| 268 | require.resolve(
|
---|
| 269 | `../hot/lazy-compilation-${
|
---|
| 270 | options.externalsPresets.node ? "node" : "web"
|
---|
| 271 | }.js`
|
---|
| 272 | ),
|
---|
| 273 | entries: !lazyOptions || lazyOptions.entries !== false,
|
---|
| 274 | imports: !lazyOptions || lazyOptions.imports !== false,
|
---|
| 275 | test: (lazyOptions && lazyOptions.test) || undefined
|
---|
| 276 | }).apply(compiler);
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | if (options.experiments.buildHttp) {
|
---|
| 280 | const HttpUriPlugin = require("./schemes/HttpUriPlugin");
|
---|
| 281 | const httpOptions = options.experiments.buildHttp;
|
---|
| 282 | if (httpOptions === true)
|
---|
| 283 | throw new Error("Unexpected due to normalization");
|
---|
| 284 | new HttpUriPlugin(httpOptions).apply(compiler);
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | new EntryOptionPlugin().apply(compiler);
|
---|
| 288 | compiler.hooks.entryOption.call(options.context, options.entry);
|
---|
| 289 |
|
---|
| 290 | new RuntimePlugin().apply(compiler);
|
---|
| 291 |
|
---|
| 292 | new InferAsyncModulesPlugin().apply(compiler);
|
---|
| 293 |
|
---|
| 294 | new DataUriPlugin().apply(compiler);
|
---|
| 295 | new FileUriPlugin().apply(compiler);
|
---|
| 296 |
|
---|
| 297 | new CompatibilityPlugin().apply(compiler);
|
---|
| 298 | new HarmonyModulesPlugin({
|
---|
| 299 | topLevelAwait: options.experiments.topLevelAwait
|
---|
| 300 | }).apply(compiler);
|
---|
| 301 | if (options.amd !== false) {
|
---|
| 302 | const AMDPlugin = require("./dependencies/AMDPlugin");
|
---|
| 303 | const RequireJsStuffPlugin = require("./RequireJsStuffPlugin");
|
---|
| 304 | new AMDPlugin(options.amd || {}).apply(compiler);
|
---|
| 305 | new RequireJsStuffPlugin().apply(compiler);
|
---|
| 306 | }
|
---|
| 307 | new CommonJsPlugin().apply(compiler);
|
---|
| 308 | new LoaderPlugin({
|
---|
| 309 | enableExecuteModule: options.experiments.executeModule
|
---|
| 310 | }).apply(compiler);
|
---|
| 311 | if (options.node !== false) {
|
---|
| 312 | const NodeStuffPlugin = require("./NodeStuffPlugin");
|
---|
| 313 | new NodeStuffPlugin(options.node).apply(compiler);
|
---|
| 314 | }
|
---|
| 315 | new APIPlugin().apply(compiler);
|
---|
| 316 | new ExportsInfoApiPlugin().apply(compiler);
|
---|
| 317 | new WebpackIsIncludedPlugin().apply(compiler);
|
---|
| 318 | new ConstPlugin().apply(compiler);
|
---|
| 319 | new UseStrictPlugin().apply(compiler);
|
---|
| 320 | new RequireIncludePlugin().apply(compiler);
|
---|
| 321 | new RequireEnsurePlugin().apply(compiler);
|
---|
| 322 | new RequireContextPlugin().apply(compiler);
|
---|
| 323 | new ImportPlugin().apply(compiler);
|
---|
| 324 | new SystemPlugin().apply(compiler);
|
---|
| 325 | new ImportMetaPlugin().apply(compiler);
|
---|
| 326 | new URLPlugin().apply(compiler);
|
---|
| 327 | new WorkerPlugin(
|
---|
| 328 | options.output.workerChunkLoading,
|
---|
| 329 | options.output.workerWasmLoading,
|
---|
| 330 | options.output.module
|
---|
| 331 | ).apply(compiler);
|
---|
| 332 |
|
---|
| 333 | new DefaultStatsFactoryPlugin().apply(compiler);
|
---|
| 334 | new DefaultStatsPresetPlugin().apply(compiler);
|
---|
| 335 | new DefaultStatsPrinterPlugin().apply(compiler);
|
---|
| 336 |
|
---|
| 337 | new JavascriptMetaInfoPlugin().apply(compiler);
|
---|
| 338 |
|
---|
| 339 | if (typeof options.mode !== "string") {
|
---|
| 340 | const WarnNoModeSetPlugin = require("./WarnNoModeSetPlugin");
|
---|
| 341 | new WarnNoModeSetPlugin().apply(compiler);
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | const EnsureChunkConditionsPlugin = require("./optimize/EnsureChunkConditionsPlugin");
|
---|
| 345 | new EnsureChunkConditionsPlugin().apply(compiler);
|
---|
| 346 | if (options.optimization.removeAvailableModules) {
|
---|
| 347 | const RemoveParentModulesPlugin = require("./optimize/RemoveParentModulesPlugin");
|
---|
| 348 | new RemoveParentModulesPlugin().apply(compiler);
|
---|
| 349 | }
|
---|
| 350 | if (options.optimization.removeEmptyChunks) {
|
---|
| 351 | const RemoveEmptyChunksPlugin = require("./optimize/RemoveEmptyChunksPlugin");
|
---|
| 352 | new RemoveEmptyChunksPlugin().apply(compiler);
|
---|
| 353 | }
|
---|
| 354 | if (options.optimization.mergeDuplicateChunks) {
|
---|
| 355 | const MergeDuplicateChunksPlugin = require("./optimize/MergeDuplicateChunksPlugin");
|
---|
| 356 | new MergeDuplicateChunksPlugin().apply(compiler);
|
---|
| 357 | }
|
---|
| 358 | if (options.optimization.flagIncludedChunks) {
|
---|
| 359 | const FlagIncludedChunksPlugin = require("./optimize/FlagIncludedChunksPlugin");
|
---|
| 360 | new FlagIncludedChunksPlugin().apply(compiler);
|
---|
| 361 | }
|
---|
| 362 | if (options.optimization.sideEffects) {
|
---|
| 363 | const SideEffectsFlagPlugin = require("./optimize/SideEffectsFlagPlugin");
|
---|
| 364 | new SideEffectsFlagPlugin(
|
---|
| 365 | options.optimization.sideEffects === true
|
---|
| 366 | ).apply(compiler);
|
---|
| 367 | }
|
---|
| 368 | if (options.optimization.providedExports) {
|
---|
| 369 | const FlagDependencyExportsPlugin = require("./FlagDependencyExportsPlugin");
|
---|
| 370 | new FlagDependencyExportsPlugin().apply(compiler);
|
---|
| 371 | }
|
---|
| 372 | if (options.optimization.usedExports) {
|
---|
| 373 | const FlagDependencyUsagePlugin = require("./FlagDependencyUsagePlugin");
|
---|
| 374 | new FlagDependencyUsagePlugin(
|
---|
| 375 | options.optimization.usedExports === "global"
|
---|
| 376 | ).apply(compiler);
|
---|
| 377 | }
|
---|
| 378 | if (options.optimization.innerGraph) {
|
---|
| 379 | const InnerGraphPlugin = require("./optimize/InnerGraphPlugin");
|
---|
| 380 | new InnerGraphPlugin().apply(compiler);
|
---|
| 381 | }
|
---|
| 382 | if (options.optimization.mangleExports) {
|
---|
| 383 | const MangleExportsPlugin = require("./optimize/MangleExportsPlugin");
|
---|
| 384 | new MangleExportsPlugin(
|
---|
| 385 | options.optimization.mangleExports !== "size"
|
---|
| 386 | ).apply(compiler);
|
---|
| 387 | }
|
---|
| 388 | if (options.optimization.concatenateModules) {
|
---|
| 389 | const ModuleConcatenationPlugin = require("./optimize/ModuleConcatenationPlugin");
|
---|
| 390 | new ModuleConcatenationPlugin().apply(compiler);
|
---|
| 391 | }
|
---|
| 392 | if (options.optimization.splitChunks) {
|
---|
| 393 | const SplitChunksPlugin = require("./optimize/SplitChunksPlugin");
|
---|
| 394 | new SplitChunksPlugin(options.optimization.splitChunks).apply(compiler);
|
---|
| 395 | }
|
---|
| 396 | if (options.optimization.runtimeChunk) {
|
---|
| 397 | const RuntimeChunkPlugin = require("./optimize/RuntimeChunkPlugin");
|
---|
| 398 | new RuntimeChunkPlugin(options.optimization.runtimeChunk).apply(compiler);
|
---|
| 399 | }
|
---|
| 400 | if (!options.optimization.emitOnErrors) {
|
---|
| 401 | const NoEmitOnErrorsPlugin = require("./NoEmitOnErrorsPlugin");
|
---|
| 402 | new NoEmitOnErrorsPlugin().apply(compiler);
|
---|
| 403 | }
|
---|
| 404 | if (options.optimization.realContentHash) {
|
---|
| 405 | const RealContentHashPlugin = require("./optimize/RealContentHashPlugin");
|
---|
| 406 | new RealContentHashPlugin({
|
---|
| 407 | hashFunction: options.output.hashFunction,
|
---|
| 408 | hashDigest: options.output.hashDigest
|
---|
| 409 | }).apply(compiler);
|
---|
| 410 | }
|
---|
| 411 | if (options.optimization.checkWasmTypes) {
|
---|
| 412 | const WasmFinalizeExportsPlugin = require("./wasm-sync/WasmFinalizeExportsPlugin");
|
---|
| 413 | new WasmFinalizeExportsPlugin().apply(compiler);
|
---|
| 414 | }
|
---|
| 415 | const moduleIds = options.optimization.moduleIds;
|
---|
| 416 | if (moduleIds) {
|
---|
| 417 | switch (moduleIds) {
|
---|
| 418 | case "natural": {
|
---|
| 419 | const NaturalModuleIdsPlugin = require("./ids/NaturalModuleIdsPlugin");
|
---|
| 420 | new NaturalModuleIdsPlugin().apply(compiler);
|
---|
| 421 | break;
|
---|
| 422 | }
|
---|
| 423 | case "named": {
|
---|
| 424 | const NamedModuleIdsPlugin = require("./ids/NamedModuleIdsPlugin");
|
---|
| 425 | new NamedModuleIdsPlugin().apply(compiler);
|
---|
| 426 | break;
|
---|
| 427 | }
|
---|
| 428 | case "hashed": {
|
---|
| 429 | const WarnDeprecatedOptionPlugin = require("./WarnDeprecatedOptionPlugin");
|
---|
| 430 | const HashedModuleIdsPlugin = require("./ids/HashedModuleIdsPlugin");
|
---|
| 431 | new WarnDeprecatedOptionPlugin(
|
---|
| 432 | "optimization.moduleIds",
|
---|
| 433 | "hashed",
|
---|
| 434 | "deterministic"
|
---|
| 435 | ).apply(compiler);
|
---|
| 436 | new HashedModuleIdsPlugin().apply(compiler);
|
---|
| 437 | break;
|
---|
| 438 | }
|
---|
| 439 | case "deterministic": {
|
---|
| 440 | const DeterministicModuleIdsPlugin = require("./ids/DeterministicModuleIdsPlugin");
|
---|
| 441 | new DeterministicModuleIdsPlugin().apply(compiler);
|
---|
| 442 | break;
|
---|
| 443 | }
|
---|
| 444 | case "size": {
|
---|
| 445 | const OccurrenceModuleIdsPlugin = require("./ids/OccurrenceModuleIdsPlugin");
|
---|
| 446 | new OccurrenceModuleIdsPlugin({
|
---|
| 447 | prioritiseInitial: true
|
---|
| 448 | }).apply(compiler);
|
---|
| 449 | break;
|
---|
| 450 | }
|
---|
| 451 | default:
|
---|
| 452 | throw new Error(
|
---|
| 453 | `webpack bug: moduleIds: ${moduleIds} is not implemented`
|
---|
| 454 | );
|
---|
| 455 | }
|
---|
| 456 | }
|
---|
| 457 | const chunkIds = options.optimization.chunkIds;
|
---|
| 458 | if (chunkIds) {
|
---|
| 459 | switch (chunkIds) {
|
---|
| 460 | case "natural": {
|
---|
| 461 | const NaturalChunkIdsPlugin = require("./ids/NaturalChunkIdsPlugin");
|
---|
| 462 | new NaturalChunkIdsPlugin().apply(compiler);
|
---|
| 463 | break;
|
---|
| 464 | }
|
---|
| 465 | case "named": {
|
---|
| 466 | const NamedChunkIdsPlugin = require("./ids/NamedChunkIdsPlugin");
|
---|
| 467 | new NamedChunkIdsPlugin().apply(compiler);
|
---|
| 468 | break;
|
---|
| 469 | }
|
---|
| 470 | case "deterministic": {
|
---|
| 471 | const DeterministicChunkIdsPlugin = require("./ids/DeterministicChunkIdsPlugin");
|
---|
| 472 | new DeterministicChunkIdsPlugin().apply(compiler);
|
---|
| 473 | break;
|
---|
| 474 | }
|
---|
| 475 | case "size": {
|
---|
| 476 | //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 477 | const OccurrenceChunkIdsPlugin = require("./ids/OccurrenceChunkIdsPlugin");
|
---|
| 478 | new OccurrenceChunkIdsPlugin({
|
---|
| 479 | prioritiseInitial: true
|
---|
| 480 | }).apply(compiler);
|
---|
| 481 | break;
|
---|
| 482 | }
|
---|
| 483 | case "total-size": {
|
---|
| 484 | //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 485 | const OccurrenceChunkIdsPlugin = require("./ids/OccurrenceChunkIdsPlugin");
|
---|
| 486 | new OccurrenceChunkIdsPlugin({
|
---|
| 487 | prioritiseInitial: false
|
---|
| 488 | }).apply(compiler);
|
---|
| 489 | break;
|
---|
| 490 | }
|
---|
| 491 | default:
|
---|
| 492 | throw new Error(
|
---|
| 493 | `webpack bug: chunkIds: ${chunkIds} is not implemented`
|
---|
| 494 | );
|
---|
| 495 | }
|
---|
| 496 | }
|
---|
| 497 | if (options.optimization.nodeEnv) {
|
---|
| 498 | const DefinePlugin = require("./DefinePlugin");
|
---|
| 499 | new DefinePlugin({
|
---|
| 500 | "process.env.NODE_ENV": JSON.stringify(options.optimization.nodeEnv)
|
---|
| 501 | }).apply(compiler);
|
---|
| 502 | }
|
---|
| 503 | if (options.optimization.minimize) {
|
---|
| 504 | for (const minimizer of options.optimization.minimizer) {
|
---|
| 505 | if (typeof minimizer === "function") {
|
---|
| 506 | minimizer.call(compiler, compiler);
|
---|
| 507 | } else if (minimizer !== "...") {
|
---|
| 508 | minimizer.apply(compiler);
|
---|
| 509 | }
|
---|
| 510 | }
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | if (options.performance) {
|
---|
| 514 | const SizeLimitsPlugin = require("./performance/SizeLimitsPlugin");
|
---|
| 515 | new SizeLimitsPlugin(options.performance).apply(compiler);
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | new TemplatedPathPlugin().apply(compiler);
|
---|
| 519 |
|
---|
| 520 | new RecordIdsPlugin({
|
---|
| 521 | portableIds: options.optimization.portableRecords
|
---|
| 522 | }).apply(compiler);
|
---|
| 523 |
|
---|
| 524 | new WarnCaseSensitiveModulesPlugin().apply(compiler);
|
---|
| 525 |
|
---|
| 526 | const AddManagedPathsPlugin = require("./cache/AddManagedPathsPlugin");
|
---|
| 527 | new AddManagedPathsPlugin(
|
---|
| 528 | options.snapshot.managedPaths,
|
---|
| 529 | options.snapshot.immutablePaths
|
---|
| 530 | ).apply(compiler);
|
---|
| 531 |
|
---|
| 532 | if (options.cache && typeof options.cache === "object") {
|
---|
| 533 | const cacheOptions = options.cache;
|
---|
| 534 | switch (cacheOptions.type) {
|
---|
| 535 | case "memory": {
|
---|
| 536 | if (isFinite(cacheOptions.maxGenerations)) {
|
---|
| 537 | //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 538 | const MemoryWithGcCachePlugin = require("./cache/MemoryWithGcCachePlugin");
|
---|
| 539 | new MemoryWithGcCachePlugin({
|
---|
| 540 | maxGenerations: cacheOptions.maxGenerations
|
---|
| 541 | }).apply(compiler);
|
---|
| 542 | } else {
|
---|
| 543 | //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 544 | const MemoryCachePlugin = require("./cache/MemoryCachePlugin");
|
---|
| 545 | new MemoryCachePlugin().apply(compiler);
|
---|
| 546 | }
|
---|
| 547 | break;
|
---|
| 548 | }
|
---|
| 549 | case "filesystem": {
|
---|
| 550 | const AddBuildDependenciesPlugin = require("./cache/AddBuildDependenciesPlugin");
|
---|
| 551 | for (const key in cacheOptions.buildDependencies) {
|
---|
| 552 | const list = cacheOptions.buildDependencies[key];
|
---|
| 553 | new AddBuildDependenciesPlugin(list).apply(compiler);
|
---|
| 554 | }
|
---|
| 555 | if (!isFinite(cacheOptions.maxMemoryGenerations)) {
|
---|
| 556 | //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 557 | const MemoryCachePlugin = require("./cache/MemoryCachePlugin");
|
---|
| 558 | new MemoryCachePlugin().apply(compiler);
|
---|
| 559 | } else if (cacheOptions.maxMemoryGenerations !== 0) {
|
---|
| 560 | //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 561 | const MemoryWithGcCachePlugin = require("./cache/MemoryWithGcCachePlugin");
|
---|
| 562 | new MemoryWithGcCachePlugin({
|
---|
| 563 | maxGenerations: cacheOptions.maxMemoryGenerations
|
---|
| 564 | }).apply(compiler);
|
---|
| 565 | }
|
---|
| 566 | switch (cacheOptions.store) {
|
---|
| 567 | case "pack": {
|
---|
| 568 | const IdleFileCachePlugin = require("./cache/IdleFileCachePlugin");
|
---|
| 569 | const PackFileCacheStrategy = require("./cache/PackFileCacheStrategy");
|
---|
| 570 | new IdleFileCachePlugin(
|
---|
| 571 | new PackFileCacheStrategy({
|
---|
| 572 | compiler,
|
---|
| 573 | fs: compiler.intermediateFileSystem,
|
---|
| 574 | context: options.context,
|
---|
| 575 | cacheLocation: cacheOptions.cacheLocation,
|
---|
| 576 | version: cacheOptions.version,
|
---|
| 577 | logger: compiler.getInfrastructureLogger(
|
---|
| 578 | "webpack.cache.PackFileCacheStrategy"
|
---|
| 579 | ),
|
---|
| 580 | snapshot: options.snapshot,
|
---|
| 581 | maxAge: cacheOptions.maxAge,
|
---|
| 582 | profile: cacheOptions.profile,
|
---|
| 583 | allowCollectingMemory: cacheOptions.allowCollectingMemory,
|
---|
| 584 | compression: cacheOptions.compression
|
---|
| 585 | }),
|
---|
| 586 | cacheOptions.idleTimeout,
|
---|
| 587 | cacheOptions.idleTimeoutForInitialStore,
|
---|
| 588 | cacheOptions.idleTimeoutAfterLargeChanges
|
---|
| 589 | ).apply(compiler);
|
---|
| 590 | break;
|
---|
| 591 | }
|
---|
| 592 | default:
|
---|
| 593 | throw new Error("Unhandled value for cache.store");
|
---|
| 594 | }
|
---|
| 595 | break;
|
---|
| 596 | }
|
---|
| 597 | default:
|
---|
| 598 | // @ts-expect-error Property 'type' does not exist on type 'never'. ts(2339)
|
---|
| 599 | throw new Error(`Unknown cache type ${cacheOptions.type}`);
|
---|
| 600 | }
|
---|
| 601 | }
|
---|
| 602 | new ResolverCachePlugin().apply(compiler);
|
---|
| 603 |
|
---|
| 604 | if (options.ignoreWarnings && options.ignoreWarnings.length > 0) {
|
---|
| 605 | const IgnoreWarningsPlugin = require("./IgnoreWarningsPlugin");
|
---|
| 606 | new IgnoreWarningsPlugin(options.ignoreWarnings).apply(compiler);
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | compiler.hooks.afterPlugins.call(compiler);
|
---|
| 610 | if (!compiler.inputFileSystem) {
|
---|
| 611 | throw new Error("No input filesystem provided");
|
---|
| 612 | }
|
---|
| 613 | compiler.resolverFactory.hooks.resolveOptions
|
---|
| 614 | .for("normal")
|
---|
| 615 | .tap("WebpackOptionsApply", resolveOptions => {
|
---|
| 616 | resolveOptions = cleverMerge(options.resolve, resolveOptions);
|
---|
| 617 | resolveOptions.fileSystem = compiler.inputFileSystem;
|
---|
| 618 | return resolveOptions;
|
---|
| 619 | });
|
---|
| 620 | compiler.resolverFactory.hooks.resolveOptions
|
---|
| 621 | .for("context")
|
---|
| 622 | .tap("WebpackOptionsApply", resolveOptions => {
|
---|
| 623 | resolveOptions = cleverMerge(options.resolve, resolveOptions);
|
---|
| 624 | resolveOptions.fileSystem = compiler.inputFileSystem;
|
---|
| 625 | resolveOptions.resolveToContext = true;
|
---|
| 626 | return resolveOptions;
|
---|
| 627 | });
|
---|
| 628 | compiler.resolverFactory.hooks.resolveOptions
|
---|
| 629 | .for("loader")
|
---|
| 630 | .tap("WebpackOptionsApply", resolveOptions => {
|
---|
| 631 | resolveOptions = cleverMerge(options.resolveLoader, resolveOptions);
|
---|
| 632 | resolveOptions.fileSystem = compiler.inputFileSystem;
|
---|
| 633 | return resolveOptions;
|
---|
| 634 | });
|
---|
| 635 | compiler.hooks.afterResolvers.call(compiler);
|
---|
| 636 | return options;
|
---|
| 637 | }
|
---|
| 638 | }
|
---|
| 639 |
|
---|
| 640 | module.exports = WebpackOptionsApply;
|
---|