@mui/internal-babel-plugin-resolve-imports 2.0.0 → 2.0.1

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 (39) hide show
  1. package/__fixtures__/basic-js-imports/input.js +19 -0
  2. package/__fixtures__/basic-js-imports/output.js +19 -0
  3. package/__fixtures__/basic-js-imports/src/components/Button.js +3 -0
  4. package/__fixtures__/basic-js-imports/src/utils.js +3 -0
  5. package/__fixtures__/declaration-files/input.d.ts +17 -0
  6. package/__fixtures__/declaration-files/output.d.ts +17 -0
  7. package/__fixtures__/declaration-files/types/components.d.ts +9 -0
  8. package/__fixtures__/declaration-files/types/components.js +8 -0
  9. package/__fixtures__/declaration-files/types/index.d.ts +2 -0
  10. package/__fixtures__/declaration-files/types/index.js +3 -0
  11. package/__fixtures__/declaration-files/types/models.d.ts +6 -0
  12. package/__fixtures__/declaration-files/types/models.js +7 -0
  13. package/__fixtures__/different-extension/input.js +10 -0
  14. package/__fixtures__/different-extension/output.js +10 -0
  15. package/__fixtures__/different-extension/src/components/Button.js +3 -0
  16. package/__fixtures__/different-extension/src/utils.js +3 -0
  17. package/__fixtures__/index-resolution/input.js +16 -0
  18. package/__fixtures__/index-resolution/output.js +16 -0
  19. package/__fixtures__/index-resolution/src/components/Button/index.js +3 -0
  20. package/__fixtures__/index-resolution/src/components/Checkbox/index.js +3 -0
  21. package/__fixtures__/index-resolution/src/components/index.js +2 -0
  22. package/__fixtures__/nested-paths/input.js +19 -0
  23. package/__fixtures__/nested-paths/output.js +19 -0
  24. package/__fixtures__/nested-paths/src/components/ui/elements/Button.js +3 -0
  25. package/__fixtures__/nested-paths/src/utils/helpers/formatting.js +3 -0
  26. package/__fixtures__/no-extension/input.js +10 -0
  27. package/__fixtures__/no-extension/output.js +10 -0
  28. package/__fixtures__/no-extension/src/components/Button.js +3 -0
  29. package/__fixtures__/no-extension/src/utils.js +3 -0
  30. package/__fixtures__/non-relative-imports/input.js +16 -0
  31. package/__fixtures__/non-relative-imports/output.js +16 -0
  32. package/__fixtures__/non-relative-imports/src/local-file.js +3 -0
  33. package/__fixtures__/typescript-imports/input.ts +14 -0
  34. package/__fixtures__/typescript-imports/output.ts +14 -0
  35. package/__fixtures__/typescript-imports/src/components/Button.tsx +7 -0
  36. package/__fixtures__/typescript-imports/src/types.ts +6 -0
  37. package/index.js +9 -0
  38. package/index.test.js +100 -0
  39. package/package.json +12 -4
