@modern-js/server-utils 2.26.0 → 2.26.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/index.js +56 -0
  2. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/log.js +13 -0
  3. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/mapPath.js +28 -0
  4. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/mapToRelative.js +21 -0
  5. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/normalizeOptions.js +157 -0
  6. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/resolvePath.js +92 -0
  7. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/call.js +24 -0
  8. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/import.js +19 -0
  9. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/utils.js +98 -0
  10. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/call.test.js +92 -0
  11. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/custom-call.test.js +73 -0
  12. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/dynamicImport.test.js +74 -0
  13. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/import.test.js +118 -0
  14. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/index.test.js +863 -0
  15. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/mapToRelative.test.js +21 -0
  16. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/normalizeOptions.test.js +50 -0
  17. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/example-file.js +1 -0
  18. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/node_modules/first/index.js +1 -0
  19. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/node_modules/second/index.js +1 -0
  20. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/package.json +1 -0
  21. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/actions/something.js +1 -0
  22. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/app.js +1 -0
  23. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/App.js +1 -0
  24. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/SubHeader.js +1 -0
  25. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/header.css +0 -0
  26. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/index.js +1 -0
  27. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Root.js +1 -0
  28. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/SidebarFooterButton.js +1 -0
  29. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/index.js +1 -0
  30. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/index.js +1 -0
  31. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants/actions.js +1 -0
  32. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants.js +1 -0
  33. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/libs/custom.modernizr3.js +1 -0
  34. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/nodemodule.js +1 -0
  35. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.android.js +1 -0
  36. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.ios.js +1 -0
  37. package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/test/tools.js +12 -0
  38. package/dist/cjs/compilers/babel/preset/alias.js +2 -2
  39. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/index.js +41 -0
  40. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/log.js +8 -0
  41. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/mapPath.js +17 -0
  42. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/mapToRelative.js +10 -0
  43. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/normalizeOptions.js +162 -0
  44. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/resolvePath.js +86 -0
  45. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/call.js +15 -0
  46. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/import.js +8 -0
  47. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/utils.js +57 -0
  48. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/call.test.js +87 -0
  49. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/custom-call.test.js +68 -0
  50. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/dynamicImport.test.js +71 -0
  51. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/import.test.js +124 -0
  52. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/index.test.js +862 -0
  53. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/mapToRelative.test.js +16 -0
  54. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/normalizeOptions.test.js +45 -0
  55. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/example-file.js +0 -0
  56. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/node_modules/first/index.js +0 -0
  57. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/node_modules/second/index.js +0 -0
  58. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/package.json +1 -0
  59. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/actions/something.js +0 -0
  60. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/app.js +0 -0
  61. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/App.js +0 -0
  62. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/SubHeader.js +0 -0
  63. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/header.css +0 -0
  64. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/index.js +0 -0
  65. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Root.js +0 -0
  66. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/SidebarFooterButton.js +0 -0
  67. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/index.js +0 -0
  68. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/index.js +0 -0
  69. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants/actions.js +0 -0
  70. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants.js +0 -0
  71. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/libs/custom.modernizr3.js +0 -0
  72. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/nodemodule.js +0 -0
  73. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.android.js +0 -0
  74. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.ios.js +0 -0
  75. package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/test/tools.js +2 -0
  76. package/dist/esm/compilers/babel/preset/alias.js +2 -2
  77. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/index.js +37 -0
  78. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/log.js +3 -0
  79. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/mapPath.js +17 -0
  80. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/mapToRelative.js +10 -0
  81. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/normalizeOptions.js +146 -0
  82. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/resolvePath.js +81 -0
  83. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/call.js +13 -0
  84. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/import.js +8 -0
  85. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/utils.js +57 -0
  86. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/call.test.js +87 -0
  87. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/custom-call.test.js +68 -0
  88. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/dynamicImport.test.js +69 -0
  89. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/import.test.js +113 -0
  90. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/index.test.js +857 -0
  91. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/mapToRelative.test.js +16 -0
  92. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/normalizeOptions.test.js +45 -0
  93. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/example-file.js +0 -0
  94. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/node_modules/first/index.js +0 -0
  95. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/node_modules/second/index.js +0 -0
  96. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/package.json +1 -0
  97. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/actions/something.js +0 -0
  98. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/app.js +0 -0
  99. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/App.js +0 -0
  100. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/SubHeader.js +0 -0
  101. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/header.css +0 -0
  102. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/index.js +0 -0
  103. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Root.js +0 -0
  104. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/SidebarFooterButton.js +0 -0
  105. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/index.js +0 -0
  106. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/index.js +0 -0
  107. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants/actions.js +0 -0
  108. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants.js +0 -0
  109. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/libs/custom.modernizr3.js +0 -0
  110. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/nodemodule.js +0 -0
  111. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.android.js +0 -0
  112. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.ios.js +0 -0
  113. package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/test/tools.js +2 -0
  114. package/dist/esm-node/compilers/babel/preset/alias.js +2 -2
  115. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/index.d.ts +19 -0
  116. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/log.d.ts +1 -0
  117. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/mapPath.d.ts +1 -0
  118. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/mapToRelative.d.ts +1 -0
  119. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/normalizeOptions.d.ts +24 -0
  120. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/resolvePath.d.ts +1 -0
  121. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/call.d.ts +1 -0
  122. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/import.d.ts +1 -0
  123. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/utils.d.ts +9 -0
  124. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/call.test.d.ts +1 -0
  125. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/custom-call.test.d.ts +1 -0
  126. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/dynamicImport.test.d.ts +1 -0
  127. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/import.test.d.ts +1 -0
  128. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/index.test.d.ts +1 -0
  129. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/mapToRelative.test.d.ts +1 -0
  130. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/normalizeOptions.test.d.ts +1 -0
  131. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/example-file.d.ts +0 -0
  132. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/actions/something.d.ts +0 -0
  133. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/app.d.ts +0 -0
  134. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/App.d.ts +0 -0
  135. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/SubHeader.d.ts +0 -0
  136. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/index.d.ts +0 -0
  137. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Root.d.ts +0 -0
  138. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/SidebarFooterButton.d.ts +0 -0
  139. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/index.d.ts +0 -0
  140. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/index.d.ts +0 -0
  141. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants/actions.d.ts +0 -0
  142. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants.d.ts +0 -0
  143. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/libs/custom.modernizr3.d.ts +0 -0
  144. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/nodemodule.d.mts +1 -0
  145. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.android.d.ts +0 -0
  146. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.ios.d.ts +0 -0
  147. package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/test/tools.d.ts +1 -0
  148. package/package.json +13 -4
  149. package/test-setup.js +7 -0
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
6
+ const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
7
+ const _mapToRelative = /* @__PURE__ */ _interop_require_default._(require("../src/mapToRelative"));
8
+ describe("mapToRelative", () => {
9
+ describe("should map to relative path with a custom cwd", () => {
10
+ it("with a relative filename", () => {
11
+ const currentFile = "./utils/test/file.js";
12
+ const result = (0, _mapToRelative.default)(_path.default.resolve("./test"), currentFile, "utils/dep");
13
+ expect(result).toBe("../dep");
14
+ });
15
+ it("with an absolute filename", () => {
16
+ const currentFile = _path.default.resolve("./utils/test/file.js");
17
+ const result = (0, _mapToRelative.default)(_path.default.resolve("."), currentFile, "utils/dep");
18
+ expect(result).toBe("../dep");
19
+ });
20
+ });
21
+ });
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
6
+ const _normalizeOptions = /* @__PURE__ */ _interop_require_default._(require("../src/normalizeOptions"));
7
+ describe("normalizeOptions", () => {
8
+ beforeEach(() => {
9
+ _normalizeOptions.default.resetRecomputations();
10
+ });
11
+ it("should return the memoized options when the dirnames are the same", () => {
12
+ const options = {};
13
+ const result = (0, _normalizeOptions.default)("path/a.js", options);
14
+ const result2 = (0, _normalizeOptions.default)("path/b.js", options);
15
+ expect(result).toBe(result2);
16
+ expect(_normalizeOptions.default.recomputations()).toEqual(1);
17
+ });
18
+ it("should return the memoized options when the special paths are the same", () => {
19
+ const options = {};
20
+ const result = (0, _normalizeOptions.default)("unknown", options);
21
+ const result2 = (0, _normalizeOptions.default)("unknown", options);
22
+ expect(result).toBe(result2);
23
+ expect(_normalizeOptions.default.recomputations()).toEqual(1);
24
+ });
25
+ it("should recompute when the options object is not the same", () => {
26
+ const options = {};
27
+ const options2 = {};
28
+ const result = (0, _normalizeOptions.default)("path/to/file.js", options);
29
+ const result2 = (0, _normalizeOptions.default)("path/to/file.js", options2);
30
+ expect(result).not.toBe(result2);
31
+ expect(_normalizeOptions.default.recomputations()).toEqual(2);
32
+ });
33
+ it("should correctly normalize alias option if it is an array", () => {
34
+ const options = {
35
+ alias: [
36
+ {
37
+ foo: "A",
38
+ bar: "B"
39
+ },
40
+ {
41
+ baz: "C"
42
+ }
43
+ ]
44
+ };
45
+ const { alias } = (0, _normalizeOptions.default)("path/to/file.js", options);
46
+ expect(alias[0][0]).toEqual(/^foo(\/.*|)$/);
47
+ expect(alias[1][0]).toEqual(/^bar(\/.*|)$/);
48
+ expect(alias[2][0]).toEqual(/^baz(\/.*|)$/);
49
+ });
50
+ });
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return x;
9
+ }
10
+ });
11
+ function x() {
12
+ }
@@ -12,7 +12,7 @@ const _interop_require_default = require("@swc/helpers/_/_interop_require_defaul
12
12
  const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
13
13
  const _tsconfigpaths = require("@modern-js/utils/tsconfig-paths");
14
14
  const _utils = require("@modern-js/utils");
15
- const { resolvePath } = require("babel-plugin-module-resolver");
15
+ const { resolvePath } = require("../plugins/babel-plugin-module-resolver/src");
16
16
  const defaultPaths = {
17
17
  "@": [
18
18
  "./src"
@@ -73,7 +73,7 @@ const aliasPlugin = (alias) => {
73
73
  ".mjs"
74
74
  ];
75
75
  return [
76
- "babel-plugin-module-resolver",
76
+ "../plugins/babel-plugin-module-resolver/src",
77
77
  {
78
78
  root: absoluteBaseUrl,
79
79
  alias: mergedPaths,
@@ -0,0 +1,41 @@
1
+ import normalizeOptions from "./normalizeOptions";
2
+ import resolvePath from "./resolvePath";
3
+ import transformCall from "./transformers/call";
4
+ import transformImport from "./transformers/import";
5
+ export { resolvePath };
6
+ var importVisitors = {
7
+ CallExpression: transformCall,
8
+ "ImportDeclaration|ExportDeclaration": transformImport
9
+ };
10
+ var visitor = {
11
+ Program: {
12
+ enter: function enter(programPath, state) {
13
+ programPath.traverse(importVisitors, state);
14
+ },
15
+ exit: function exit(programPath, state) {
16
+ programPath.traverse(importVisitors, state);
17
+ }
18
+ }
19
+ };
20
+ export default function(param) {
21
+ var types = param.types;
22
+ return {
23
+ name: "module-resolver",
24
+ manipulateOptions: function manipulateOptions(opts) {
25
+ if (opts.filename === void 0) {
26
+ opts.filename = "unknown";
27
+ }
28
+ },
29
+ pre: function pre(file) {
30
+ this.types = types;
31
+ var currentFile = file.opts.filename;
32
+ this.normalizedOpts = normalizeOptions(currentFile, this.opts);
33
+ this.moduleResolverVisited = /* @__PURE__ */ new Set();
34
+ },
35
+ visitor: visitor,
36
+ post: function post() {
37
+ this.moduleResolverVisited.clear();
38
+ }
39
+ };
40
+ }
41
+ ;
@@ -0,0 +1,8 @@
1
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
2
+ export function warn() {
3
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
4
+ args[_key] = arguments[_key];
5
+ }
6
+ var _console;
7
+ (_console = console).warn.apply(_console, _to_consumable_array(args));
8
+ }
@@ -0,0 +1,17 @@
1
+ import defaultResolvePath from "./resolvePath";
2
+ export default function mapPathString(nodePath, state) {
3
+ if (!state.types.isStringLiteral(nodePath)) {
4
+ return;
5
+ }
6
+ var sourcePath = nodePath.node.value;
7
+ var currentFile = state.file.opts.filename;
8
+ var resolvePath = state.normalizedOpts.customResolvePath || defaultResolvePath;
9
+ var modulePath = resolvePath(sourcePath, currentFile, state.opts);
10
+ if (modulePath) {
11
+ if (nodePath.node.pathResolved) {
12
+ return;
13
+ }
14
+ nodePath.replaceWith(state.types.stringLiteral(modulePath));
15
+ nodePath.node.pathResolved = true;
16
+ }
17
+ }
@@ -0,0 +1,10 @@
1
+ import path from "path";
2
+ import { toPosixPath } from "./utils";
3
+ export default function mapToRelative(cwd, currentFile, module) {
4
+ var from = path.dirname(currentFile);
5
+ var to = path.normalize(module);
6
+ from = path.resolve(cwd, from);
7
+ to = path.resolve(cwd, to);
8
+ var moduleMapped = path.relative(from, to);
9
+ return toPosixPath(moduleMapped);
10
+ }
@@ -0,0 +1,162 @@
1
+ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
2
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
3
+ import fs from "fs";
4
+ import path from "path";
5
+ import { createSelector } from "reselect";
6
+ import findBabelConfig from "find-babel-config";
7
+ import glob from "glob";
8
+ import pkgUp from "pkg-up";
9
+ import { escapeRegExp } from "./utils";
10
+ var defaultExtensions = [
11
+ ".js",
12
+ ".jsx",
13
+ ".es",
14
+ ".es6",
15
+ ".mjs"
16
+ ];
17
+ var defaultTransformedFunctions = [
18
+ "require",
19
+ "require.resolve",
20
+ "System.import",
21
+ // Jest methods
22
+ "jest.genMockFromModule",
23
+ "jest.mock",
24
+ "jest.unmock",
25
+ "jest.doMock",
26
+ "jest.dontMock",
27
+ "jest.setMock",
28
+ "jest.requireActual",
29
+ "jest.requireMock",
30
+ // Older Jest methods
31
+ "require.requireActual",
32
+ "require.requireMock"
33
+ ];
34
+ function isRegExp(string) {
35
+ return string.startsWith("^") || string.endsWith("$");
36
+ }
37
+ var specialCwd = {
38
+ babelrc: function(startPath) {
39
+ return findBabelConfig.sync(startPath).file;
40
+ },
41
+ packagejson: function(startPath) {
42
+ return pkgUp.sync({
43
+ cwd: startPath
44
+ });
45
+ }
46
+ };
47
+ function normalizeCwd(optsCwd, currentFile) {
48
+ var cwd;
49
+ if (optsCwd in specialCwd) {
50
+ var startPath = currentFile === "unknown" ? "./" : currentFile;
51
+ var computedCwd = specialCwd[optsCwd](startPath);
52
+ cwd = computedCwd ? path.dirname(computedCwd) : null;
53
+ } else {
54
+ cwd = optsCwd;
55
+ }
56
+ return cwd || process.cwd();
57
+ }
58
+ function normalizeRoot(optsRoot, cwd) {
59
+ if (!optsRoot) {
60
+ return [];
61
+ }
62
+ var rootArray = Array.isArray(optsRoot) ? optsRoot : [
63
+ optsRoot
64
+ ];
65
+ return rootArray.map(function(dirPath) {
66
+ return path.resolve(cwd, dirPath);
67
+ }).reduce(function(resolvedDirs, absDirPath) {
68
+ if (glob.hasMagic(absDirPath)) {
69
+ var roots = glob.sync(absDirPath).filter(function(resolvedPath) {
70
+ return fs.lstatSync(resolvedPath).isDirectory();
71
+ });
72
+ return _to_consumable_array(resolvedDirs).concat(_to_consumable_array(roots));
73
+ }
74
+ return _to_consumable_array(resolvedDirs).concat([
75
+ absDirPath
76
+ ]);
77
+ }, []);
78
+ }
79
+ function getAliasTarget(key, isKeyRegExp) {
80
+ var regExpPattern = isKeyRegExp ? key : "^".concat(escapeRegExp(key), "(/.*|)$");
81
+ return new RegExp(regExpPattern);
82
+ }
83
+ function getAliasSubstitute(value, isKeyRegExp) {
84
+ if (typeof value === "function") {
85
+ return value;
86
+ }
87
+ if (!isKeyRegExp) {
88
+ return function(param) {
89
+ var _param = _sliced_to_array(param, 2), match = _param[1];
90
+ if (Array.isArray(value)) {
91
+ return value.map(function(v) {
92
+ return "".concat(v).concat(match);
93
+ });
94
+ }
95
+ return "".concat(value).concat(match);
96
+ };
97
+ }
98
+ var parts = value.split("\\\\");
99
+ return function(execResult) {
100
+ return parts.map(function(part) {
101
+ return part.replace(/\\\d+/g, function(number) {
102
+ return execResult[number.slice(1)] || "";
103
+ });
104
+ }).join("\\");
105
+ };
106
+ }
107
+ function normalizeAlias(optsAlias) {
108
+ if (!optsAlias) {
109
+ return [];
110
+ }
111
+ var aliasArray = Array.isArray(optsAlias) ? optsAlias : [
112
+ optsAlias
113
+ ];
114
+ return aliasArray.reduce(function(aliasPairs, alias) {
115
+ var aliasKeys = Object.keys(alias);
116
+ aliasKeys.forEach(function(key) {
117
+ var isKeyRegExp = isRegExp(key);
118
+ aliasPairs.push([
119
+ getAliasTarget(key, isKeyRegExp),
120
+ getAliasSubstitute(alias[key], isKeyRegExp)
121
+ ]);
122
+ });
123
+ return aliasPairs;
124
+ }, []);
125
+ }
126
+ function normalizeTransformedFunctions(optsTransformFunctions) {
127
+ if (!optsTransformFunctions) {
128
+ return defaultTransformedFunctions;
129
+ }
130
+ return _to_consumable_array(defaultTransformedFunctions).concat(_to_consumable_array(optsTransformFunctions));
131
+ }
132
+ function normalizeLoglevel(optsLoglevel) {
133
+ return optsLoglevel || "warn";
134
+ }
135
+ export default createSelector(
136
+ // The currentFile should have an extension; otherwise it's considered a special value
137
+ function(currentFile) {
138
+ return currentFile.includes(".") ? path.dirname(currentFile) : currentFile;
139
+ },
140
+ function(_, opts) {
141
+ return opts;
142
+ },
143
+ function(currentFile, opts) {
144
+ var cwd = normalizeCwd(opts.cwd, currentFile);
145
+ var root = normalizeRoot(opts.root, cwd);
146
+ var alias = normalizeAlias(opts.alias);
147
+ var loglevel = normalizeLoglevel(opts.loglevel);
148
+ var transformFunctions = normalizeTransformedFunctions(opts.transformFunctions);
149
+ var extensions = opts.extensions || defaultExtensions;
150
+ var stripExtensions = opts.stripExtensions || extensions;
151
+ return {
152
+ cwd: cwd,
153
+ root: root,
154
+ alias: alias,
155
+ loglevel: loglevel,
156
+ transformFunctions: transformFunctions,
157
+ extensions: extensions,
158
+ stripExtensions: stripExtensions,
159
+ customResolvePath: opts.resolvePath
160
+ };
161
+ }
162
+ );
@@ -0,0 +1,86 @@
1
+ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
2
+ import path from "path";
3
+ import { warn } from "./log";
4
+ import mapToRelative from "./mapToRelative";
5
+ import normalizeOptions from "./normalizeOptions";
6
+ import { nodeResolvePath, replaceExtension, isRelativePath, toLocalPath, toPosixPath } from "./utils";
7
+ function getRelativePath(sourcePath, currentFile, absFileInRoot, opts) {
8
+ var realSourceFileExtension = path.extname(absFileInRoot);
9
+ var sourceFileExtension = path.extname(sourcePath);
10
+ var relativePath = mapToRelative(opts.cwd, currentFile, absFileInRoot);
11
+ if (realSourceFileExtension !== sourceFileExtension) {
12
+ relativePath = replaceExtension(relativePath, opts);
13
+ }
14
+ return toLocalPath(toPosixPath(relativePath));
15
+ }
16
+ function findPathInRoots(sourcePath, param) {
17
+ var extensions = param.extensions, root = param.root;
18
+ var resolvedSourceFile;
19
+ root.some(function(basedir) {
20
+ resolvedSourceFile = nodeResolvePath("./".concat(sourcePath), basedir, extensions);
21
+ return resolvedSourceFile !== null;
22
+ });
23
+ return resolvedSourceFile;
24
+ }
25
+ function resolvePathFromRootConfig(sourcePath, currentFile, opts) {
26
+ var absFileInRoot = findPathInRoots(sourcePath, opts);
27
+ if (!absFileInRoot) {
28
+ return null;
29
+ }
30
+ return getRelativePath(sourcePath, currentFile, absFileInRoot, opts);
31
+ }
32
+ function checkIfPackageExists(modulePath, currentFile, extensions, loglevel) {
33
+ var resolvedPath = nodeResolvePath(modulePath, currentFile, extensions);
34
+ if (resolvedPath === null && loglevel !== "silent") {
35
+ warn('Could not resolve "'.concat(modulePath, '" in file ').concat(currentFile, "."));
36
+ }
37
+ }
38
+ function resolvePathFromAliasConfig(sourcePath, currentFile, opts) {
39
+ var aliasedSourceFile;
40
+ opts.alias.find(function(param) {
41
+ var _param = _sliced_to_array(param, 2), regExp = _param[0], substitute = _param[1];
42
+ var execResult = regExp.exec(sourcePath);
43
+ if (execResult === null) {
44
+ return false;
45
+ }
46
+ aliasedSourceFile = substitute(execResult);
47
+ return true;
48
+ });
49
+ if (!aliasedSourceFile) {
50
+ return null;
51
+ }
52
+ if (Array.isArray(aliasedSourceFile)) {
53
+ return aliasedSourceFile.map(function(asf) {
54
+ if (isRelativePath(asf)) {
55
+ return toLocalPath(toPosixPath(mapToRelative(opts.cwd, currentFile, asf)));
56
+ }
57
+ return asf;
58
+ }).find(function(src) {
59
+ return nodeResolvePath(src, path.dirname(currentFile), opts.extensions);
60
+ });
61
+ }
62
+ if (isRelativePath(aliasedSourceFile)) {
63
+ return toLocalPath(toPosixPath(mapToRelative(opts.cwd, currentFile, aliasedSourceFile)));
64
+ }
65
+ if (process.env.NODE_ENV !== "production") {
66
+ checkIfPackageExists(aliasedSourceFile, currentFile, opts.extensions, opts.loglevel);
67
+ }
68
+ return aliasedSourceFile;
69
+ }
70
+ var resolvers = [
71
+ resolvePathFromAliasConfig,
72
+ resolvePathFromRootConfig
73
+ ];
74
+ export default function resolvePath(sourcePath, currentFile, opts) {
75
+ if (isRelativePath(sourcePath)) {
76
+ return sourcePath;
77
+ }
78
+ var normalizedOpts = normalizeOptions(currentFile, opts);
79
+ var absoluteCurrentFile = path.resolve(currentFile);
80
+ var resolvedPath = null;
81
+ resolvers.some(function(resolver) {
82
+ resolvedPath = resolver(sourcePath, absoluteCurrentFile, normalizedOpts);
83
+ return resolvedPath !== null;
84
+ });
85
+ return resolvedPath;
86
+ }
@@ -0,0 +1,15 @@
1
+ import { matchesPattern, isImportCall } from "../utils";
2
+ import mapPathString from "../mapPath";
3
+ export default function transformCall(nodePath, state) {
4
+ if (state.moduleResolverVisited.has(nodePath)) {
5
+ return;
6
+ }
7
+ var calleePath = nodePath.get("callee");
8
+ var isNormalCall = state.normalizedOpts.transformFunctions.some(function(pattern) {
9
+ return matchesPattern(state.types, calleePath, pattern);
10
+ });
11
+ if (isNormalCall || isImportCall(state.types, nodePath)) {
12
+ state.moduleResolverVisited.add(nodePath);
13
+ mapPathString(nodePath.get("arguments.0"), state);
14
+ }
15
+ }
@@ -0,0 +1,8 @@
1
+ import mapPathString from "../mapPath";
2
+ export default function transformImport(nodePath, state) {
3
+ if (state.moduleResolverVisited.has(nodePath)) {
4
+ return;
5
+ }
6
+ state.moduleResolverVisited.add(nodePath);
7
+ mapPathString(nodePath.get("source"), state);
8
+ }
@@ -0,0 +1,57 @@
1
+ import path from "path";
2
+ import resolve from "resolve";
3
+ export function nodeResolvePath(modulePath, basedir, extensions) {
4
+ try {
5
+ return resolve.sync(modulePath, {
6
+ basedir: basedir,
7
+ extensions: extensions
8
+ });
9
+ } catch (e) {
10
+ return null;
11
+ }
12
+ }
13
+ export function isRelativePath(nodePath) {
14
+ return nodePath.match(/^\.?\.\//);
15
+ }
16
+ export function toPosixPath(modulePath) {
17
+ return modulePath.replace(/\\/g, "/");
18
+ }
19
+ export function toLocalPath(modulePath) {
20
+ var localPath = modulePath.replace(/\/index$/, "");
21
+ if (!isRelativePath(localPath)) {
22
+ localPath = "./".concat(localPath);
23
+ }
24
+ return localPath;
25
+ }
26
+ export function stripExtension(modulePath, stripExtensions) {
27
+ var name = path.basename(modulePath);
28
+ stripExtensions.some(function(extension) {
29
+ if (name.endsWith(extension)) {
30
+ name = name.slice(0, name.length - extension.length);
31
+ return true;
32
+ }
33
+ return false;
34
+ });
35
+ return name;
36
+ }
37
+ export function replaceExtension(modulePath, opts) {
38
+ var filename = stripExtension(modulePath, opts.stripExtensions);
39
+ return path.join(path.dirname(modulePath), filename);
40
+ }
41
+ export function matchesPattern(types, calleePath, pattern) {
42
+ var node = calleePath.node;
43
+ if (types.isMemberExpression(node)) {
44
+ return calleePath.matchesPattern(pattern);
45
+ }
46
+ if (!types.isIdentifier(node) || pattern.includes(".")) {
47
+ return false;
48
+ }
49
+ var name = pattern.split(".")[0];
50
+ return node.name === name;
51
+ }
52
+ export function isImportCall(types, calleePath) {
53
+ return types.isImport(calleePath.node.callee);
54
+ }
55
+ export function escapeRegExp(string) {
56
+ return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
57
+ }