Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[code-infra] Babel plugin to fully resolve imported paths #43294

Merged
merged 29 commits into from
Aug 22, 2024
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e26e204
Babel plugin to fully resolve imported paths
Janpot Aug 14, 2024
a2e9fe3
Update babel.config.js
Janpot Aug 14, 2024
0cf1f75
Update babel.config.js
Janpot Aug 14, 2024
e3a9915
Update babel.config.js
Janpot Aug 14, 2024
d8a432e
fw
Janpot Aug 14, 2024
08785b3
improvements
Janpot Aug 14, 2024
399ffcc
Add types
Janpot Aug 14, 2024
a1cf4e8
Update babel.config.js
Janpot Aug 14, 2024
8ceb06b
Update babel.config.js
Janpot Aug 14, 2024
25bdc7d
Update babelPluginResolveRelativeImports.js
Janpot Aug 14, 2024
d3574fd
Update babelPluginResolveRelativeImports.js
Janpot Aug 14, 2024
e26fa02
Update babelPluginResolveRelativeImports.js
Janpot Aug 14, 2024
70b8261
Update babelPluginResolveRelativeImports.js
Janpot Aug 14, 2024
16bc6cd
Update babelPluginResolveRelativeImports.js
Janpot Aug 14, 2024
3f5915c
Update babelPluginResolveRelativeImports.js
Janpot Aug 14, 2024
73bb2ce
Update babel.config.js
Janpot Aug 14, 2024
ff2c258
Update babel.config.js
Janpot Aug 14, 2024
baa5247
Update babel.config.js
Janpot Aug 14, 2024
6406fdb
fixes
Janpot Aug 14, 2024
fed743d
win
Janpot Aug 14, 2024
ef2d0fc
Update babelPluginResolveRelativeImports.js
Janpot Aug 14, 2024
baa646d
Merge branch 'next' into babel-resolve-imports
Janpot Aug 16, 2024
7e2636d
Merge branch 'next' into babel-resolve-imports
Janpot Aug 19, 2024
0f21e37
move to internal package
Janpot Aug 21, 2024
7280c93
Update README.md
Janpot Aug 21, 2024
eb63fd3
Update ci.json
Janpot Aug 21, 2024
d2c6ef5
Update ci.json
Janpot Aug 21, 2024
28db8b7
Update ci.json
Janpot Aug 21, 2024
86fae26
Update ci.json
Janpot Aug 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move to internal package
Janpot committed Aug 21, 2024
commit 0f21e377434b0390d8f3454f7e7d31a1ee495722
6 changes: 2 additions & 4 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -22,8 +22,6 @@ const productionPlugins = [
['babel-plugin-react-remove-properties', { properties: ['data-mui-test'] }],
];

const importResolverPlugin = path.resolve(__dirname, './scripts/babelPluginResolveRelativeImports');

