| 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 util = require("util");
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {import("../../declarations/WebpackOptions").CacheOptionsNormalized} CacheOptions */
|
|---|
| 11 | /** @typedef {import("../../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescriptionNormalized */
|
|---|
| 12 | /** @typedef {import("../../declarations/WebpackOptions").EntryStatic} EntryStatic */
|
|---|
| 13 | /** @typedef {import("../../declarations/WebpackOptions").EntryStaticNormalized} EntryStaticNormalized */
|
|---|
| 14 | /** @typedef {import("../../declarations/WebpackOptions").Externals} Externals */
|
|---|
| 15 | /** @typedef {import("../../declarations/WebpackOptions").LibraryName} LibraryName */
|
|---|
| 16 | /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
|
|---|
| 17 | /** @typedef {import("../../declarations/WebpackOptions").ModuleOptionsNormalized} ModuleOptionsNormalized */
|
|---|
| 18 | /** @typedef {import("../../declarations/WebpackOptions").OptimizationNormalized} OptimizationNormalized */
|
|---|
| 19 | /** @typedef {import("../../declarations/WebpackOptions").OptimizationRuntimeChunk} OptimizationRuntimeChunk */
|
|---|
| 20 | /** @typedef {import("../../declarations/WebpackOptions").OptimizationRuntimeChunkNormalized} OptimizationRuntimeChunkNormalized */
|
|---|
| 21 | /** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} OutputNormalized */
|
|---|
| 22 | /** @typedef {import("../../declarations/WebpackOptions").PluginsNormalized} PluginsNormalized */
|
|---|
| 23 | /** @typedef {import("../../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
|
|---|
| 24 | /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptionsNormalized */
|
|---|
| 25 | /** @typedef {import("../errors/WebpackError")} WebpackError */
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Defines the webpack options interception type used by this module.
|
|---|
| 29 | * @typedef {object} WebpackOptionsInterception
|
|---|
| 30 | * @property {WebpackOptionsNormalized["devtool"]=} devtool
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | const handledDeprecatedNoEmitOnErrors = util.deprecate(
|
|---|
| 34 | /**
|
|---|
| 35 | * Handles the callback logic for this hook.
|
|---|
| 36 | * @param {boolean} noEmitOnErrors no emit on errors
|
|---|
| 37 | * @param {boolean | undefined} emitOnErrors emit on errors
|
|---|
| 38 | * @returns {boolean} emit on errors
|
|---|
| 39 | */
|
|---|
| 40 | (noEmitOnErrors, emitOnErrors) => {
|
|---|
| 41 | if (emitOnErrors !== undefined && !noEmitOnErrors === !emitOnErrors) {
|
|---|
| 42 | throw new Error(
|
|---|
| 43 | "Conflicting use of 'optimization.noEmitOnErrors' and 'optimization.emitOnErrors'. Remove deprecated 'optimization.noEmitOnErrors' from config."
|
|---|
| 44 | );
|
|---|
| 45 | }
|
|---|
| 46 | return !noEmitOnErrors;
|
|---|
| 47 | },
|
|---|
| 48 | "optimization.noEmitOnErrors is deprecated in favor of optimization.emitOnErrors",
|
|---|
| 49 | "DEP_WEBPACK_CONFIGURATION_OPTIMIZATION_NO_EMIT_ON_ERRORS"
|
|---|
| 50 | );
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Returns result value.
|
|---|
| 54 | * @template T
|
|---|
| 55 | * @template R
|
|---|
| 56 | * @param {T | undefined} value value or not
|
|---|
| 57 | * @param {(value: T) => R} fn nested handler
|
|---|
| 58 | * @returns {R} result value
|
|---|
| 59 | */
|
|---|
| 60 | const nestedConfig = (value, fn) =>
|
|---|
| 61 | value === undefined ? fn(/** @type {T} */ ({})) : fn(value);
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Returns result value.
|
|---|
| 65 | * @template T
|
|---|
| 66 | * @param {T | undefined} value value or not
|
|---|
| 67 | * @returns {T} result value
|
|---|
| 68 | */
|
|---|
| 69 | const cloneObject = (value) => /** @type {T} */ ({ ...value });
|
|---|
| 70 | /**
|
|---|
| 71 | * Optional nested config.
|
|---|
| 72 | * @template T
|
|---|
| 73 | * @template R
|
|---|
| 74 | * @param {T | undefined} value value or not
|
|---|
| 75 | * @param {(value: T) => R} fn nested handler
|
|---|
| 76 | * @returns {R | undefined} result value
|
|---|
| 77 | */
|
|---|
| 78 | const optionalNestedConfig = (value, fn) =>
|
|---|
| 79 | value === undefined ? undefined : fn(value);
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * Returns cloned value.
|
|---|
| 83 | * @template T
|
|---|
| 84 | * @template R
|
|---|
| 85 | * @param {T[] | undefined} value array or not
|
|---|
| 86 | * @param {(value: T[]) => R[]} fn nested handler
|
|---|
| 87 | * @returns {R[] | undefined} cloned value
|
|---|
| 88 | */
|
|---|
| 89 | const nestedArray = (value, fn) => (Array.isArray(value) ? fn(value) : fn([]));
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * Optional nested array.
|
|---|
| 93 | * @template T
|
|---|
| 94 | * @template R
|
|---|
| 95 | * @param {T[] | undefined} value array or not
|
|---|
| 96 | * @param {(value: T[]) => R[]} fn nested handler
|
|---|
| 97 | * @returns {R[] | undefined} cloned value
|
|---|
| 98 | */
|
|---|
| 99 | const optionalNestedArray = (value, fn) =>
|
|---|
| 100 | Array.isArray(value) ? fn(value) : undefined;
|
|---|
| 101 |
|
|---|
| 102 | /**
|
|---|
| 103 | * Keyed nested config.
|
|---|
| 104 | * @template T
|
|---|
| 105 | * @template R
|
|---|
| 106 | * @param {Record<string, T> | undefined} value value or not
|
|---|
| 107 | * @param {(value: T) => R} fn nested handler
|
|---|
| 108 | * @param {Record<string, (value: T) => R>=} customKeys custom nested handler for some keys
|
|---|
| 109 | * @returns {Record<string, R>} result value
|
|---|
| 110 | */
|
|---|
| 111 | const keyedNestedConfig = (value, fn, customKeys) => {
|
|---|
| 112 | /* eslint-disable no-sequences */
|
|---|
| 113 | const result =
|
|---|
| 114 | value === undefined
|
|---|
| 115 | ? {}
|
|---|
| 116 | : Object.keys(value).reduce(
|
|---|
| 117 | (obj, key) => (
|
|---|
| 118 | (obj[key] = (
|
|---|
| 119 | customKeys && key in customKeys ? customKeys[key] : fn
|
|---|
| 120 | )(value[key])),
|
|---|
| 121 | obj
|
|---|
| 122 | ),
|
|---|
| 123 | /** @type {Record<string, R>} */ ({})
|
|---|
| 124 | );
|
|---|
| 125 | /* eslint-enable no-sequences */
|
|---|
| 126 | if (customKeys) {
|
|---|
| 127 | for (const key of Object.keys(customKeys)) {
|
|---|
| 128 | if (!(key in result)) {
|
|---|
| 129 | result[key] = customKeys[key](/** @type {T} */ ({}));
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 | return result;
|
|---|
| 134 | };
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Gets normalized webpack options.
|
|---|
| 138 | * @param {WebpackOptions} config input config
|
|---|
| 139 | * @returns {WebpackOptionsNormalized} normalized options
|
|---|
| 140 | */
|
|---|
| 141 | const getNormalizedWebpackOptions = (config) => ({
|
|---|
| 142 | amd: config.amd,
|
|---|
| 143 | bail: config.bail,
|
|---|
| 144 | cache:
|
|---|
| 145 | /** @type {NonNullable<CacheOptions>} */
|
|---|
| 146 | (
|
|---|
| 147 | optionalNestedConfig(config.cache, (cache) => {
|
|---|
| 148 | if (cache === false) return false;
|
|---|
| 149 | if (cache === true) {
|
|---|
| 150 | return {
|
|---|
| 151 | type: "memory",
|
|---|
| 152 | maxGenerations: undefined
|
|---|
| 153 | };
|
|---|
| 154 | }
|
|---|
| 155 | switch (cache.type) {
|
|---|
| 156 | case "filesystem":
|
|---|
| 157 | return {
|
|---|
| 158 | type: "filesystem",
|
|---|
| 159 | allowCollectingMemory: cache.allowCollectingMemory,
|
|---|
| 160 | maxMemoryGenerations: cache.maxMemoryGenerations,
|
|---|
| 161 | maxAge: cache.maxAge,
|
|---|
| 162 | profile: cache.profile,
|
|---|
| 163 | buildDependencies: cloneObject(cache.buildDependencies),
|
|---|
| 164 | cacheDirectory: cache.cacheDirectory,
|
|---|
| 165 | cacheLocation: cache.cacheLocation,
|
|---|
| 166 | hashAlgorithm: cache.hashAlgorithm,
|
|---|
| 167 | compression: cache.compression,
|
|---|
| 168 | idleTimeout: cache.idleTimeout,
|
|---|
| 169 | idleTimeoutForInitialStore: cache.idleTimeoutForInitialStore,
|
|---|
| 170 | idleTimeoutAfterLargeChanges: cache.idleTimeoutAfterLargeChanges,
|
|---|
| 171 | name: cache.name,
|
|---|
| 172 | store: cache.store,
|
|---|
| 173 | version: cache.version,
|
|---|
| 174 | readonly: cache.readonly
|
|---|
| 175 | };
|
|---|
| 176 | case undefined:
|
|---|
| 177 | case "memory":
|
|---|
| 178 | return {
|
|---|
| 179 | type: "memory",
|
|---|
| 180 | maxGenerations: cache.maxGenerations
|
|---|
| 181 | };
|
|---|
| 182 | default:
|
|---|
| 183 | // @ts-expect-error Property 'type' does not exist on type 'never'. ts(2339)
|
|---|
| 184 | throw new Error(`Not implemented cache.type ${cache.type}`);
|
|---|
| 185 | }
|
|---|
| 186 | })
|
|---|
| 187 | ),
|
|---|
| 188 | context: config.context,
|
|---|
| 189 | dependencies: config.dependencies,
|
|---|
| 190 | devServer: optionalNestedConfig(config.devServer, (devServer) => {
|
|---|
| 191 | if (devServer === false) return false;
|
|---|
| 192 | return { ...devServer };
|
|---|
| 193 | }),
|
|---|
| 194 | devtool: config.devtool,
|
|---|
| 195 | dotenv: config.dotenv,
|
|---|
| 196 | entry:
|
|---|
| 197 | config.entry === undefined
|
|---|
| 198 | ? { main: {} }
|
|---|
| 199 | : typeof config.entry === "function"
|
|---|
| 200 | ? (
|
|---|
| 201 | (fn) => () =>
|
|---|
| 202 | Promise.resolve().then(fn).then(getNormalizedEntryStatic)
|
|---|
| 203 | )(config.entry)
|
|---|
| 204 | : getNormalizedEntryStatic(config.entry),
|
|---|
| 205 | experiments: nestedConfig(config.experiments, (experiments) => ({
|
|---|
| 206 | ...experiments,
|
|---|
| 207 | buildHttp: optionalNestedConfig(experiments.buildHttp, (options) =>
|
|---|
| 208 | Array.isArray(options) ? { allowedUris: options } : options
|
|---|
| 209 | ),
|
|---|
| 210 | lazyCompilation: optionalNestedConfig(
|
|---|
| 211 | experiments.lazyCompilation,
|
|---|
| 212 | (options) => (options === true ? {} : options)
|
|---|
| 213 | )
|
|---|
| 214 | })),
|
|---|
| 215 | externals: /** @type {NonNullable<Externals>} */ (config.externals),
|
|---|
| 216 | externalsPresets: cloneObject(config.externalsPresets),
|
|---|
| 217 | externalsType: config.externalsType,
|
|---|
| 218 | ignoreWarnings: config.ignoreWarnings
|
|---|
| 219 | ? config.ignoreWarnings.map((ignore) => {
|
|---|
| 220 | if (typeof ignore === "function") return ignore;
|
|---|
| 221 | const i = ignore instanceof RegExp ? { message: ignore } : ignore;
|
|---|
| 222 | return (warning, { requestShortener }) => {
|
|---|
| 223 | if (!i.message && !i.module && !i.file) return false;
|
|---|
| 224 | if (i.message && !i.message.test(warning.message)) {
|
|---|
| 225 | return false;
|
|---|
| 226 | }
|
|---|
| 227 | if (
|
|---|
| 228 | i.module &&
|
|---|
| 229 | (!(/** @type {WebpackError} */ (warning).module) ||
|
|---|
| 230 | !i.module.test(
|
|---|
| 231 | /** @type {WebpackError} */
|
|---|
| 232 | (warning).module.readableIdentifier(requestShortener)
|
|---|
| 233 | ))
|
|---|
| 234 | ) {
|
|---|
| 235 | return false;
|
|---|
| 236 | }
|
|---|
| 237 | if (
|
|---|
| 238 | i.file &&
|
|---|
| 239 | (!(/** @type {WebpackError} */ (warning).file) ||
|
|---|
| 240 | !i.file.test(/** @type {WebpackError} */ (warning).file))
|
|---|
| 241 | ) {
|
|---|
| 242 | return false;
|
|---|
| 243 | }
|
|---|
| 244 | return true;
|
|---|
| 245 | };
|
|---|
| 246 | })
|
|---|
| 247 | : undefined,
|
|---|
| 248 | infrastructureLogging: cloneObject(config.infrastructureLogging),
|
|---|
| 249 | loader: cloneObject(config.loader),
|
|---|
| 250 | mode: config.mode,
|
|---|
| 251 | module:
|
|---|
| 252 | /** @type {ModuleOptionsNormalized} */
|
|---|
| 253 | (
|
|---|
| 254 | nestedConfig(config.module, (module) => ({
|
|---|
| 255 | noParse: module.noParse,
|
|---|
| 256 | unsafeCache: module.unsafeCache,
|
|---|
| 257 | parser: keyedNestedConfig(module.parser, cloneObject, {
|
|---|
| 258 | javascript: (parserOptions) => ({
|
|---|
| 259 | // TODO webpack 6 remove from `ModuleOptions`, keep only `*ByModuleType`
|
|---|
| 260 | unknownContextRequest: module.unknownContextRequest,
|
|---|
| 261 | unknownContextRegExp: module.unknownContextRegExp,
|
|---|
| 262 | unknownContextRecursive: module.unknownContextRecursive,
|
|---|
| 263 | unknownContextCritical: module.unknownContextCritical,
|
|---|
| 264 | exprContextRequest: module.exprContextRequest,
|
|---|
| 265 | exprContextRegExp: module.exprContextRegExp,
|
|---|
| 266 | exprContextRecursive: module.exprContextRecursive,
|
|---|
| 267 | exprContextCritical: module.exprContextCritical,
|
|---|
| 268 | wrappedContextRegExp: module.wrappedContextRegExp,
|
|---|
| 269 | wrappedContextRecursive: module.wrappedContextRecursive,
|
|---|
| 270 | wrappedContextCritical: module.wrappedContextCritical,
|
|---|
| 271 | strictExportPresence: module.strictExportPresence,
|
|---|
| 272 | strictThisContextOnImports: module.strictThisContextOnImports,
|
|---|
| 273 | ...parserOptions
|
|---|
| 274 | })
|
|---|
| 275 | }),
|
|---|
| 276 | generator: cloneObject(module.generator),
|
|---|
| 277 | defaultRules: optionalNestedArray(module.defaultRules, (r) => [...r]),
|
|---|
| 278 | rules: nestedArray(module.rules, (r) => [...r])
|
|---|
| 279 | }))
|
|---|
| 280 | ),
|
|---|
| 281 | name: config.name,
|
|---|
| 282 | node: nestedConfig(
|
|---|
| 283 | config.node,
|
|---|
| 284 | (node) =>
|
|---|
| 285 | node && {
|
|---|
| 286 | ...node
|
|---|
| 287 | }
|
|---|
| 288 | ),
|
|---|
| 289 | optimization: nestedConfig(config.optimization, (optimization) => ({
|
|---|
| 290 | ...optimization,
|
|---|
| 291 | runtimeChunk: getNormalizedOptimizationRuntimeChunk(
|
|---|
| 292 | optimization.runtimeChunk
|
|---|
| 293 | ),
|
|---|
| 294 | splitChunks: nestedConfig(
|
|---|
| 295 | optimization.splitChunks,
|
|---|
| 296 | (splitChunks) =>
|
|---|
| 297 | splitChunks && {
|
|---|
| 298 | ...splitChunks,
|
|---|
| 299 | defaultSizeTypes: splitChunks.defaultSizeTypes
|
|---|
| 300 | ? [...splitChunks.defaultSizeTypes]
|
|---|
| 301 | : ["..."],
|
|---|
| 302 | cacheGroups: cloneObject(splitChunks.cacheGroups)
|
|---|
| 303 | }
|
|---|
| 304 | ),
|
|---|
| 305 | minimizer:
|
|---|
| 306 | optimization.minimizer !== undefined
|
|---|
| 307 | ? /** @type {OptimizationNormalized["minimizer"]} */ (
|
|---|
| 308 | nestedArray(optimization.minimizer, (p) => p.filter(Boolean))
|
|---|
| 309 | )
|
|---|
| 310 | : optimization.minimizer,
|
|---|
| 311 | emitOnErrors:
|
|---|
| 312 | optimization.noEmitOnErrors !== undefined
|
|---|
| 313 | ? handledDeprecatedNoEmitOnErrors(
|
|---|
| 314 | optimization.noEmitOnErrors,
|
|---|
| 315 | optimization.emitOnErrors
|
|---|
| 316 | )
|
|---|
| 317 | : optimization.emitOnErrors
|
|---|
| 318 | })),
|
|---|
| 319 | output: nestedConfig(config.output, (output) => {
|
|---|
| 320 | const { library } = output;
|
|---|
| 321 | const libraryAsName = /** @type {LibraryName} */ (library);
|
|---|
| 322 | const libraryBase =
|
|---|
| 323 | typeof library === "object" &&
|
|---|
| 324 | library &&
|
|---|
| 325 | !Array.isArray(library) &&
|
|---|
| 326 | "type" in library
|
|---|
| 327 | ? library
|
|---|
| 328 | : libraryAsName || output.libraryTarget
|
|---|
| 329 | ? /** @type {LibraryOptions} */ ({
|
|---|
| 330 | name: libraryAsName
|
|---|
| 331 | })
|
|---|
| 332 | : undefined;
|
|---|
| 333 | /** @type {OutputNormalized} */
|
|---|
| 334 | const result = {
|
|---|
| 335 | assetModuleFilename: output.assetModuleFilename,
|
|---|
| 336 | asyncChunks: output.asyncChunks,
|
|---|
| 337 | charset: output.charset,
|
|---|
| 338 | chunkFilename: output.chunkFilename,
|
|---|
| 339 | chunkFormat: output.chunkFormat,
|
|---|
| 340 | chunkLoading: output.chunkLoading,
|
|---|
| 341 | chunkLoadingGlobal: output.chunkLoadingGlobal,
|
|---|
| 342 | chunkLoadTimeout: output.chunkLoadTimeout,
|
|---|
| 343 | cssFilename: output.cssFilename,
|
|---|
| 344 | cssChunkFilename: output.cssChunkFilename,
|
|---|
| 345 | clean: output.clean,
|
|---|
| 346 | compareBeforeEmit: output.compareBeforeEmit,
|
|---|
| 347 | crossOriginLoading: output.crossOriginLoading,
|
|---|
| 348 | devtoolFallbackModuleFilenameTemplate:
|
|---|
| 349 | output.devtoolFallbackModuleFilenameTemplate,
|
|---|
| 350 | devtoolModuleFilenameTemplate: output.devtoolModuleFilenameTemplate,
|
|---|
| 351 | devtoolNamespace: output.devtoolNamespace,
|
|---|
| 352 | environment: cloneObject(output.environment),
|
|---|
| 353 | enabledChunkLoadingTypes: output.enabledChunkLoadingTypes
|
|---|
| 354 | ? [...output.enabledChunkLoadingTypes]
|
|---|
| 355 | : ["..."],
|
|---|
| 356 | enabledLibraryTypes: output.enabledLibraryTypes
|
|---|
| 357 | ? [...output.enabledLibraryTypes]
|
|---|
| 358 | : ["..."],
|
|---|
| 359 | enabledWasmLoadingTypes: output.enabledWasmLoadingTypes
|
|---|
| 360 | ? [...output.enabledWasmLoadingTypes]
|
|---|
| 361 | : ["..."],
|
|---|
| 362 | filename: output.filename,
|
|---|
| 363 | globalObject: output.globalObject,
|
|---|
| 364 | hashDigest: output.hashDigest,
|
|---|
| 365 | hashDigestLength: output.hashDigestLength,
|
|---|
| 366 | hashFunction: output.hashFunction,
|
|---|
| 367 | hashSalt: output.hashSalt,
|
|---|
| 368 | hotUpdateChunkFilename: output.hotUpdateChunkFilename,
|
|---|
| 369 | hotUpdateGlobal: output.hotUpdateGlobal,
|
|---|
| 370 | hotUpdateMainFilename: output.hotUpdateMainFilename,
|
|---|
| 371 | htmlChunkFilename: output.htmlChunkFilename,
|
|---|
| 372 | htmlFilename: output.htmlFilename,
|
|---|
| 373 | ignoreBrowserWarnings: output.ignoreBrowserWarnings,
|
|---|
| 374 | iife: output.iife,
|
|---|
| 375 | importFunctionName: output.importFunctionName,
|
|---|
| 376 | importMetaName: output.importMetaName,
|
|---|
| 377 | scriptType: output.scriptType,
|
|---|
| 378 | // TODO webpack 6 remove `libraryTarget`/`auxiliaryComment`/`amdContainer`/etc in favor of the `library` option
|
|---|
| 379 | library: libraryBase && {
|
|---|
| 380 | type:
|
|---|
| 381 | output.libraryTarget !== undefined
|
|---|
| 382 | ? output.libraryTarget
|
|---|
| 383 | : libraryBase.type,
|
|---|
| 384 | auxiliaryComment:
|
|---|
| 385 | output.auxiliaryComment !== undefined
|
|---|
| 386 | ? output.auxiliaryComment
|
|---|
| 387 | : libraryBase.auxiliaryComment,
|
|---|
| 388 | amdContainer:
|
|---|
| 389 | output.amdContainer !== undefined
|
|---|
| 390 | ? output.amdContainer
|
|---|
| 391 | : libraryBase.amdContainer,
|
|---|
| 392 | export:
|
|---|
| 393 | output.libraryExport !== undefined
|
|---|
| 394 | ? output.libraryExport
|
|---|
| 395 | : libraryBase.export,
|
|---|
| 396 | name: libraryBase.name,
|
|---|
| 397 | umdNamedDefine:
|
|---|
| 398 | output.umdNamedDefine !== undefined
|
|---|
| 399 | ? output.umdNamedDefine
|
|---|
| 400 | : libraryBase.umdNamedDefine
|
|---|
| 401 | },
|
|---|
| 402 | module: output.module,
|
|---|
| 403 | path: output.path,
|
|---|
| 404 | pathinfo: output.pathinfo,
|
|---|
| 405 | publicPath: output.publicPath,
|
|---|
| 406 | sourceMapFilename: output.sourceMapFilename,
|
|---|
| 407 | sourcePrefix: output.sourcePrefix,
|
|---|
| 408 | strictModuleErrorHandling: output.strictModuleErrorHandling,
|
|---|
| 409 | strictModuleExceptionHandling: output.strictModuleExceptionHandling,
|
|---|
| 410 | trustedTypes: optionalNestedConfig(
|
|---|
| 411 | output.trustedTypes,
|
|---|
| 412 | (trustedTypes) => {
|
|---|
| 413 | if (trustedTypes === true) return {};
|
|---|
| 414 | if (typeof trustedTypes === "string") {
|
|---|
| 415 | return { policyName: trustedTypes };
|
|---|
| 416 | }
|
|---|
| 417 | return { ...trustedTypes };
|
|---|
| 418 | }
|
|---|
| 419 | ),
|
|---|
| 420 | uniqueName: output.uniqueName,
|
|---|
| 421 | wasmLoading: output.wasmLoading,
|
|---|
| 422 | webassemblyModuleFilename: output.webassemblyModuleFilename,
|
|---|
| 423 | workerPublicPath: output.workerPublicPath,
|
|---|
| 424 | workerChunkLoading: output.workerChunkLoading,
|
|---|
| 425 | workerWasmLoading: output.workerWasmLoading
|
|---|
| 426 | };
|
|---|
| 427 | return result;
|
|---|
| 428 | }),
|
|---|
| 429 | parallelism: config.parallelism,
|
|---|
| 430 | validate: config.validate,
|
|---|
| 431 | performance: optionalNestedConfig(config.performance, (performance) => {
|
|---|
| 432 | if (performance === false) return false;
|
|---|
| 433 | return {
|
|---|
| 434 | ...performance
|
|---|
| 435 | };
|
|---|
| 436 | }),
|
|---|
| 437 | plugins: /** @type {PluginsNormalized} */ (
|
|---|
| 438 | nestedArray(config.plugins, (p) => p.filter(Boolean))
|
|---|
| 439 | ),
|
|---|
| 440 | profile: config.profile,
|
|---|
| 441 | recordsInputPath:
|
|---|
| 442 | config.recordsInputPath !== undefined
|
|---|
| 443 | ? config.recordsInputPath
|
|---|
| 444 | : config.recordsPath,
|
|---|
| 445 | recordsOutputPath:
|
|---|
| 446 | config.recordsOutputPath !== undefined
|
|---|
| 447 | ? config.recordsOutputPath
|
|---|
| 448 | : config.recordsPath,
|
|---|
| 449 | resolve: nestedConfig(config.resolve, (resolve) => ({
|
|---|
| 450 | ...resolve,
|
|---|
| 451 | byDependency: keyedNestedConfig(resolve.byDependency, cloneObject)
|
|---|
| 452 | })),
|
|---|
| 453 | resolveLoader: cloneObject(config.resolveLoader),
|
|---|
| 454 | snapshot: nestedConfig(config.snapshot, (snapshot) => ({
|
|---|
| 455 | resolveBuildDependencies: optionalNestedConfig(
|
|---|
| 456 | snapshot.resolveBuildDependencies,
|
|---|
| 457 | (resolveBuildDependencies) => ({
|
|---|
| 458 | timestamp: resolveBuildDependencies.timestamp,
|
|---|
| 459 | hash: resolveBuildDependencies.hash
|
|---|
| 460 | })
|
|---|
| 461 | ),
|
|---|
| 462 | buildDependencies: optionalNestedConfig(
|
|---|
| 463 | snapshot.buildDependencies,
|
|---|
| 464 | (buildDependencies) => ({
|
|---|
| 465 | timestamp: buildDependencies.timestamp,
|
|---|
| 466 | hash: buildDependencies.hash
|
|---|
| 467 | })
|
|---|
| 468 | ),
|
|---|
| 469 | resolve: optionalNestedConfig(snapshot.resolve, (resolve) => ({
|
|---|
| 470 | timestamp: resolve.timestamp,
|
|---|
| 471 | hash: resolve.hash
|
|---|
| 472 | })),
|
|---|
| 473 | module: optionalNestedConfig(snapshot.module, (module) => ({
|
|---|
| 474 | timestamp: module.timestamp,
|
|---|
| 475 | hash: module.hash
|
|---|
| 476 | })),
|
|---|
| 477 | contextModule: optionalNestedConfig(
|
|---|
| 478 | snapshot.contextModule,
|
|---|
| 479 | (contextModule) => ({
|
|---|
| 480 | timestamp: contextModule.timestamp,
|
|---|
| 481 | hash: contextModule.hash
|
|---|
| 482 | })
|
|---|
| 483 | ),
|
|---|
| 484 | immutablePaths: optionalNestedArray(snapshot.immutablePaths, (p) => [...p]),
|
|---|
| 485 | managedPaths: optionalNestedArray(snapshot.managedPaths, (p) => [...p]),
|
|---|
| 486 | unmanagedPaths: optionalNestedArray(snapshot.unmanagedPaths, (p) => [...p])
|
|---|
| 487 | })),
|
|---|
| 488 | stats: nestedConfig(config.stats, (stats) => {
|
|---|
| 489 | if (stats === false) {
|
|---|
| 490 | return {
|
|---|
| 491 | preset: "none"
|
|---|
| 492 | };
|
|---|
| 493 | }
|
|---|
| 494 | if (stats === true) {
|
|---|
| 495 | return {
|
|---|
| 496 | preset: "normal"
|
|---|
| 497 | };
|
|---|
| 498 | }
|
|---|
| 499 | if (typeof stats === "string") {
|
|---|
| 500 | return {
|
|---|
| 501 | preset: stats
|
|---|
| 502 | };
|
|---|
| 503 | }
|
|---|
| 504 | return {
|
|---|
| 505 | ...stats
|
|---|
| 506 | };
|
|---|
| 507 | }),
|
|---|
| 508 | target: config.target,
|
|---|
| 509 | watch: config.watch,
|
|---|
| 510 | watchOptions: cloneObject(config.watchOptions)
|
|---|
| 511 | });
|
|---|
| 512 |
|
|---|
| 513 | /**
|
|---|
| 514 | * Gets normalized entry static.
|
|---|
| 515 | * @param {EntryStatic} entry static entry options
|
|---|
| 516 | * @returns {EntryStaticNormalized} normalized static entry options
|
|---|
| 517 | */
|
|---|
| 518 | const getNormalizedEntryStatic = (entry) => {
|
|---|
| 519 | if (typeof entry === "string") {
|
|---|
| 520 | return {
|
|---|
| 521 | main: {
|
|---|
| 522 | import: [entry]
|
|---|
| 523 | }
|
|---|
| 524 | };
|
|---|
| 525 | }
|
|---|
| 526 | if (Array.isArray(entry)) {
|
|---|
| 527 | return {
|
|---|
| 528 | main: {
|
|---|
| 529 | import: entry
|
|---|
| 530 | }
|
|---|
| 531 | };
|
|---|
| 532 | }
|
|---|
| 533 | /** @type {EntryStaticNormalized} */
|
|---|
| 534 | const result = {};
|
|---|
| 535 | for (const key of Object.keys(entry)) {
|
|---|
| 536 | const value = entry[key];
|
|---|
| 537 | if (typeof value === "string") {
|
|---|
| 538 | result[key] = {
|
|---|
| 539 | import: [value]
|
|---|
| 540 | };
|
|---|
| 541 | } else if (Array.isArray(value)) {
|
|---|
| 542 | result[key] = {
|
|---|
| 543 | import: value
|
|---|
| 544 | };
|
|---|
| 545 | } else {
|
|---|
| 546 | result[key] = {
|
|---|
| 547 | import:
|
|---|
| 548 | /** @type {EntryDescriptionNormalized["import"]} */
|
|---|
| 549 | (
|
|---|
| 550 | value.import &&
|
|---|
| 551 | (Array.isArray(value.import) ? value.import : [value.import])
|
|---|
| 552 | ),
|
|---|
| 553 | filename: value.filename,
|
|---|
| 554 | layer: value.layer,
|
|---|
| 555 | runtime: value.runtime,
|
|---|
| 556 | baseUri: value.baseUri,
|
|---|
| 557 | publicPath: value.publicPath,
|
|---|
| 558 | chunkLoading: value.chunkLoading,
|
|---|
| 559 | asyncChunks: value.asyncChunks,
|
|---|
| 560 | wasmLoading: value.wasmLoading,
|
|---|
| 561 | dependOn:
|
|---|
| 562 | /** @type {EntryDescriptionNormalized["dependOn"]} */
|
|---|
| 563 | (
|
|---|
| 564 | value.dependOn &&
|
|---|
| 565 | (Array.isArray(value.dependOn)
|
|---|
| 566 | ? value.dependOn
|
|---|
| 567 | : [value.dependOn])
|
|---|
| 568 | ),
|
|---|
| 569 | library: value.library
|
|---|
| 570 | };
|
|---|
| 571 | }
|
|---|
| 572 | }
|
|---|
| 573 | return result;
|
|---|
| 574 | };
|
|---|
| 575 |
|
|---|
| 576 | /**
|
|---|
| 577 | * Gets normalized optimization runtime chunk.
|
|---|
| 578 | * @param {OptimizationRuntimeChunk=} runtimeChunk runtimeChunk option
|
|---|
| 579 | * @returns {OptimizationRuntimeChunkNormalized=} normalized runtimeChunk option
|
|---|
| 580 | */
|
|---|
| 581 | const getNormalizedOptimizationRuntimeChunk = (runtimeChunk) => {
|
|---|
| 582 | if (runtimeChunk === undefined) return;
|
|---|
| 583 | if (runtimeChunk === false) return false;
|
|---|
| 584 | if (runtimeChunk === "single") {
|
|---|
| 585 | return {
|
|---|
| 586 | name: () => "runtime"
|
|---|
| 587 | };
|
|---|
| 588 | }
|
|---|
| 589 | if (runtimeChunk === true || runtimeChunk === "multiple") {
|
|---|
| 590 | return {
|
|---|
| 591 | name: (entrypoint) => `runtime~${entrypoint.name}`
|
|---|
| 592 | };
|
|---|
| 593 | }
|
|---|
| 594 | const { name } = runtimeChunk;
|
|---|
| 595 | return {
|
|---|
| 596 | name:
|
|---|
| 597 | typeof name === "function"
|
|---|
| 598 | ? /** @type {Exclude<OptimizationRuntimeChunkNormalized, false>["name"]} */
|
|---|
| 599 | (name)
|
|---|
| 600 | : () => /** @type {string} */ (name)
|
|---|
| 601 | };
|
|---|
| 602 | };
|
|---|
| 603 |
|
|---|
| 604 | /**
|
|---|
| 605 | * Apply webpack options interception.
|
|---|
| 606 | * @param {WebpackOptionsNormalized} options options to be intercepted
|
|---|
| 607 | * @returns {{ options: WebpackOptionsNormalized, interception?: WebpackOptionsInterception }} options and interception
|
|---|
| 608 | */
|
|---|
| 609 | const applyWebpackOptionsInterception = (options) => {
|
|---|
| 610 | // Return origin options when backCompat is disabled
|
|---|
| 611 | if (options.experiments.futureDefaults) {
|
|---|
| 612 | return {
|
|---|
| 613 | options
|
|---|
| 614 | };
|
|---|
| 615 | }
|
|---|
| 616 |
|
|---|
| 617 | // TODO webpack 6 - remove compatibility logic and move `devtools` fully into `devtool` with multi-type support
|
|---|
| 618 | let _devtool = options.devtool;
|
|---|
| 619 | /** @type {WebpackOptionsNormalized["devtool"]} */
|
|---|
| 620 | let cached;
|
|---|
| 621 |
|
|---|
| 622 | const devtoolBackCompat = () => {
|
|---|
| 623 | if (Array.isArray(_devtool)) {
|
|---|
| 624 | if (cached) return cached;
|
|---|
| 625 | // Prefer `all`, then `javascript`, then `css`
|
|---|
| 626 | const match = ["all", "javascript", "css"]
|
|---|
| 627 | .map((type) =>
|
|---|
| 628 | /** @type {Extract<WebpackOptionsNormalized["devtool"], EXPECTED_ANY[]>} */ (
|
|---|
| 629 | _devtool
|
|---|
| 630 | ).find((item) => item.type === type)
|
|---|
| 631 | )
|
|---|
| 632 | .find(Boolean);
|
|---|
| 633 |
|
|---|
| 634 | // If `devtool: []` is specified, return `false` here
|
|---|
| 635 | return (cached = match ? match.use : false);
|
|---|
| 636 | }
|
|---|
| 637 | return _devtool;
|
|---|
| 638 | };
|
|---|
| 639 |
|
|---|
| 640 | /** @type {ProxyHandler<WebpackOptionsNormalized>} */
|
|---|
| 641 | const handler = Object.create(null);
|
|---|
| 642 | handler.get = (target, prop, receiver) => {
|
|---|
| 643 | if (prop === "devtool") {
|
|---|
| 644 | return devtoolBackCompat();
|
|---|
| 645 | }
|
|---|
| 646 | return Reflect.get(target, prop, receiver);
|
|---|
| 647 | };
|
|---|
| 648 | handler.set = (target, prop, value, receiver) => {
|
|---|
| 649 | if (prop === "devtool") {
|
|---|
| 650 | _devtool = value;
|
|---|
| 651 | cached = undefined;
|
|---|
| 652 | return true;
|
|---|
| 653 | }
|
|---|
| 654 | return Reflect.set(target, prop, value, receiver);
|
|---|
| 655 | };
|
|---|
| 656 | handler.deleteProperty = (target, prop) => {
|
|---|
| 657 | if (prop === "devtool") {
|
|---|
| 658 | _devtool = undefined;
|
|---|
| 659 | cached = undefined;
|
|---|
| 660 | return true;
|
|---|
| 661 | }
|
|---|
| 662 | return Reflect.deleteProperty(target, prop);
|
|---|
| 663 | };
|
|---|
| 664 | handler.defineProperty = (target, prop, descriptor) => {
|
|---|
| 665 | if (prop === "devtool") {
|
|---|
| 666 | _devtool = descriptor.value;
|
|---|
| 667 | cached = undefined;
|
|---|
| 668 | return true;
|
|---|
| 669 | }
|
|---|
| 670 | return Reflect.defineProperty(target, prop, descriptor);
|
|---|
| 671 | };
|
|---|
| 672 | handler.getOwnPropertyDescriptor = (target, prop) => {
|
|---|
| 673 | if (prop === "devtool") {
|
|---|
| 674 | return {
|
|---|
| 675 | configurable: true,
|
|---|
| 676 | enumerable: true,
|
|---|
| 677 | value: devtoolBackCompat(),
|
|---|
| 678 | writable: true
|
|---|
| 679 | };
|
|---|
| 680 | }
|
|---|
| 681 | return Reflect.getOwnPropertyDescriptor(target, prop);
|
|---|
| 682 | };
|
|---|
| 683 |
|
|---|
| 684 | return {
|
|---|
| 685 | options: new Proxy(options, handler),
|
|---|
| 686 | interception: {
|
|---|
| 687 | get devtool() {
|
|---|
| 688 | return _devtool;
|
|---|
| 689 | }
|
|---|
| 690 | }
|
|---|
| 691 | };
|
|---|
| 692 | };
|
|---|
| 693 |
|
|---|
| 694 | module.exports.applyWebpackOptionsInterception =
|
|---|
| 695 | applyWebpackOptionsInterception;
|
|---|
| 696 | module.exports.getNormalizedWebpackOptions = getNormalizedWebpackOptions;
|
|---|