@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.
- package/CHANGELOG.md +10 -0
- package/dist/cjs/common/index.js +4 -4
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/index.js +56 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/log.js +13 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/mapPath.js +28 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/mapToRelative.js +21 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/normalizeOptions.js +157 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/resolvePath.js +92 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/call.js +24 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/import.js +19 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/src/utils.js +98 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/call.test.js +92 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/custom-call.test.js +73 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/dynamicImport.test.js +74 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/import.test.js +118 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/index.test.js +863 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/mapToRelative.test.js +21 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/normalizeOptions.test.js +50 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/example-file.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/node_modules/first/index.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/node_modules/second/index.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/package.json +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/actions/something.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/app.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/App.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/SubHeader.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/header.css +0 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/index.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Root.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/SidebarFooterButton.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/index.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/index.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants/actions.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/libs/custom.modernizr3.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/nodemodule.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.android.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.ios.js +1 -0
- package/dist/cjs/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/test/tools.js +12 -0
- package/dist/cjs/compilers/babel/preset/alias.js +2 -2
- package/dist/cjs/compilers/typescript/index.js +3 -4
- package/dist/esm/common/index.js +20 -10
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/index.js +41 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/log.js +8 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/mapPath.js +17 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/mapToRelative.js +10 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/normalizeOptions.js +162 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/resolvePath.js +86 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/call.js +15 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/import.js +8 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/src/utils.js +57 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/call.test.js +87 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/custom-call.test.js +68 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/dynamicImport.test.js +71 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/import.test.js +124 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/index.test.js +862 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/mapToRelative.test.js +16 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/normalizeOptions.test.js +45 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/example-file.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/node_modules/first/index.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/node_modules/second/index.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/package.json +1 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/actions/something.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/app.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/App.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/SubHeader.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/header.css +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/index.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Root.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/SidebarFooterButton.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/index.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/index.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants/actions.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/libs/custom.modernizr3.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/nodemodule.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.android.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.ios.js +0 -0
- package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/test/tools.js +2 -0
- package/dist/esm/compilers/babel/preset/alias.js +2 -2
- package/dist/esm/compilers/typescript/index.js +14 -15
- package/dist/esm-node/common/index.js +2 -2
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/index.js +37 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/log.js +3 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/mapPath.js +17 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/mapToRelative.js +10 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/normalizeOptions.js +146 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/resolvePath.js +81 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/call.js +13 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/import.js +8 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/utils.js +57 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/call.test.js +87 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/custom-call.test.js +68 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/dynamicImport.test.js +69 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/import.test.js +113 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/index.test.js +857 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/mapToRelative.test.js +16 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/normalizeOptions.test.js +45 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/example-file.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/node_modules/first/index.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/node_modules/second/index.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/package.json +1 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/actions/something.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/app.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/App.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/SubHeader.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/header.css +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/index.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Root.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/SidebarFooterButton.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/index.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/index.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants/actions.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/libs/custom.modernizr3.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/nodemodule.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.android.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.ios.js +0 -0
- package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/test/tools.js +2 -0
- package/dist/esm-node/compilers/babel/preset/alias.js +2 -2
- package/dist/esm-node/compilers/typescript/index.js +13 -14
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/index.d.ts +19 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/log.d.ts +1 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/mapPath.d.ts +1 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/mapToRelative.d.ts +1 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/normalizeOptions.d.ts +24 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/resolvePath.d.ts +1 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/call.d.ts +1 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/import.d.ts +1 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/src/utils.d.ts +9 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/call.test.d.ts +1 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/custom-call.test.d.ts +1 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/dynamicImport.test.d.ts +1 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/import.test.d.ts +1 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/index.test.d.ts +1 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/mapToRelative.test.d.ts +1 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/normalizeOptions.test.d.ts +1 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/example-file.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/actions/something.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/app.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/App.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/SubHeader.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Header/index.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Root.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/SidebarFooterButton.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/Sidebar/Footer/index.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/components/index.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants/actions.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/constants.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/libs/custom.modernizr3.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/nodemodule.d.mts +1 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.android.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/rn/index.ios.d.ts +0 -0
- package/dist/types/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/test/tools.d.ts +1 -0
- package/package.json +19 -10
- package/test-setup.js +7 -0
package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/mapToRelative.test.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
});
|
package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/normalizeOptions.test.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
});
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{}
|
|
File without changes
|
package/dist/esm/compilers/babel/plugins/babel-plugin-module-resolver/test/testproject/src/app.js
ADDED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -4,7 +4,7 @@ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
|
4
4
|
import path from "path";
|
|
5
5
|
import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
|
|
6
6
|
import { getUserAlias } from "@modern-js/utils";
|
|
7
|
-
var resolvePath = require("babel-plugin-module-resolver").resolvePath;
|
|
7
|
+
var resolvePath = require("../plugins/babel-plugin-module-resolver/src").resolvePath;
|
|
8
8
|
var defaultPaths = {
|
|
9
9
|
"@": [
|
|
10
10
|
"./src"
|
|
@@ -56,7 +56,7 @@ export var aliasPlugin = function(alias) {
|
|
|
56
56
|
".mjs"
|
|
57
57
|
];
|
|
58
58
|
return [
|
|
59
|
-
"babel-plugin-module-resolver",
|
|
59
|
+
"../plugins/babel-plugin-module-resolver/src",
|
|
60
60
|
{
|
|
61
61
|
root: absoluteBaseUrl,
|
|
62
62
|
alias: mergedPaths,
|
|
@@ -4,11 +4,10 @@ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
|
4
4
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
5
5
|
import path from "path";
|
|
6
6
|
import { logger, getAliasConfig, fs } from "@modern-js/utils";
|
|
7
|
-
import ts from "typescript";
|
|
8
7
|
import { TypescriptLoader } from "./typescriptLoader";
|
|
9
8
|
import { tsconfigPathsBeforeHookFactory } from "./tsconfigPathsPlugin";
|
|
10
|
-
var readTsConfigByFile = function(tsConfigFile) {
|
|
11
|
-
var parsedCmd =
|
|
9
|
+
var readTsConfigByFile = function(tsConfigFile, tsInstance) {
|
|
10
|
+
var parsedCmd = tsInstance.getParsedCommandLineOfConfigFile(tsConfigFile, void 0, tsInstance.sys);
|
|
12
11
|
var options = parsedCmd.options, fileNames = parsedCmd.fileNames, projectReferences = parsedCmd.projectReferences;
|
|
13
12
|
return {
|
|
14
13
|
options: options,
|
|
@@ -60,7 +59,7 @@ var copyFiles = function() {
|
|
|
60
59
|
}();
|
|
61
60
|
export var compileByTs = function() {
|
|
62
61
|
var _ref = _async_to_generator(function(appDirectory, config, compileOptions) {
|
|
63
|
-
var sourceDirs, distDir, tsconfigPath,
|
|
62
|
+
var sourceDirs, distDir, tsconfigPath, ts, createProgram, formatHost, alias, aliasOption, _aliasOption_paths, paths, _aliasOption_absoluteBaseUrl, absoluteBaseUrl, _readTsConfigByFile, options, fileNames, projectReferences, sourcePosixPaths, rootNames, program, tsconfigPathsPlugin, emitResult, allDiagnostics, noEmitOnError, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, source, err;
|
|
64
63
|
return _ts_generator(this, function(_state) {
|
|
65
64
|
switch (_state.label) {
|
|
66
65
|
case 0:
|
|
@@ -71,18 +70,18 @@ export var compileByTs = function() {
|
|
|
71
70
|
2
|
|
72
71
|
];
|
|
73
72
|
}
|
|
74
|
-
|
|
73
|
+
ts = new TypescriptLoader({
|
|
75
74
|
appDirectory: appDirectory
|
|
76
75
|
}).load();
|
|
77
|
-
createProgram =
|
|
78
|
-
formatHost = getFormatHost(
|
|
76
|
+
createProgram = ts.createIncrementalProgram || ts.createProgram;
|
|
77
|
+
formatHost = getFormatHost(ts);
|
|
79
78
|
alias = config.alias;
|
|
80
79
|
aliasOption = getAliasConfig(alias, {
|
|
81
80
|
appDirectory: appDirectory,
|
|
82
81
|
tsconfigPath: tsconfigPath
|
|
83
82
|
});
|
|
84
83
|
_aliasOption_paths = aliasOption.paths, paths = _aliasOption_paths === void 0 ? {} : _aliasOption_paths, _aliasOption_absoluteBaseUrl = aliasOption.absoluteBaseUrl, absoluteBaseUrl = _aliasOption_absoluteBaseUrl === void 0 ? "./" : _aliasOption_absoluteBaseUrl;
|
|
85
|
-
_readTsConfigByFile = readTsConfigByFile(tsconfigPath), options = _readTsConfigByFile.options, fileNames = _readTsConfigByFile.fileNames, projectReferences = _readTsConfigByFile.projectReferences;
|
|
84
|
+
_readTsConfigByFile = readTsConfigByFile(tsconfigPath, ts), options = _readTsConfigByFile.options, fileNames = _readTsConfigByFile.fileNames, projectReferences = _readTsConfigByFile.projectReferences;
|
|
86
85
|
sourcePosixPaths = sourceDirs.map(function(sourceDir) {
|
|
87
86
|
return sourceDir.split(path.sep).join(path.posix.sep);
|
|
88
87
|
});
|
|
@@ -91,7 +90,7 @@ export var compileByTs = function() {
|
|
|
91
90
|
return fileName.includes(sourceDir);
|
|
92
91
|
});
|
|
93
92
|
});
|
|
94
|
-
program = createProgram.call(
|
|
93
|
+
program = createProgram.call(ts, {
|
|
95
94
|
rootNames: rootNames,
|
|
96
95
|
projectReferences: projectReferences,
|
|
97
96
|
options: _object_spread({
|
|
@@ -99,16 +98,16 @@ export var compileByTs = function() {
|
|
|
99
98
|
outDir: distDir
|
|
100
99
|
}, options)
|
|
101
100
|
});
|
|
102
|
-
tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(
|
|
101
|
+
tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
|
|
103
102
|
emitResult = program.emit(void 0, void 0, void 0, void 0, {
|
|
104
103
|
before: [
|
|
105
104
|
tsconfigPathsPlugin
|
|
106
105
|
]
|
|
107
106
|
});
|
|
108
|
-
allDiagnostics =
|
|
107
|
+
allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
109
108
|
noEmitOnError = options.noEmitOnError;
|
|
110
109
|
if (allDiagnostics.length > 0) {
|
|
111
|
-
logger.error(
|
|
110
|
+
logger.error(ts.formatDiagnosticsWithColorAndContext(_to_consumable_array(new Set(allDiagnostics)), formatHost));
|
|
112
111
|
if (typeof noEmitOnError === "undefined" || noEmitOnError === true) {
|
|
113
112
|
process.exit(1);
|
|
114
113
|
}
|
|
@@ -182,14 +181,14 @@ export var compileByTs = function() {
|
|
|
182
181
|
return _ref.apply(this, arguments);
|
|
183
182
|
};
|
|
184
183
|
}();
|
|
185
|
-
var getFormatHost = function(
|
|
184
|
+
var getFormatHost = function(ts) {
|
|
186
185
|
return {
|
|
187
186
|
getCanonicalFileName: function(path2) {
|
|
188
187
|
return path2;
|
|
189
188
|
},
|
|
190
|
-
getCurrentDirectory:
|
|
189
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
191
190
|
getNewLine: function() {
|
|
192
|
-
return
|
|
191
|
+
return ts.sys.newLine;
|
|
193
192
|
}
|
|
194
193
|
};
|
|
195
194
|
};
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import * as path from "path";
|
|
2
2
|
import { fs } from "@modern-js/utils";
|
|
3
|
-
import { compileByTs } from "../compilers/typescript";
|
|
4
|
-
import { compileByBabel } from "../compilers/babel";
|
|
5
3
|
export const FILE_EXTENSIONS = [
|
|
6
4
|
".js",
|
|
7
5
|
".ts",
|
|
@@ -24,8 +22,10 @@ export const compile = async (appDirectory, modernConfig, compileOptions) => {
|
|
|
24
22
|
const compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig_server = modernConfig.server) === null || _modernConfig_server === void 0 ? void 0 : _modernConfig_server.compiler;
|
|
25
23
|
const isTsProject = tsconfigPath && await fs.pathExists(tsconfigPath);
|
|
26
24
|
if (!isTsProject || compiler === "babel") {
|
|
25
|
+
const { compileByBabel } = await import("../compilers/babel");
|
|
27
26
|
await compileByBabel(appDirectory, modernConfig, compileOptions);
|
|
28
27
|
} else {
|
|
28
|
+
const { compileByTs } = await import("../compilers/typescript");
|
|
29
29
|
await compileByTs(appDirectory, modernConfig, compileOptions);
|
|
30
30
|
}
|
|
31
31
|
};
|
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
});
|
|
@@ -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
|
+
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
|
+
}
|
package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/mapToRelative.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
}
|
package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/normalizeOptions.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
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
|
+
);
|
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
}
|
package/dist/esm-node/compilers/babel/plugins/babel-plugin-module-resolver/src/transformers/call.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
}
|
|
@@ -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,
|
|
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
|
+
}
|