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