defaults.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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 Template = require("../Template");
  9. const { cleverMerge } = require("../util/cleverMerge");
  10. const {
  11. getTargetsProperties,
  12. getTargetProperties,
  13. getDefaultTarget
  14. } = require("./target");
  15. /** @typedef {import("../../declarations/WebpackOptions").CacheOptionsNormalized} CacheOptions */
  16. /** @typedef {import("../../declarations/WebpackOptions").CssExperimentOptions} CssExperimentOptions */
  17. /** @typedef {import("../../declarations/WebpackOptions").EntryDescription} EntryDescription */
  18. /** @typedef {import("../../declarations/WebpackOptions").EntryNormalized} Entry */
  19. /** @typedef {import("../../declarations/WebpackOptions").Experiments} Experiments */
  20. /** @typedef {import("../../declarations/WebpackOptions").ExperimentsNormalized} ExperimentsNormalized */
  21. /** @typedef {import("../../declarations/WebpackOptions").ExternalsPresets} ExternalsPresets */
  22. /** @typedef {import("../../declarations/WebpackOptions").ExternalsType} ExternalsType */
  23. /** @typedef {import("../../declarations/WebpackOptions").InfrastructureLogging} InfrastructureLogging */
  24. /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
  25. /** @typedef {import("../../declarations/WebpackOptions").Library} Library */
  26. /** @typedef {import("../../declarations/WebpackOptions").LibraryName} LibraryName */
  27. /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
  28. /** @typedef {import("../../declarations/WebpackOptions").Loader} Loader */
  29. /** @typedef {import("../../declarations/WebpackOptions").Mode} Mode */
  30. /** @typedef {import("../../declarations/WebpackOptions").ModuleOptionsNormalized} ModuleOptions */
  31. /** @typedef {import("../../declarations/WebpackOptions").Node} WebpackNode */
  32. /** @typedef {import("../../declarations/WebpackOptions").Optimization} Optimization */
  33. /** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} Output */
  34. /** @typedef {import("../../declarations/WebpackOptions").Performance} Performance */
  35. /** @typedef {import("../../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
  36. /** @typedef {import("../../declarations/WebpackOptions").RuleSetRules} RuleSetRules */
  37. /** @typedef {import("../../declarations/WebpackOptions").SnapshotOptions} SnapshotOptions */
  38. /** @typedef {import("../../declarations/WebpackOptions").Target} Target */
  39. /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
  40. /** @typedef {import("./target").TargetProperties} TargetProperties */
  41. const NODE_MODULES_REGEXP = /[\\/]node_modules[\\/]/i;
  42. /**
  43. * Sets a constant default value when undefined
  44. * @template T
  45. * @template {keyof T} P
  46. * @param {T} obj an object
  47. * @param {P} prop a property of this object
  48. * @param {T[P]} value a default value of the property
  49. * @returns {void}
  50. */
  51. const D = (obj, prop, value) => {
  52. if (obj[prop] === undefined) {
  53. obj[prop] = value;
  54. }
  55. };
  56. /**
  57. * Sets a dynamic default value when undefined, by calling the factory function
  58. * @template T
  59. * @template {keyof T} P
  60. * @param {T} obj an object
  61. * @param {P} prop a property of this object
  62. * @param {function(): T[P]} factory a default value factory for the property
  63. * @returns {void}
  64. */
  65. const F = (obj, prop, factory) => {
  66. if (obj[prop] === undefined) {
  67. obj[prop] = factory();
  68. }
  69. };
  70. /**
  71. * Sets a dynamic default value when undefined, by calling the factory function.
  72. * factory must return an array or undefined
  73. * When the current value is already an array an contains "..." it's replaced with
  74. * the result of the factory function
  75. * @template T
  76. * @template {keyof T} P
  77. * @param {T} obj an object
  78. * @param {P} prop a property of this object
  79. * @param {function(): T[P]} factory a default value factory for the property
  80. * @returns {void}
  81. */
  82. const A = (obj, prop, factory) => {
  83. const value = obj[prop];
  84. if (value === undefined) {
  85. obj[prop] = factory();
  86. } else if (Array.isArray(value)) {
  87. /** @type {any[]} */
  88. let newArray = undefined;
  89. for (let i = 0; i < value.length; i++) {
  90. const item = value[i];
  91. if (item === "...") {
  92. if (newArray === undefined) {
  93. newArray = value.slice(0, i);
  94. obj[prop] = /** @type {T[P]} */ (/** @type {unknown} */ (newArray));
  95. }
  96. const items = /** @type {any[]} */ (/** @type {unknown} */ (factory()));
  97. if (items !== undefined) {
  98. for (const item of items) {
  99. newArray.push(item);
  100. }
  101. }
  102. } else if (newArray !== undefined) {
  103. newArray.push(item);
  104. }
  105. }
  106. }
  107. };
  108. /**
  109. * @param {WebpackOptions} options options to be modified
  110. * @returns {void}
  111. */
  112. const applyWebpackOptionsBaseDefaults = options => {
  113. F(options, "context", () => process.cwd());
  114. applyInfrastructureLoggingDefaults(options.infrastructureLogging);
  115. };
  116. /**
  117. * @param {WebpackOptions} options options to be modified
  118. * @returns {void}
  119. */
  120. const applyWebpackOptionsDefaults = options => {
  121. F(options, "context", () => process.cwd());
  122. F(options, "target", () => {
  123. return getDefaultTarget(options.context);
  124. });
  125. const { mode, name, target } = options;
  126. let targetProperties =
  127. target === false
  128. ? /** @type {false} */ (false)
  129. : typeof target === "string"
  130. ? getTargetProperties(target, options.context)
  131. : getTargetsProperties(target, options.context);
  132. const development = mode === "development";
  133. const production = mode === "production" || !mode;
  134. if (typeof options.entry !== "function") {
  135. for (const key of Object.keys(options.entry)) {
  136. F(
  137. options.entry[key],
  138. "import",
  139. () => /** @type {[string]} */ (["./src"])
  140. );
  141. }
  142. }
  143. F(options, "devtool", () => (development ? "eval" : false));
  144. D(options, "watch", false);
  145. D(options, "profile", false);
  146. D(options, "parallelism", 100);
  147. D(options, "recordsInputPath", false);
  148. D(options, "recordsOutputPath", false);
  149. applyExperimentsDefaults(options.experiments, {
  150. production,
  151. development,
  152. targetProperties
  153. });
  154. const futureDefaults = options.experiments.futureDefaults;
  155. F(options, "cache", () =>
  156. development ? { type: /** @type {"memory"} */ ("memory") } : false
  157. );
  158. applyCacheDefaults(options.cache, {
  159. name: name || "default",
  160. mode: mode || "production",
  161. development,
  162. cacheUnaffected: options.experiments.cacheUnaffected
  163. });
  164. const cache = !!options.cache;
  165. applySnapshotDefaults(options.snapshot, {
  166. production,
  167. futureDefaults
  168. });
  169. applyModuleDefaults(options.module, {
  170. cache,
  171. syncWebAssembly: options.experiments.syncWebAssembly,
  172. asyncWebAssembly: options.experiments.asyncWebAssembly,
  173. css: options.experiments.css,
  174. futureDefaults
  175. });
  176. applyOutputDefaults(options.output, {
  177. context: options.context,
  178. targetProperties,
  179. isAffectedByBrowserslist:
  180. target === undefined ||
  181. (typeof target === "string" && target.startsWith("browserslist")) ||
  182. (Array.isArray(target) &&
  183. target.some(target => target.startsWith("browserslist"))),
  184. outputModule: options.experiments.outputModule,
  185. development,
  186. entry: options.entry,
  187. module: options.module,
  188. futureDefaults
  189. });
  190. applyExternalsPresetsDefaults(options.externalsPresets, {
  191. targetProperties,
  192. buildHttp: !!options.experiments.buildHttp
  193. });
  194. applyLoaderDefaults(options.loader, { targetProperties });
  195. F(options, "externalsType", () => {
  196. const validExternalTypes = require("../../schemas/WebpackOptions.json")
  197. .definitions.ExternalsType.enum;
  198. return options.output.library &&
  199. validExternalTypes.includes(options.output.library.type)
  200. ? /** @type {ExternalsType} */ (options.output.library.type)
  201. : options.output.module
  202. ? "module"
  203. : "var";
  204. });
  205. applyNodeDefaults(options.node, {
  206. futureDefaults: options.experiments.futureDefaults,
  207. targetProperties
  208. });
  209. F(options, "performance", () =>
  210. production &&
  211. targetProperties &&
  212. (targetProperties.browser || targetProperties.browser === null)
  213. ? {}
  214. : false
  215. );
  216. applyPerformanceDefaults(options.performance, {
  217. production
  218. });
  219. applyOptimizationDefaults(options.optimization, {
  220. development,
  221. production,
  222. css: options.experiments.css,
  223. records: !!(options.recordsInputPath || options.recordsOutputPath)
  224. });
  225. options.resolve = cleverMerge(
  226. getResolveDefaults({
  227. cache,
  228. context: options.context,
  229. targetProperties,
  230. mode: options.mode
  231. }),
  232. options.resolve
  233. );
  234. options.resolveLoader = cleverMerge(
  235. getResolveLoaderDefaults({ cache }),
  236. options.resolveLoader
  237. );
  238. };
  239. /**
  240. * @param {ExperimentsNormalized} experiments options
  241. * @param {Object} options options
  242. * @param {boolean} options.production is production
  243. * @param {boolean} options.development is development mode
  244. * @param {TargetProperties | false} options.targetProperties target properties
  245. * @returns {void}
  246. */
  247. const applyExperimentsDefaults = (
  248. experiments,
  249. { production, development, targetProperties }
  250. ) => {
  251. D(experiments, "futureDefaults", false);
  252. D(experiments, "backCompat", !experiments.futureDefaults);
  253. D(experiments, "topLevelAwait", experiments.futureDefaults);
  254. D(experiments, "syncWebAssembly", false);
  255. D(experiments, "asyncWebAssembly", experiments.futureDefaults);
  256. D(experiments, "outputModule", false);
  257. D(experiments, "layers", false);
  258. D(experiments, "lazyCompilation", undefined);
  259. D(experiments, "buildHttp", undefined);
  260. D(experiments, "cacheUnaffected", experiments.futureDefaults);
  261. F(experiments, "css", () => (experiments.futureDefaults ? {} : undefined));
  262. if (typeof experiments.buildHttp === "object") {
  263. D(experiments.buildHttp, "frozen", production);
  264. D(experiments.buildHttp, "upgrade", false);
  265. }
  266. if (typeof experiments.css === "object") {
  267. D(
  268. experiments.css,
  269. "exportsOnly",
  270. !targetProperties || !targetProperties.document
  271. );
  272. }
  273. };
  274. /**
  275. * @param {CacheOptions} cache options
  276. * @param {Object} options options
  277. * @param {string} options.name name
  278. * @param {string} options.mode mode
  279. * @param {boolean} options.development is development mode
  280. * @param {boolean} options.cacheUnaffected the cacheUnaffected experiment is enabled
  281. * @returns {void}
  282. */
  283. const applyCacheDefaults = (
  284. cache,
  285. { name, mode, development, cacheUnaffected }
  286. ) => {
  287. if (cache === false) return;
  288. switch (cache.type) {
  289. case "filesystem":
  290. F(cache, "name", () => name + "-" + mode);
  291. D(cache, "version", "");
  292. F(cache, "cacheDirectory", () => {
  293. const cwd = process.cwd();
  294. let dir = cwd;
  295. for (;;) {
  296. try {
  297. if (fs.statSync(path.join(dir, "package.json")).isFile()) break;
  298. // eslint-disable-next-line no-empty
  299. } catch (e) {}
  300. const parent = path.dirname(dir);
  301. if (dir === parent) {
  302. dir = undefined;
  303. break;
  304. }
  305. dir = parent;
  306. }
  307. if (!dir) {
  308. return path.resolve(cwd, ".cache/webpack");
  309. } else if (process.versions.pnp === "1") {
  310. return path.resolve(dir, ".pnp/.cache/webpack");
  311. } else if (process.versions.pnp === "3") {
  312. return path.resolve(dir, ".yarn/.cache/webpack");
  313. } else {
  314. return path.resolve(dir, "node_modules/.cache/webpack");
  315. }
  316. });
  317. F(cache, "cacheLocation", () =>
  318. path.resolve(cache.cacheDirectory, cache.name)
  319. );
  320. D(cache, "hashAlgorithm", "md4");
  321. D(cache, "store", "pack");
  322. D(cache, "compression", false);
  323. D(cache, "profile", false);
  324. D(cache, "idleTimeout", 60000);
  325. D(cache, "idleTimeoutForInitialStore", 5000);
  326. D(cache, "idleTimeoutAfterLargeChanges", 1000);
  327. D(cache, "maxMemoryGenerations", development ? 5 : Infinity);
  328. D(cache, "maxAge", 1000 * 60 * 60 * 24 * 60); // 1 month
  329. D(cache, "allowCollectingMemory", development);
  330. D(cache, "memoryCacheUnaffected", development && cacheUnaffected);
  331. D(cache.buildDependencies, "defaultWebpack", [
  332. path.resolve(__dirname, "..") + path.sep
  333. ]);
  334. break;
  335. case "memory":
  336. D(cache, "maxGenerations", Infinity);
  337. D(cache, "cacheUnaffected", development && cacheUnaffected);
  338. break;
  339. }
  340. };
  341. /**
  342. * @param {SnapshotOptions} snapshot options
  343. * @param {Object} options options
  344. * @param {boolean} options.production is production
  345. * @param {boolean} options.futureDefaults is future defaults enabled
  346. * @returns {void}
  347. */
  348. const applySnapshotDefaults = (snapshot, { production, futureDefaults }) => {
  349. if (futureDefaults) {
  350. F(snapshot, "managedPaths", () =>
  351. process.versions.pnp === "3"
  352. ? [
  353. /^(.+?(?:[\\/]\.yarn[\\/]unplugged[\\/][^\\/]+)?[\\/]node_modules[\\/])/
  354. ]
  355. : [/^(.+?[\\/]node_modules[\\/])/]
  356. );
  357. F(snapshot, "immutablePaths", () =>
  358. process.versions.pnp === "3"
  359. ? [/^(.+?[\\/]cache[\\/][^\\/]+\.zip[\\/]node_modules[\\/])/]
  360. : []
  361. );
  362. } else {
  363. A(snapshot, "managedPaths", () => {
  364. if (process.versions.pnp === "3") {
  365. const match =
  366. /^(.+?)[\\/]cache[\\/]watchpack-npm-[^\\/]+\.zip[\\/]node_modules[\\/]/.exec(
  367. require.resolve("watchpack")
  368. );
  369. if (match) {
  370. return [path.resolve(match[1], "unplugged")];
  371. }
  372. } else {
  373. const match = /^(.+?[\\/]node_modules[\\/])/.exec(
  374. // eslint-disable-next-line node/no-extraneous-require
  375. require.resolve("watchpack")
  376. );
  377. if (match) {
  378. return [match[1]];
  379. }
  380. }
  381. return [];
  382. });
  383. A(snapshot, "immutablePaths", () => {
  384. if (process.versions.pnp === "1") {
  385. const match =
  386. /^(.+?[\\/]v4)[\\/]npm-watchpack-[^\\/]+-[\da-f]{40}[\\/]node_modules[\\/]/.exec(
  387. require.resolve("watchpack")
  388. );
  389. if (match) {
  390. return [match[1]];
  391. }
  392. } else if (process.versions.pnp === "3") {
  393. const match =
  394. /^(.+?)[\\/]watchpack-npm-[^\\/]+\.zip[\\/]node_modules[\\/]/.exec(
  395. require.resolve("watchpack")
  396. );
  397. if (match) {
  398. return [match[1]];
  399. }
  400. }
  401. return [];
  402. });
  403. }
  404. F(snapshot, "resolveBuildDependencies", () => ({
  405. timestamp: true,
  406. hash: true
  407. }));
  408. F(snapshot, "buildDependencies", () => ({ timestamp: true, hash: true }));
  409. F(snapshot, "module", () =>
  410. production ? { timestamp: true, hash: true } : { timestamp: true }
  411. );
  412. F(snapshot, "resolve", () =>
  413. production ? { timestamp: true, hash: true } : { timestamp: true }
  414. );
  415. };
  416. /**
  417. * @param {JavascriptParserOptions} parserOptions parser options
  418. * @param {Object} options options
  419. * @param {boolean} options.futureDefaults is future defaults enabled
  420. * @returns {void}
  421. */
  422. const applyJavascriptParserOptionsDefaults = (
  423. parserOptions,
  424. { futureDefaults }
  425. ) => {
  426. D(parserOptions, "unknownContextRequest", ".");
  427. D(parserOptions, "unknownContextRegExp", false);
  428. D(parserOptions, "unknownContextRecursive", true);
  429. D(parserOptions, "unknownContextCritical", true);
  430. D(parserOptions, "exprContextRequest", ".");
  431. D(parserOptions, "exprContextRegExp", false);
  432. D(parserOptions, "exprContextRecursive", true);
  433. D(parserOptions, "exprContextCritical", true);
  434. D(parserOptions, "wrappedContextRegExp", /.*/);
  435. D(parserOptions, "wrappedContextRecursive", true);
  436. D(parserOptions, "wrappedContextCritical", false);
  437. D(parserOptions, "strictThisContextOnImports", false);
  438. D(parserOptions, "importMeta", true);
  439. if (futureDefaults) D(parserOptions, "exportsPresence", "error");
  440. };
  441. /**
  442. * @param {ModuleOptions} module options
  443. * @param {Object} options options
  444. * @param {boolean} options.cache is caching enabled
  445. * @param {boolean} options.syncWebAssembly is syncWebAssembly enabled
  446. * @param {boolean} options.asyncWebAssembly is asyncWebAssembly enabled
  447. * @param {CssExperimentOptions} options.css is css enabled
  448. * @param {boolean} options.futureDefaults is future defaults enabled
  449. * @returns {void}
  450. */
  451. const applyModuleDefaults = (
  452. module,
  453. { cache, syncWebAssembly, asyncWebAssembly, css, futureDefaults }
  454. ) => {
  455. if (cache) {
  456. D(module, "unsafeCache", module => {
  457. const name = module.nameForCondition();
  458. return name && NODE_MODULES_REGEXP.test(name);
  459. });
  460. } else {
  461. D(module, "unsafeCache", false);
  462. }
  463. F(module.parser, "asset", () => ({}));
  464. F(module.parser.asset, "dataUrlCondition", () => ({}));
  465. if (typeof module.parser.asset.dataUrlCondition === "object") {
  466. D(module.parser.asset.dataUrlCondition, "maxSize", 8096);
  467. }
  468. F(module.parser, "javascript", () => ({}));
  469. applyJavascriptParserOptionsDefaults(module.parser.javascript, {
  470. futureDefaults
  471. });
  472. A(module, "defaultRules", () => {
  473. const esm = {
  474. type: "javascript/esm",
  475. resolve: {
  476. byDependency: {
  477. esm: {
  478. fullySpecified: true
  479. }
  480. }
  481. }
  482. };
  483. const commonjs = {
  484. type: "javascript/dynamic"
  485. };
  486. /** @type {RuleSetRules} */
  487. const rules = [
  488. {
  489. mimetype: "application/node",
  490. type: "javascript/auto"
  491. },
  492. {
  493. test: /\.json$/i,
  494. type: "json"
  495. },
  496. {
  497. mimetype: "application/json",
  498. type: "json"
  499. },
  500. {
  501. test: /\.mjs$/i,
  502. ...esm
  503. },
  504. {
  505. test: /\.js$/i,
  506. descriptionData: {
  507. type: "module"
  508. },
  509. ...esm
  510. },
  511. {
  512. test: /\.cjs$/i,
  513. ...commonjs
  514. },
  515. {
  516. test: /\.js$/i,
  517. descriptionData: {
  518. type: "commonjs"
  519. },
  520. ...commonjs
  521. },
  522. {
  523. mimetype: {
  524. or: ["text/javascript", "application/javascript"]
  525. },
  526. ...esm
  527. }
  528. ];
  529. if (asyncWebAssembly) {
  530. const wasm = {
  531. type: "webassembly/async",
  532. rules: [
  533. {
  534. descriptionData: {
  535. type: "module"
  536. },
  537. resolve: {
  538. fullySpecified: true
  539. }
  540. }
  541. ]
  542. };
  543. rules.push({
  544. test: /\.wasm$/i,
  545. ...wasm
  546. });
  547. rules.push({
  548. mimetype: "application/wasm",
  549. ...wasm
  550. });
  551. } else if (syncWebAssembly) {
  552. const wasm = {
  553. type: "webassembly/sync",
  554. rules: [
  555. {
  556. descriptionData: {
  557. type: "module"
  558. },
  559. resolve: {
  560. fullySpecified: true
  561. }
  562. }
  563. ]
  564. };
  565. rules.push({
  566. test: /\.wasm$/i,
  567. ...wasm
  568. });
  569. rules.push({
  570. mimetype: "application/wasm",
  571. ...wasm
  572. });
  573. }
  574. if (css) {
  575. const cssRule = {
  576. type: "css",
  577. resolve: {
  578. fullySpecified: true,
  579. preferRelative: true
  580. }
  581. };
  582. const cssModulesRule = {
  583. type: "css/module",
  584. resolve: {
  585. fullySpecified: true
  586. }
  587. };
  588. rules.push({
  589. test: /\.css$/i,
  590. oneOf: [
  591. {
  592. test: /\.module\.css$/i,
  593. ...cssModulesRule
  594. },
  595. {
  596. ...cssRule
  597. }
  598. ]
  599. });
  600. rules.push({
  601. mimetype: "text/css+module",
  602. ...cssModulesRule
  603. });
  604. rules.push({
  605. mimetype: "text/css",
  606. ...cssRule
  607. });
  608. }
  609. rules.push(
  610. {
  611. dependency: "url",
  612. oneOf: [
  613. {
  614. scheme: /^data$/,
  615. type: "asset/inline"
  616. },
  617. {
  618. type: "asset/resource"
  619. }
  620. ]
  621. },
  622. {
  623. assert: { type: "json" },
  624. type: "json"
  625. }
  626. );
  627. return rules;
  628. });
  629. };
  630. /**
  631. * @param {Output} output options
  632. * @param {Object} options options
  633. * @param {string} options.context context
  634. * @param {TargetProperties | false} options.targetProperties target properties
  635. * @param {boolean} options.isAffectedByBrowserslist is affected by browserslist
  636. * @param {boolean} options.outputModule is outputModule experiment enabled
  637. * @param {boolean} options.development is development mode
  638. * @param {Entry} options.entry entry option
  639. * @param {ModuleOptions} options.module module option
  640. * @param {boolean} options.futureDefaults is future defaults enabled
  641. * @returns {void}
  642. */
  643. const applyOutputDefaults = (
  644. output,
  645. {
  646. context,
  647. targetProperties: tp,
  648. isAffectedByBrowserslist,
  649. outputModule,
  650. development,
  651. entry,
  652. module,
  653. futureDefaults
  654. }
  655. ) => {
  656. /**
  657. * @param {Library=} library the library option
  658. * @returns {string} a readable library name
  659. */
  660. const getLibraryName = library => {
  661. const libraryName =
  662. typeof library === "object" &&
  663. library &&
  664. !Array.isArray(library) &&
  665. "type" in library
  666. ? library.name
  667. : /** @type {LibraryName=} */ (library);
  668. if (Array.isArray(libraryName)) {
  669. return libraryName.join(".");
  670. } else if (typeof libraryName === "object") {
  671. return getLibraryName(libraryName.root);
  672. } else if (typeof libraryName === "string") {
  673. return libraryName;
  674. }
  675. return "";
  676. };
  677. F(output, "uniqueName", () => {
  678. const libraryName = getLibraryName(output.library).replace(
  679. /^\[(\\*[\w:]+\\*)\](\.)|(\.)\[(\\*[\w:]+\\*)\](?=\.|$)|\[(\\*[\w:]+\\*)\]/g,
  680. (m, a, d1, d2, b, c) => {
  681. const content = a || b || c;
  682. return content.startsWith("\\") && content.endsWith("\\")
  683. ? `${d2 || ""}[${content.slice(1, -1)}]${d1 || ""}`
  684. : "";
  685. }
  686. );
  687. if (libraryName) return libraryName;
  688. const pkgPath = path.resolve(context, "package.json");
  689. try {
  690. const packageInfo = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
  691. return packageInfo.name || "";
  692. } catch (e) {
  693. if (e.code !== "ENOENT") {
  694. e.message += `\nwhile determining default 'output.uniqueName' from 'name' in ${pkgPath}`;
  695. throw e;
  696. }
  697. return "";
  698. }
  699. });
  700. F(output, "module", () => !!outputModule);
  701. D(output, "filename", output.module ? "[name].mjs" : "[name].js");
  702. F(output, "iife", () => !output.module);
  703. D(output, "importFunctionName", "import");
  704. D(output, "importMetaName", "import.meta");
  705. F(output, "chunkFilename", () => {
  706. const filename = output.filename;
  707. if (typeof filename !== "function") {
  708. const hasName = filename.includes("[name]");
  709. const hasId = filename.includes("[id]");
  710. const hasChunkHash = filename.includes("[chunkhash]");
  711. const hasContentHash = filename.includes("[contenthash]");
  712. // Anything changing depending on chunk is fine
  713. if (hasChunkHash || hasContentHash || hasName || hasId) return filename;
  714. // Otherwise prefix "[id]." in front of the basename to make it changing
  715. return filename.replace(/(^|\/)([^/]*(?:\?|$))/, "$1[id].$2");
  716. }
  717. return output.module ? "[id].mjs" : "[id].js";
  718. });
  719. F(output, "cssFilename", () => {
  720. const filename = output.filename;
  721. if (typeof filename !== "function") {
  722. return filename.replace(/\.[mc]?js(\?|$)/, ".css$1");
  723. }
  724. return "[id].css";
  725. });
  726. F(output, "cssChunkFilename", () => {
  727. const chunkFilename = output.chunkFilename;
  728. if (typeof chunkFilename !== "function") {
  729. return chunkFilename.replace(/\.[mc]?js(\?|$)/, ".css$1");
  730. }
  731. return "[id].css";
  732. });
  733. D(output, "assetModuleFilename", "[hash][ext][query]");
  734. D(output, "webassemblyModuleFilename", "[hash].module.wasm");
  735. D(output, "compareBeforeEmit", true);
  736. D(output, "charset", true);
  737. F(output, "hotUpdateGlobal", () =>
  738. Template.toIdentifier(
  739. "webpackHotUpdate" + Template.toIdentifier(output.uniqueName)
  740. )
  741. );
  742. F(output, "chunkLoadingGlobal", () =>
  743. Template.toIdentifier(
  744. "webpackChunk" + Template.toIdentifier(output.uniqueName)
  745. )
  746. );
  747. F(output, "globalObject", () => {
  748. if (tp) {
  749. if (tp.global) return "global";
  750. if (tp.globalThis) return "globalThis";
  751. }
  752. return "self";
  753. });
  754. F(output, "chunkFormat", () => {
  755. if (tp) {
  756. const helpMessage = isAffectedByBrowserslist
  757. ? "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."
  758. : "Select an appropriate 'target' to allow selecting one by default, or specify the 'output.chunkFormat' directly.";
  759. if (output.module) {
  760. if (tp.dynamicImport) return "module";
  761. if (tp.document) return "array-push";
  762. throw new Error(
  763. "For the selected environment is no default ESM chunk format available:\n" +
  764. "ESM exports can be chosen when 'import()' is available.\n" +
  765. "JSONP Array push can be chosen when 'document' is available.\n" +
  766. helpMessage
  767. );
  768. } else {
  769. if (tp.document) return "array-push";
  770. if (tp.require) return "commonjs";
  771. if (tp.nodeBuiltins) return "commonjs";
  772. if (tp.importScripts) return "array-push";
  773. throw new Error(
  774. "For the selected environment is no default script chunk format available:\n" +
  775. "JSONP Array push can be chosen when 'document' or 'importScripts' is available.\n" +
  776. "CommonJs exports can be chosen when 'require' or node builtins are available.\n" +
  777. helpMessage
  778. );
  779. }
  780. }
  781. throw new Error(
  782. "Chunk format can't be selected by default when no target is specified"
  783. );
  784. });
  785. D(output, "asyncChunks", true);
  786. F(output, "chunkLoading", () => {
  787. if (tp) {
  788. switch (output.chunkFormat) {
  789. case "array-push":
  790. if (tp.document) return "jsonp";
  791. if (tp.importScripts) return "import-scripts";
  792. break;
  793. case "commonjs":
  794. if (tp.require) return "require";
  795. if (tp.nodeBuiltins) return "async-node";
  796. break;
  797. case "module":
  798. if (tp.dynamicImport) return "import";
  799. break;
  800. }
  801. if (
  802. tp.require === null ||
  803. tp.nodeBuiltins === null ||
  804. tp.document === null ||
  805. tp.importScripts === null
  806. ) {
  807. return "universal";
  808. }
  809. }
  810. return false;
  811. });
  812. F(output, "workerChunkLoading", () => {
  813. if (tp) {
  814. switch (output.chunkFormat) {
  815. case "array-push":
  816. if (tp.importScriptsInWorker) return "import-scripts";
  817. break;
  818. case "commonjs":
  819. if (tp.require) return "require";
  820. if (tp.nodeBuiltins) return "async-node";
  821. break;
  822. case "module":
  823. if (tp.dynamicImportInWorker) return "import";
  824. break;
  825. }
  826. if (
  827. tp.require === null ||
  828. tp.nodeBuiltins === null ||
  829. tp.importScriptsInWorker === null
  830. ) {
  831. return "universal";
  832. }
  833. }
  834. return false;
  835. });
  836. F(output, "wasmLoading", () => {
  837. if (tp) {
  838. if (tp.fetchWasm) return "fetch";
  839. if (tp.nodeBuiltins)
  840. return output.module ? "async-node-module" : "async-node";
  841. if (tp.nodeBuiltins === null || tp.fetchWasm === null) {
  842. return "universal";
  843. }
  844. }
  845. return false;
  846. });
  847. F(output, "workerWasmLoading", () => output.wasmLoading);
  848. F(output, "devtoolNamespace", () => output.uniqueName);
  849. if (output.library) {
  850. F(output.library, "type", () => (output.module ? "module" : "var"));
  851. }
  852. F(output, "path", () => path.join(process.cwd(), "dist"));
  853. F(output, "pathinfo", () => development);
  854. D(output, "sourceMapFilename", "[file].map[query]");
  855. D(
  856. output,
  857. "hotUpdateChunkFilename",
  858. `[id].[fullhash].hot-update.${output.module ? "mjs" : "js"}`
  859. );
  860. D(output, "hotUpdateMainFilename", "[runtime].[fullhash].hot-update.json");
  861. D(output, "crossOriginLoading", false);
  862. F(output, "scriptType", () => (output.module ? "module" : false));
  863. D(
  864. output,
  865. "publicPath",
  866. (tp && (tp.document || tp.importScripts)) || output.scriptType === "module"
  867. ? "auto"
  868. : ""
  869. );
  870. D(output, "chunkLoadTimeout", 120000);
  871. D(output, "hashFunction", futureDefaults ? "xxhash64" : "md4");
  872. D(output, "hashDigest", "hex");
  873. D(output, "hashDigestLength", futureDefaults ? 16 : 20);
  874. D(output, "strictModuleExceptionHandling", false);
  875. const optimistic = v => v || v === undefined;
  876. const conditionallyOptimistic = (v, c) => (v === undefined && c) || v;
  877. F(
  878. output.environment,
  879. "arrowFunction",
  880. () => tp && optimistic(tp.arrowFunction)
  881. );
  882. F(output.environment, "const", () => tp && optimistic(tp.const));
  883. F(
  884. output.environment,
  885. "destructuring",
  886. () => tp && optimistic(tp.destructuring)
  887. );
  888. F(output.environment, "forOf", () => tp && optimistic(tp.forOf));
  889. F(output.environment, "bigIntLiteral", () => tp && tp.bigIntLiteral);
  890. F(output.environment, "dynamicImport", () =>
  891. conditionallyOptimistic(tp && tp.dynamicImport, output.module)
  892. );
  893. F(output.environment, "module", () =>
  894. conditionallyOptimistic(tp && tp.module, output.module)
  895. );
  896. const { trustedTypes } = output;
  897. if (trustedTypes) {
  898. F(
  899. trustedTypes,
  900. "policyName",
  901. () =>
  902. output.uniqueName.replace(/[^a-zA-Z0-9\-#=_/@.%]+/g, "_") || "webpack"
  903. );
  904. }
  905. /**
  906. * @param {function(EntryDescription): void} fn iterator
  907. * @returns {void}
  908. */
  909. const forEachEntry = fn => {
  910. for (const name of Object.keys(entry)) {
  911. fn(entry[name]);
  912. }
  913. };
  914. A(output, "enabledLibraryTypes", () => {
  915. const enabledLibraryTypes = [];
  916. if (output.library) {
  917. enabledLibraryTypes.push(output.library.type);
  918. }
  919. forEachEntry(desc => {
  920. if (desc.library) {
  921. enabledLibraryTypes.push(desc.library.type);
  922. }
  923. });
  924. return enabledLibraryTypes;
  925. });
  926. A(output, "enabledChunkLoadingTypes", () => {
  927. const enabledChunkLoadingTypes = new Set();
  928. if (output.chunkLoading) {
  929. enabledChunkLoadingTypes.add(output.chunkLoading);
  930. }
  931. if (output.workerChunkLoading) {
  932. enabledChunkLoadingTypes.add(output.workerChunkLoading);
  933. }
  934. forEachEntry(desc => {
  935. if (desc.chunkLoading) {
  936. enabledChunkLoadingTypes.add(desc.chunkLoading);
  937. }
  938. });
  939. return Array.from(enabledChunkLoadingTypes);
  940. });
  941. A(output, "enabledWasmLoadingTypes", () => {
  942. const enabledWasmLoadingTypes = new Set();
  943. if (output.wasmLoading) {
  944. enabledWasmLoadingTypes.add(output.wasmLoading);
  945. }
  946. if (output.workerWasmLoading) {
  947. enabledWasmLoadingTypes.add(output.workerWasmLoading);
  948. }
  949. forEachEntry(desc => {
  950. if (desc.wasmLoading) {
  951. enabledWasmLoadingTypes.add(desc.wasmLoading);
  952. }
  953. });
  954. return Array.from(enabledWasmLoadingTypes);
  955. });
  956. };
  957. /**
  958. * @param {ExternalsPresets} externalsPresets options
  959. * @param {Object} options options
  960. * @param {TargetProperties | false} options.targetProperties target properties
  961. * @param {boolean} options.buildHttp buildHttp experiment enabled
  962. * @returns {void}
  963. */
  964. const applyExternalsPresetsDefaults = (
  965. externalsPresets,
  966. { targetProperties, buildHttp }
  967. ) => {
  968. D(
  969. externalsPresets,
  970. "web",
  971. !buildHttp && targetProperties && targetProperties.web
  972. );
  973. D(externalsPresets, "node", targetProperties && targetProperties.node);
  974. D(externalsPresets, "nwjs", targetProperties && targetProperties.nwjs);
  975. D(
  976. externalsPresets,
  977. "electron",
  978. targetProperties && targetProperties.electron
  979. );
  980. D(
  981. externalsPresets,
  982. "electronMain",
  983. targetProperties &&
  984. targetProperties.electron &&
  985. targetProperties.electronMain
  986. );
  987. D(
  988. externalsPresets,
  989. "electronPreload",
  990. targetProperties &&
  991. targetProperties.electron &&
  992. targetProperties.electronPreload
  993. );
  994. D(
  995. externalsPresets,
  996. "electronRenderer",
  997. targetProperties &&
  998. targetProperties.electron &&
  999. targetProperties.electronRenderer
  1000. );
  1001. };
  1002. /**
  1003. * @param {Loader} loader options
  1004. * @param {Object} options options
  1005. * @param {TargetProperties | false} options.targetProperties target properties
  1006. * @returns {void}
  1007. */
  1008. const applyLoaderDefaults = (loader, { targetProperties }) => {
  1009. F(loader, "target", () => {
  1010. if (targetProperties) {
  1011. if (targetProperties.electron) {
  1012. if (targetProperties.electronMain) return "electron-main";
  1013. if (targetProperties.electronPreload) return "electron-preload";
  1014. if (targetProperties.electronRenderer) return "electron-renderer";
  1015. return "electron";
  1016. }
  1017. if (targetProperties.nwjs) return "nwjs";
  1018. if (targetProperties.node) return "node";
  1019. if (targetProperties.web) return "web";
  1020. }
  1021. });
  1022. };
  1023. /**
  1024. * @param {WebpackNode} node options
  1025. * @param {Object} options options
  1026. * @param {TargetProperties | false} options.targetProperties target properties
  1027. * @param {boolean} options.futureDefaults is future defaults enabled
  1028. * @returns {void}
  1029. */
  1030. const applyNodeDefaults = (node, { futureDefaults, targetProperties }) => {
  1031. if (node === false) return;
  1032. F(node, "global", () => {
  1033. if (targetProperties && targetProperties.global) return false;
  1034. // TODO webpack 6 should always default to false
  1035. return futureDefaults ? "warn" : true;
  1036. });
  1037. F(node, "__filename", () => {
  1038. if (targetProperties && targetProperties.node) return "eval-only";
  1039. // TODO webpack 6 should always default to false
  1040. return futureDefaults ? "warn-mock" : "mock";
  1041. });
  1042. F(node, "__dirname", () => {
  1043. if (targetProperties && targetProperties.node) return "eval-only";
  1044. // TODO webpack 6 should always default to false
  1045. return futureDefaults ? "warn-mock" : "mock";
  1046. });
  1047. };
  1048. /**
  1049. * @param {Performance} performance options
  1050. * @param {Object} options options
  1051. * @param {boolean} options.production is production
  1052. * @returns {void}
  1053. */
  1054. const applyPerformanceDefaults = (performance, { production }) => {
  1055. if (performance === false) return;
  1056. D(performance, "maxAssetSize", 250000);
  1057. D(performance, "maxEntrypointSize", 250000);
  1058. F(performance, "hints", () => (production ? "warning" : false));
  1059. };
  1060. /**
  1061. * @param {Optimization} optimization options
  1062. * @param {Object} options options
  1063. * @param {boolean} options.production is production
  1064. * @param {boolean} options.development is development
  1065. * @param {CssExperimentOptions} options.css is css enabled
  1066. * @param {boolean} options.records using records
  1067. * @returns {void}
  1068. */
  1069. const applyOptimizationDefaults = (
  1070. optimization,
  1071. { production, development, css, records }
  1072. ) => {
  1073. D(optimization, "removeAvailableModules", false);
  1074. D(optimization, "removeEmptyChunks", true);
  1075. D(optimization, "mergeDuplicateChunks", true);
  1076. D(optimization, "flagIncludedChunks", production);
  1077. F(optimization, "moduleIds", () => {
  1078. if (production) return "deterministic";
  1079. if (development) return "named";
  1080. return "natural";
  1081. });
  1082. F(optimization, "chunkIds", () => {
  1083. if (production) return "deterministic";
  1084. if (development) return "named";
  1085. return "natural";
  1086. });
  1087. F(optimization, "sideEffects", () => (production ? true : "flag"));
  1088. D(optimization, "providedExports", true);
  1089. D(optimization, "usedExports", production);
  1090. D(optimization, "innerGraph", production);
  1091. D(optimization, "mangleExports", production);
  1092. D(optimization, "concatenateModules", production);
  1093. D(optimization, "runtimeChunk", false);
  1094. D(optimization, "emitOnErrors", !production);
  1095. D(optimization, "checkWasmTypes", production);
  1096. D(optimization, "mangleWasmImports", false);
  1097. D(optimization, "portableRecords", records);
  1098. D(optimization, "realContentHash", production);
  1099. D(optimization, "minimize", production);
  1100. A(optimization, "minimizer", () => [
  1101. {
  1102. apply: compiler => {
  1103. // Lazy load the Terser plugin
  1104. const TerserPlugin = require("terser-webpack-plugin");
  1105. new TerserPlugin({
  1106. terserOptions: {
  1107. compress: {
  1108. passes: 2
  1109. }
  1110. }
  1111. }).apply(compiler);
  1112. }
  1113. }
  1114. ]);
  1115. F(optimization, "nodeEnv", () => {
  1116. if (production) return "production";
  1117. if (development) return "development";
  1118. return false;
  1119. });
  1120. const { splitChunks } = optimization;
  1121. if (splitChunks) {
  1122. A(splitChunks, "defaultSizeTypes", () =>
  1123. css ? ["javascript", "css", "unknown"] : ["javascript", "unknown"]
  1124. );
  1125. D(splitChunks, "hidePathInfo", production);
  1126. D(splitChunks, "chunks", "async");
  1127. D(splitChunks, "usedExports", optimization.usedExports === true);
  1128. D(splitChunks, "minChunks", 1);
  1129. F(splitChunks, "minSize", () => (production ? 20000 : 10000));
  1130. F(splitChunks, "minRemainingSize", () => (development ? 0 : undefined));
  1131. F(splitChunks, "enforceSizeThreshold", () => (production ? 50000 : 30000));
  1132. F(splitChunks, "maxAsyncRequests", () => (production ? 30 : Infinity));
  1133. F(splitChunks, "maxInitialRequests", () => (production ? 30 : Infinity));
  1134. D(splitChunks, "automaticNameDelimiter", "-");
  1135. const { cacheGroups } = splitChunks;
  1136. F(cacheGroups, "default", () => ({
  1137. idHint: "",
  1138. reuseExistingChunk: true,
  1139. minChunks: 2,
  1140. priority: -20
  1141. }));
  1142. F(cacheGroups, "defaultVendors", () => ({
  1143. idHint: "vendors",
  1144. reuseExistingChunk: true,
  1145. test: NODE_MODULES_REGEXP,
  1146. priority: -10
  1147. }));
  1148. }
  1149. };
  1150. /**
  1151. * @param {Object} options options
  1152. * @param {boolean} options.cache is cache enable
  1153. * @param {string} options.context build context
  1154. * @param {TargetProperties | false} options.targetProperties target properties
  1155. * @param {Mode} options.mode mode
  1156. * @returns {ResolveOptions} resolve options
  1157. */
  1158. const getResolveDefaults = ({ cache, context, targetProperties, mode }) => {
  1159. /** @type {string[]} */
  1160. const conditions = ["webpack"];
  1161. conditions.push(mode === "development" ? "development" : "production");
  1162. if (targetProperties) {
  1163. if (targetProperties.webworker) conditions.push("worker");
  1164. if (targetProperties.node) conditions.push("node");
  1165. if (targetProperties.web) conditions.push("browser");
  1166. if (targetProperties.electron) conditions.push("electron");
  1167. if (targetProperties.nwjs) conditions.push("nwjs");
  1168. }
  1169. const jsExtensions = [".js", ".json", ".wasm"];
  1170. const tp = targetProperties;
  1171. const browserField =
  1172. tp && tp.web && (!tp.node || (tp.electron && tp.electronRenderer));
  1173. /** @type {function(): ResolveOptions} */
  1174. const cjsDeps = () => ({
  1175. aliasFields: browserField ? ["browser"] : [],
  1176. mainFields: browserField ? ["browser", "module", "..."] : ["module", "..."],
  1177. conditionNames: ["require", "module", "..."],
  1178. extensions: [...jsExtensions]
  1179. });
  1180. /** @type {function(): ResolveOptions} */
  1181. const esmDeps = () => ({
  1182. aliasFields: browserField ? ["browser"] : [],
  1183. mainFields: browserField ? ["browser", "module", "..."] : ["module", "..."],
  1184. conditionNames: ["import", "module", "..."],
  1185. extensions: [...jsExtensions]
  1186. });
  1187. /** @type {ResolveOptions} */
  1188. const resolveOptions = {
  1189. cache,
  1190. modules: ["node_modules"],
  1191. conditionNames: conditions,
  1192. mainFiles: ["index"],
  1193. extensions: [],
  1194. aliasFields: [],
  1195. exportsFields: ["exports"],
  1196. roots: [context],
  1197. mainFields: ["main"],
  1198. byDependency: {
  1199. wasm: esmDeps(),
  1200. esm: esmDeps(),
  1201. loaderImport: esmDeps(),
  1202. url: {
  1203. preferRelative: true
  1204. },
  1205. worker: {
  1206. ...esmDeps(),
  1207. preferRelative: true
  1208. },
  1209. commonjs: cjsDeps(),
  1210. amd: cjsDeps(),
  1211. // for backward-compat: loadModule
  1212. loader: cjsDeps(),
  1213. // for backward-compat: Custom Dependency
  1214. unknown: cjsDeps(),
  1215. // for backward-compat: getResolve without dependencyType
  1216. undefined: cjsDeps()
  1217. }
  1218. };
  1219. return resolveOptions;
  1220. };
  1221. /**
  1222. * @param {Object} options options
  1223. * @param {boolean} options.cache is cache enable
  1224. * @returns {ResolveOptions} resolve options
  1225. */
  1226. const getResolveLoaderDefaults = ({ cache }) => {
  1227. /** @type {ResolveOptions} */
  1228. const resolveOptions = {
  1229. cache,
  1230. conditionNames: ["loader", "require", "node"],
  1231. exportsFields: ["exports"],
  1232. mainFields: ["loader", "main"],
  1233. extensions: [".js"],
  1234. mainFiles: ["index"]
  1235. };
  1236. return resolveOptions;
  1237. };
  1238. /**
  1239. * @param {InfrastructureLogging} infrastructureLogging options
  1240. * @returns {void}
  1241. */
  1242. const applyInfrastructureLoggingDefaults = infrastructureLogging => {
  1243. F(infrastructureLogging, "stream", () => process.stderr);
  1244. const tty =
  1245. /** @type {any} */ (infrastructureLogging.stream).isTTY &&
  1246. process.env.TERM !== "dumb";
  1247. D(infrastructureLogging, "level", "info");
  1248. D(infrastructureLogging, "debug", false);
  1249. D(infrastructureLogging, "colors", tty);
  1250. D(infrastructureLogging, "appendOnly", !tty);
  1251. };
  1252. exports.applyWebpackOptionsBaseDefaults = applyWebpackOptionsBaseDefaults;
  1253. exports.applyWebpackOptionsDefaults = applyWebpackOptionsDefaults;