@@ -0,0 +1,19 @@
1
+ // Named import
2
+ import { helper } from './src/utils';
3
+
4
+ // Default import
5
+ import Button from './src/components/Button';
6
+
7
+ // Namespace import
8
+ import * as Utils from './src/utils';
9
+
10
+ // Export from
11
+ export { helper } from './src/utils';
12
+
13
+ // Export all
14
+ export * from './src/components/Button';
15
+
16
+ // Dynamic import
17
+ const loadUtils = () => import('./src/utils');
18
+
19
+ console.log(helper, Button, Utils, loadUtils);
@@ -0,0 +1,19 @@
1
+ // Named import
2
+ import { helper } from './src/utils.js';
3
+
4
+ // Default import
5
+ import Button from './src/components/Button.js';
6
+
7
+ // Namespace import
8
+ import * as Utils from './src/utils.js';
9
+
10
+ // Export from
11
+ export { helper } from './src/utils.js';
12
+
13
+ // Export all
14
+ export * from './src/components/Button.js';
15
+
16
+ // Dynamic import
17
+ const loadUtils = () => import('./src/utils.js');
18
+
19
+ console.log(helper, Button, Utils, loadUtils);
@@ -0,0 +1,3 @@
1
+ export default function Button() {
2
+ return 'Button';
3
+ }
@@ -0,0 +1,3 @@
1
+ export function helper() {
2
+ return 'helper';
3
+ }
@@ -0,0 +1,17 @@
1
+ // Importing from declaration files
2
+ import { User, UserRole } from './types';
3
+ import { Button, ButtonProps } from './types/components';
4
+
5
+ // Type imports
6
+ import type { User as UserType } from './types/models';
7
+ import type { ButtonProps as ButtonPropsType } from './types/components';
8
+
9
+ // Re-exporting from declaration files
10
+ export { User, UserRole } from './types';
11
+ export * from './types/components';
12
+
13
+ // Type exports
14
+ export type { UserType } from './types/models';
15
+ export type { ButtonPropsType } from './types/components';
16
+
17
+ export declare const createUser: () => User;
@@ -0,0 +1,17 @@
1
+ // Importing from declaration files
2
+ import { User, UserRole } from './types/index.js';
3
+ import { Button, ButtonProps } from './types/components.js';
4
+
5
+ // Type imports
6
+ import type { User as UserType } from './types/models.js';
7
+ import type { ButtonProps as ButtonPropsType } from './types/components.js';
8
+
9
+ // Re-exporting from declaration files
10
+ export { User, UserRole } from './types/index.js';
11
+ export * from './types/components.js';
12
+
13
+ // Type exports
14
+ export type { UserType } from './types/models.js';
15
+ export type { ButtonPropsType } from './types/components.js';
16
+
17
+ export declare const createUser: () => User;
@@ -0,0 +1,9 @@
1
+ import { User } from './models';
2
+
3
+ export interface ButtonProps {
4
+ onClick?: () => void;
5
+ label: string;
6
+ user?: User;
7
+ }
8
+
9
+ export declare function Button(props: ButtonProps): JSX.Element;
@@ -0,0 +1,8 @@
1
+ // Implementation of components
2
+ import { createUser } from './models';
3
+
4
+ function Button({ label, onClick, user }) {
5
+ return { type: 'button', label, onClick, user };
6
+ }
7
+
8
+ export { Button };
@@ -0,0 +1,2 @@
1
+ export * from './models';
2
+ export * from './components';
@@ -0,0 +1,3 @@
1
+ // Re-export from index
2
+ export * from './models';
3
+ export * from './components';
@@ -0,0 +1,6 @@
1
+ export interface User {
2
+ id: number;
3
+ name: string;
4
+ }
5
+
6
+ export type UserRole = 'admin' | 'user' | 'guest';
@@ -0,0 +1,7 @@
1
+ // Implementation of models
2
+ const createUser = (id, name) => ({
3
+ id,
4
+ name,
5
+ });
6
+
7
+ export { createUser };
@@ -0,0 +1,10 @@
1
+ // Setting outExtension to .mjs in test options
2
+ import Button from './src/components/Button';
3
+ import { helper } from './src/utils';
4
+
5
+ export { helper } from './src/utils';
6
+ export * from './src/components/Button';
7
+
8
+ const loadUtils = () => import('./src/utils');
9
+
10
+ console.log(Button, helper, loadUtils);
@@ -0,0 +1,10 @@
1
+ // Setting outExtension to .mjs in test options
2
+ import Button from './src/components/Button.mjs';
3
+ import { helper } from './src/utils.mjs';
4
+
5
+ export { helper } from './src/utils.mjs';
6
+ export * from './src/components/Button.mjs';
7
+
8
+ const loadUtils = () => import('./src/utils.mjs');
9
+
10
+ console.log(Button, helper, loadUtils);
@@ -0,0 +1,3 @@
1
+ export default function Button() {
2
+ return 'Button';
3
+ }
@@ -0,0 +1,3 @@
1
+ export function helper() {
2
+ return 'helper';
3
+ }
@@ -0,0 +1,16 @@
1
+ // Import from directory (should resolve to index.js)
2
+ import Button from './src/components/Button';
3
+
4
+ // Import from directory with index.js
5
+ import { Button as ButtonAlias, Checkbox } from './src/components';
6
+
7
+ // Export from directory
8
+ export { Button } from './src/components/Button';
9
+
10
+ // Export all from directory
11
+ export * from './src/components';
12
+
13
+ // Dynamic import of directory
14
+ const loadComponents = () => import('./src/components');
15
+
16
+ console.log(Button, ButtonAlias, Checkbox, loadComponents);
@@ -0,0 +1,16 @@
1
+ // Import from directory (should resolve to index.js)
2
+ import Button from './src/components/Button/index.js';
3
+
4
+ // Import from directory with index.js
5
+ import { Button as ButtonAlias, Checkbox } from './src/components/index.js';
6
+
7
+ // Export from directory
8
+ export { Button } from './src/components/Button/index.js';
9
+
10
+ // Export all from directory
11
+ export * from './src/components/index.js';
12
+
13
+ // Dynamic import of directory
14
+ const loadComponents = () => import('./src/components/index.js');
15
+
16
+ console.log(Button, ButtonAlias, Checkbox, loadComponents);
@@ -0,0 +1,3 @@
1
+ export default function Button() {
2
+ return 'Button';
3
+ }
@@ -0,0 +1,3 @@
1
+ export function Checkbox() {
2
+ return 'Checkbox';
3
+ }
@@ -0,0 +1,2 @@
1
+ export { default as Button } from './Button';
2
+ export { Checkbox } from './Checkbox';
@@ -0,0 +1,19 @@
1
+ // Multiple level deep import
2
+ import Button from './src/components/ui/elements/Button';
3
+
4
+ // Multiple level with parent directory traversal
5
+ import { formatDate } from './src/utils/helpers/formatting';
6
+
7
+ // Export multiple levels deep
8
+ export { formatDate } from './src/utils/helpers/formatting';
9
+
10
+ // Export all from deeply nested path
11
+ export * from './src/components/ui/elements/Button';
12
+
13
+ // Dynamic import with deep path
14
+ const loadButton = () => import('./src/components/ui/elements/Button');
15
+
16
+ // Complex parent directory traversal
17
+ import { helper } from './src/components/ui/elements/../../../utils/helpers/formatting';
18
+
19
+ console.log(Button, formatDate, loadButton, helper);
@@ -0,0 +1,19 @@
1
+ // Multiple level deep import
2
+ import Button from './src/components/ui/elements/Button.js';
3
+
4
+ // Multiple level with parent directory traversal
5
+ import { formatDate } from './src/utils/helpers/formatting.js';
6
+
7
+ // Export multiple levels deep
8
+ export { formatDate } from './src/utils/helpers/formatting.js';
9
+
10
+ // Export all from deeply nested path
11
+ export * from './src/components/ui/elements/Button.js';
12
+
13
+ // Dynamic import with deep path
14
+ const loadButton = () => import('./src/components/ui/elements/Button.js');
15
+
16
+ // Complex parent directory traversal
17
+ import { helper } from './src/utils/helpers/formatting.js';
18
+
19
+ console.log(Button, formatDate, loadButton, helper);
@@ -0,0 +1,3 @@
1
+ export default function Button() {
2
+ return 'Button';
3
+ }
@@ -0,0 +1,3 @@
1
+ export function formatDate(date) {
2
+ return date.toISOString();
3
+ }
@@ -0,0 +1,10 @@
1
+ // No outExtension configured - should only resolve to full paths
2
+ import Button from './src/components/Button';
3
+ import { helper } from './src/utils';
4
+
5
+ export { helper } from './src/utils';
6
+ export * from './src/components/Button';
7
+
8
+ const loadUtils = () => import('./src/utils');
9
+
10
+ console.log(Button, helper, loadUtils);
@@ -0,0 +1,10 @@
1
+ // No outExtension configured - should only resolve to full paths
2
+ import Button from './src/components/Button.js';
3
+ import { helper } from './src/utils.js';
4
+
5
+ export { helper } from './src/utils.js';
6
+ export * from './src/components/Button.js';
7
+
8
+ const loadUtils = () => import('./src/utils.js');
9
+
10
+ console.log(Button, helper, loadUtils);
@@ -0,0 +1,3 @@
1
+ export default function Button() {
2
+ return 'Button';
3
+ }
@@ -0,0 +1,3 @@
1
+ export function helper() {
2
+ return 'helper';
3
+ }
@@ -0,0 +1,16 @@
1
+ // Non-relative imports should not be modified
2
+ import React from 'react';
3
+ import { Button } from '@mui/material';
4
+ import axios from 'axios';
5
+
6
+ // Relative import should be modified
7
+ import { localFunction } from './src/local-file';
8
+
9
+ // Re-export non-relative
10
+ export { useState } from 'react';
11
+ export * from '@mui/material';
12
+
13
+ // Dynamic import non-relative
14
+ const loadLodash = () => import('lodash');
15
+
16
+ console.log(React, Button, axios, localFunction, loadLodash);
@@ -0,0 +1,16 @@
1
+ // Non-relative imports should not be modified
2
+ import React from 'react';
3
+ import { Button } from '@mui/material';
4
+ import axios from 'axios';
5
+
6
+ // Relative import should be modified
7
+ import { localFunction } from './src/local-file.js';
8
+
9
+ // Re-export non-relative
10
+ export { useState } from 'react';
11
+ export * from '@mui/material';
12
+
13
+ // Dynamic import non-relative
14
+ const loadLodash = () => import('lodash');
15
+
16
+ console.log(React, Button, axios, localFunction, loadLodash);
@@ -0,0 +1,3 @@
1
+ export function localFunction() {
2
+ return 'local';
3
+ }
@@ -0,0 +1,14 @@
1
+ // Importing from TypeScript files
2
+ import Button from './src/components/Button';
3
+ import { User, UserRole } from './src/types';
4
+
5
+ // TypeScript type imports
6
+ import type { User as UserType } from './src/types';
7
+
8
+ // Re-exporting types
9
+ export type { UserRole } from './src/types';
10
+
11
+ // Dynamic import of TS file
12
+ const loadButton = () => import('./src/components/Button');
13
+
14
+ console.log(Button, User, UserRole, loadButton);
@@ -0,0 +1,14 @@
1
+ // Importing from TypeScript files
2
+ import Button from './src/components/Button.js';
3
+ import { User, UserRole } from './src/types.js';
4
+
5
+ // TypeScript type imports
6
+ import type { User as UserType } from './src/types.js';
7
+
8
+ // Re-exporting types
9
+ export type { UserRole } from './src/types.js';
10
+
11
+ // Dynamic import of TS file
12
+ const loadButton = () => import('./src/components/Button.js');
13
+
14
+ console.log(Button, User, UserRole, loadButton);
@@ -0,0 +1,7 @@
1
+ type ButtonProps = {
2
+ label: string;
3
+ };
4
+
5
+ export default function Button(props: ButtonProps) {
6
+ return props.label;
7
+ }
@@ -0,0 +1,6 @@
1
+ export interface User {
2
+ id: number;
3
+ name: string;
4
+ }
5
+
6
+ export type UserRole = 'admin' | 'user' | 'guest';
package/index.js CHANGED
@@ -98,6 +98,15 @@ module.exports = function plugin({ types: t }, { outExtension }) {
98
98
  const source = path.get('argument');
99
99
  doResolve(source, state);
100
100
  },
