index.js 3.3 KB

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