[6a3a178] | 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").ResolveContext} ResolveContext */
|
---|
| 16 | /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
|
---|
| 17 | /** @typedef {import("./ResolverFactory").Plugin} Plugin */
|
---|
| 18 | /** @typedef {import("./ResolverFactory").UserResolveOptions} ResolveOptions */
|
---|
| 19 |
|
---|
| 20 | const nodeFileSystem = new CachedInputFileSystem(fs, 4000);
|
---|
| 21 |
|
---|
| 22 | const nodeContext = {
|
---|
| 23 | environments: ["node+es3+es5+process+native"]
|
---|
| 24 | };
|
---|
| 25 |
|
---|
| 26 | const asyncResolver = ResolverFactory.createResolver({
|
---|
| 27 | conditionNames: ["node"],
|
---|
| 28 | extensions: [".js", ".json", ".node"],
|
---|
| 29 | fileSystem: nodeFileSystem
|
---|
| 30 | });
|
---|
| 31 | function resolve(context, path, request, resolveContext, callback) {
|
---|
| 32 | if (typeof context === "string") {
|
---|
| 33 | callback = resolveContext;
|
---|
| 34 | resolveContext = request;
|
---|
| 35 | request = path;
|
---|
| 36 | path = context;
|
---|
| 37 | context = nodeContext;
|
---|
| 38 | }
|
---|
| 39 | if (typeof callback !== "function") {
|
---|
| 40 | callback = resolveContext;
|
---|
| 41 | }
|
---|
| 42 | asyncResolver.resolve(context, path, request, resolveContext, callback);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | const syncResolver = ResolverFactory.createResolver({
|
---|
| 46 | conditionNames: ["node"],
|
---|
| 47 | extensions: [".js", ".json", ".node"],
|
---|
| 48 | useSyncFileSystemCalls: true,
|
---|
| 49 | fileSystem: nodeFileSystem
|
---|
| 50 | });
|
---|
| 51 | function resolveSync(context, path, request) {
|
---|
| 52 | if (typeof context === "string") {
|
---|
| 53 | request = path;
|
---|
| 54 | path = context;
|
---|
| 55 | context = nodeContext;
|
---|
| 56 | }
|
---|
| 57 | return syncResolver.resolveSync(context, path, request);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | function create(options) {
|
---|
| 61 | options = {
|
---|
| 62 | fileSystem: nodeFileSystem,
|
---|
| 63 | ...options
|
---|
| 64 | };
|
---|
| 65 | const resolver = ResolverFactory.createResolver(options);
|
---|
| 66 | return function (context, path, request, resolveContext, callback) {
|
---|
| 67 | if (typeof context === "string") {
|
---|
| 68 | callback = resolveContext;
|
---|
| 69 | resolveContext = request;
|
---|
| 70 | request = path;
|
---|
| 71 | path = context;
|
---|
| 72 | context = nodeContext;
|
---|
| 73 | }
|
---|
| 74 | if (typeof callback !== "function") {
|
---|
| 75 | callback = resolveContext;
|
---|
| 76 | }
|
---|
| 77 | resolver.resolve(context, path, request, resolveContext, callback);
|
---|
| 78 | };
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | function createSync(options) {
|
---|
| 82 | options = {
|
---|
| 83 | useSyncFileSystemCalls: true,
|
---|
| 84 | fileSystem: nodeFileSystem,
|
---|
| 85 | ...options
|
---|
| 86 | };
|
---|
| 87 | const resolver = ResolverFactory.createResolver(options);
|
---|
| 88 | return function (context, path, request) {
|
---|
| 89 | if (typeof context === "string") {
|
---|
| 90 | request = path;
|
---|
| 91 | path = context;
|
---|
| 92 | context = nodeContext;
|
---|
| 93 | }
|
---|
| 94 | return resolver.resolveSync(context, path, request);
|
---|
| 95 | };
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * @template A
|
---|
| 100 | * @template B
|
---|
| 101 | * @param {A} obj input a
|
---|
| 102 | * @param {B} exports input b
|
---|
| 103 | * @returns {A & B} merged
|
---|
| 104 | */
|
---|
| 105 | const mergeExports = (obj, exports) => {
|
---|
| 106 | const descriptors = Object.getOwnPropertyDescriptors(exports);
|
---|
| 107 | Object.defineProperties(obj, descriptors);
|
---|
| 108 | return /** @type {A & B} */ (Object.freeze(obj));
|
---|
| 109 | };
|
---|
| 110 |
|
---|
| 111 | module.exports = mergeExports(resolve, {
|
---|
| 112 | get sync() {
|
---|
| 113 | return resolveSync;
|
---|
| 114 | },
|
---|
| 115 | create: mergeExports(create, {
|
---|
| 116 | get sync() {
|
---|
| 117 | return createSync;
|
---|
| 118 | }
|
---|
| 119 | }),
|
---|
| 120 | ResolverFactory,
|
---|
| 121 | CachedInputFileSystem,
|
---|
| 122 | get CloneBasenamePlugin() {
|
---|
| 123 | return require("./CloneBasenamePlugin");
|
---|
| 124 | },
|
---|
| 125 | get LogInfoPlugin() {
|
---|
| 126 | return require("./LogInfoPlugin");
|
---|
| 127 | },
|
---|
| 128 | get forEachBail() {
|
---|
| 129 | return require("./forEachBail");
|
---|
| 130 | }
|
---|
| 131 | });
|
---|