[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const fs = require("graceful-fs");
|
---|
| 9 | const CachedInputFileSystem = require("./CachedInputFileSystem");
|
---|
| 10 | const ResolverFactory = require("./ResolverFactory");
|
---|
| 11 |
|
---|
| 12 | /** @typedef {import("./PnpPlugin").PnpApiImpl} PnpApi */
|
---|
| 13 | /** @typedef {import("./Resolver")} Resolver */
|
---|
| 14 | /** @typedef {import("./Resolver").FileSystem} FileSystem */
|
---|
| 15 | /** @typedef {import("./Resolver").ResolveCallback} ResolveCallback */
|
---|
| 16 | /** @typedef {import("./Resolver").ResolveContext} ResolveContext */
|
---|
| 17 | /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
|
---|
| 18 | /** @typedef {import("./ResolverFactory").Plugin} Plugin */
|
---|
| 19 | /** @typedef {import("./ResolverFactory").UserResolveOptions} ResolveOptions */
|
---|
| 20 | /** @typedef {{
|
---|
| 21 | * (context: object, path: string, request: string, resolveContext: ResolveContext, callback: ResolveCallback): void;
|
---|
| 22 | * (context: object, path: string, request: string, callback: ResolveCallback): void;
|
---|
| 23 | * (path: string, request: string, resolveContext: ResolveContext, callback: ResolveCallback): void;
|
---|
| 24 | * (path: string, request: string, callback: ResolveCallback): void;
|
---|
| 25 | * }} ResolveFunctionAsync
|
---|
| 26 | */
|
---|
| 27 | /** @typedef {{
|
---|
| 28 | * (context: object, path: string, request: string): string|false;
|
---|
| 29 | * (path: string, request: string): string|false;
|
---|
| 30 | * }} ResolveFunction
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | const nodeFileSystem = new CachedInputFileSystem(fs, 4000);
|
---|
| 34 |
|
---|
| 35 | const nodeContext = {
|
---|
| 36 | environments: ["node+es3+es5+process+native"]
|
---|
| 37 | };
|
---|
| 38 |
|
---|
| 39 | const asyncResolver = ResolverFactory.createResolver({
|
---|
| 40 | conditionNames: ["node"],
|
---|
| 41 | extensions: [".js", ".json", ".node"],
|
---|
| 42 | fileSystem: nodeFileSystem
|
---|
| 43 | });
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * @type {ResolveFunctionAsync}
|
---|
| 47 | */
|
---|
| 48 | const resolve =
|
---|
| 49 | /**
|
---|
| 50 | * @param {object|string} context
|
---|
| 51 | * @param {string} path
|
---|
| 52 | * @param {string|ResolveContext|ResolveCallback} request
|
---|
| 53 | * @param {ResolveContext|ResolveCallback=} resolveContext
|
---|
| 54 | * @param {ResolveCallback=} callback
|
---|
| 55 | */
|
---|
| 56 | (context, path, request, resolveContext, callback) => {
|
---|
| 57 | if (typeof context === "string") {
|
---|
| 58 | callback = /** @type {ResolveCallback} */ (resolveContext);
|
---|
| 59 | resolveContext = /** @type {ResolveContext} */ (request);
|
---|
| 60 | request = path;
|
---|
| 61 | path = context;
|
---|
| 62 | context = nodeContext;
|
---|
| 63 | }
|
---|
| 64 | if (typeof callback !== "function") {
|
---|
| 65 | callback = /** @type {ResolveCallback} */ (resolveContext);
|
---|
| 66 | }
|
---|
| 67 | asyncResolver.resolve(
|
---|
| 68 | context,
|
---|
| 69 | path,
|
---|
| 70 | /** @type {string} */ (request),
|
---|
| 71 | /** @type {ResolveContext} */ (resolveContext),
|
---|
| 72 | /** @type {ResolveCallback} */ (callback)
|
---|
| 73 | );
|
---|
| 74 | };
|
---|
| 75 |
|
---|
| 76 | const syncResolver = ResolverFactory.createResolver({
|
---|
| 77 | conditionNames: ["node"],
|
---|
| 78 | extensions: [".js", ".json", ".node"],
|
---|
| 79 | useSyncFileSystemCalls: true,
|
---|
| 80 | fileSystem: nodeFileSystem
|
---|
| 81 | });
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | * @type {ResolveFunction}
|
---|
| 85 | */
|
---|
| 86 | const resolveSync =
|
---|
| 87 | /**
|
---|
| 88 | * @param {object|string} context
|
---|
| 89 | * @param {string} path
|
---|
| 90 | * @param {string=} request
|
---|
| 91 | */
|
---|
| 92 | (context, path, request) => {
|
---|
| 93 | if (typeof context === "string") {
|
---|
| 94 | request = path;
|
---|
| 95 | path = context;
|
---|
| 96 | context = nodeContext;
|
---|
| 97 | }
|
---|
| 98 | return syncResolver.resolveSync(
|
---|
| 99 | context,
|
---|
| 100 | path,
|
---|
| 101 | /** @type {string} */ (request)
|
---|
| 102 | );
|
---|
| 103 | };
|
---|
| 104 |
|
---|
| 105 | /** @typedef {Omit<ResolveOptions, "fileSystem"> & Partial<Pick<ResolveOptions, "fileSystem">>} ResolveOptionsOptionalFS */
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * @param {ResolveOptionsOptionalFS} options Resolver options
|
---|
| 109 | * @returns {ResolveFunctionAsync} Resolver function
|
---|
| 110 | */
|
---|
| 111 | function create(options) {
|
---|
| 112 | const resolver = ResolverFactory.createResolver({
|
---|
| 113 | fileSystem: nodeFileSystem,
|
---|
| 114 | ...options
|
---|
| 115 | });
|
---|
| 116 | /**
|
---|
| 117 | * @param {object|string} context Custom context
|
---|
| 118 | * @param {string} path Base path
|
---|
| 119 | * @param {string|ResolveContext|ResolveCallback} request String to resolve
|
---|
| 120 | * @param {ResolveContext|ResolveCallback=} resolveContext Resolve context
|
---|
| 121 | * @param {ResolveCallback=} callback Result callback
|
---|
| 122 | */
|
---|
| 123 | return function (context, path, request, resolveContext, callback) {
|
---|
| 124 | if (typeof context === "string") {
|
---|
| 125 | callback = /** @type {ResolveCallback} */ (resolveContext);
|
---|
| 126 | resolveContext = /** @type {ResolveContext} */ (request);
|
---|
| 127 | request = path;
|
---|
| 128 | path = context;
|
---|
| 129 | context = nodeContext;
|
---|
| 130 | }
|
---|
| 131 | if (typeof callback !== "function") {
|
---|
| 132 | callback = /** @type {ResolveCallback} */ (resolveContext);
|
---|
| 133 | }
|
---|
| 134 | resolver.resolve(
|
---|
| 135 | context,
|
---|
| 136 | path,
|
---|
| 137 | /** @type {string} */ (request),
|
---|
| 138 | /** @type {ResolveContext} */ (resolveContext),
|
---|
| 139 | callback
|
---|
| 140 | );
|
---|
| 141 | };
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /**
|
---|
| 145 | * @param {ResolveOptionsOptionalFS} options Resolver options
|
---|
| 146 | * @returns {ResolveFunction} Resolver function
|
---|
| 147 | */
|
---|
| 148 | function createSync(options) {
|
---|
| 149 | const resolver = ResolverFactory.createResolver({
|
---|
| 150 | useSyncFileSystemCalls: true,
|
---|
| 151 | fileSystem: nodeFileSystem,
|
---|
| 152 | ...options
|
---|
| 153 | });
|
---|
| 154 | /**
|
---|
| 155 | * @param {object|string} context custom context
|
---|
| 156 | * @param {string} path base path
|
---|
| 157 | * @param {string=} request request to resolve
|
---|
| 158 | * @returns {string|false} Resolved path or false
|
---|
| 159 | */
|
---|
| 160 | return function (context, path, request) {
|
---|
| 161 | if (typeof context === "string") {
|
---|
| 162 | request = path;
|
---|
| 163 | path = context;
|
---|
| 164 | context = nodeContext;
|
---|
| 165 | }
|
---|
| 166 | return resolver.resolveSync(context, path, /** @type {string} */ (request));
|
---|
| 167 | };
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /**
|
---|
| 171 | * @template A
|
---|
| 172 | * @template B
|
---|
| 173 | * @param {A} obj input a
|
---|
| 174 | * @param {B} exports input b
|
---|
| 175 | * @returns {A & B} merged
|
---|
| 176 | */
|
---|
| 177 | const mergeExports = (obj, exports) => {
|
---|
| 178 | const descriptors = Object.getOwnPropertyDescriptors(exports);
|
---|
| 179 | Object.defineProperties(obj, descriptors);
|
---|
| 180 | return /** @type {A & B} */ (Object.freeze(obj));
|
---|
| 181 | };
|
---|
| 182 |
|
---|
| 183 | module.exports = mergeExports(resolve, {
|
---|
| 184 | get sync() {
|
---|
| 185 | return resolveSync;
|
---|
| 186 | },
|
---|
| 187 | create: mergeExports(create, {
|
---|
| 188 | get sync() {
|
---|
| 189 | return createSync;
|
---|
| 190 | }
|
---|
| 191 | }),
|
---|
| 192 | ResolverFactory,
|
---|
| 193 | CachedInputFileSystem,
|
---|
| 194 | get CloneBasenamePlugin() {
|
---|
| 195 | return require("./CloneBasenamePlugin");
|
---|
| 196 | },
|
---|
| 197 | get LogInfoPlugin() {
|
---|
| 198 | return require("./LogInfoPlugin");
|
---|
| 199 | },
|
---|
| 200 | get forEachBail() {
|
---|
| 201 | return require("./forEachBail");
|
---|
| 202 | }
|
---|
| 203 | });
|
---|