101
+ CallExpression(path, state) {
102
+ const callee = path.get('callee');
103
+ if (callee.isImport()) {
104
+ const source = path.get('arguments')[0];
105
+ if (source.isStringLiteral()) {
106
+ doResolve(source, state);
107
+ }
108
+ }
109
+ },
101
110
  ImportExpression(path, state) {
102
111
  const source = path.get('source');
103
112
  if (source.isStringLiteral()) {
package/index.test.js ADDED
@@ -0,0 +1,100 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import { pluginTester } from 'babel-plugin-tester';
4
+ import plugin from './index';
5
+
6
+ const fixturePath = path.resolve(__dirname, './__fixtures__');
7
+
8
+ function readOutputFixtureSync(fixture, file) {
9
+ // babel hardcodes the linefeed to \n and can add extra newlines
10
+ return normalizeQuotesAndWhitespace(
11
+ fs
12
+ .readFileSync(path.join(fixturePath, fixture, file), { encoding: 'utf8' })
13
+ .replace(/\r?\n/g, '\n'),
14
+ );
15
+ }
16
+
17
+ // Normalize string quotes and whitespace for comparison
18
+ function normalizeQuotesAndWhitespace(str) {
19
+ return str
20
+ .trim()
21
+ .replace(/["']/g, "'") // Convert all quotes to single quotes for comparison
22
+ .replace(/\s+\n/g, '\n'); // Normalize whitespace before line breaks
23
+ }
24
+
25
+ pluginTester({
26
+ plugin,
27
+ filepath: __filename,
28
+ formatResult: (r) => normalizeQuotesAndWhitespace(r),
29
+
30
+ tests: [
31
+ {
32
+ title: 'basic js imports',
33
+ pluginOptions: {
34
+ outExtension: '.js',
35
+ },
36
+ fixture: path.join(fixturePath, 'basic-js-imports', 'input.js'),
37
+ output: readOutputFixtureSync('basic-js-imports', 'output.js'),
38
+ },
39
+ {
40
+ title: 'typescript imports',
41
+ pluginOptions: {
42
+ outExtension: '.js',
43
+ },
44
+ babelOptions: {
45
+ plugins: [['@babel/plugin-syntax-typescript']],
46
+ },
47
+ fixture: path.join(fixturePath, 'typescript-imports', 'input.ts'),
48
+ output: readOutputFixtureSync('typescript-imports', 'output.ts'),
49
+ },
50
+ {
51
+ title: 'index resolution',
52
+ pluginOptions: {
53
+ outExtension: '.js',
54
+ },
55
+ fixture: path.join(fixturePath, 'index-resolution', 'input.js'),
56
+ output: readOutputFixtureSync('index-resolution', 'output.js'),
57
+ },
58
+ {
59
+ title: 'nested paths',
60
+ pluginOptions: {
61
+ outExtension: '.js',
62
+ },
63
+ fixture: path.join(fixturePath, 'nested-paths', 'input.js'),
64
+ output: readOutputFixtureSync('nested-paths', 'output.js'),
65
+ },
66
+ {
67
+ title: 'different output extension',
68
+ pluginOptions: {
69
+ outExtension: '.mjs',
70
+ },
71
+ fixture: path.join(fixturePath, 'different-extension', 'input.js'),
72
+ output: readOutputFixtureSync('different-extension', 'output.js'),
73
+ },
74
+ {
75
+ title: 'no output extension',
76
+ pluginOptions: {},
77
+ fixture: path.join(fixturePath, 'no-extension', 'input.js'),
78
+ output: readOutputFixtureSync('no-extension', 'output.js'),
79
+ },
80
+ {
81
+ title: 'non-relative imports',
82
+ pluginOptions: {
83
+ outExtension: '.js',
84
+ },
85
+ fixture: path.join(fixturePath, 'non-relative-imports', 'input.js'),
86
+ output: readOutputFixtureSync('non-relative-imports', 'output.js'),
87
+ },
88
+ {
89
+ title: 'declaration files',
90
+ pluginOptions: {
91
+ outExtension: '.js',
92
+ },
93
+ babelOptions: {
94
+ plugins: [['@babel/plugin-syntax-typescript', { dts: true }]],
95
+ },
96
+ fixture: path.join(fixturePath, 'declaration-files', 'input.d.ts'),
97
+ output: readOutputFixtureSync('declaration-files', 'output.d.ts'),
98
+ },
99
+ ],
100
+ });
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@mui/internal-babel-plugin-resolve-imports",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "author": "MUI Team",
5
- "description": "babel plugin that resolves import specifiers to their actual output file.",
5
+ "description": "Babel plugin that resolves import specifiers to their actual output file.",
6
6
  "main": "./index.js",
7
7
  "exports": {
8
8
  ".": "./index.js"
@@ -17,8 +17,14 @@
17
17
  "resolve": "^1.22.10"
18
18
  },
19
19
  "devDependencies": {
20
+ "@babel/plugin-syntax-typescript": "^7.27.1",
21
+ "@babel/preset-typescript": "^7.27.1",
20
22
  "@types/babel__core": "^7.20.5",
21
- "@types/resolve": "^1.20.6"
23
+ "@types/chai": "^4.3.20",
24
+ "@types/node": "^20.17.32",
25
+ "@types/resolve": "^1.20.6",
26
+ "babel-plugin-tester": "^11.0.4",
27
+ "chai": "^4.5.0"
22
28
  },
23
29
  "peerDependencies": {
24
30
  "@babel/core": "7"
@@ -26,5 +32,7 @@
26
32
  "publishConfig": {
27
33
  "access": "public"
28
34
  },
29
- "scripts": {}
35
+ "scripts": {
36
+ "test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages-internal/babel-plugin-resolve-imports/**/*.test.?(c|m)[jt]s?(x)'"
37
+ }
30
38
  }