@mui/internal-babel-plugin-resolve-imports 1.0.20 → 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.
Files changed (2) hide show
  1. package/index.js +63 -55
  2. package/package.json +1 -1
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
- if (
53
- (path.isExportDeclaration() && path.node.exportKind === 'type') ||
54
- (path.isImportDeclaration() && path.node.importKind === 'type')
55
- ) {
56
- // Ignore type imports, they will get compiled away anyway
57
- return;
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
- const source =
61
- /** @type {babel.NodePath<babel.types.StringLiteral | null | undefined> } */ (
62
- path.get('source')
63
- );
53
+ if (!importedPath.startsWith('.')) {
54
+ // Only handle relative imports
55
+ return;
56
+ }
64
57
 
65
- if (!source.node) {
66
- // Ignore import without source
67
- return;
68
- }
58
+ if (!state.filename) {
59
+ throw new Error('filename is not defined');
60
+ }
69
61
 
70
- const importedPath = source.node.value;
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
- if (!importedPath.startsWith('.')) {
73
- // Only handle relative imports
74
- return;
75
- }
67
+ let resolvedPath = cache.get(absoluteImportPath);
76
68
 
77
- if (!state.filename) {
78
- throw new Error('filename is not defined');
79
- }
69
+ if (!resolvedPath) {
70
+ // resolve to actual file
71
+ resolvedPath = resolve(absoluteImportPath, { extensions });
80
72
 
81
- const importerPath = state.filename;
82
- const importerDir = nodePath.dirname(importerPath);
83
- // start from fully resolved import path
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
- let resolvedPath = cache.get(absoluteImportPath);
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
- if (!resolvedPath) {
89
- // resolve to actual file
90
- resolvedPath = resolve(absoluteImportPath, { extensions });
86
+ cache.set(absoluteImportPath, resolvedPath);
87
+ }
91
88
 
92
- if (!resolvedPath) {
93
- throw new Error(`could not resolve "${importedPath}" from "${state.filename}"`);
94
- }
89
+ const relativeResolvedPath = nodePath.relative(importerDir, resolvedPath);
90
+ const importSpecifier = pathToNodeImportSpecifier(relativeResolvedPath);
95
91
 
96
- const resolvedExtension = nodePath.extname(resolvedPath);
97
- if (outExtension && extensionsSet.has(resolvedExtension)) {
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
- cache.set(absoluteImportPath, resolvedPath);
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
- const relativeResolvedPath = nodePath.relative(importerDir, resolvedPath);
109
- const importSpecifier = pathToNodeImportSpecifier(relativeResolvedPath);
110
-
111
- source.replaceWith(t.stringLiteral(importSpecifier));
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": "1.0.20",
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",