match-path-data.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.tests = void 0;
  4. var path_1 = require("path");
  5. var filesystem_1 = require("../../filesystem");
  6. var defaultExtensionsWhenRunningInTsNode = [
  7. ".js",
  8. ".json",
  9. ".node",
  10. ".ts",
  11. ".tsx",
  12. ];
  13. exports.tests = [
  14. {
  15. name: "should locate path that matches with star and exists",
  16. absoluteBaseUrl: "/root/",
  17. paths: {
  18. "lib/*": ["location/*"],
  19. },
  20. existingFiles: [(0, path_1.join)("/root", "location", "mylib", "index.ts")],
  21. requestedModule: "lib/mylib",
  22. expectedPath: (0, path_1.dirname)((0, path_1.join)("/root", "location", "mylib", "index.ts")),
  23. extensions: defaultExtensionsWhenRunningInTsNode,
  24. },
  25. {
  26. name: "should resolve to correct path when many are specified",
  27. absoluteBaseUrl: "/root/",
  28. paths: {
  29. "lib/*": ["foo1/*", "foo2/*", "location/*", "foo3/*"],
  30. },
  31. existingFiles: [(0, path_1.join)("/root", "location", "mylib", "index.ts")],
  32. requestedModule: "lib/mylib",
  33. extensions: [".ts"],
  34. expectedPath: (0, path_1.dirname)((0, path_1.join)("/root", "location", "mylib", "index.ts")),
  35. },
  36. {
  37. name: "should locate path that matches with star and prioritize pattern with longest prefix",
  38. absoluteBaseUrl: "/root/",
  39. paths: {
  40. "*": ["location/*"],
  41. "lib/*": ["location/*"],
  42. },
  43. existingFiles: [
  44. (0, path_1.join)("/root", "location", "lib", "mylib", "index.ts"),
  45. (0, path_1.join)("/root", "location", "mylib", "index.ts"),
  46. ],
  47. requestedModule: "lib/mylib",
  48. expectedPath: (0, path_1.dirname)((0, path_1.join)("/root", "location", "mylib", "index.ts")),
  49. extensions: defaultExtensionsWhenRunningInTsNode,
  50. },
  51. {
  52. name: "should locate path that matches with star and exists with extension",
  53. absoluteBaseUrl: "/root/",
  54. paths: { "lib/*": ["location/*"] },
  55. existingFiles: [(0, path_1.join)("/root", "location", "mylib.myext")],
  56. requestedModule: "lib/mylib",
  57. extensions: [".js", ".myext"],
  58. expectedPath: (0, filesystem_1.removeExtension)((0, path_1.join)("/root", "location", "mylib.myext")),
  59. },
  60. {
  61. name: "should resolve request with extension specified",
  62. absoluteBaseUrl: "/root/",
  63. paths: { "lib/*": ["location/*"] },
  64. existingFiles: [(0, path_1.join)("/root", "location", "test.jpg")],
  65. requestedModule: "lib/test.jpg",
  66. expectedPath: (0, path_1.join)("/root", "location", "test.jpg"),
  67. extensions: defaultExtensionsWhenRunningInTsNode,
  68. },
  69. {
  70. name: "should locate path that matches without star and exists",
  71. absoluteBaseUrl: "/root/",
  72. paths: {
  73. "lib/foo": ["location/foo"],
  74. },
  75. existingFiles: [(0, path_1.join)("/root", "location", "foo.ts")],
  76. requestedModule: "lib/foo",
  77. expectedPath: (0, filesystem_1.removeExtension)((0, path_1.join)("/root", "location", "foo.ts")),
  78. extensions: defaultExtensionsWhenRunningInTsNode,
  79. },
  80. {
  81. name: "should resolve to parent folder when filename is in subfolder",
  82. absoluteBaseUrl: "/root/",
  83. paths: { "lib/*": ["location/*"] },
  84. existingFiles: [(0, path_1.join)("/root", "location", "mylib", "index.ts")],
  85. requestedModule: "lib/mylib",
  86. expectedPath: (0, path_1.dirname)((0, path_1.join)("/root", "location", "mylib", "index.ts")),
  87. extensions: defaultExtensionsWhenRunningInTsNode,
  88. },
  89. {
  90. name: "should resolve from main field in package.json",
  91. absoluteBaseUrl: "/root/",
  92. paths: { "lib/*": ["location/*"] },
  93. existingFiles: [(0, path_1.join)("/root", "location", "mylib", "kalle.ts")],
  94. packageJson: { main: "./kalle.ts" },
  95. requestedModule: "lib/mylib",
  96. expectedPath: (0, path_1.join)("/root", "location", "mylib", "kalle.ts"),
  97. extensions: defaultExtensionsWhenRunningInTsNode,
  98. },
  99. {
  100. name: "should resolve from main field in package.json (js)",
  101. absoluteBaseUrl: "/root",
  102. paths: { "lib/*": ["location/*"] },
  103. existingFiles: [(0, path_1.join)("/root", "location", "mylib.js", "kalle.js")],
  104. packageJson: { main: "./kalle.js" },
  105. requestedModule: "lib/mylib.js",
  106. extensions: [".ts", ".js"],
  107. expectedPath: (0, path_1.join)("/root", "location", "mylib.js", "kalle.js"),
  108. },
  109. {
  110. name: "should resolve from list of fields by priority in package.json",
  111. absoluteBaseUrl: "/root/",
  112. paths: { "lib/*": ["location/*"] },
  113. mainFields: ["missing", "browser", "main"],
  114. packageJson: { main: "./main.js", browser: "./browser.js" },
  115. existingFiles: [
  116. (0, path_1.join)("/root", "location", "mylibjs", "main.js"),
  117. (0, path_1.join)("/root", "location", "mylibjs", "browser.js"), // browserFilePath
  118. ],
  119. extensions: [".ts", ".js"],
  120. requestedModule: "lib/mylibjs",
  121. expectedPath: (0, path_1.join)("/root", "location", "mylibjs", "browser.js"),
  122. },
  123. {
  124. name: "should ignore field mappings to missing files in package.json",
  125. absoluteBaseUrl: "/root/",
  126. paths: { "lib/*": ["location/*"] },
  127. mainFields: ["browser", "main"],
  128. existingFiles: [(0, path_1.join)("/root", "location", "mylibjs", "kalle.js")],
  129. requestedModule: "lib/mylibjs",
  130. packageJson: {
  131. main: "./kalle.js",
  132. browser: "./nope.js",
  133. },
  134. extensions: [".ts", ".js"],
  135. expectedPath: (0, path_1.join)("/root", "location", "mylibjs", "kalle.js"),
  136. },
  137. {
  138. name: "should ignore advanced field mappings in package.json",
  139. absoluteBaseUrl: "/root/",
  140. paths: { "lib/*": ["location/*"] },
  141. existingFiles: [
  142. (0, path_1.join)("/root", "location", "mylibjs", "kalle.js"),
  143. (0, path_1.join)("/root", "location", "mylibjs", "browser.js"),
  144. ],
  145. requestedModule: "lib/mylibjs",
  146. packageJson: {
  147. main: "./kalle.js",
  148. browser: { mylibjs: "./browser.js", "./kalle.js": "./browser.js" },
  149. },
  150. extensions: [".ts", ".js"],
  151. expectedPath: (0, path_1.join)("/root", "location", "mylibjs", "kalle.js"),
  152. },
  153. {
  154. name: "should resolve to with the help of baseUrl when not explicitly set",
  155. absoluteBaseUrl: "/root/",
  156. paths: {},
  157. existingFiles: [(0, path_1.join)("/root", "mylib", "index.ts")],
  158. requestedModule: "mylib",
  159. expectedPath: (0, path_1.dirname)((0, path_1.join)("/root", "mylib", "index.ts")),
  160. extensions: defaultExtensionsWhenRunningInTsNode,
  161. },
  162. {
  163. name: "should not resolve with the help of baseUrl when asked not to",
  164. absoluteBaseUrl: "/root/",
  165. paths: {},
  166. addMatchAll: false,
  167. existingFiles: [(0, path_1.join)("/root", "mylib", "index.ts")],
  168. requestedModule: "mylib",
  169. expectedPath: undefined,
  170. extensions: defaultExtensionsWhenRunningInTsNode,
  171. },
  172. {
  173. name: "should not locate path that does not match",
  174. absoluteBaseUrl: "/root/",
  175. paths: { "lib/*": ["location/*"] },
  176. existingFiles: [(0, path_1.join)("root", "location", "mylib")],
  177. requestedModule: "mylib",
  178. expectedPath: undefined,
  179. extensions: defaultExtensionsWhenRunningInTsNode,
  180. },
  181. {
  182. name: "should not resolve typings file (index.d.ts)",
  183. absoluteBaseUrl: "/root/",
  184. paths: {
  185. "lib/*": ["location/*"],
  186. },
  187. existingFiles: [(0, path_1.join)("/root", "location", "mylib", "index.d.ts")],
  188. requestedModule: "lib/mylib",
  189. expectedPath: undefined,
  190. extensions: defaultExtensionsWhenRunningInTsNode,
  191. },
  192. {
  193. name: "should resolve main file with cjs file extension",
  194. absoluteBaseUrl: "/root/",
  195. paths: {},
  196. existingFiles: [(0, path_1.join)("/root", "mylib", "index.cjs")],
  197. packageJson: {
  198. main: "./index.cjs",
  199. },
  200. requestedModule: "mylib",
  201. expectedPath: (0, path_1.join)("/root", "mylib", "index.cjs"),
  202. extensions: defaultExtensionsWhenRunningInTsNode,
  203. },
  204. ];
  205. //# sourceMappingURL=match-path-data.js.map