ResolverFactory.js 20 KB

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