@mui/internal-babel-plugin-resolve-imports 1.0.16-dev.20240822-123416-f4a704707a
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/LICENSE +21 -0
- package/README.md +33 -0
- package/index.js +115 -0
- package/package.json +30 -0
- package/resolve.d.ts +6 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014 Call-Em-All
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# @mui/internal-babel-plugin-resolve-imports
|
|
2
|
+
|
|
3
|
+
A babel plugin that resolves import specifiers that are created under the Node.js resolution algorithm to specifiers that adhere to ESM resolution algorithm.
|
|
4
|
+
|
|
5
|
+
See https://nodejs.org/docs/v20.16.0/api/esm.html#mandatory-file-extensions
|
|
6
|
+
|
|
7
|
+
> A file extension must be provided when using the import keyword to resolve relative or absolute specifiers. Directory indexes (For example './startup/index.js') must also be fully specified.
|
|
8
|
+
>
|
|
9
|
+
> This behavior matches how import behaves in browser environments, assuming a typically configured server.
|
|
10
|
+
|
|
11
|
+
This changes imports in the build output from
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
// packages/mui-material/build/index.js
|
|
15
|
+
export * from './Accordion';
|
|
16
|
+
|
|
17
|
+
// packages/mui-material/build/Breadcrumbs/BreadcrumbCollapsed.js
|
|
18
|
+
import MoreHorizIcon from '../internal/svg-icons/MoreHoriz';
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
to
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
// packages/mui-material/build/index.js
|
|
25
|
+
export * from './Accordion/index.js';
|
|
26
|
+
|
|
27
|
+
// packages/mui-material/build/Breadcrumbs/BreadcrumbCollapsed.js
|
|
28
|
+
import MoreHorizIcon from '../internal/svg-icons/MoreHoriz.js';
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## options
|
|
32
|
+
|
|
33
|
+
- `outExtension`: The extension to use when writing the output. Careful: if not specified, this plugin does not replace extensions at all, your bundles will likely be broken. We left this optional to allow for using this plugin together with the aliasing to source that we do everywhere. That way we can keep it in the pipeline even when not strictly necessary.
|
package/index.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/// <reference path="./resolve.d.ts" />
|
|
3
|
+
|
|
4
|
+
const nodePath = require('path');
|
|
5
|
+
const resolve = require('resolve/sync');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {import('@babel/core')} babel
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Normalize a file path to POSIX in order for it to be platform-agnostic.
|
|
13
|
+
* @param {string} importPath
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
16
|
+
function toPosixPath(importPath) {
|
|
17
|
+
return nodePath.normalize(importPath).split(nodePath.sep).join(nodePath.posix.sep);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Converts a file path to a node import specifier.
|
|
22
|
+
* @param {string} importPath
|
|
23
|
+
* @returns {string}
|
|
24
|
+
*/
|
|
25
|
+
function pathToNodeImportSpecifier(importPath) {
|
|
26
|
+
const normalized = toPosixPath(importPath);
|
|
27
|
+
return normalized.startsWith('/') || normalized.startsWith('.') ? normalized : `./${normalized}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @typedef {{ outExtension?: string }} Options
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {babel} file
|
|
36
|
+
* @param {Options} options
|
|
37
|
+
* @returns {babel.PluginObj}
|
|
38
|
+
*/
|
|
39
|
+
module.exports = function plugin({ types: t }, { outExtension }) {
|
|
40
|
+
/** @type {Map<string, string>} */
|
|
41
|
+
const cache = new Map();
|
|
42
|
+
const extensions = ['.ts', '.tsx', '.js', '.jsx'];
|
|
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
|
+
|
|
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
|
+
}
|
|
59
|
+
|
|
60
|
+
const source =
|
|
61
|
+
/** @type {babel.NodePath<babel.types.StringLiteral | null | undefined> } */ (
|
|
62
|
+
path.get('source')
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
if (!source.node) {
|
|
66
|
+
// Ignore import without source
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const importedPath = source.node.value;
|
|
71
|
+
|
|
72
|
+
if (!importedPath.startsWith('.')) {
|
|
73
|
+
// Only handle relative imports
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!state.filename) {
|
|
78
|
+
throw new Error('filename is not defined');
|
|
79
|
+
}
|
|
80
|
+
|
|
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);
|
|
85
|
+
|
|
86
|
+
let resolvedPath = cache.get(absoluteImportPath);
|
|
87
|
+
|
|
88
|
+
if (!resolvedPath) {
|
|
89
|
+
// resolve to actual file
|
|
90
|
+
resolvedPath = resolve(absoluteImportPath, { extensions });
|
|
91
|
+
|
|
92
|
+
if (!resolvedPath) {
|
|
93
|
+
throw new Error(`could not resolve "${importedPath}" from "${state.filename}"`);
|
|
94
|
+
}
|
|
95
|
+
|
|
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
|
+
}
|
|
104
|
+
|
|
105
|
+
cache.set(absoluteImportPath, resolvedPath);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const relativeResolvedPath = nodePath.relative(importerDir, resolvedPath);
|
|
109
|
+
const importSpecifier = pathToNodeImportSpecifier(relativeResolvedPath);
|
|
110
|
+
|
|
111
|
+
source.replaceWith(t.stringLiteral(importSpecifier));
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mui/internal-babel-plugin-resolve-imports",
|
|
3
|
+
"version": "1.0.16-dev.20240822-123416-f4a704707a",
|
|
4
|
+
"author": "MUI Team",
|
|
5
|
+
"description": "babel plugin that resolves import specifiers to their actual output file.",
|
|
6
|
+
"main": "./index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/mui/material-ui.git",
|
|
13
|
+
"directory": "packages-internal/babel-plugin-resolve-imports"
|
|
14
|
+
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"resolve": "^1.22.8"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/resolve": "^1.20.6",
|
|
21
|
+
"@types/babel__core": "^7.20.5"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"@babel/core": "7"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {}
|
|
30
|
+
}
|