defaults.js 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const fs = require("fs");
  7. const path = require("path");
  8. const {
  9. CSS_TYPE,
  10. JAVASCRIPT_TYPE,
  11. UNKNOWN_TYPE
  12. } = require("../ModuleSourceTypeConstants");
  13. const {
  14. ASSET_MODULE_TYPE,
  15. ASSET_MODULE_TYPE_BYTES,
  16. ASSET_MODULE_TYPE_INLINE,
  17. ASSET_MODULE_TYPE_RESOURCE,
  18. ASSET_MODULE_TYPE_SOURCE,
  19. CSS_MODULE_TYPE,
  20. CSS_MODULE_TYPE_AUTO,
  21. CSS_MODULE_TYPE_GLOBAL,
  22. CSS_MODULE_TYPE_MODULE,
  23. JAVASCRIPT_MODULE_TYPE_AUTO,
  24. JAVASCRIPT_MODULE_TYPE_DYNAMIC,
  25. JAVASCRIPT_MODULE_TYPE_ESM,
  26. JSON_MODULE_TYPE,
  27. WEBASSEMBLY_MODULE_TYPE_ASYNC,
  28. WEBASSEMBLY_MODULE_TYPE_SYNC
  29. } = require("../ModuleTypeConstants");
  30. const Template = require("../Template");
  31. const { cleverMerge } = require("../util/cleverMerge");
  32. const {
  33. getDefaultTarget,
  34. getTargetProperties,
  35. getTargetsProperties
  36. } = require("./target");
  37. /** @typedef {import("../../declarations/WebpackOptions").CacheOptionsNormalized} CacheOptionsNormalized */
  38. /** @typedef {import("../../declarations/WebpackOptions").Context} Context */
  39. /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorOptions} CssGeneratorOptions */
  40. /** @typedef {import("../../declarations/WebpackOptions").EntryDescription} EntryDescription */
  41. /** @typedef {import("../../declarations/WebpackOptions").EntryNormalized} Entry */
  42. /** @typedef {import("../../declarations/WebpackOptions").Environment} Environment */
  43. /** @typedef {import("../../declarations/WebpackOptions").Experiments} Experiments */
  44. /** @typedef {import("../../declarations/WebpackOptions").ExperimentsNormalized} ExperimentsNormalized */
  45. /** @typedef {import("../../declarations/WebpackOptions").ExternalsPresets} ExternalsPresets */
  46. /** @typedef {import("../../declarations/WebpackOptions").ExternalsType} ExternalsType */
  47. /** @typedef {import("../../declarations/WebpackOptions").FileCacheOptions} FileCacheOptions */
  48. /** @typedef {import("../../declarations/WebpackOptions").GeneratorOptionsByModuleTypeKnown} GeneratorOptionsByModuleTypeKnown */
  49. /** @typedef {import("../../declarations/WebpackOptions").InfrastructureLogging} InfrastructureLogging */
  50. /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
  51. /** @typedef {import("../../declarations/WebpackOptions").JsonGeneratorOptions} JsonGeneratorOptions */
  52. /** @typedef {import("../../declarations/WebpackOptions").Library} Library */
  53. /** @typedef {import("../../declarations/WebpackOptions").LibraryName} LibraryName */
  54. /** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
  55. /** @typedef {import("../../declarations/WebpackOptions").Loader} Loader */
  56. /** @typedef {import("../../declarations/WebpackOptions").Mode} Mode */
  57. /** @typedef {import("../../declarations/WebpackOptions").HashSalt} HashSalt */
  58. /** @typedef {import("../../declarations/WebpackOptions").HashSalt} HashDigest */
  59. /** @typedef {import("../../declarations/WebpackOptions").HashDigestLength} HashDigestLength */
  60. /** @typedef {import("../../declarations/WebpackOptions").ModuleOptionsNormalized} ModuleOptions */
  61. /** @typedef {import("../../declarations/WebpackOptions").Node} WebpackNode */
  62. /** @typedef {import("../../declarations/WebpackOptions").OptimizationNormalized} Optimization */
  63. /** @typedef {import("../../declarations/WebpackOptions").OptimizationSplitChunksOptions} OptimizationSplitChunksOptions */
  64. /** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} Output */
  65. /** @typedef {import("../../declarations/WebpackOptions").ParserOptionsByModuleTypeKnown} ParserOptionsByModuleTypeKnown */
  66. /** @typedef {import("../../declarations/WebpackOptions").Performance} Performance */
  67. /** @typedef {import("../../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
  68. /** @typedef {import("../../declarations/WebpackOptions").RuleSetRules} RuleSetRules */
  69. /** @typedef {import("../../declarations/WebpackOptions").SnapshotOptions} SnapshotOptions */
  70. /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptionsNormalized */
  71. /** @typedef {import("../Module")} Module */
  72. /** @typedef {import("./target").PlatformTargetProperties} PlatformTargetProperties */
  73. /** @typedef {import("./target").TargetProperties} TargetProperties */
  74. /**
  75. * @template T
  76. * @typedef {{ [P in keyof T]-?: T[P] extends object ? RecursiveNonNullable<NonNullable<T[P]>> : NonNullable<T[P]> }} RecursiveNonNullable
  77. */
  78. /**
  79. * @typedef {Output & {
  80. * uniqueName: NonNullable<Output["uniqueName"]>,
  81. * filename: NonNullable<Output["filename"]>,
  82. * cssFilename: NonNullable<Output["cssFilename"]>,
  83. * chunkFilename: NonNullable<Output["chunkFilename"]>,
  84. * cssChunkFilename: NonNullable<Output["cssChunkFilename"]>,
  85. * hotUpdateChunkFilename: NonNullable<Output["hotUpdateChunkFilename"]>,
  86. * hotUpdateGlobal: NonNullable<Output["hotUpdateGlobal"]>,
  87. * assetModuleFilename: NonNullable<Output["assetModuleFilename"]>,
  88. * webassemblyModuleFilename: NonNullable<Output["webassemblyModuleFilename"]>
  89. * sourceMapFilename: NonNullable<Output["sourceMapFilename"]>,
  90. * hotUpdateMainFilename: NonNullable<Output["hotUpdateMainFilename"]>,
  91. * devtoolNamespace: NonNullable<Output["devtoolNamespace"]>,
  92. * publicPath: NonNullable<Output["publicPath"]>
  93. * workerPublicPath: NonNullable<Output["workerPublicPath"]>
  94. * workerWasmLoading: NonNullable<Output["workerWasmLoading"]>
  95. * workerChunkLoading: NonNullable<Output["workerChunkLoading"]>
  96. * chunkFormat: NonNullable<Output["chunkFormat"]>,
  97. * module: NonNullable<Output["module"]>,
  98. * asyncChunks: NonNullable<Output["asyncChunks"]>,
  99. * charset: NonNullable<Output["charset"]>,
  100. * iife: NonNullable<Output["iife"]>,
  101. * globalObject: NonNullable<Output["globalObject"]>,
  102. * scriptType: NonNullable<Output["scriptType"]>,
  103. * path: NonNullable<Output["path"]>,
  104. * pathinfo: NonNullable<Output["pathinfo"]>,
  105. * hashFunction: NonNullable<Output["hashFunction"]>,
  106. * hashDigest: NonNullable<Output["hashDigest"]>,
  107. * hashDigestLength: NonNullable<Output["hashDigestLength"]>,
  108. * chunkLoadTimeout: NonNullable<Output["chunkLoadTimeout"]>,
  109. * chunkLoading: NonNullable<Output["chunkLoading"]>,
  110. * chunkLoadingGlobal: NonNullable<Output["chunkLoadingGlobal"]>,
  111. * compareBeforeEmit: NonNullable<Output["compareBeforeEmit"]>,
  112. * strictModuleErrorHandling: NonNullable<Output["strictModuleErrorHandling"]>,
  113. * strictModuleExceptionHandling: NonNullable<Output["strictModuleExceptionHandling"]>,
  114. * importFunctionName: NonNullable<Output["importFunctionName"]>,
  115. * importMetaName: NonNullable<Output["importMetaName"]>,
  116. * environment: RecursiveNonNullable<Output["environment"]>,
  117. * crossOriginLoading: NonNullable<Output["crossOriginLoading"]>,
  118. * wasmLoading: NonNullable<Output["wasmLoading"]>,
  119. * }} OutputNormalizedWithDefaults
  120. */
  121. /**
  122. * @typedef {SnapshotOptions & {
  123. * managedPaths: NonNullable<SnapshotOptions["managedPaths"]>,
  124. * unmanagedPaths: NonNullable<SnapshotOptions["unmanagedPaths"]>,
  125. * immutablePaths: NonNullable<SnapshotOptions["immutablePaths"]>,
  126. * resolveBuildDependencies: NonNullable<SnapshotOptions["resolveBuildDependencies"]>,
  127. * buildDependencies: NonNullable<SnapshotOptions["buildDependencies"]>,
  128. * module: NonNullable<SnapshotOptions["module"]>,
  129. * resolve: NonNullable<SnapshotOptions["resolve"]>,
  130. * }} SnapshotNormalizedWithDefaults
  131. */
  132. /**
  133. * @typedef {Optimization & {
  134. * runtimeChunk: NonNullable<Optimization["runtimeChunk"]>,
  135. * splitChunks: NonNullable<Optimization["splitChunks"]>,
  136. * mergeDuplicateChunks: NonNullable<Optimization["mergeDuplicateChunks"]>,
  137. * removeAvailableModules: NonNullable<Optimization["removeAvailableModules"]>,
  138. * removeEmptyChunks: NonNullable<Optimization["removeEmptyChunks"]>,
  139. * flagIncludedChunks: NonNullable<Optimization["flagIncludedChunks"]>,
  140. * moduleIds: NonNullable<Optimization["moduleIds"]>,
  141. * chunkIds: NonNullable<Optimization["chunkIds"]>,
  142. * sideEffects: NonNullable<Optimization["sideEffects"]>,
  143. * providedExports: NonNullable<Optimization["providedExports"]>,
  144. * usedExports: NonNullable<Optimization["usedExports"]>,
  145. * mangleExports: NonNullable<Optimization["mangleExports"]>,
  146. * innerGraph: NonNullable<Optimization["innerGraph"]>,
  147. * concatenateModules: NonNullable<Optimization["concatenateModules"]>,
  148. * avoidEntryIife: NonNullable<Optimization["avoidEntryIife"]>,
  149. * emitOnErrors: NonNullable<Optimization["emitOnErrors"]>,
  150. * checkWasmTypes: NonNullable<Optimization["checkWasmTypes"]>,
  151. * mangleWasmImports: NonNullable<Optimization["mangleWasmImports"]>,
  152. * portableRecords: NonNullable<Optimization["portableRecords"]>,
  153. * realContentHash: NonNullable<Optimization["realContentHash"]>,
  154. * minimize: NonNullable<Optimization["minimize"]>,
  155. * minimizer: NonNullable<Exclude<Optimization["minimizer"], "...">>,
  156. * nodeEnv: NonNullable<Optimization["nodeEnv"]>,
  157. * }} OptimizationNormalizedWithDefaults
  158. */
  159. /**
  160. * @typedef {ExternalsPresets & {
  161. * web: NonNullable<ExternalsPresets["web"]>,
  162. * node: NonNullable<ExternalsPresets["node"]>,
  163. * nwjs: NonNullable<ExternalsPresets["nwjs"]>,
  164. * electron: NonNullable<ExternalsPresets["electron"]>,
  165. * electronMain: NonNullable<ExternalsPresets["electronMain"]>,
  166. * electronPreload: NonNullable<ExternalsPresets["electronPreload"]>,
  167. * electronRenderer: NonNullable<ExternalsPresets["electronRenderer"]>,
  168. * }} ExternalsPresetsNormalizedWithDefaults
  169. */
  170. /**
  171. * @typedef {InfrastructureLogging & {
  172. * stream: NonNullable<InfrastructureLogging["stream"]>,
  173. * level: NonNullable<InfrastructureLogging["level"]>,
  174. * debug: NonNullable<InfrastructureLogging["debug"]>,
  175. * colors: NonNullable<InfrastructureLogging["colors"]>,
  176. * appendOnly: NonNullable<InfrastructureLogging["appendOnly"]>,
  177. * }} InfrastructureLoggingNormalizedWithDefaults
  178. */
  179. /**
  180. * @typedef {WebpackOptionsNormalized
  181. * & { context: NonNullable<WebpackOptionsNormalized["context"]> }
  182. * & { infrastructureLogging: InfrastructureLoggingNormalizedWithDefaults }
  183. * } WebpackOptionsNormalizedWithBaseDefaults
  184. */
  185. /**
  186. * @typedef {WebpackOptionsNormalizedWithBaseDefaults
  187. * & { target: NonNullable<WebpackOptionsNormalized["target"]> }
  188. * & { output: OutputNormalizedWithDefaults }
  189. * & { optimization: OptimizationNormalizedWithDefaults }
  190. * & { devtool: NonNullable<WebpackOptionsNormalized["devtool"]> }
  191. * & { stats: NonNullable<WebpackOptionsNormalized["stats"]> }
  192. * & { node: NonNullable<WebpackOptionsNormalized["node"]> }
  193. * & { profile: NonNullable<WebpackOptionsNormalized["profile"]> }
  194. * & { parallelism: NonNullable<WebpackOptionsNormalized["parallelism"]> }
  195. * & { snapshot: SnapshotNormalizedWithDefaults }
  196. * & { externalsPresets: ExternalsPresetsNormalizedWithDefaults }
  197. * & { externalsType: NonNullable<WebpackOptionsNormalized["externalsType"]> }
  198. * & { watch: NonNullable<WebpackOptionsNormalized["watch"]> }
  199. * & { performance: NonNullable<WebpackOptionsNormalized["performance"]> }
  200. * & { recordsInputPath: NonNullable<WebpackOptionsNormalized["recordsInputPath"]> }
  201. * & { recordsOutputPath: NonNullable<WebpackOptionsNormalized["recordsOutputPath"]>
  202. * & { dotenv: NonNullable<WebpackOptionsNormalized["dotenv"]> }
  203. * }} WebpackOptionsNormalizedWithDefaults
  204. */
  205. /**
  206. * @typedef {object} ResolvedOptions
  207. * @property {PlatformTargetProperties | false} platform - platform target properties
  208. */
  209. const NODE_MODULES_REGEXP = /[\\/]node_modules[\\/]/i;
  210. const DEFAULT_CACHE_NAME = "default";
  211. const DEFAULTS = {
  212. // TODO webpack 6 - use xxhash64
  213. HASH_FUNCTION: "md4"
  214. };
  215. /**
  216. * Sets a constant default value when undefined
  217. * @template T
  218. * @template {keyof T} P
  219. * @param {T} obj an object
  220. * @param {P} prop a property of this object
  221. * @param {T[P]} value a default value of the property
  222. * @returns {void}
  223. */
  224. const D = (obj, prop, value) => {
  225. if (obj[prop] === undefined) {
  226. obj[prop] = value;
  227. }
  228. };
  229. /**
  230. * Sets a dynamic default value when undefined, by calling the factory function
  231. * @template T
  232. * @template {keyof T} P
  233. * @param {T} obj an object
  234. * @param {P} prop a property of this object
  235. * @param {() => T[P]} factory a default value factory for the property
  236. * @returns {void}
  237. */
  238. const F = (obj, prop, factory) => {
  239. if (obj[prop] === undefined) {
  240. obj[prop] = factory();
  241. }
  242. };
  243. /**
  244. * Sets a dynamic default value when undefined, by calling the factory function.
  245. * factory must return an array or undefined
  246. * When the current value is already an array an contains "..." it's replaced with
  247. * the result of the factory function
  248. * @template T
  249. * @template {keyof T} P
  250. * @param {T} obj an object
  251. * @param {P} prop a property of this object
  252. * @param {() => T[P]} factory a default value factory for the property
  253. * @returns {void}
  254. */
  255. const A = (obj, prop, factory) => {
  256. const value = obj[prop];
  257. if (value === undefined) {
  258. obj[prop] = factory();
  259. } else if (Array.isArray(value)) {
  260. /** @type {EXPECTED_ANY[] | undefined} */
  261. let newArray;
  262. for (let i = 0; i < value.length; i++) {
  263. const item = value[i];
  264. if (item === "...") {
  265. if (newArray === undefined) {
  266. newArray = value.slice(0, i);
  267. obj[prop] = /** @type {T[P]} */ (/** @type {unknown} */ (newArray));
  268. }
  269. const items =
  270. /** @type {EXPECTED_ANY[]} */
  271. (/** @type {unknown} */ (factory()));
  272. if (items !== undefined) {
  273. for (const item of items) {
  274. newArray.push(item);
  275. }
  276. }
  277. } else if (newArray !== undefined) {
  278. newArray.push(item);
  279. }
  280. }
  281. }
  282. };
  283. /**
  284. * @param {WebpackOptionsNormalized} options options to be modified
  285. * @returns {void}
  286. */
  287. const applyWebpackOptionsBaseDefaults = (options) => {
  288. F(options, "context", () => process.cwd());
  289. applyInfrastructureLoggingDefaults(options.infrastructureLogging);
  290. };
  291. /**
  292. * @param {WebpackOptionsNormalized} options options to be modified
  293. * @param {number=} compilerIndex index of compiler
  294. * @returns {ResolvedOptions} Resolved options after apply defaults
  295. */
  296. const applyWebpackOptionsDefaults = (options, compilerIndex) => {
  297. F(options, "context", () => process.cwd());
  298. F(options, "target", () =>
  299. getDefaultTarget(/** @type {string} */ (options.context))
  300. );
  301. const { mode, name, target } = options;
  302. const targetProperties =
  303. target === false
  304. ? /** @type {false} */ (false)
  305. : typeof target === "string"
  306. ? getTargetProperties(target, /** @type {Context} */ (options.context))
  307. : getTargetsProperties(
  308. /** @type {string[]} */ (target),
  309. /** @type {Context} */ (options.context)
  310. );
  311. const development = mode === "development";
  312. const production = mode === "production" || !mode;
  313. if (typeof options.entry !== "function") {
  314. for (const key of Object.keys(options.entry)) {
  315. F(
  316. options.entry[key],
  317. "import",
  318. () => /** @type {[string]} */ (["./src"])
  319. );
  320. }
  321. }
  322. F(options, "devtool", () => (development ? "eval" : false));
  323. D(options, "watch", false);
  324. D(options, "profile", false);
  325. D(options, "parallelism", 100);
  326. D(options, "recordsInputPath", false);
  327. D(options, "recordsOutputPath", false);
  328. applyExperimentsDefaults(options.experiments, {
  329. production,
  330. development,
  331. targetProperties
  332. });
  333. const futureDefaults =
  334. /** @type {NonNullable<ExperimentsNormalized["futureDefaults"]>} */
  335. (options.experiments.futureDefaults);
  336. F(options, "cache", () =>
  337. development ? { type: /** @type {"memory"} */ ("memory") } : false
  338. );
  339. applyCacheDefaults(options.cache, {
  340. name: name || DEFAULT_CACHE_NAME,
  341. mode: mode || "production",
  342. development,
  343. cacheUnaffected: options.experiments.cacheUnaffected,
  344. futureDefaults,
  345. compilerIndex
  346. });
  347. const cache = Boolean(options.cache);
  348. applySnapshotDefaults(options.snapshot, {
  349. production,
  350. futureDefaults
  351. });
  352. applyOutputDefaults(options.output, {
  353. context: /** @type {Context} */ (options.context),
  354. targetProperties,
  355. isAffectedByBrowserslist:
  356. target === undefined ||
  357. (typeof target === "string" && target.startsWith("browserslist")) ||
  358. (Array.isArray(target) &&
  359. target.some((target) => target.startsWith("browserslist"))),
  360. outputModule:
  361. /** @type {NonNullable<ExperimentsNormalized["outputModule"]>} */
  362. (options.experiments.outputModule),
  363. development,
  364. entry: options.entry,
  365. futureDefaults,
  366. asyncWebAssembly:
  367. /** @type {NonNullable<ExperimentsNormalized["asyncWebAssembly"]>} */
  368. (options.experiments.asyncWebAssembly)
  369. });
  370. applyModuleDefaults(options.module, {
  371. cache,
  372. hashSalt: /** @type {NonNullable<Output["hashSalt"]>} */ (
  373. options.output.hashSalt
  374. ),
  375. hashDigest: /** @type {NonNullable<Output["hashDigest"]>} */ (
  376. options.output.hashDigest
  377. ),
  378. hashDigestLength: /** @type {NonNullable<Output["hashDigestLength"]>} */ (
  379. options.output.hashDigestLength
  380. ),
  381. syncWebAssembly:
  382. /** @type {NonNullable<ExperimentsNormalized["syncWebAssembly"]>} */
  383. (options.experiments.syncWebAssembly),
  384. asyncWebAssembly:
  385. /** @type {NonNullable<ExperimentsNormalized["asyncWebAssembly"]>} */
  386. (options.experiments.asyncWebAssembly),
  387. css:
  388. /** @type {NonNullable<ExperimentsNormalized["css"]>} */
  389. (options.experiments.css),
  390. deferImport:
  391. /** @type {NonNullable<ExperimentsNormalized["deferImport"]>} */
  392. (options.experiments.deferImport),
  393. futureDefaults,
  394. isNode: targetProperties && targetProperties.node === true,
  395. uniqueName: /** @type {string} */ (options.output.uniqueName),
  396. targetProperties,
  397. mode: options.mode
  398. });
  399. applyExternalsPresetsDefaults(options.externalsPresets, {
  400. targetProperties,
  401. buildHttp: Boolean(options.experiments.buildHttp),
  402. outputModule:
  403. /** @type {NonNullable<WebpackOptionsNormalized["output"]["module"]>} */
  404. (options.output.module)
  405. });
  406. applyLoaderDefaults(
  407. /** @type {NonNullable<WebpackOptionsNormalized["loader"]>} */ (
  408. options.loader
  409. ),
  410. { targetProperties, environment: options.output.environment }
  411. );
  412. F(options, "externalsType", () => {
  413. const validExternalTypes = require("../../schemas/WebpackOptions.json")
  414. .definitions.ExternalsType.enum;
  415. return options.output.library &&
  416. validExternalTypes.includes(options.output.library.type)
  417. ? /** @type {ExternalsType} */ (options.output.library.type)
  418. : options.output.module
  419. ? "module-import"
  420. : "var";
  421. });
  422. applyNodeDefaults(options.node, {
  423. futureDefaults:
  424. /** @type {NonNullable<WebpackOptionsNormalized["experiments"]["futureDefaults"]>} */
  425. (options.experiments.futureDefaults),
  426. outputModule:
  427. /** @type {NonNullable<WebpackOptionsNormalized["output"]["module"]>} */
  428. (options.output.module),
  429. targetProperties
  430. });
  431. F(options, "performance", () =>
  432. production &&
  433. targetProperties &&
  434. (targetProperties.browser || targetProperties.browser === null)
  435. ? {}
  436. : false
  437. );
  438. applyPerformanceDefaults(
  439. /** @type {NonNullable<WebpackOptionsNormalized["performance"]>} */
  440. (options.performance),
  441. {
  442. production
  443. }
  444. );
  445. applyOptimizationDefaults(options.optimization, {
  446. development,
  447. production,
  448. css:
  449. /** @type {NonNullable<ExperimentsNormalized["css"]>} */
  450. (options.experiments.css),
  451. records: Boolean(options.recordsInputPath || options.recordsOutputPath)
  452. });
  453. options.resolve = cleverMerge(
  454. getResolveDefaults({
  455. cache,
  456. context: /** @type {Context} */ (options.context),
  457. targetProperties,
  458. mode: /** @type {Mode} */ (options.mode),
  459. css:
  460. /** @type {NonNullable<ExperimentsNormalized["css"]>} */
  461. (options.experiments.css)
  462. }),
  463. options.resolve
  464. );
  465. options.resolveLoader = cleverMerge(
  466. getResolveLoaderDefaults({ cache }),
  467. options.resolveLoader
  468. );
  469. return {
  470. platform:
  471. targetProperties === false
  472. ? targetProperties
  473. : {
  474. web: targetProperties.web,
  475. browser: targetProperties.browser,
  476. webworker: targetProperties.webworker,
  477. node: targetProperties.node,
  478. nwjs: targetProperties.nwjs,
  479. electron: targetProperties.electron
  480. }
  481. };
  482. };
  483. /**
  484. * @param {ExperimentsNormalized} experiments options
  485. * @param {object} options options
  486. * @param {boolean} options.production is production
  487. * @param {boolean} options.development is development mode
  488. * @param {TargetProperties | false} options.targetProperties target properties
  489. * @returns {void}
  490. */
  491. const applyExperimentsDefaults = (
  492. experiments,
  493. { production, development, targetProperties }
  494. ) => {
  495. D(experiments, "futureDefaults", false);
  496. D(experiments, "backCompat", !experiments.futureDefaults);
  497. // TODO do we need sync web assembly in webpack@6?
  498. D(experiments, "syncWebAssembly", false);
  499. D(experiments, "asyncWebAssembly", experiments.futureDefaults);
  500. D(experiments, "outputModule", false);
  501. D(experiments, "lazyCompilation", undefined);
  502. D(experiments, "buildHttp", undefined);
  503. D(experiments, "cacheUnaffected", experiments.futureDefaults);
  504. D(experiments, "deferImport", false);
  505. F(experiments, "css", () => (experiments.futureDefaults ? true : undefined));
  506. if (typeof experiments.buildHttp === "object") {
  507. D(experiments.buildHttp, "frozen", production);
  508. D(experiments.buildHttp, "upgrade", false);
  509. }
  510. };
  511. /**
  512. * @param {CacheOptionsNormalized} cache options
  513. * @param {object} options options
  514. * @param {string} options.name name
  515. * @param {Mode} options.mode mode
  516. * @param {boolean} options.futureDefaults is future defaults enabled
  517. * @param {boolean} options.development is development mode
  518. * @param {number=} options.compilerIndex index of compiler
  519. * @param {Experiments["cacheUnaffected"]} options.cacheUnaffected the cacheUnaffected experiment is enabled
  520. * @returns {void}
  521. */
  522. const applyCacheDefaults = (
  523. cache,
  524. { name, mode, development, cacheUnaffected, compilerIndex, futureDefaults }
  525. ) => {
  526. if (cache === false) return;
  527. switch (cache.type) {
  528. case "filesystem":
  529. F(cache, "name", () =>
  530. compilerIndex !== undefined
  531. ? `${`${name}-${mode}`}__compiler${compilerIndex + 1}__`
  532. : `${name}-${mode}`
  533. );
  534. D(cache, "version", "");
  535. F(cache, "cacheDirectory", () => {
  536. const cwd = process.cwd();
  537. /** @type {string | undefined} */
  538. let dir = cwd;
  539. for (;;) {
  540. try {
  541. if (fs.statSync(path.join(dir, "package.json")).isFile()) break;
  542. // eslint-disable-next-line no-empty
  543. } catch (_err) {}
  544. const parent = path.dirname(dir);
  545. if (dir === parent) {
  546. dir = undefined;
  547. break;
  548. }
  549. dir = parent;
  550. }
  551. if (!dir) {
  552. return path.resolve(cwd, ".cache/webpack");
  553. } else if (process.versions.pnp === "1") {
  554. return path.resolve(dir, ".pnp/.cache/webpack");
  555. } else if (process.versions.pnp === "3") {
  556. return path.resolve(dir, ".yarn/.cache/webpack");
  557. }
  558. return path.resolve(dir, "node_modules/.cache/webpack");
  559. });
  560. F(cache, "cacheLocation", () =>
  561. path.resolve(
  562. /** @type {NonNullable<FileCacheOptions["cacheDirectory"]>} */
  563. (cache.cacheDirectory),
  564. /** @type {NonNullable<FileCacheOptions["name"]>} */ (cache.name)
  565. )
  566. );
  567. D(cache, "hashAlgorithm", futureDefaults ? "xxhash64" : "md4");
  568. D(cache, "store", "pack");
  569. D(cache, "compression", false);
  570. D(cache, "profile", false);
  571. D(cache, "idleTimeout", 60000);
  572. D(cache, "idleTimeoutForInitialStore", 5000);
  573. D(cache, "idleTimeoutAfterLargeChanges", 1000);
  574. D(cache, "maxMemoryGenerations", development ? 5 : Infinity);
  575. D(cache, "maxAge", 1000 * 60 * 60 * 24 * 60); // 1 month
  576. D(cache, "allowCollectingMemory", development);
  577. D(cache, "memoryCacheUnaffected", development && cacheUnaffected);
  578. D(cache, "readonly", false);
  579. D(
  580. /** @type {NonNullable<FileCacheOptions["buildDependencies"]>} */
  581. (cache.buildDependencies),
  582. "defaultWebpack",
  583. [path.resolve(__dirname, "..") + path.sep]
  584. );
  585. break;
  586. case "memory":
  587. D(cache, "maxGenerations", Infinity);
  588. D(cache, "cacheUnaffected", development && cacheUnaffected);
  589. break;
  590. }
  591. };
  592. /**
  593. * @param {SnapshotOptions} snapshot options
  594. * @param {object} options options
  595. * @param {boolean} options.production is production
  596. * @param {boolean} options.futureDefaults is future defaults enabled
  597. * @returns {void}
  598. */
  599. const applySnapshotDefaults = (snapshot, { production, futureDefaults }) => {
  600. if (futureDefaults) {
  601. F(snapshot, "managedPaths", () =>
  602. process.versions.pnp === "3"
  603. ? [
  604. /^(.+?(?:[\\/]\.yarn[\\/]unplugged[\\/][^\\/]+)?[\\/]node_modules[\\/])/
  605. ]
  606. : [/^(.+?[\\/]node_modules[\\/])/]
  607. );
  608. F(snapshot, "immutablePaths", () =>
  609. process.versions.pnp === "3"
  610. ? [/^(.+?[\\/]cache[\\/][^\\/]+\.zip[\\/]node_modules[\\/])/]
  611. : []
  612. );
  613. } else {
  614. A(snapshot, "managedPaths", () => {
  615. if (process.versions.pnp === "3") {
  616. const match =
  617. /^(.+?)[\\/]cache[\\/]watchpack-npm-[^\\/]+\.zip[\\/]node_modules[\\/]/.exec(
  618. require.resolve("watchpack")
  619. );
  620. if (match) {
  621. return [path.resolve(match[1], "unplugged")];
  622. }
  623. } else {
  624. const match = /^(.+?[\\/]node_modules[\\/])/.exec(
  625. require.resolve("watchpack")
  626. );
  627. if (match) {
  628. return [match[1]];
  629. }
  630. }
  631. return [];
  632. });
  633. A(snapshot, "immutablePaths", () => {
  634. if (process.versions.pnp === "1") {
  635. const match =
  636. /^(.+?[\\/]v4)[\\/]npm-watchpack-[^\\/]+-[\da-f]{40}[\\/]node_modules[\\/]/.exec(
  637. require.resolve("watchpack")
  638. );
  639. if (match) {
  640. return [match[1]];
  641. }
  642. } else if (process.versions.pnp === "3") {
  643. const match =
  644. /^(.+?)[\\/]watchpack-npm-[^\\/]+\.zip[\\/]node_modules[\\/]/.exec(
  645. require.resolve("watchpack")
  646. );
  647. if (match) {
  648. return [match[1]];
  649. }
  650. }
  651. return [];
  652. });
  653. }
  654. F(snapshot, "unmanagedPaths", () => []);
  655. F(snapshot, "resolveBuildDependencies", () => ({
  656. timestamp: true,
  657. hash: true
  658. }));
  659. F(snapshot, "buildDependencies", () => ({ timestamp: true, hash: true }));
  660. F(snapshot, "module", () =>
  661. production ? { timestamp: true, hash: true } : { timestamp: true }
  662. );
  663. F(snapshot, "contextModule", () => ({ timestamp: true }));
  664. F(snapshot, "resolve", () =>
  665. production ? { timestamp: true, hash: true } : { timestamp: true }
  666. );
  667. };
  668. /**
  669. * @param {JavascriptParserOptions} parserOptions parser options
  670. * @param {object} options options
  671. * @param {boolean} options.futureDefaults is future defaults enabled
  672. * @param {boolean} options.deferImport is defer import enabled
  673. * @param {boolean} options.isNode is node target platform
  674. * @returns {void}
  675. */
  676. const applyJavascriptParserOptionsDefaults = (
  677. parserOptions,
  678. { futureDefaults, deferImport, isNode }
  679. ) => {
  680. D(parserOptions, "unknownContextRequest", ".");
  681. D(parserOptions, "unknownContextRegExp", false);
  682. D(parserOptions, "unknownContextRecursive", true);
  683. D(parserOptions, "unknownContextCritical", true);
  684. D(parserOptions, "exprContextRequest", ".");
  685. D(parserOptions, "exprContextRegExp", false);
  686. D(parserOptions, "exprContextRecursive", true);
  687. D(parserOptions, "exprContextCritical", true);
  688. D(parserOptions, "wrappedContextRegExp", /.*/);
  689. D(parserOptions, "wrappedContextRecursive", true);
  690. D(parserOptions, "wrappedContextCritical", false);
  691. D(parserOptions, "strictThisContextOnImports", false);
  692. D(parserOptions, "importMeta", true);
  693. D(parserOptions, "dynamicImportMode", "lazy");
  694. D(parserOptions, "dynamicImportPrefetch", false);
  695. D(parserOptions, "dynamicImportPreload", false);
  696. D(parserOptions, "dynamicImportFetchPriority", false);
  697. D(parserOptions, "createRequire", isNode);
  698. D(parserOptions, "dynamicUrl", true);
  699. D(parserOptions, "deferImport", deferImport);
  700. if (futureDefaults) D(parserOptions, "exportsPresence", "error");
  701. };
  702. /**
  703. * @param {JsonGeneratorOptions} generatorOptions generator options
  704. * @returns {void}
  705. */
  706. const applyJsonGeneratorOptionsDefaults = (generatorOptions) => {
  707. D(generatorOptions, "JSONParse", true);
  708. };
  709. /**
  710. * @param {CssGeneratorOptions} generatorOptions generator options
  711. * @param {object} options options
  712. * @param {TargetProperties | false} options.targetProperties target properties
  713. * @returns {void}
  714. */
  715. const applyCssGeneratorOptionsDefaults = (
  716. generatorOptions,
  717. { targetProperties }
  718. ) => {
  719. D(
  720. generatorOptions,
  721. "exportsOnly",
  722. !targetProperties || targetProperties.document === false
  723. );
  724. D(generatorOptions, "esModule", true);
  725. };
  726. /**
  727. * @param {ModuleOptions} module options
  728. * @param {object} options options
  729. * @param {boolean} options.cache is caching enabled
  730. * @param {boolean} options.syncWebAssembly is syncWebAssembly enabled
  731. * @param {boolean} options.asyncWebAssembly is asyncWebAssembly enabled
  732. * @param {boolean} options.css is css enabled
  733. * @param {boolean} options.futureDefaults is future defaults enabled
  734. * @param {string} options.uniqueName the unique name
  735. * @param {boolean} options.isNode is node target platform
  736. * @param {boolean} options.deferImport is defer import enabled
  737. * @param {TargetProperties | false} options.targetProperties target properties
  738. * @param {Mode | undefined} options.mode mode
  739. * @param {HashSalt} options.hashSalt hash salt
  740. * @param {HashDigest} options.hashDigest hash digest
  741. * @param {HashDigestLength} options.hashDigestLength hash digest length
  742. * @returns {void}
  743. */
  744. const applyModuleDefaults = (
  745. module,
  746. {
  747. hashSalt,
  748. hashDigest,
  749. hashDigestLength,
  750. cache,
  751. syncWebAssembly,
  752. asyncWebAssembly,
  753. css,
  754. futureDefaults,
  755. isNode,
  756. uniqueName,
  757. targetProperties,
  758. mode,
  759. deferImport
  760. }
  761. ) => {
  762. if (cache) {
  763. D(
  764. module,
  765. "unsafeCache",
  766. /**
  767. * @param {Module} module module
  768. * @returns {boolean} true, if we want to cache the module
  769. */
  770. (module) => {
  771. const name = module.nameForCondition();
  772. if (!name) {
  773. return false;
  774. }
  775. return NODE_MODULES_REGEXP.test(name);
  776. }
  777. );
  778. } else {
  779. D(module, "unsafeCache", false);
  780. }
  781. F(module.parser, ASSET_MODULE_TYPE, () => ({}));
  782. F(
  783. /** @type {NonNullable<ParserOptionsByModuleTypeKnown[ASSET_MODULE_TYPE]>} */
  784. (module.parser[ASSET_MODULE_TYPE]),
  785. "dataUrlCondition",
  786. () => ({})
  787. );
  788. if (
  789. typeof (
  790. /** @type {NonNullable<ParserOptionsByModuleTypeKnown[ASSET_MODULE_TYPE]>} */
  791. (module.parser[ASSET_MODULE_TYPE]).dataUrlCondition
  792. ) === "object"
  793. ) {
  794. D(
  795. /** @type {NonNullable<ParserOptionsByModuleTypeKnown[ASSET_MODULE_TYPE]>} */
  796. (module.parser[ASSET_MODULE_TYPE]).dataUrlCondition,
  797. "maxSize",
  798. 8096
  799. );
  800. }
  801. F(module.parser, "javascript", () => ({}));
  802. F(module.parser, JSON_MODULE_TYPE, () => ({}));
  803. D(
  804. /** @type {NonNullable<ParserOptionsByModuleTypeKnown[JSON_MODULE_TYPE]>} */
  805. (module.parser[JSON_MODULE_TYPE]),
  806. "exportsDepth",
  807. mode === "development" ? 1 : Infinity
  808. );
  809. applyJavascriptParserOptionsDefaults(
  810. /** @type {NonNullable<ParserOptionsByModuleTypeKnown["javascript"]>} */
  811. (module.parser.javascript),
  812. {
  813. futureDefaults,
  814. deferImport,
  815. isNode
  816. }
  817. );
  818. F(module.generator, "json", () => ({}));
  819. applyJsonGeneratorOptionsDefaults(
  820. /** @type {NonNullable<GeneratorOptionsByModuleTypeKnown["json"]>} */
  821. (module.generator.json)
  822. );
  823. if (css) {
  824. F(module.parser, CSS_MODULE_TYPE, () => ({}));
  825. D(
  826. /** @type {NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE]>} */
  827. (module.parser[CSS_MODULE_TYPE]),
  828. "import",
  829. true
  830. );
  831. D(
  832. /** @type {NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE]>} */
  833. (module.parser[CSS_MODULE_TYPE]),
  834. "url",
  835. true
  836. );
  837. D(
  838. /** @type {NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE]>} */
  839. (module.parser[CSS_MODULE_TYPE]),
  840. "namedExports",
  841. true
  842. );
  843. for (const type of [
  844. CSS_MODULE_TYPE_AUTO,
  845. CSS_MODULE_TYPE_MODULE,
  846. CSS_MODULE_TYPE_GLOBAL
  847. ]) {
  848. F(module.parser, type, () => ({}));
  849. D(
  850. /** @type {NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_AUTO]> | NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_MODULE]> | NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_GLOBAL]>} */
  851. (module.parser[type]),
  852. "animation",
  853. true
  854. );
  855. D(
  856. /** @type {NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_AUTO]> | NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_MODULE]> | NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_GLOBAL]>} */
  857. (module.parser[type]),
  858. "container",
  859. true
  860. );
  861. D(
  862. /** @type {NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_AUTO]> | NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_MODULE]> | NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_GLOBAL]>} */
  863. (module.parser[type]),
  864. "customIdents",
  865. true
  866. );
  867. D(
  868. /** @type {NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_AUTO]> | NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_MODULE]> | NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_GLOBAL]>} */
  869. (module.parser[type]),
  870. "dashedIdents",
  871. true
  872. );
  873. D(
  874. /** @type {NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_AUTO]> | NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_MODULE]> | NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_GLOBAL]>} */
  875. (module.parser[type]),
  876. "function",
  877. true
  878. );
  879. D(
  880. /** @type {NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_AUTO]> | NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_MODULE]> | NonNullable<ParserOptionsByModuleTypeKnown[CSS_MODULE_TYPE_GLOBAL]>} */
  881. (module.parser[type]),
  882. "grid",
  883. true
  884. );
  885. }
  886. F(module.generator, CSS_MODULE_TYPE, () => ({}));
  887. applyCssGeneratorOptionsDefaults(
  888. /** @type {NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE]>} */
  889. (module.generator[CSS_MODULE_TYPE]),
  890. { targetProperties }
  891. );
  892. const localIdentName =
  893. mode === "development"
  894. ? uniqueName.length > 0
  895. ? "[uniqueName]-[id]-[local]"
  896. : "[id]-[local]"
  897. : "[fullhash]";
  898. const localIdentHashSalt = hashSalt;
  899. const localIdentHashDigest = "base64url";
  900. const localIdentHashDigestLength = 6;
  901. const exportsConvention = "as-is";
  902. for (const type of [
  903. CSS_MODULE_TYPE_AUTO,
  904. CSS_MODULE_TYPE_MODULE,
  905. CSS_MODULE_TYPE_GLOBAL
  906. ]) {
  907. F(module.generator, type, () => ({}));
  908. D(
  909. /** @type {NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_AUTO]> | NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_MODULE]> | NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_GLOBAL]>} */
  910. (module.generator[type]),
  911. "localIdentName",
  912. localIdentName
  913. );
  914. D(
  915. /** @type {NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_AUTO]> | NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_MODULE]> | NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_GLOBAL]>} */
  916. (module.generator[type]),
  917. "localIdentHashSalt",
  918. localIdentHashSalt
  919. );
  920. D(
  921. /** @type {NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_AUTO]> | NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_MODULE]> | NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_MODULE]>} */
  922. (module.generator[type]),
  923. "localIdentHashDigest",
  924. localIdentHashDigest
  925. );
  926. D(
  927. /** @type {NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_AUTO]> | NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_MODULE]> | NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_MODULE]>} */
  928. (module.generator[type]),
  929. "localIdentHashDigestLength",
  930. localIdentHashDigestLength
  931. );
  932. D(
  933. /** @type {NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_AUTO]> | NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_MODULE]> | NonNullable<GeneratorOptionsByModuleTypeKnown[CSS_MODULE_TYPE_GLOBAL]>} */
  934. (module.generator[type]),
  935. "exportsConvention",
  936. exportsConvention
  937. );
  938. }
  939. }
  940. A(module, "defaultRules", () => {
  941. const esm = {
  942. type: JAVASCRIPT_MODULE_TYPE_ESM,
  943. resolve: {
  944. byDependency: {
  945. esm: {
  946. fullySpecified: true
  947. }
  948. }
  949. }
  950. };
  951. const commonjs = {
  952. type: JAVASCRIPT_MODULE_TYPE_DYNAMIC
  953. };
  954. /** @type {RuleSetRules} */
  955. const rules = [
  956. {
  957. mimetype: "application/node",
  958. type: JAVASCRIPT_MODULE_TYPE_AUTO
  959. },
  960. {
  961. test: /\.json$/i,
  962. type: JSON_MODULE_TYPE
  963. },
  964. {
  965. mimetype: "application/json",
  966. type: JSON_MODULE_TYPE
  967. },
  968. {
  969. test: /\.mjs$/i,
  970. ...esm
  971. },
  972. {
  973. test: /\.js$/i,
  974. descriptionData: {
  975. type: "module"
  976. },
  977. ...esm
  978. },
  979. {
  980. test: /\.cjs$/i,
  981. ...commonjs
  982. },
  983. {
  984. test: /\.js$/i,
  985. descriptionData: {
  986. type: "commonjs"
  987. },
  988. ...commonjs
  989. },
  990. {
  991. mimetype: {
  992. or: ["text/javascript", "application/javascript"]
  993. },
  994. ...esm
  995. }
  996. ];
  997. if (asyncWebAssembly) {
  998. const wasm = {
  999. type: WEBASSEMBLY_MODULE_TYPE_ASYNC,
  1000. rules: [
  1001. {
  1002. descriptionData: {
  1003. type: "module"
  1004. },
  1005. resolve: {
  1006. fullySpecified: true
  1007. }
  1008. }
  1009. ]
  1010. };
  1011. rules.push({
  1012. test: /\.wasm$/i,
  1013. ...wasm
  1014. });
  1015. rules.push({
  1016. mimetype: "application/wasm",
  1017. ...wasm
  1018. });
  1019. } else if (syncWebAssembly) {
  1020. const wasm = {
  1021. type: WEBASSEMBLY_MODULE_TYPE_SYNC,
  1022. rules: [
  1023. {
  1024. descriptionData: {
  1025. type: "module"
  1026. },
  1027. resolve: {
  1028. fullySpecified: true
  1029. }
  1030. }
  1031. ]
  1032. };
  1033. rules.push({
  1034. test: /\.wasm$/i,
  1035. ...wasm
  1036. });
  1037. rules.push({
  1038. mimetype: "application/wasm",
  1039. ...wasm
  1040. });
  1041. }
  1042. if (css) {
  1043. const resolve = {
  1044. fullySpecified: true,
  1045. preferRelative: true
  1046. };
  1047. rules.push({
  1048. test: /\.css$/i,
  1049. type: CSS_MODULE_TYPE_AUTO,
  1050. resolve
  1051. });
  1052. rules.push({
  1053. mimetype: "text/css+module",
  1054. type: CSS_MODULE_TYPE_MODULE,
  1055. resolve
  1056. });
  1057. rules.push({
  1058. mimetype: "text/css",
  1059. type: CSS_MODULE_TYPE,
  1060. resolve
  1061. });
  1062. // For CSS modules, i.e. `.class { composes: className from "./style.css" }`
  1063. // We inherit for such constructions
  1064. rules.push({
  1065. dependency: /css-import-local-module/,
  1066. type: CSS_MODULE_TYPE_MODULE,
  1067. resolve
  1068. });
  1069. rules.push({
  1070. dependency: /css-import-global-module/,
  1071. type: CSS_MODULE_TYPE_GLOBAL,
  1072. resolve
  1073. });
  1074. rules.push(
  1075. {
  1076. with: { type: "css" },
  1077. parser: {
  1078. exportType: "css-style-sheet"
  1079. },
  1080. resolve
  1081. },
  1082. {
  1083. assert: { type: "css" },
  1084. parser: {
  1085. exportType: "css-style-sheet"
  1086. },
  1087. resolve
  1088. }
  1089. );
  1090. }
  1091. rules.push(
  1092. {
  1093. dependency: "url",
  1094. oneOf: [
  1095. {
  1096. scheme: /^data$/,
  1097. type: ASSET_MODULE_TYPE_INLINE
  1098. },
  1099. {
  1100. type: ASSET_MODULE_TYPE_RESOURCE
  1101. }
  1102. ]
  1103. },
  1104. {
  1105. with: { type: JSON_MODULE_TYPE },
  1106. type: JSON_MODULE_TYPE,
  1107. parser: { namedExports: false }
  1108. },
  1109. {
  1110. assert: { type: JSON_MODULE_TYPE },
  1111. type: JSON_MODULE_TYPE,
  1112. parser: { namedExports: false }
  1113. },
  1114. {
  1115. with: { type: "text" },
  1116. type: ASSET_MODULE_TYPE_SOURCE
  1117. },
  1118. {
  1119. with: { type: "bytes" },
  1120. type: ASSET_MODULE_TYPE_BYTES
  1121. }
  1122. );
  1123. return rules;
  1124. });
  1125. };
  1126. /**
  1127. * @param {Output} output options
  1128. * @param {object} options options
  1129. * @param {string} options.context context
  1130. * @param {TargetProperties | false} options.targetProperties target properties
  1131. * @param {boolean} options.isAffectedByBrowserslist is affected by browserslist
  1132. * @param {boolean} options.outputModule is outputModule experiment enabled
  1133. * @param {boolean} options.development is development mode
  1134. * @param {Entry} options.entry entry option
  1135. * @param {boolean} options.futureDefaults is future defaults enabled
  1136. * @param {boolean} options.asyncWebAssembly is asyncWebAssembly enabled
  1137. * @returns {void}
  1138. */
  1139. const applyOutputDefaults = (
  1140. output,
  1141. {
  1142. context,
  1143. targetProperties: tp,
  1144. isAffectedByBrowserslist,
  1145. outputModule,
  1146. development,
  1147. entry,
  1148. futureDefaults,
  1149. asyncWebAssembly
  1150. }
  1151. ) => {
  1152. /**
  1153. * @param {Library=} library the library option
  1154. * @returns {string} a readable library name
  1155. */
  1156. const getLibraryName = (library) => {
  1157. const libraryName =
  1158. typeof library === "object" &&
  1159. library &&
  1160. !Array.isArray(library) &&
  1161. "type" in library
  1162. ? library.name
  1163. : /** @type {LibraryName} */ (library);
  1164. if (Array.isArray(libraryName)) {
  1165. return libraryName.join(".");
  1166. } else if (typeof libraryName === "object") {
  1167. return getLibraryName(libraryName.root);
  1168. } else if (typeof libraryName === "string") {
  1169. return libraryName;
  1170. }
  1171. return "";
  1172. };
  1173. F(output, "uniqueName", () => {
  1174. const libraryName = getLibraryName(output.library).replace(
  1175. /^\[(\\*[\w:]+\\*)\](\.)|(\.)\[(\\*[\w:]+\\*)\](?=\.|$)|\[(\\*[\w:]+\\*)\]/g,
  1176. (m, a, d1, d2, b, c) => {
  1177. const content = a || b || c;
  1178. return content.startsWith("\\") && content.endsWith("\\")
  1179. ? `${d2 || ""}[${content.slice(1, -1)}]${d1 || ""}`
  1180. : "";
  1181. }
  1182. );
  1183. if (libraryName) return libraryName;
  1184. const pkgPath = path.resolve(context, "package.json");
  1185. try {
  1186. const packageInfo = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
  1187. return packageInfo.name || "";
  1188. } catch (err) {
  1189. if (/** @type {Error & { code: string }} */ (err).code !== "ENOENT") {
  1190. /** @type {Error & { code: string }} */
  1191. (err).message +=
  1192. `\nwhile determining default 'output.uniqueName' from 'name' in ${pkgPath}`;
  1193. throw err;
  1194. }
  1195. return "";
  1196. }
  1197. });
  1198. F(output, "module", () => Boolean(outputModule));
  1199. const environment = /** @type {Environment} */ (output.environment);
  1200. /**
  1201. * @param {boolean | undefined} v value
  1202. * @returns {boolean} true, when v is truthy or undefined
  1203. */
  1204. const optimistic = (v) => v || v === undefined;
  1205. /**
  1206. * @param {boolean | undefined} v value
  1207. * @param {boolean | undefined} c condition
  1208. * @returns {boolean | undefined} true, when v is truthy or undefined, or c is truthy
  1209. */
  1210. const conditionallyOptimistic = (v, c) => (v === undefined && c) || v;
  1211. F(
  1212. environment,
  1213. "globalThis",
  1214. () => /** @type {boolean | undefined} */ (tp && tp.globalThis)
  1215. );
  1216. F(
  1217. environment,
  1218. "bigIntLiteral",
  1219. () =>
  1220. tp && optimistic(/** @type {boolean | undefined} */ (tp.bigIntLiteral))
  1221. );
  1222. F(
  1223. environment,
  1224. "const",
  1225. () => tp && optimistic(/** @type {boolean | undefined} */ (tp.const))
  1226. );
  1227. F(
  1228. environment,
  1229. "methodShorthand",
  1230. () =>
  1231. tp && optimistic(/** @type {boolean | undefined} */ (tp.methodShorthand))
  1232. );
  1233. F(
  1234. environment,
  1235. "arrowFunction",
  1236. () =>
  1237. tp && optimistic(/** @type {boolean | undefined} */ (tp.arrowFunction))
  1238. );
  1239. F(
  1240. environment,
  1241. "asyncFunction",
  1242. () =>
  1243. tp && optimistic(/** @type {boolean | undefined} */ (tp.asyncFunction))
  1244. );
  1245. F(
  1246. environment,
  1247. "forOf",
  1248. () => tp && optimistic(/** @type {boolean | undefined} */ (tp.forOf))
  1249. );
  1250. F(
  1251. environment,
  1252. "destructuring",
  1253. () =>
  1254. tp && optimistic(/** @type {boolean | undefined} */ (tp.destructuring))
  1255. );
  1256. F(
  1257. environment,
  1258. "optionalChaining",
  1259. () =>
  1260. tp && optimistic(/** @type {boolean | undefined} */ (tp.optionalChaining))
  1261. );
  1262. F(
  1263. environment,
  1264. "nodePrefixForCoreModules",
  1265. () =>
  1266. tp &&
  1267. optimistic(
  1268. /** @type {boolean | undefined} */ (tp.nodePrefixForCoreModules)
  1269. )
  1270. );
  1271. F(
  1272. environment,
  1273. "importMetaDirnameAndFilename",
  1274. () =>
  1275. // No optimistic, because it is new
  1276. tp && /** @type {boolean | undefined} */ (tp.importMetaDirnameAndFilename)
  1277. );
  1278. F(
  1279. environment,
  1280. "templateLiteral",
  1281. () =>
  1282. tp && optimistic(/** @type {boolean | undefined} */ (tp.templateLiteral))
  1283. );
  1284. F(environment, "dynamicImport", () =>
  1285. conditionallyOptimistic(
  1286. /** @type {boolean | undefined} */ (tp && tp.dynamicImport),
  1287. output.module
  1288. )
  1289. );
  1290. F(environment, "dynamicImportInWorker", () =>
  1291. conditionallyOptimistic(
  1292. /** @type {boolean | undefined} */ (tp && tp.dynamicImportInWorker),
  1293. output.module
  1294. )
  1295. );
  1296. F(environment, "module", () =>
  1297. conditionallyOptimistic(
  1298. /** @type {boolean | undefined} */ (tp && tp.module),
  1299. output.module
  1300. )
  1301. );
  1302. F(
  1303. environment,
  1304. "document",
  1305. () => tp && optimistic(/** @type {boolean | undefined} */ (tp.document))
  1306. );
  1307. D(output, "filename", output.module ? "[name].mjs" : "[name].js");
  1308. F(output, "iife", () => !output.module);
  1309. D(output, "importFunctionName", "import");
  1310. D(output, "importMetaName", "import.meta");
  1311. F(output, "chunkFilename", () => {
  1312. const filename =
  1313. /** @type {NonNullable<Output["chunkFilename"]>} */
  1314. (output.filename);
  1315. if (typeof filename !== "function") {
  1316. const hasName = filename.includes("[name]");
  1317. const hasId = filename.includes("[id]");
  1318. const hasChunkHash = filename.includes("[chunkhash]");
  1319. const hasContentHash = filename.includes("[contenthash]");
  1320. // Anything changing depending on chunk is fine
  1321. if (hasChunkHash || hasContentHash || hasName || hasId) return filename;
  1322. // Otherwise prefix "[id]." in front of the basename to make it changing
  1323. return filename.replace(/(^|\/)([^/]*(?:\?|$))/, "$1[id].$2");
  1324. }
  1325. return output.module ? "[id].mjs" : "[id].js";
  1326. });
  1327. F(output, "cssFilename", () => {
  1328. const filename =
  1329. /** @type {NonNullable<Output["cssFilename"]>} */
  1330. (output.filename);
  1331. if (typeof filename !== "function") {
  1332. return filename.replace(/\.[mc]?js(\?|$)/, ".css$1");
  1333. }
  1334. return "[id].css";
  1335. });
  1336. F(output, "cssChunkFilename", () => {
  1337. const chunkFilename =
  1338. /** @type {NonNullable<Output["cssChunkFilename"]>} */
  1339. (output.chunkFilename);
  1340. if (typeof chunkFilename !== "function") {
  1341. return chunkFilename.replace(/\.[mc]?js(\?|$)/, ".css$1");
  1342. }
  1343. return "[id].css";
  1344. });
  1345. D(output, "assetModuleFilename", "[hash][ext][query][fragment]");
  1346. D(output, "webassemblyModuleFilename", "[hash].module.wasm");
  1347. D(output, "compareBeforeEmit", true);
  1348. D(output, "charset", !futureDefaults);
  1349. const uniqueNameId = Template.toIdentifier(
  1350. /** @type {NonNullable<Output["uniqueName"]>} */ (output.uniqueName)
  1351. );
  1352. F(output, "hotUpdateGlobal", () => `webpackHotUpdate${uniqueNameId}`);
  1353. F(output, "chunkLoadingGlobal", () => `webpackChunk${uniqueNameId}`);
  1354. F(output, "globalObject", () => {
  1355. if (tp) {
  1356. if (tp.global) return "global";
  1357. if (tp.globalThis) return "globalThis";
  1358. // For universal target (i.e. code can be run in browser/node/worker etc.)
  1359. if (tp.web === null && tp.node === null && tp.module) return "globalThis";
  1360. }
  1361. return "self";
  1362. });
  1363. F(output, "chunkFormat", () => {
  1364. if (tp) {
  1365. const helpMessage = isAffectedByBrowserslist
  1366. ? "Make sure that your 'browserslist' includes only platforms that support these features or select an appropriate 'target' to allow selecting a chunk format by default. Alternatively specify the 'output.chunkFormat' directly."
  1367. : "Select an appropriate 'target' to allow selecting one by default, or specify the 'output.chunkFormat' directly.";
  1368. if (output.module) {
  1369. if (environment.dynamicImport) return "module";
  1370. if (tp.document) return "array-push";
  1371. throw new Error(
  1372. "For the selected environment is no default ESM chunk format available:\n" +
  1373. "ESM exports can be chosen when 'import()' is available.\n" +
  1374. `JSONP Array push can be chosen when 'document' is available.\n${
  1375. helpMessage
  1376. }`
  1377. );
  1378. } else {
  1379. if (tp.document) return "array-push";
  1380. if (tp.require) return "commonjs";
  1381. if (tp.nodeBuiltins) return "commonjs";
  1382. if (tp.importScripts) return "array-push";
  1383. throw new Error(
  1384. "For the selected environment is no default script chunk format available:\n" +
  1385. `${
  1386. tp.module
  1387. ? "Module ('module') can be chosen when ES modules are available (please set 'experiments.outputModule' and 'output.module' to `true`)"
  1388. : ""
  1389. }\n` +
  1390. "JSONP Array push ('array-push') can be chosen when 'document' or 'importScripts' is available.\n" +
  1391. `CommonJs exports ('commonjs') can be chosen when 'require' or node builtins are available.\n${
  1392. helpMessage
  1393. }`
  1394. );
  1395. }
  1396. }
  1397. throw new Error(
  1398. "Chunk format can't be selected by default when no target is specified"
  1399. );
  1400. });
  1401. D(output, "asyncChunks", true);
  1402. F(output, "chunkLoading", () => {
  1403. if (tp) {
  1404. switch (output.chunkFormat) {
  1405. case "array-push":
  1406. if (tp.document) return "jsonp";
  1407. if (tp.importScripts) return "import-scripts";
  1408. break;
  1409. case "commonjs":
  1410. if (tp.require) return "require";
  1411. if (tp.nodeBuiltins) return "async-node";
  1412. break;
  1413. case "module":
  1414. if (environment.dynamicImport) return "import";
  1415. break;
  1416. }
  1417. if (
  1418. (tp.require === null ||
  1419. tp.nodeBuiltins === null ||
  1420. tp.document === null ||
  1421. tp.importScripts === null) &&
  1422. output.module &&
  1423. environment.dynamicImport
  1424. ) {
  1425. return "universal";
  1426. }
  1427. }
  1428. return false;
  1429. });
  1430. F(output, "workerChunkLoading", () => {
  1431. if (tp) {
  1432. switch (output.chunkFormat) {
  1433. case "array-push":
  1434. if (tp.importScriptsInWorker) return "import-scripts";
  1435. break;
  1436. case "commonjs":
  1437. if (tp.require) return "require";
  1438. if (tp.nodeBuiltins) return "async-node";
  1439. break;
  1440. case "module":
  1441. if (environment.dynamicImportInWorker) return "import";
  1442. break;
  1443. }
  1444. if (
  1445. (tp.require === null ||
  1446. tp.nodeBuiltins === null ||
  1447. tp.importScriptsInWorker === null) &&
  1448. output.module &&
  1449. environment.dynamicImportInWorker
  1450. ) {
  1451. return "universal";
  1452. }
  1453. }
  1454. return false;
  1455. });
  1456. F(output, "wasmLoading", () => {
  1457. if (tp) {
  1458. if (tp.fetchWasm) return "fetch";
  1459. if (tp.nodeBuiltins) return "async-node";
  1460. if (
  1461. (tp.nodeBuiltins === null || tp.fetchWasm === null) &&
  1462. output.module &&
  1463. environment.dynamicImport
  1464. ) {
  1465. return "universal";
  1466. }
  1467. }
  1468. return false;
  1469. });
  1470. F(output, "workerWasmLoading", () => output.wasmLoading);
  1471. F(output, "devtoolNamespace", () => output.uniqueName);
  1472. if (output.library) {
  1473. F(output.library, "type", () => (output.module ? "module" : "var"));
  1474. }
  1475. F(output, "path", () => path.join(process.cwd(), "dist"));
  1476. F(output, "pathinfo", () => development);
  1477. D(output, "sourceMapFilename", "[file].map[query]");
  1478. D(
  1479. output,
  1480. "hotUpdateChunkFilename",
  1481. `[id].[fullhash].hot-update.${output.module ? "mjs" : "js"}`
  1482. );
  1483. D(
  1484. output,
  1485. "hotUpdateMainFilename",
  1486. `[runtime].[fullhash].hot-update.${output.module ? "json.mjs" : "json"}`
  1487. );
  1488. D(output, "crossOriginLoading", false);
  1489. F(output, "scriptType", () => (output.module ? "module" : false));
  1490. D(
  1491. output,
  1492. "publicPath",
  1493. (tp && (tp.document || tp.importScripts)) || output.scriptType === "module"
  1494. ? "auto"
  1495. : ""
  1496. );
  1497. D(output, "workerPublicPath", "");
  1498. D(output, "chunkLoadTimeout", 120000);
  1499. F(output, "hashFunction", () => {
  1500. if (futureDefaults) {
  1501. DEFAULTS.HASH_FUNCTION = "xxhash64";
  1502. return "xxhash64";
  1503. }
  1504. return "md4";
  1505. });
  1506. D(output, "hashDigest", "hex");
  1507. D(output, "hashDigestLength", futureDefaults ? 16 : 20);
  1508. D(output, "strictModuleErrorHandling", false);
  1509. D(output, "strictModuleExceptionHandling", false);
  1510. const { trustedTypes } = output;
  1511. if (trustedTypes) {
  1512. F(
  1513. trustedTypes,
  1514. "policyName",
  1515. () =>
  1516. /** @type {NonNullable<Output["uniqueName"]>} */
  1517. (output.uniqueName).replace(/[^a-zA-Z0-9\-#=_/@.%]+/g, "_") || "webpack"
  1518. );
  1519. D(trustedTypes, "onPolicyCreationFailure", "stop");
  1520. }
  1521. /**
  1522. * @param {(entryDescription: EntryDescription) => void} fn iterator
  1523. * @returns {void}
  1524. */
  1525. const forEachEntry = (fn) => {
  1526. for (const name of Object.keys(entry)) {
  1527. fn(/** @type {{ [k: string]: EntryDescription }} */ (entry)[name]);
  1528. }
  1529. };
  1530. A(output, "enabledLibraryTypes", () => {
  1531. /** @type {LibraryType[]} */
  1532. const enabledLibraryTypes = [];
  1533. if (output.library) {
  1534. enabledLibraryTypes.push(output.library.type);
  1535. }
  1536. forEachEntry((desc) => {
  1537. if (desc.library) {
  1538. enabledLibraryTypes.push(desc.library.type);
  1539. }
  1540. });
  1541. return enabledLibraryTypes;
  1542. });
  1543. A(output, "enabledChunkLoadingTypes", () => {
  1544. const enabledChunkLoadingTypes = new Set();
  1545. if (output.chunkLoading) {
  1546. enabledChunkLoadingTypes.add(output.chunkLoading);
  1547. }
  1548. if (output.workerChunkLoading) {
  1549. enabledChunkLoadingTypes.add(output.workerChunkLoading);
  1550. }
  1551. forEachEntry((desc) => {
  1552. if (desc.chunkLoading) {
  1553. enabledChunkLoadingTypes.add(desc.chunkLoading);
  1554. }
  1555. });
  1556. return [...enabledChunkLoadingTypes];
  1557. });
  1558. A(output, "enabledWasmLoadingTypes", () => {
  1559. const enabledWasmLoadingTypes = new Set();
  1560. if (output.wasmLoading) {
  1561. enabledWasmLoadingTypes.add(output.wasmLoading);
  1562. }
  1563. if (output.workerWasmLoading) {
  1564. enabledWasmLoadingTypes.add(output.workerWasmLoading);
  1565. }
  1566. forEachEntry((desc) => {
  1567. if (desc.wasmLoading) {
  1568. enabledWasmLoadingTypes.add(desc.wasmLoading);
  1569. }
  1570. });
  1571. return [...enabledWasmLoadingTypes];
  1572. });
  1573. };
  1574. /**
  1575. * @param {ExternalsPresets} externalsPresets options
  1576. * @param {object} options options
  1577. * @param {TargetProperties | false} options.targetProperties target properties
  1578. * @param {boolean} options.buildHttp buildHttp experiment enabled
  1579. * @param {boolean} options.outputModule is output type is module
  1580. * @returns {void}
  1581. */
  1582. const applyExternalsPresetsDefaults = (
  1583. externalsPresets,
  1584. { targetProperties, buildHttp, outputModule }
  1585. ) => {
  1586. /**
  1587. * @param {keyof TargetProperties} key a key
  1588. * @returns {boolean} true when target is universal, otherwise false
  1589. */
  1590. const isUniversal = (key) =>
  1591. Boolean(outputModule && targetProperties && targetProperties[key] === null);
  1592. D(
  1593. externalsPresets,
  1594. "web",
  1595. /** @type {boolean | undefined} */
  1596. (
  1597. !buildHttp &&
  1598. targetProperties &&
  1599. (targetProperties.web || isUniversal("node"))
  1600. )
  1601. );
  1602. D(
  1603. externalsPresets,
  1604. "node",
  1605. /** @type {boolean | undefined} */
  1606. (targetProperties && (targetProperties.node || isUniversal("node")))
  1607. );
  1608. D(
  1609. externalsPresets,
  1610. "nwjs",
  1611. /** @type {boolean | undefined} */
  1612. (targetProperties && (targetProperties.nwjs || isUniversal("nwjs")))
  1613. );
  1614. D(
  1615. externalsPresets,
  1616. "electron",
  1617. /** @type {boolean | undefined} */
  1618. ((targetProperties && targetProperties.electron) || isUniversal("electron"))
  1619. );
  1620. D(
  1621. externalsPresets,
  1622. "electronMain",
  1623. /** @type {boolean | undefined} */
  1624. (
  1625. targetProperties &&
  1626. targetProperties.electron &&
  1627. (targetProperties.electronMain || isUniversal("electronMain"))
  1628. )
  1629. );
  1630. D(
  1631. externalsPresets,
  1632. "electronPreload",
  1633. /** @type {boolean | undefined} */
  1634. (
  1635. targetProperties &&
  1636. targetProperties.electron &&
  1637. (targetProperties.electronPreload || isUniversal("electronPreload"))
  1638. )
  1639. );
  1640. D(
  1641. externalsPresets,
  1642. "electronRenderer",
  1643. /** @type {boolean | undefined} */
  1644. (
  1645. targetProperties &&
  1646. targetProperties.electron &&
  1647. (targetProperties.electronRenderer || isUniversal("electronRenderer"))
  1648. )
  1649. );
  1650. };
  1651. /**
  1652. * @param {Loader} loader options
  1653. * @param {object} options options
  1654. * @param {TargetProperties | false} options.targetProperties target properties
  1655. * @param {Environment} options.environment environment
  1656. * @returns {void}
  1657. */
  1658. const applyLoaderDefaults = (loader, { targetProperties, environment }) => {
  1659. F(loader, "target", () => {
  1660. if (targetProperties) {
  1661. if (targetProperties.electron) {
  1662. if (targetProperties.electronMain) return "electron-main";
  1663. if (targetProperties.electronPreload) return "electron-preload";
  1664. if (targetProperties.electronRenderer) return "electron-renderer";
  1665. return "electron";
  1666. }
  1667. if (targetProperties.nwjs) return "nwjs";
  1668. if (targetProperties.node) return "node";
  1669. if (targetProperties.web) return "web";
  1670. }
  1671. });
  1672. D(loader, "environment", environment);
  1673. };
  1674. /**
  1675. * @param {WebpackNode} node options
  1676. * @param {object} options options
  1677. * @param {TargetProperties | false} options.targetProperties target properties
  1678. * @param {boolean} options.futureDefaults is future defaults enabled
  1679. * @param {boolean} options.outputModule is output type is module
  1680. * @returns {void}
  1681. */
  1682. const applyNodeDefaults = (
  1683. node,
  1684. { futureDefaults, outputModule, targetProperties }
  1685. ) => {
  1686. if (node === false) return;
  1687. F(node, "global", () => {
  1688. if (targetProperties && targetProperties.global) return false;
  1689. // We use `warm` because overriding `global` with `globalThis` (or a polyfill) is sometimes safe (global.URL), sometimes unsafe (global.process), but we need to warn about it
  1690. return futureDefaults ? "warn" : true;
  1691. });
  1692. const handlerForNames = () => {
  1693. // TODO webpack@6 remove `node-module` in favor of `eval-only`
  1694. if (targetProperties) {
  1695. if (targetProperties.node) {
  1696. return "eval-only";
  1697. }
  1698. // For the "universal" target we only evaluate these values
  1699. if (
  1700. outputModule &&
  1701. targetProperties.node === null &&
  1702. targetProperties.web === null
  1703. ) {
  1704. return "eval-only";
  1705. }
  1706. }
  1707. // TODO webpack@6 should we use `warn-even-only`?
  1708. return futureDefaults ? "warn-mock" : "mock";
  1709. };
  1710. F(node, "__filename", handlerForNames);
  1711. F(node, "__dirname", handlerForNames);
  1712. };
  1713. /**
  1714. * @param {Performance} performance options
  1715. * @param {object} options options
  1716. * @param {boolean} options.production is production
  1717. * @returns {void}
  1718. */
  1719. const applyPerformanceDefaults = (performance, { production }) => {
  1720. if (performance === false) return;
  1721. D(performance, "maxAssetSize", 250000);
  1722. D(performance, "maxEntrypointSize", 250000);
  1723. F(performance, "hints", () => (production ? "warning" : false));
  1724. };
  1725. /**
  1726. * @param {Optimization} optimization options
  1727. * @param {object} options options
  1728. * @param {boolean} options.production is production
  1729. * @param {boolean} options.development is development
  1730. * @param {boolean} options.css is css enabled
  1731. * @param {boolean} options.records using records
  1732. * @returns {void}
  1733. */
  1734. const applyOptimizationDefaults = (
  1735. optimization,
  1736. { production, development, css, records }
  1737. ) => {
  1738. D(optimization, "removeAvailableModules", false);
  1739. D(optimization, "removeEmptyChunks", true);
  1740. D(optimization, "mergeDuplicateChunks", true);
  1741. D(optimization, "flagIncludedChunks", production);
  1742. F(optimization, "moduleIds", () => {
  1743. if (production) return "deterministic";
  1744. if (development) return "named";
  1745. return "natural";
  1746. });
  1747. F(optimization, "chunkIds", () => {
  1748. if (production) return "deterministic";
  1749. if (development) return "named";
  1750. return "natural";
  1751. });
  1752. F(optimization, "sideEffects", () => (production ? true : "flag"));
  1753. D(optimization, "providedExports", true);
  1754. D(optimization, "usedExports", production);
  1755. D(optimization, "innerGraph", production);
  1756. D(optimization, "mangleExports", production);
  1757. D(optimization, "concatenateModules", production);
  1758. D(optimization, "avoidEntryIife", production);
  1759. D(optimization, "runtimeChunk", false);
  1760. D(optimization, "emitOnErrors", !production);
  1761. D(optimization, "checkWasmTypes", production);
  1762. D(optimization, "mangleWasmImports", false);
  1763. D(optimization, "portableRecords", records);
  1764. D(optimization, "realContentHash", production);
  1765. D(optimization, "minimize", production);
  1766. A(optimization, "minimizer", () => [
  1767. {
  1768. apply: (compiler) => {
  1769. // Lazy load the Terser plugin
  1770. const TerserPlugin = require("terser-webpack-plugin");
  1771. new TerserPlugin({
  1772. terserOptions: {
  1773. compress: {
  1774. passes: 2
  1775. }
  1776. }
  1777. }).apply(/** @type {EXPECTED_ANY} */ (compiler));
  1778. }
  1779. }
  1780. ]);
  1781. F(optimization, "nodeEnv", () => {
  1782. if (production) return "production";
  1783. if (development) return "development";
  1784. return false;
  1785. });
  1786. const { splitChunks } = optimization;
  1787. if (splitChunks) {
  1788. A(splitChunks, "defaultSizeTypes", () =>
  1789. css
  1790. ? [JAVASCRIPT_TYPE, CSS_TYPE, UNKNOWN_TYPE]
  1791. : [JAVASCRIPT_TYPE, UNKNOWN_TYPE]
  1792. );
  1793. D(splitChunks, "hidePathInfo", production);
  1794. D(splitChunks, "chunks", "async");
  1795. D(splitChunks, "usedExports", optimization.usedExports === true);
  1796. D(splitChunks, "minChunks", 1);
  1797. F(splitChunks, "minSize", () => (production ? 20000 : 10000));
  1798. F(splitChunks, "minRemainingSize", () => (development ? 0 : undefined));
  1799. F(splitChunks, "enforceSizeThreshold", () => (production ? 50000 : 30000));
  1800. F(splitChunks, "maxAsyncRequests", () => (production ? 30 : Infinity));
  1801. F(splitChunks, "maxInitialRequests", () => (production ? 30 : Infinity));
  1802. D(splitChunks, "automaticNameDelimiter", "-");
  1803. const cacheGroups =
  1804. /** @type {NonNullable<OptimizationSplitChunksOptions["cacheGroups"]>} */
  1805. (splitChunks.cacheGroups);
  1806. F(cacheGroups, "default", () => ({
  1807. idHint: "",
  1808. reuseExistingChunk: true,
  1809. minChunks: 2,
  1810. priority: -20
  1811. }));
  1812. F(cacheGroups, "defaultVendors", () => ({
  1813. idHint: "vendors",
  1814. reuseExistingChunk: true,
  1815. test: NODE_MODULES_REGEXP,
  1816. priority: -10
  1817. }));
  1818. }
  1819. };
  1820. /**
  1821. * @param {object} options options
  1822. * @param {boolean} options.cache is cache enable
  1823. * @param {string} options.context build context
  1824. * @param {TargetProperties | false} options.targetProperties target properties
  1825. * @param {Mode} options.mode mode
  1826. * @param {boolean} options.css is css enabled
  1827. * @returns {ResolveOptions} resolve options
  1828. */
  1829. const getResolveDefaults = ({
  1830. cache,
  1831. context,
  1832. targetProperties,
  1833. mode,
  1834. css
  1835. }) => {
  1836. /** @type {string[]} */
  1837. const conditions = ["webpack"];
  1838. conditions.push(mode === "development" ? "development" : "production");
  1839. if (targetProperties) {
  1840. if (targetProperties.webworker) conditions.push("worker");
  1841. if (targetProperties.node) conditions.push("node");
  1842. if (targetProperties.web) conditions.push("browser");
  1843. if (targetProperties.electron) conditions.push("electron");
  1844. if (targetProperties.nwjs) conditions.push("nwjs");
  1845. }
  1846. const jsExtensions = [".js", ".json", ".wasm"];
  1847. const tp = targetProperties;
  1848. const browserField =
  1849. tp && tp.web && (!tp.node || (tp.electron && tp.electronRenderer));
  1850. /** @type {() => ResolveOptions} */
  1851. const cjsDeps = () => ({
  1852. aliasFields: browserField ? ["browser"] : [],
  1853. mainFields: browserField ? ["browser", "module", "..."] : ["module", "..."],
  1854. conditionNames: ["require", "module", "..."],
  1855. extensions: [...jsExtensions]
  1856. });
  1857. /** @type {() => ResolveOptions} */
  1858. const esmDeps = () => ({
  1859. aliasFields: browserField ? ["browser"] : [],
  1860. mainFields: browserField ? ["browser", "module", "..."] : ["module", "..."],
  1861. conditionNames: ["import", "module", "..."],
  1862. extensions: [...jsExtensions]
  1863. });
  1864. /** @type {ResolveOptions} */
  1865. const resolveOptions = {
  1866. cache,
  1867. modules: ["node_modules"],
  1868. conditionNames: conditions,
  1869. mainFiles: ["index"],
  1870. extensions: [],
  1871. aliasFields: [],
  1872. exportsFields: ["exports"],
  1873. roots: [context],
  1874. mainFields: ["main"],
  1875. importsFields: ["imports"],
  1876. byDependency: {
  1877. wasm: esmDeps(),
  1878. esm: esmDeps(),
  1879. loaderImport: esmDeps(),
  1880. url: {
  1881. preferRelative: true
  1882. },
  1883. worker: {
  1884. ...esmDeps(),
  1885. preferRelative: true
  1886. },
  1887. commonjs: cjsDeps(),
  1888. amd: cjsDeps(),
  1889. // for backward-compat: loadModule
  1890. loader: cjsDeps(),
  1891. // for backward-compat: Custom Dependency
  1892. unknown: cjsDeps(),
  1893. // for backward-compat: getResolve without dependencyType
  1894. undefined: cjsDeps()
  1895. }
  1896. };
  1897. if (css) {
  1898. const styleConditions = [];
  1899. styleConditions.push("webpack");
  1900. styleConditions.push(mode === "development" ? "development" : "production");
  1901. styleConditions.push("style");
  1902. /** @type {ResolveOptions} */
  1903. const cssResolveOptions = {
  1904. // We avoid using any main files because we have to be consistent with CSS `@import`
  1905. // and CSS `@import` does not handle `main` files in directories,
  1906. // you should always specify the full URL for styles
  1907. mainFiles: [],
  1908. mainFields: ["style", "..."],
  1909. conditionNames: styleConditions,
  1910. extensions: [".css"],
  1911. preferRelative: true
  1912. };
  1913. /** @type {NonNullable<ResolveOptions["byDependency"]>} */
  1914. (resolveOptions.byDependency)["css-import"] = cssResolveOptions;
  1915. // For CSS modules, i.e. `.class { composes: className from "./style.css" }`
  1916. // We inherit for such constructions
  1917. /** @type {NonNullable<ResolveOptions["byDependency"]>} */
  1918. (resolveOptions.byDependency)["css-import-local-module"] =
  1919. cssResolveOptions;
  1920. /** @type {NonNullable<ResolveOptions["byDependency"]>} */
  1921. (resolveOptions.byDependency)["css-import-global-module"] =
  1922. cssResolveOptions;
  1923. }
  1924. return resolveOptions;
  1925. };
  1926. /**
  1927. * @param {object} options options
  1928. * @param {boolean} options.cache is cache enable
  1929. * @returns {ResolveOptions} resolve options
  1930. */
  1931. const getResolveLoaderDefaults = ({ cache }) => {
  1932. /** @type {ResolveOptions} */
  1933. const resolveOptions = {
  1934. cache,
  1935. conditionNames: ["loader", "require", "node"],
  1936. exportsFields: ["exports"],
  1937. mainFields: ["loader", "main"],
  1938. extensions: [".js"],
  1939. mainFiles: ["index"]
  1940. };
  1941. return resolveOptions;
  1942. };
  1943. /**
  1944. * @param {InfrastructureLogging} infrastructureLogging options
  1945. * @returns {void}
  1946. */
  1947. const applyInfrastructureLoggingDefaults = (infrastructureLogging) => {
  1948. F(infrastructureLogging, "stream", () => process.stderr);
  1949. const tty =
  1950. /** @type {NonNullable<InfrastructureLogging["stream"]>} */
  1951. (infrastructureLogging.stream).isTTY && process.env.TERM !== "dumb";
  1952. D(infrastructureLogging, "level", "info");
  1953. D(infrastructureLogging, "debug", false);
  1954. D(infrastructureLogging, "colors", tty);
  1955. D(infrastructureLogging, "appendOnly", !tty);
  1956. };
  1957. module.exports.DEFAULTS = DEFAULTS;
  1958. module.exports.applyWebpackOptionsBaseDefaults =
  1959. applyWebpackOptionsBaseDefaults;
  1960. module.exports.applyWebpackOptionsDefaults = applyWebpackOptionsDefaults;