| | |
| | | const SymlinkPlugin = require("./SymlinkPlugin"); |
| | | const SyncAsyncFileSystemDecorator = require("./SyncAsyncFileSystemDecorator"); |
| | | const TryNextPlugin = require("./TryNextPlugin"); |
| | | const TsconfigPathsPlugin = require("./TsconfigPathsPlugin"); |
| | | const UnsafeCachePlugin = require("./UnsafeCachePlugin"); |
| | | const UseFilePlugin = require("./UseFilePlugin"); |
| | | const { PathType, getType } = require("./util/path"); |
| | |
| | | /** @typedef {{ [k: string]: string|string[] }} ExtensionAliasOptions */ |
| | | /** @typedef {false | 0 | "" | null | undefined} Falsy */ |
| | | /** @typedef {{apply: (resolver: Resolver) => void} | ((this: Resolver, resolver: Resolver) => void) | Falsy} Plugin */ |
| | | |
| | | /** |
| | | * @typedef {object} TsconfigOptions |
| | | * @property {string=} configFile A relative path to the tsconfig file based on cwd, or an absolute path of tsconfig file |
| | | * @property {string[] | "auto"=} references References to other tsconfig files. 'auto' inherits from TypeScript config, or an array of relative/absolute paths |
| | | * @property {string=} baseUrl Override baseUrl from tsconfig.json. If provided, this value will be used instead of the baseUrl in the tsconfig file |
| | | */ |
| | | |
| | | /** |
| | | * @typedef {object} UserResolveOptions |
| | |
| | | * @property {boolean=} useSyncFileSystemCalls Use only the sync constraints of the file system calls |
| | | * @property {boolean=} preferRelative Prefer to resolve module requests as relative requests before falling back to modules |
| | | * @property {boolean=} preferAbsolute Prefer to resolve server-relative urls as absolute paths before falling back to resolve in roots |
| | | * @property {string | boolean | TsconfigOptions=} tsconfig TypeScript config file path or config object with configFile and references |
| | | */ |
| | | |
| | | /** |
| | |
| | | * @property {Cache | false} unsafeCache unsafe cache |
| | | * @property {boolean} symlinks symlinks |
| | | * @property {Resolver=} resolver resolver |
| | | * @property {Array<string | string[]>} modules modules |
| | | * @property {(string | string[])[]} modules modules |
| | | * @property {{ name: string[], forceRelative: boolean }[]} mainFields main fields |
| | | * @property {Set<string>} mainFiles main files |
| | | * @property {Plugin[]} plugins plugins |
| | |
| | | * @property {Set<string | RegExp>} restrictions restrictions |
| | | * @property {boolean} preferRelative prefer relative |
| | | * @property {boolean} preferAbsolute prefer absolute |
| | | * @property {string | boolean | TsconfigOptions} tsconfig tsconfig file path or config object |
| | | */ |
| | | |
| | | /** |
| | |
| | | |
| | | return obj; |
| | | }) |
| | | : /** @type {Array<AliasOptionEntry>} */ (alias) || []; |
| | | : /** @type {AliasOptionEntry[]} */ (alias) || []; |
| | | } |
| | | |
| | | /** |
| | | * Merging filtered elements |
| | | * @param {string[]} array source array |
| | | * @param {(item: string) => boolean} filter predicate |
| | | * @returns {Array<string | string[]>} merge result |
| | | * @returns {(string | string[])[]} merge result |
| | | */ |
| | | function mergeFilteredToArray(array, filter) { |
| | | /** @type {Array<string | string[]>} */ |
| | | /** @type {(string | string[])[]} */ |
| | | const result = []; |
| | | const set = new Set(array); |
| | | |
| | |
| | | preferRelative: options.preferRelative || false, |
| | | preferAbsolute: options.preferAbsolute || false, |
| | | restrictions: new Set(options.restrictions), |
| | | tsconfig: |
| | | typeof options.tsconfig === "undefined" ? false : options.tsconfig, |
| | | }; |
| | | } |
| | | |
| | |
| | | resolver: customResolver, |
| | | restrictions, |
| | | roots, |
| | | tsconfig, |
| | | } = normalizedOptions; |
| | | |
| | | const plugins = [...userPlugins]; |
| | |
| | | { source: "resolve", resolveOptions: { fullySpecified } }, |
| | | { source: "internal-resolve", resolveOptions: { fullySpecified: false } }, |
| | | ]) { |
| | | if (unsafeCache) { |
| | | plugins.push( |
| | | new UnsafeCachePlugin( |
| | | source, |
| | | cachePredicate, |
| | | /** @type {import("./UnsafeCachePlugin").Cache} */ (unsafeCache), |
| | | cacheWithContext, |
| | | `new-${source}`, |
| | | ), |
| | | ); |
| | | plugins.push( |
| | | new ParsePlugin(`new-${source}`, resolveOptions, "parsed-resolve"), |
| | | ); |
| | | } else { |
| | | plugins.push(new ParsePlugin(source, resolveOptions, "parsed-resolve")); |
| | | } |
| | | } |
| | | |
| | | // parsed-resolve |
| | |
| | | plugins.push(new NextPlugin("after-parsed-resolve", "described-resolve")); |
| | | |
| | | // described-resolve |
| | | if (unsafeCache) { |
| | | plugins.push( |
| | | new UnsafeCachePlugin( |
| | | "described-resolve", |
| | | cachePredicate, |
| | | /** @type {import("./UnsafeCachePlugin").Cache} */ (unsafeCache), |
| | | cacheWithContext, |
| | | "raw-resolve", |
| | | ), |
| | | ); |
| | | } else { |
| | | plugins.push(new NextPlugin("described-resolve", "raw-resolve")); |
| | | } |
| | | if (fallback.length > 0) { |
| | | plugins.push( |
| | | new AliasPlugin("described-resolve", fallback, "internal-resolve"), |
| | | ); |
| | | } |
| | | |
| | | // raw-resolve |
| | | if (alias.length > 0) { |
| | | plugins.push(new AliasPlugin("raw-resolve", alias, "internal-resolve")); |
| | | } |
| | | if (tsconfig) { |
| | | plugins.push(new TsconfigPathsPlugin(tsconfig)); |
| | | } |
| | | for (const item of aliasFields) { |
| | | plugins.push(new AliasFieldPlugin("raw-resolve", item, "internal-resolve")); |
| | | } |