@mui/internal-babel-plugin-resolve-imports 1.0.19 → 2.0.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/index.js +63 -55
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -41,74 +41,82 @@ module.exports = function plugin({ types: t }, { outExtension }) {
|
|
|
41
41
|
const cache = new Map();
|
|
42
42
|
const extensions = ['.ts', '.tsx', '.js', '.jsx'];
|
|
43
43
|
const extensionsSet = new Set(extensions);
|
|
44
|
-
return {
|
|
45
|
-
visitor: {
|
|
46
|
-
ImportOrExportDeclaration(path, state) {
|
|
47
|
-
if (path.isExportDefaultDeclaration()) {
|
|
48
|
-
// Can't export default from an import specifier
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
44
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
45
|
+
/**
|
|
46
|
+
*
|
|
47
|
+
* @param {babel.NodePath<babel.types.StringLiteral>} importSource
|
|
48
|
+
* @param {babel.PluginPass} state
|
|
49
|
+
*/
|
|
50
|
+
function doResolve(importSource, state) {
|
|
51
|
+
const importedPath = importSource.node.value;
|
|
59
52
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
53
|
+
if (!importedPath.startsWith('.')) {
|
|
54
|
+
// Only handle relative imports
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
64
57
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
58
|
+
if (!state.filename) {
|
|
59
|
+
throw new Error('filename is not defined');
|
|
60
|
+
}
|
|
69
61
|
|
|
70
|
-
|
|
62
|
+
const importerPath = state.filename;
|
|
63
|
+
const importerDir = nodePath.dirname(importerPath);
|
|
64
|
+
// start from fully resolved import path
|
|
65
|
+
const absoluteImportPath = nodePath.resolve(importerDir, importedPath);
|
|
71
66
|
|
|
72
|
-
|
|
73
|
-
// Only handle relative imports
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
67
|
+
let resolvedPath = cache.get(absoluteImportPath);
|
|
76
68
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
69
|
+
if (!resolvedPath) {
|
|
70
|
+
// resolve to actual file
|
|
71
|
+
resolvedPath = resolve(absoluteImportPath, { extensions });
|
|
80
72
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const absoluteImportPath = nodePath.resolve(importerDir, importedPath);
|
|
73
|
+
if (!resolvedPath) {
|
|
74
|
+
throw new Error(`could not resolve "${importedPath}" from "${state.filename}"`);
|
|
75
|
+
}
|
|
85
76
|
|
|
86
|
-
|
|
77
|
+
const resolvedExtension = nodePath.extname(resolvedPath);
|
|
78
|
+
if (outExtension && extensionsSet.has(resolvedExtension)) {
|
|
79
|
+
// replace extension
|
|
80
|
+
resolvedPath = nodePath.resolve(
|
|
81
|
+
nodePath.dirname(resolvedPath),
|
|
82
|
+
nodePath.basename(resolvedPath, resolvedExtension) + outExtension,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
87
85
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
resolvedPath = resolve(absoluteImportPath, { extensions });
|
|
86
|
+
cache.set(absoluteImportPath, resolvedPath);
|
|
87
|
+
}
|
|
91
88
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
89
|
+
const relativeResolvedPath = nodePath.relative(importerDir, resolvedPath);
|
|
90
|
+
const importSpecifier = pathToNodeImportSpecifier(relativeResolvedPath);
|
|
95
91
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
// replace extension
|
|
99
|
-
resolvedPath = nodePath.resolve(
|
|
100
|
-
nodePath.dirname(resolvedPath),
|
|
101
|
-
nodePath.basename(resolvedPath, resolvedExtension) + outExtension,
|
|
102
|
-
);
|
|
103
|
-
}
|
|
92
|
+
importSource.replaceWith(t.stringLiteral(importSpecifier));
|
|
93
|
+
}
|
|
104
94
|
|
|
105
|
-
|
|
95
|
+
return {
|
|
96
|
+
visitor: {
|
|
97
|
+
TSImportType(path, state) {
|
|
98
|
+
const source = path.get('argument');
|
|
99
|
+
doResolve(source, state);
|
|
100
|
+
},
|
|
101
|
+
ImportExpression(path, state) {
|
|
102
|
+
const source = path.get('source');
|
|
103
|
+
if (source.isStringLiteral()) {
|
|
104
|
+
doResolve(source, state);
|
|
106
105
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
106
|
+
},
|
|
107
|
+
ImportDeclaration(path, state) {
|
|
108
|
+
const source = path.get('source');
|
|
109
|
+
doResolve(source, state);
|
|
110
|
+
},
|
|
111
|
+
ExportNamedDeclaration(path, state) {
|
|
112
|
+
const source = path.get('source');
|
|
113
|
+
if (source.isStringLiteral()) {
|
|
114
|
+
doResolve(source, state);
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
ExportAllDeclaration(path, state) {
|
|
118
|
+
const source = path.get('source');
|
|
119
|
+
doResolve(source, state);
|
|
112
120
|
},
|
|
113
121
|
},
|
|
114
122
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mui/internal-babel-plugin-resolve-imports",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"author": "MUI Team",
|
|
5
5
|
"description": "babel plugin that resolves import specifiers to their actual output file.",
|
|
6
6
|
"main": "./index.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
},
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"resolve": "^1.22.
|
|
17
|
+
"resolve": "^1.22.10"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@types/babel__core": "^7.20.5",
|