/** @type {babel.ConfigFunction} */
module.exports = function getBabelConfig(api) {
const useESModules = api.env(['regressions', 'modern', 'stable']);
@@ -120,7 +118,7 @@ module.exports = function getBabelConfig(api) {
...(useESModules
? [
[
importResolverPlugin,
'@mui/internal-babel-plugin-resolve-imports',
{
// Don't replace the extension when we're using aliases.
// Essentially only replace in production builds.
@@ -158,7 +156,7 @@ module.exports = function getBabelConfig(api) {
},
{
test: /(\.test\.[^.]+$|\.test\/)/,
plugins: [[importResolverPlugin, false]],
plugins: [['@mui/internal-babel-plugin-resolve-imports', false]],
},
],
env: {
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -127,7 +127,6 @@
"@types/mocha": "^10.0.7",
"@types/node": "^18.19.44",
"@types/react": "^18.3.3",
"@types/resolve": "^1.20.6",
"@types/yargs": "^17.0.33",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
@@ -181,7 +180,6 @@
"prettier": "^3.3.3",
"pretty-quick": "^4.0.0",
"process": "^0.11.10",
"resolve": "^1.22.8",
"rimraf": "^5.0.10",
"serve": "^14.2.3",
"stylelint": "^15.11.0",
33 changes: 33 additions & 0 deletions packages-internal/babel-plugin-resolve-imports/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# @mui/internal-babel-plugin-resolve-imports

A babel plugin that resolves import specifiers that are created under the Node.js resolution algorithm to specifiers that adhere to ESM resolution algorithm.

See https://nodejs.org/docs/v20.16.0/api/esm.html#mandatory-file-extensions

> A file extension must be provided when using the import keyword to resolve relative or absolute specifiers. Directory indexes (e.g. './startup/index.js') must also be fully specified.
>
> This behavior matches how import behaves in browser environments, assuming a typically configured server.

This changes imports in the build output from

```tsx
// packages/mui-material/build/index.js
export * from './Accordion';

// packages/mui-material/build/Breadcrumbs/BreadcrumbCollapsed.js
import MoreHorizIcon from '../internal/svg-icons/MoreHoriz';
```

to

```tsx
// packages/mui-material/build/index.js
export * from './Accordion/index.js';

// packages/mui-material/build/Breadcrumbs/BreadcrumbCollapsed.js
import MoreHorizIcon from '../internal/svg-icons/MoreHoriz.js';
```

## options

- `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.

Check warning on line 33 in packages-internal/babel-plugin-resolve-imports/README.md

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.Will] Avoid using 'will'. Raw Output: {"message": "[Google.Will] Avoid using 'will'.", "location": {"path": "packages-internal/babel-plugin-resolve-imports/README.md", "range": {"start": {"line": 33, "column": 153}}}, "severity": "WARNING"}

Check warning on line 33 in packages-internal/babel-plugin-resolve-imports/README.md

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.We] Try to avoid using first-person plural like 'We'. Raw Output: {"message": "[Google.We] Try to avoid using first-person plural like 'We'.", "location": {"path": "packages-internal/babel-plugin-resolve-imports/README.md", "range": {"start": {"line": 33, "column": 176}}}, "severity": "WARNING"}

Check warning on line 33 in packages-internal/babel-plugin-resolve-imports/README.md

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.We] Try to avoid using first-person plural like 'we'. Raw Output: {"message": "[Google.We] Try to avoid using first-person plural like 'we'.", "location": {"path": "packages-internal/babel-plugin-resolve-imports/README.md", "range": {"start": {"line": 33, "column": 271}}}, "severity": "WARNING"}

Check warning on line 33 in packages-internal/babel-plugin-resolve-imports/README.md

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.We] Try to avoid using first-person plural like 'we'. Raw Output: {"message": "[Google.We] Try to avoid using first-person plural like 'we'.", "location": {"path": "packages-internal/babel-plugin-resolve-imports/README.md", "range": {"start": {"line": 33, "column": 298}}}, "severity": "WARNING"}
30 changes: 30 additions & 0 deletions packages-internal/babel-plugin-resolve-imports/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@mui/internal-babel-plugin-resolve-imports",
"version": "1.0.16",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's unusual initial version ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 I copied the package.json of @mui/internal-scripts and didn't give it any further thought.

"author": "MUI Team",
"description": "babel plugin that resolves import specifiers to their actual output file.",
"main": "./index.js",
"exports": {
".": "./index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/mui/material-ui.git",
"directory": "packages-internal/babel-plugin-resolve-imports"
},
"license": "MIT",
"scripts": {},
"dependencies": {
"resolve": "^1.22.8"
},
"devDependencies": {
"@types/resolve": "^1.20.6",
"@types/babel__core": "^7.20.5"
},
"peerDependencies": {
"@babel/core": "7"
},
"publishConfig": {
"access": "public"
}
}
File renamed without changes.
22 changes: 16 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.