123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695 |
- "use strict";
- const versions = require("process").versions;
- const Resolver = require("./Resolver");
- const { getType, PathType } = require("./util/path");
- const SyncAsyncFileSystemDecorator = require("./SyncAsyncFileSystemDecorator");
- const AliasFieldPlugin = require("./AliasFieldPlugin");
- const AliasPlugin = require("./AliasPlugin");
- const AppendPlugin = require("./AppendPlugin");
- const ConditionalPlugin = require("./ConditionalPlugin");
- const DescriptionFilePlugin = require("./DescriptionFilePlugin");
- const DirectoryExistsPlugin = require("./DirectoryExistsPlugin");
- const ExportsFieldPlugin = require("./ExportsFieldPlugin");
- const ExtensionAliasPlugin = require("./ExtensionAliasPlugin");
- const FileExistsPlugin = require("./FileExistsPlugin");
- const ImportsFieldPlugin = require("./ImportsFieldPlugin");
- const JoinRequestPartPlugin = require("./JoinRequestPartPlugin");
- const JoinRequestPlugin = require("./JoinRequestPlugin");
- const MainFieldPlugin = require("./MainFieldPlugin");
- const ModulesInHierarchicalDirectoriesPlugin = require("./ModulesInHierarchicalDirectoriesPlugin");
- const ModulesInRootPlugin = require("./ModulesInRootPlugin");
- const NextPlugin = require("./NextPlugin");
- const ParsePlugin = require("./ParsePlugin");
- const PnpPlugin = require("./PnpPlugin");
- const RestrictionsPlugin = require("./RestrictionsPlugin");
- const ResultPlugin = require("./ResultPlugin");
- const RootsPlugin = require("./RootsPlugin");
- const SelfReferencePlugin = require("./SelfReferencePlugin");
- const SymlinkPlugin = require("./SymlinkPlugin");
- const TryNextPlugin = require("./TryNextPlugin");
- const UnsafeCachePlugin = require("./UnsafeCachePlugin");
- const UseFilePlugin = require("./UseFilePlugin");
- function processPnpApiOption(option) {
- if (
- option === undefined &&
- versions.pnp
- ) {
-
- return require("pnpapi");
- }
- return option || null;
- }
- function normalizeAlias(alias) {
- return typeof alias === "object" && !Array.isArray(alias) && alias !== null
- ? Object.keys(alias).map(key => {
-
- const obj = { name: key, onlyModule: false, alias: alias[key] };
- if (/\$$/.test(key)) {
- obj.onlyModule = true;
- obj.name = key.slice(0, -1);
- }
- return obj;
- })
- : (alias) || [];
- }
- function createOptions(options) {
- const mainFieldsSet = new Set(options.mainFields || ["main"]);
-
- const mainFields = [];
- for (const item of mainFieldsSet) {
- if (typeof item === "string") {
- mainFields.push({
- name: [item],
- forceRelative: true
- });
- } else if (Array.isArray(item)) {
- mainFields.push({
- name: item,
- forceRelative: true
- });
- } else {
- mainFields.push({
- name: Array.isArray(item.name) ? item.name : [item.name],
- forceRelative: item.forceRelative
- });
- }
- }
- return {
- alias: normalizeAlias(options.alias),
- fallback: normalizeAlias(options.fallback),
- aliasFields: new Set(options.aliasFields),
- cachePredicate:
- options.cachePredicate ||
- function () {
- return true;
- },
- cacheWithContext:
- typeof options.cacheWithContext !== "undefined"
- ? options.cacheWithContext
- : true,
- exportsFields: new Set(options.exportsFields || ["exports"]),
- importsFields: new Set(options.importsFields || ["imports"]),
- conditionNames: new Set(options.conditionNames),
- descriptionFiles: Array.from(
- new Set(options.descriptionFiles || ["package.json"])
- ),
- enforceExtension:
- options.enforceExtension === undefined
- ? options.extensions && options.extensions.includes("")
- ? true
- : false
- : options.enforceExtension,
- extensions: new Set(options.extensions || [".js", ".json", ".node"]),
- extensionAlias: options.extensionAlias
- ? Object.keys(options.extensionAlias).map(k => ({
- extension: k,
- alias: (options.extensionAlias)[
- k
- ]
- }))
- : [],
- fileSystem: options.useSyncFileSystemCalls
- ? new SyncAsyncFileSystemDecorator(
- (
- (options.fileSystem)
- )
- )
- : options.fileSystem,
- unsafeCache:
- options.unsafeCache && typeof options.unsafeCache !== "object"
- ? {}
- : options.unsafeCache || false,
- symlinks: typeof options.symlinks !== "undefined" ? options.symlinks : true,
- resolver: options.resolver,
- modules: mergeFilteredToArray(
- Array.isArray(options.modules)
- ? options.modules
- : options.modules
- ? [options.modules]
- : ["node_modules"],
- item => {
- const type = getType(item);
- return type === PathType.Normal || type === PathType.Relative;
- }
- ),
- mainFields,
- mainFiles: new Set(options.mainFiles || ["index"]),
- plugins: options.plugins || [],
- pnpApi: processPnpApiOption(options.pnpApi),
- roots: new Set(options.roots || undefined),
- fullySpecified: options.fullySpecified || false,
- resolveToContext: options.resolveToContext || false,
- preferRelative: options.preferRelative || false,
- preferAbsolute: options.preferAbsolute || false,
- restrictions: new Set(options.restrictions)
- };
- }
- exports.createResolver = function (options) {
- const normalizedOptions = createOptions(options);
- const {
- alias,
- fallback,
- aliasFields,
- cachePredicate,
- cacheWithContext,
- conditionNames,
- descriptionFiles,
- enforceExtension,
- exportsFields,
- extensionAlias,
- importsFields,
- extensions,
- fileSystem,
- fullySpecified,
- mainFields,
- mainFiles,
- modules,
- plugins: userPlugins,
- pnpApi,
- resolveToContext,
- preferRelative,
- preferAbsolute,
- symlinks,
- unsafeCache,
- resolver: customResolver,
- restrictions,
- roots
- } = normalizedOptions;
- const plugins = userPlugins.slice();
- const resolver = customResolver
- ? customResolver
- : new Resolver(fileSystem, normalizedOptions);
-
- resolver.ensureHook("resolve");
- resolver.ensureHook("internalResolve");
- resolver.ensureHook("newInternalResolve");
- resolver.ensureHook("parsedResolve");
- resolver.ensureHook("describedResolve");
- resolver.ensureHook("rawResolve");
- resolver.ensureHook("normalResolve");
- resolver.ensureHook("internal");
- resolver.ensureHook("rawModule");
- resolver.ensureHook("module");
- resolver.ensureHook("resolveAsModule");
- resolver.ensureHook("undescribedResolveInPackage");
- resolver.ensureHook("resolveInPackage");
- resolver.ensureHook("resolveInExistingDirectory");
- resolver.ensureHook("relative");
- resolver.ensureHook("describedRelative");
- resolver.ensureHook("directory");
- resolver.ensureHook("undescribedExistingDirectory");
- resolver.ensureHook("existingDirectory");
- resolver.ensureHook("undescribedRawFile");
- resolver.ensureHook("rawFile");
- resolver.ensureHook("file");
- resolver.ensureHook("finalFile");
- resolver.ensureHook("existingFile");
- resolver.ensureHook("resolved");
-
-
-
-
- resolver.hooks.newInteralResolve = resolver.hooks.newInternalResolve;
-
- for (const { source, resolveOptions } of [
- { source: "resolve", resolveOptions: { fullySpecified } },
- { source: "internal-resolve", resolveOptions: { fullySpecified: false } }
- ]) {
- if (unsafeCache) {
- plugins.push(
- new UnsafeCachePlugin(
- source,
- cachePredicate,
- (unsafeCache),
- cacheWithContext,
- `new-${source}`
- )
- );
- plugins.push(
- new ParsePlugin(`new-${source}`, resolveOptions, "parsed-resolve")
- );
- } else {
- plugins.push(new ParsePlugin(source, resolveOptions, "parsed-resolve"));
- }
- }
-
- plugins.push(
- new DescriptionFilePlugin(
- "parsed-resolve",
- descriptionFiles,
- false,
- "described-resolve"
- )
- );
- plugins.push(new NextPlugin("after-parsed-resolve", "described-resolve"));
-
- plugins.push(new NextPlugin("described-resolve", "raw-resolve"));
- if (fallback.length > 0) {
- plugins.push(
- new AliasPlugin("described-resolve", fallback, "internal-resolve")
- );
- }
-
- if (alias.length > 0) {
- plugins.push(new AliasPlugin("raw-resolve", alias, "internal-resolve"));
- }
- aliasFields.forEach(item => {
- plugins.push(new AliasFieldPlugin("raw-resolve", item, "internal-resolve"));
- });
- extensionAlias.forEach(item =>
- plugins.push(
- new ExtensionAliasPlugin("raw-resolve", item, "normal-resolve")
- )
- );
- plugins.push(new NextPlugin("raw-resolve", "normal-resolve"));
-
- if (preferRelative) {
- plugins.push(new JoinRequestPlugin("after-normal-resolve", "relative"));
- }
- plugins.push(
- new ConditionalPlugin(
- "after-normal-resolve",
- { module: true },
- "resolve as module",
- false,
- "raw-module"
- )
- );
- plugins.push(
- new ConditionalPlugin(
- "after-normal-resolve",
- { internal: true },
- "resolve as internal import",
- false,
- "internal"
- )
- );
- if (preferAbsolute) {
- plugins.push(new JoinRequestPlugin("after-normal-resolve", "relative"));
- }
- if (roots.size > 0) {
- plugins.push(new RootsPlugin("after-normal-resolve", roots, "relative"));
- }
- if (!preferRelative && !preferAbsolute) {
- plugins.push(new JoinRequestPlugin("after-normal-resolve", "relative"));
- }
-
- importsFields.forEach(importsField => {
- plugins.push(
- new ImportsFieldPlugin(
- "internal",
- conditionNames,
- importsField,
- "relative",
- "internal-resolve"
- )
- );
- });
-
- exportsFields.forEach(exportsField => {
- plugins.push(
- new SelfReferencePlugin("raw-module", exportsField, "resolve-as-module")
- );
- });
- modules.forEach(item => {
- if (Array.isArray(item)) {
- if (item.includes("node_modules") && pnpApi) {
- plugins.push(
- new ModulesInHierarchicalDirectoriesPlugin(
- "raw-module",
- item.filter(i => i !== "node_modules"),
- "module"
- )
- );
- plugins.push(
- new PnpPlugin("raw-module", pnpApi, "undescribed-resolve-in-package")
- );
- } else {
- plugins.push(
- new ModulesInHierarchicalDirectoriesPlugin(
- "raw-module",
- item,
- "module"
- )
- );
- }
- } else {
- plugins.push(new ModulesInRootPlugin("raw-module", item, "module"));
- }
- });
-
- plugins.push(new JoinRequestPartPlugin("module", "resolve-as-module"));
-
- if (!resolveToContext) {
- plugins.push(
- new ConditionalPlugin(
- "resolve-as-module",
- { directory: false, request: "." },
- "single file module",
- true,
- "undescribed-raw-file"
- )
- );
- }
- plugins.push(
- new DirectoryExistsPlugin(
- "resolve-as-module",
- "undescribed-resolve-in-package"
- )
- );
-
- plugins.push(
- new DescriptionFilePlugin(
- "undescribed-resolve-in-package",
- descriptionFiles,
- false,
- "resolve-in-package"
- )
- );
- plugins.push(
- new NextPlugin("after-undescribed-resolve-in-package", "resolve-in-package")
- );
-
- exportsFields.forEach(exportsField => {
- plugins.push(
- new ExportsFieldPlugin(
- "resolve-in-package",
- conditionNames,
- exportsField,
- "relative"
- )
- );
- });
- plugins.push(
- new NextPlugin("resolve-in-package", "resolve-in-existing-directory")
- );
-
- plugins.push(
- new JoinRequestPlugin("resolve-in-existing-directory", "relative")
- );
-
- plugins.push(
- new DescriptionFilePlugin(
- "relative",
- descriptionFiles,
- true,
- "described-relative"
- )
- );
- plugins.push(new NextPlugin("after-relative", "described-relative"));
-
- if (resolveToContext) {
- plugins.push(new NextPlugin("described-relative", "directory"));
- } else {
- plugins.push(
- new ConditionalPlugin(
- "described-relative",
- { directory: false },
- null,
- true,
- "raw-file"
- )
- );
- plugins.push(
- new ConditionalPlugin(
- "described-relative",
- { fullySpecified: false },
- "as directory",
- true,
- "directory"
- )
- );
- }
-
- plugins.push(
- new DirectoryExistsPlugin("directory", "undescribed-existing-directory")
- );
- if (resolveToContext) {
-
- plugins.push(new NextPlugin("undescribed-existing-directory", "resolved"));
- } else {
-
- plugins.push(
- new DescriptionFilePlugin(
- "undescribed-existing-directory",
- descriptionFiles,
- false,
- "existing-directory"
- )
- );
- mainFiles.forEach(item => {
- plugins.push(
- new UseFilePlugin(
- "undescribed-existing-directory",
- item,
- "undescribed-raw-file"
- )
- );
- });
-
- mainFields.forEach(item => {
- plugins.push(
- new MainFieldPlugin(
- "existing-directory",
- item,
- "resolve-in-existing-directory"
- )
- );
- });
- mainFiles.forEach(item => {
- plugins.push(
- new UseFilePlugin("existing-directory", item, "undescribed-raw-file")
- );
- });
-
- plugins.push(
- new DescriptionFilePlugin(
- "undescribed-raw-file",
- descriptionFiles,
- true,
- "raw-file"
- )
- );
- plugins.push(new NextPlugin("after-undescribed-raw-file", "raw-file"));
-
- plugins.push(
- new ConditionalPlugin(
- "raw-file",
- { fullySpecified: true },
- null,
- false,
- "file"
- )
- );
- if (!enforceExtension) {
- plugins.push(new TryNextPlugin("raw-file", "no extension", "file"));
- }
- extensions.forEach(item => {
- plugins.push(new AppendPlugin("raw-file", item, "file"));
- });
-
- if (alias.length > 0)
- plugins.push(new AliasPlugin("file", alias, "internal-resolve"));
- aliasFields.forEach(item => {
- plugins.push(new AliasFieldPlugin("file", item, "internal-resolve"));
- });
- plugins.push(new NextPlugin("file", "final-file"));
-
- plugins.push(new FileExistsPlugin("final-file", "existing-file"));
-
- if (symlinks)
- plugins.push(new SymlinkPlugin("existing-file", "existing-file"));
- plugins.push(new NextPlugin("existing-file", "resolved"));
- }
- const resolved =
-
- (resolver.hooks).resolved;
-
- if (restrictions.size > 0) {
- plugins.push(new RestrictionsPlugin(resolved, restrictions));
- }
- plugins.push(new ResultPlugin(resolved));
-
- for (const plugin of plugins) {
- if (typeof plugin === "function") {
-
- (plugin).call(resolver, resolver);
- } else if (plugin) {
- plugin.apply(resolver);
- }
- }
- return resolver;
- };
- function mergeFilteredToArray(array, filter) {
-
- const result = [];
- const set = new Set(array);
- for (const item of set) {
- if (filter(item)) {
- const lastElement =
- result.length > 0 ? result[result.length - 1] : undefined;
- if (Array.isArray(lastElement)) {
- lastElement.push(item);
- } else {
- result.push([item]);
- }
- } else {
- result.push(item);
- }
- }
- return result;
- }
|