index.js 5.6 KB

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