ResolverFactory.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const versions = require("process").versions;
  7. const Resolver = require("./Resolver");
  8. const { getType, PathType } = require("./util/path");
  9. const SyncAsyncFileSystemDecorator = require("./SyncAsyncFileSystemDecorator");
  10. const AliasFieldPlugin = require("./AliasFieldPlugin");
  11. const AliasPlugin = require("./AliasPlugin");
  12. const AppendPlugin = require("./AppendPlugin");
  13. const ConditionalPlugin = require("./ConditionalPlugin");
  14. const DescriptionFilePlugin = require("./DescriptionFilePlugin");
  15. const DirectoryExistsPlugin = require("./DirectoryExistsPlugin");
  16. const ExportsFieldPlugin = require("./ExportsFieldPlugin");
  17. const ExtensionAliasPlugin = require("./ExtensionAliasPlugin");
  18. const FileExistsPlugin = require("./FileExistsPlugin");
  19. const ImportsFieldPlugin = require("./ImportsFieldPlugin");
  20. const JoinRequestPartPlugin = require("./JoinRequestPartPlugin");
  21. const JoinRequestPlugin = require("./JoinRequestPlugin");
  22. const MainFieldPlugin = require("./MainFieldPlugin");
  23. const ModulesInHierarchicalDirectoriesPlugin = require("./ModulesInHierarchicalDirectoriesPlugin");
  24. const ModulesInRootPlugin = require("./ModulesInRootPlugin");
  25. const NextPlugin = require("./NextPlugin");
  26. const ParsePlugin = require("./ParsePlugin");
  27. const PnpPlugin = require("./PnpPlugin");
  28. const RestrictionsPlugin = require("./RestrictionsPlugin");
  29. const ResultPlugin = require("./ResultPlugin");
  30. const RootsPlugin = require("./RootsPlugin");
  31. const SelfReferencePlugin = require("./SelfReferencePlugin");
  32. const SymlinkPlugin = require("./SymlinkPlugin");
  33. const TryNextPlugin = require("./TryNextPlugin");
  34. const UnsafeCachePlugin = require("./UnsafeCachePlugin");
  35. const UseFilePlugin = require("./UseFilePlugin");
  36. /** @typedef {import("./AliasPlugin").AliasOption} AliasOptionEntry */
  37. /** @typedef {import("./ExtensionAliasPlugin").ExtensionAliasOption} ExtensionAliasOption */
  38. /** @typedef {import("./PnpPlugin").PnpApiImpl} PnpApi */
  39. /** @typedef {import("./Resolver").EnsuredHooks} EnsuredHooks */
  40. /** @typedef {import("./Resolver").FileSystem} FileSystem */
  41. /** @typedef {import("./Resolver").KnownHooks} KnownHooks */
  42. /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
  43. /** @typedef {import("./Resolver").SyncFileSystem} SyncFileSystem */
  44. /** @typedef {string|string[]|false} AliasOptionNewRequest */
  45. /** @typedef {{[k: string]: AliasOptionNewRequest}} AliasOptions */
  46. /** @typedef {{[k: string]: string|string[] }} ExtensionAliasOptions */
  47. /** @typedef {false | 0 | "" | null | undefined} Falsy */
  48. /** @typedef {{apply: function(Resolver): void} | (function(this: Resolver, Resolver): void) | Falsy} Plugin */
  49. /**
  50. * @typedef {Object} UserResolveOptions
  51. * @property {(AliasOptions | AliasOptionEntry[])=} alias A list of module alias configurations or an object which maps key to value
  52. * @property {(AliasOptions | AliasOptionEntry[])=} fallback A list of module alias configurations or an object which maps key to value, applied only after modules option
  53. * @property {ExtensionAliasOptions=} extensionAlias An object which maps extension to extension aliases
  54. * @property {(string | string[])[]=} aliasFields A list of alias fields in description files
  55. * @property {(function(ResolveRequest): boolean)=} cachePredicate A function which decides whether a request should be cached or not. An object is passed with at least `path` and `request` properties.
  56. * @property {boolean=} cacheWithContext Whether or not the unsafeCache should include request context as part of the cache key.
  57. * @property {string[]=} descriptionFiles A list of description files to read from
  58. * @property {string[]=} conditionNames A list of exports field condition names.
  59. * @property {boolean=} enforceExtension Enforce that a extension from extensions must be used
  60. * @property {(string | string[])[]=} exportsFields A list of exports fields in description files
  61. * @property {(string | string[])[]=} importsFields A list of imports fields in description files
  62. * @property {string[]=} extensions A list of extensions which should be tried for files
  63. * @property {FileSystem} fileSystem The file system which should be used
  64. * @property {(object | boolean)=} unsafeCache Use this cache object to unsafely cache the successful requests
  65. * @property {boolean=} symlinks Resolve symlinks to their symlinked location
  66. * @property {Resolver=} resolver A prepared Resolver to which the plugins are attached
  67. * @property {string[] | string=} modules A list of directories to resolve modules from, can be absolute path or folder name
  68. * @property {(string | string[] | {name: string | string[], forceRelative: boolean})[]=} mainFields A list of main fields in description files
  69. * @property {string[]=} mainFiles A list of main files in directories
  70. * @property {Plugin[]=} plugins A list of additional resolve plugins which should be applied
  71. * @property {PnpApi | null=} pnpApi A PnP API that should be used - null is "never", undefined is "auto"
  72. * @property {string[]=} roots A list of root paths
  73. * @property {boolean=} fullySpecified The request is already fully specified and no extensions or directories are resolved for it
  74. * @property {boolean=} resolveToContext Resolve to a context instead of a file
  75. * @property {(string|RegExp)[]=} restrictions A list of resolve restrictions
  76. * @property {boolean=} useSyncFileSystemCalls Use only the sync constraints of the file system calls
  77. * @property {boolean=} preferRelative Prefer to resolve module requests as relative requests before falling back to modules
  78. * @property {boolean=} preferAbsolute Prefer to resolve server-relative urls as absolute paths before falling back to resolve in roots
  79. */
  80. /**
  81. * @typedef {Object} ResolveOptions
  82. * @property {AliasOptionEntry[]} alias
  83. * @property {AliasOptionEntry[]} fallback
  84. * @property {Set<string | string[]>} aliasFields
  85. * @property {ExtensionAliasOption[]} extensionAlias
  86. * @property {(function(ResolveRequest): boolean)} cachePredicate
  87. * @property {boolean} cacheWithContext
  88. * @property {Set<string>} conditionNames A list of exports field condition names.
  89. * @property {string[]} descriptionFiles
  90. * @property {boolean} enforceExtension
  91. * @property {Set<string | string[]>} exportsFields
  92. * @property {Set<string | string[]>} importsFields
  93. * @property {Set<string>} extensions
  94. * @property {FileSystem} fileSystem
  95. * @property {object | false} unsafeCache
  96. * @property {boolean} symlinks
  97. * @property {Resolver=} resolver
  98. * @property {Array<string | string[]>} modules
  99. * @property {{name: string[], forceRelative: boolean}[]} mainFields
  100. * @property {Set<string>} mainFiles
  101. * @property {Plugin[]} plugins
  102. * @property {PnpApi | null} pnpApi
  103. * @property {Set<string>} roots
  104. * @property {boolean} fullySpecified
  105. * @property {boolean} resolveToContext
  106. * @property {Set<string|RegExp>} restrictions
  107. * @property {boolean} preferRelative
  108. * @property {boolean} preferAbsolute
  109. */
  110. /**
  111. * @param {PnpApi | null=} option option
  112. * @returns {PnpApi | null} processed option
  113. */
  114. function processPnpApiOption(option) {
  115. if (
  116. option === undefined &&
  117. /** @type {NodeJS.ProcessVersions & {pnp: string}} */ versions.pnp
  118. ) {
  119. // @ts-ignore
  120. return require("pnpapi"); // eslint-disable-line node/no-missing-require
  121. }
  122. return option || null;
  123. }
  124. /**
  125. * @param {AliasOptions | AliasOptionEntry[] | undefined} alias alias
  126. * @returns {AliasOptionEntry[]} normalized aliases
  127. */
  128. function normalizeAlias(alias) {
  129. return typeof alias === "object" && !Array.isArray(alias) && alias !== null
  130. ? Object.keys(alias).map(key => {
  131. /** @type {AliasOptionEntry} */
  132. const obj = { name: key, onlyModule: false, alias: alias[key] };
  133. if (/\$$/.test(key)) {
  134. obj.onlyModule = true;
  135. obj.name = key.slice(0, -1);
  136. }
  137. return obj;
  138. })
  139. : /** @type {Array<AliasOptionEntry>} */ (alias) || [];
  140. }
  141. /**
  142. * @param {UserResolveOptions} options input options
  143. * @returns {ResolveOptions} output options
  144. */
  145. function createOptions(options) {
  146. const mainFieldsSet = new Set(options.mainFields || ["main"]);
  147. /** @type {ResolveOptions["mainFields"]} */
  148. const mainFields = [];
  149. for (const item of mainFieldsSet) {
  150. if (typeof item === "string") {
  151. mainFields.push({
  152. name: [item],
  153. forceRelative: true
  154. });
  155. } else if (Array.isArray(item)) {
  156. mainFields.push({
  157. name: item,
  158. forceRelative: true
  159. });
  160. } else {
  161. mainFields.push({
  162. name: Array.isArray(item.name) ? item.name : [item.name],
  163. forceRelative: item.forceRelative
  164. });
  165. }
  166. }
  167. return {
  168. alias: normalizeAlias(options.alias),
  169. fallback: normalizeAlias(options.fallback),
  170. aliasFields: new Set(options.aliasFields),
  171. cachePredicate:
  172. options.cachePredicate ||
  173. function () {
  174. return true;
  175. },
  176. cacheWithContext:
  177. typeof options.cacheWithContext !== "undefined"
  178. ? options.cacheWithContext
  179. : true,
  180. exportsFields: new Set(options.exportsFields || ["exports"]),
  181. importsFields: new Set(options.importsFields || ["imports"]),
  182. conditionNames: new Set(options.conditionNames),
  183. descriptionFiles: Array.from(
  184. new Set(options.descriptionFiles || ["package.json"])
  185. ),
  186. enforceExtension:
  187. options.enforceExtension === undefined
  188. ? options.extensions && options.extensions.includes("")
  189. ? true
  190. : false
  191. : options.enforceExtension,
  192. extensions: new Set(options.extensions || [".js", ".json", ".node"]),
  193. extensionAlias: options.extensionAlias
  194. ? Object.keys(options.extensionAlias).map(k => ({
  195. extension: k,
  196. alias: /** @type {ExtensionAliasOptions} */ (options.extensionAlias)[
  197. k
  198. ]
  199. }))
  200. : [],
  201. fileSystem: options.useSyncFileSystemCalls
  202. ? new SyncAsyncFileSystemDecorator(
  203. /** @type {SyncFileSystem} */ (
  204. /** @type {unknown} */ (options.fileSystem)
  205. )
  206. )
  207. : options.fileSystem,
  208. unsafeCache:
  209. options.unsafeCache && typeof options.unsafeCache !== "object"
  210. ? {}
  211. : options.unsafeCache || false,
  212. symlinks: typeof options.symlinks !== "undefined" ? options.symlinks : true,
  213. resolver: options.resolver,
  214. modules: mergeFilteredToArray(
  215. Array.isArray(options.modules)
  216. ? options.modules
  217. : options.modules
  218. ? [options.modules]
  219. : ["node_modules"],
  220. item => {
  221. const type = getType(item);
  222. return type === PathType.Normal || type === PathType.Relative;
  223. }
  224. ),
  225. mainFields,
  226. mainFiles: new Set(options.mainFiles || ["index"]),
  227. plugins: options.plugins || [],
  228. pnpApi: processPnpApiOption(options.pnpApi),
  229. roots: new Set(options.roots || undefined),
  230. fullySpecified: options.fullySpecified || false,
  231. resolveToContext: options.resolveToContext || false,
  232. preferRelative: options.preferRelative || false,
  233. preferAbsolute: options.preferAbsolute || false,
  234. restrictions: new Set(options.restrictions)
  235. };
  236. }
  237. /**
  238. * @param {UserResolveOptions} options resolve options
  239. * @returns {Resolver} created resolver
  240. */
  241. exports.createResolver = function (options) {
  242. const normalizedOptions = createOptions(options);
  243. const {
  244. alias,
  245. fallback,
  246. aliasFields,
  247. cachePredicate,
  248. cacheWithContext,
  249. conditionNames,
  250. descriptionFiles,
  251. enforceExtension,
  252. exportsFields,
  253. extensionAlias,
  254. importsFields,
  255. extensions,
  256. fileSystem,
  257. fullySpecified,
  258. mainFields,
  259. mainFiles,
  260. modules,
  261. plugins: userPlugins,
  262. pnpApi,
  263. resolveToContext,
  264. preferRelative,
  265. preferAbsolute,
  266. symlinks,
  267. unsafeCache,
  268. resolver: customResolver,
  269. restrictions,
  270. roots
  271. } = normalizedOptions;
  272. const plugins = userPlugins.slice();
  273. const resolver = customResolver
  274. ? customResolver
  275. : new Resolver(fileSystem, normalizedOptions);
  276. //// pipeline ////
  277. resolver.ensureHook("resolve");
  278. resolver.ensureHook("internalResolve");
  279. resolver.ensureHook("newInternalResolve");
  280. resolver.ensureHook("parsedResolve");
  281. resolver.ensureHook("describedResolve");
  282. resolver.ensureHook("rawResolve");
  283. resolver.ensureHook("normalResolve");
  284. resolver.ensureHook("internal");
  285. resolver.ensureHook("rawModule");
  286. resolver.ensureHook("module");
  287. resolver.ensureHook("resolveAsModule");
  288. resolver.ensureHook("undescribedResolveInPackage");
  289. resolver.ensureHook("resolveInPackage");
  290. resolver.ensureHook("resolveInExistingDirectory");
  291. resolver.ensureHook("relative");
  292. resolver.ensureHook("describedRelative");
  293. resolver.ensureHook("directory");
  294. resolver.ensureHook("undescribedExistingDirectory");
  295. resolver.ensureHook("existingDirectory");
  296. resolver.ensureHook("undescribedRawFile");
  297. resolver.ensureHook("rawFile");
  298. resolver.ensureHook("file");
  299. resolver.ensureHook("finalFile");
  300. resolver.ensureHook("existingFile");
  301. resolver.ensureHook("resolved");
  302. // TODO remove in next major
  303. // cspell:word Interal
  304. // Backward-compat
  305. // @ts-ignore
  306. resolver.hooks.newInteralResolve = resolver.hooks.newInternalResolve;
  307. // resolve
  308. for (const { source, resolveOptions } of [
  309. { source: "resolve", resolveOptions: { fullySpecified } },
  310. { source: "internal-resolve", resolveOptions: { fullySpecified: false } }
  311. ]) {
  312. if (unsafeCache) {
  313. plugins.push(
  314. new UnsafeCachePlugin(
  315. source,
  316. cachePredicate,
  317. /** @type {import("./UnsafeCachePlugin").Cache} */ (unsafeCache),
  318. cacheWithContext,
  319. `new-${source}`
  320. )
  321. );
  322. plugins.push(
  323. new ParsePlugin(`new-${source}`, resolveOptions, "parsed-resolve")
  324. );
  325. } else {
  326. plugins.push(new ParsePlugin(source, resolveOptions, "parsed-resolve"));
  327. }
  328. }
  329. // parsed-resolve
  330. plugins.push(
  331. new DescriptionFilePlugin(
  332. "parsed-resolve",
  333. descriptionFiles,
  334. false,
  335. "described-resolve"
  336. )
  337. );
  338. plugins.push(new NextPlugin("after-parsed-resolve", "described-resolve"));
  339. // described-resolve
  340. plugins.push(new NextPlugin("described-resolve", "raw-resolve"));
  341. if (fallback.length > 0) {
  342. plugins.push(
  343. new AliasPlugin("described-resolve", fallback, "internal-resolve")
  344. );
  345. }
  346. // raw-resolve
  347. if (alias.length > 0) {
  348. plugins.push(new AliasPlugin("raw-resolve", alias, "internal-resolve"));
  349. }
  350. aliasFields.forEach(item => {
  351. plugins.push(new AliasFieldPlugin("raw-resolve", item, "internal-resolve"));
  352. });
  353. extensionAlias.forEach(item =>
  354. plugins.push(
  355. new ExtensionAliasPlugin("raw-resolve", item, "normal-resolve")
  356. )
  357. );
  358. plugins.push(new NextPlugin("raw-resolve", "normal-resolve"));
  359. // normal-resolve
  360. if (preferRelative) {
  361. plugins.push(new JoinRequestPlugin("after-normal-resolve", "relative"));
  362. }
  363. plugins.push(
  364. new ConditionalPlugin(
  365. "after-normal-resolve",
  366. { module: true },
  367. "resolve as module",
  368. false,
  369. "raw-module"
  370. )
  371. );
  372. plugins.push(
  373. new ConditionalPlugin(
  374. "after-normal-resolve",
  375. { internal: true },
  376. "resolve as internal import",
  377. false,
  378. "internal"
  379. )
  380. );
  381. if (preferAbsolute) {
  382. plugins.push(new JoinRequestPlugin("after-normal-resolve", "relative"));
  383. }
  384. if (roots.size > 0) {
  385. plugins.push(new RootsPlugin("after-normal-resolve", roots, "relative"));
  386. }
  387. if (!preferRelative && !preferAbsolute) {
  388. plugins.push(new JoinRequestPlugin("after-normal-resolve", "relative"));
  389. }
  390. // internal
  391. importsFields.forEach(importsField => {
  392. plugins.push(
  393. new ImportsFieldPlugin(
  394. "internal",
  395. conditionNames,
  396. importsField,
  397. "relative",
  398. "internal-resolve"
  399. )
  400. );
  401. });
  402. // raw-module
  403. exportsFields.forEach(exportsField => {
  404. plugins.push(
  405. new SelfReferencePlugin("raw-module", exportsField, "resolve-as-module")
  406. );
  407. });
  408. modules.forEach(item => {
  409. if (Array.isArray(item)) {
  410. if (item.includes("node_modules") && pnpApi) {
  411. plugins.push(
  412. new ModulesInHierarchicalDirectoriesPlugin(
  413. "raw-module",
  414. item.filter(i => i !== "node_modules"),
  415. "module"
  416. )
  417. );
  418. plugins.push(
  419. new PnpPlugin("raw-module", pnpApi, "undescribed-resolve-in-package")
  420. );
  421. } else {
  422. plugins.push(
  423. new ModulesInHierarchicalDirectoriesPlugin(
  424. "raw-module",
  425. item,
  426. "module"
  427. )
  428. );
  429. }
  430. } else {
  431. plugins.push(new ModulesInRootPlugin("raw-module", item, "module"));
  432. }
  433. });
  434. // module
  435. plugins.push(new JoinRequestPartPlugin("module", "resolve-as-module"));
  436. // resolve-as-module
  437. if (!resolveToContext) {
  438. plugins.push(
  439. new ConditionalPlugin(
  440. "resolve-as-module",
  441. { directory: false, request: "." },
  442. "single file module",
  443. true,
  444. "undescribed-raw-file"
  445. )
  446. );
  447. }
  448. plugins.push(
  449. new DirectoryExistsPlugin(
  450. "resolve-as-module",
  451. "undescribed-resolve-in-package"
  452. )
  453. );
  454. // undescribed-resolve-in-package
  455. plugins.push(
  456. new DescriptionFilePlugin(
  457. "undescribed-resolve-in-package",
  458. descriptionFiles,
  459. false,
  460. "resolve-in-package"
  461. )
  462. );
  463. plugins.push(
  464. new NextPlugin("after-undescribed-resolve-in-package", "resolve-in-package")
  465. );
  466. // resolve-in-package
  467. exportsFields.forEach(exportsField => {
  468. plugins.push(
  469. new ExportsFieldPlugin(
  470. "resolve-in-package",
  471. conditionNames,
  472. exportsField,
  473. "relative"
  474. )
  475. );
  476. });
  477. plugins.push(
  478. new NextPlugin("resolve-in-package", "resolve-in-existing-directory")
  479. );
  480. // resolve-in-existing-directory
  481. plugins.push(
  482. new JoinRequestPlugin("resolve-in-existing-directory", "relative")
  483. );
  484. // relative
  485. plugins.push(
  486. new DescriptionFilePlugin(
  487. "relative",
  488. descriptionFiles,
  489. true,
  490. "described-relative"
  491. )
  492. );
  493. plugins.push(new NextPlugin("after-relative", "described-relative"));
  494. // described-relative
  495. if (resolveToContext) {
  496. plugins.push(new NextPlugin("described-relative", "directory"));
  497. } else {
  498. plugins.push(
  499. new ConditionalPlugin(
  500. "described-relative",
  501. { directory: false },
  502. null,
  503. true,
  504. "raw-file"
  505. )
  506. );
  507. plugins.push(
  508. new ConditionalPlugin(
  509. "described-relative",
  510. { fullySpecified: false },
  511. "as directory",
  512. true,
  513. "directory"
  514. )
  515. );
  516. }
  517. // directory
  518. plugins.push(
  519. new DirectoryExistsPlugin("directory", "undescribed-existing-directory")
  520. );
  521. if (resolveToContext) {
  522. // undescribed-existing-directory
  523. plugins.push(new NextPlugin("undescribed-existing-directory", "resolved"));
  524. } else {
  525. // undescribed-existing-directory
  526. plugins.push(
  527. new DescriptionFilePlugin(
  528. "undescribed-existing-directory",
  529. descriptionFiles,
  530. false,
  531. "existing-directory"
  532. )
  533. );
  534. mainFiles.forEach(item => {
  535. plugins.push(
  536. new UseFilePlugin(
  537. "undescribed-existing-directory",
  538. item,
  539. "undescribed-raw-file"
  540. )
  541. );
  542. });
  543. // described-existing-directory
  544. mainFields.forEach(item => {
  545. plugins.push(
  546. new MainFieldPlugin(
  547. "existing-directory",
  548. item,
  549. "resolve-in-existing-directory"
  550. )
  551. );
  552. });
  553. mainFiles.forEach(item => {
  554. plugins.push(
  555. new UseFilePlugin("existing-directory", item, "undescribed-raw-file")
  556. );
  557. });
  558. // undescribed-raw-file
  559. plugins.push(
  560. new DescriptionFilePlugin(
  561. "undescribed-raw-file",
  562. descriptionFiles,
  563. true,
  564. "raw-file"
  565. )
  566. );
  567. plugins.push(new NextPlugin("after-undescribed-raw-file", "raw-file"));
  568. // raw-file
  569. plugins.push(
  570. new ConditionalPlugin(
  571. "raw-file",
  572. { fullySpecified: true },
  573. null,
  574. false,
  575. "file"
  576. )
  577. );
  578. if (!enforceExtension) {
  579. plugins.push(new TryNextPlugin("raw-file", "no extension", "file"));
  580. }
  581. extensions.forEach(item => {
  582. plugins.push(new AppendPlugin("raw-file", item, "file"));
  583. });
  584. // file
  585. if (alias.length > 0)
  586. plugins.push(new AliasPlugin("file", alias, "internal-resolve"));
  587. aliasFields.forEach(item => {
  588. plugins.push(new AliasFieldPlugin("file", item, "internal-resolve"));
  589. });
  590. plugins.push(new NextPlugin("file", "final-file"));
  591. // final-file
  592. plugins.push(new FileExistsPlugin("final-file", "existing-file"));
  593. // existing-file
  594. if (symlinks)
  595. plugins.push(new SymlinkPlugin("existing-file", "existing-file"));
  596. plugins.push(new NextPlugin("existing-file", "resolved"));
  597. }
  598. const resolved =
  599. /** @type {KnownHooks & EnsuredHooks} */
  600. (resolver.hooks).resolved;
  601. // resolved
  602. if (restrictions.size > 0) {
  603. plugins.push(new RestrictionsPlugin(resolved, restrictions));
  604. }
  605. plugins.push(new ResultPlugin(resolved));
  606. //// RESOLVER ////
  607. for (const plugin of plugins) {
  608. if (typeof plugin === "function") {
  609. /** @type {function(this: Resolver, Resolver): void} */
  610. (plugin).call(resolver, resolver);
  611. } else if (plugin) {
  612. plugin.apply(resolver);
  613. }
  614. }
  615. return resolver;
  616. };
  617. /**
  618. * Merging filtered elements
  619. * @param {string[]} array source array
  620. * @param {function(string): boolean} filter predicate
  621. * @returns {Array<string | string[]>} merge result
  622. */
  623. function mergeFilteredToArray(array, filter) {
  624. /** @type {Array<string | string[]>} */
  625. const result = [];
  626. const set = new Set(array);
  627. for (const item of set) {
  628. if (filter(item)) {
  629. const lastElement =
  630. result.length > 0 ? result[result.length - 1] : undefined;
  631. if (Array.isArray(lastElement)) {
  632. lastElement.push(item);
  633. } else {
  634. result.push([item]);
  635. }
  636. } else {
  637. result.push(item);
  638. }
  639. }
  640. return result;
  641. }