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