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
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .codesandbox/ci.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"packages/mui-system",
"packages/mui-types",
"packages/mui-utils",
"packages-internal/babel-plugin-resolve-imports",
"packages-internal/docs-utils",
"packages-internal/scripts",
"packages-internal/test-utils"
Expand All @@ -36,6 +37,7 @@
"@mui/internal-docs-utils": "packages-internal/docs-utils",
"@mui/internal-markdown": "packages/markdown",
"@mui/internal-scripts": "packages-internal/scripts",
"@mui/internal-babel-plugin-resolve-imports": "packages-internal/babel-plugin-resolve-imports",
"@mui/lab": "packages/mui-lab/build",
"@mui/material-nextjs": "packages/mui-material-nextjs/build",
"@mui/material": "packages/mui-material/build",
Expand Down
37 changes: 37 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
// @ts-check
const path = require('path');

/**
* @typedef {import('@babel/core')} babel
*/

const errorCodesPath = path.resolve(__dirname, './docs/public/static/error-codes.json');
const missingError = process.env.MUI_EXTRACT_ERROR_CODES === 'true' ? 'write' : 'annotate';

/**
* @param {string} relativeToBabelConf
* @returns {string}
*/
function resolveAliasPath(relativeToBabelConf) {
const resolvedPath = path.relative(process.cwd(), path.resolve(__dirname, relativeToBabelConf));
return `./${resolvedPath.replace('\\', '/')}`;
}

/** @type {babel.PluginItem[]} */
const productionPlugins = [
['babel-plugin-react-remove-properties', { properties: ['data-mui-test'] }],
];

/** @type {babel.ConfigFunction} */
module.exports = function getBabelConfig(api) {
const useESModules = api.env(['regressions', 'modern', 'stable']);

Expand Down Expand Up @@ -56,6 +67,16 @@ module.exports = function getBabelConfig(api) {
'@babel/preset-typescript',
];

const usesAliases =
// in this config:
api.env(['coverage', 'development', 'test', 'benchmark']) ||
process.env.NODE_ENV === 'test' ||
// in webpack config:
api.env(['regressions']);

const outFileExtension = '.js';

/** @type {babel.PluginItem[]} */
const plugins = [
[
'babel-plugin-macros',
Expand Down Expand Up @@ -94,6 +115,18 @@ module.exports = function getBabelConfig(api) {
],
},
],
...(useESModules
? [
[
'@mui/internal-babel-plugin-resolve-imports',
{
// Don't replace the extension when we're using aliases.
// Essentially only replace in production builds.
outExtension: usesAliases ? null : outFileExtension,
},
],
]
: []),
];

if (process.env.NODE_ENV === 'production') {
Expand Down Expand Up @@ -121,6 +154,10 @@ module.exports = function getBabelConfig(api) {
exclude: /\.test\.(js|ts|tsx)$/,
plugins: ['@babel/plugin-transform-react-constant-elements'],
},
{
test: /(\.test\.[^.]+$|\.test\/)/,
plugins: [['@mui/internal-babel-plugin-resolve-imports', false]],
},
],
env: {
coverage: {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"@octokit/rest": "^20.1.1",
"@pigment-css/react": "0.0.20",
"@playwright/test": "1.46.1",
"@types/babel__core": "^7.20.5",
"@types/fs-extra": "^11.0.4",
"@types/lodash": "^4.17.7",
"@types/mocha": "^10.0.7",
Expand Down
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 (For example './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

View workflow job for this annotation

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

View workflow job for this annotation

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

View workflow job for this annotation

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

View workflow job for this annotation

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"}
115 changes: 115 additions & 0 deletions packages-internal/babel-plugin-resolve-imports/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// @ts-check
/// <reference path="./resolve.d.ts" />

const nodePath = require('path');
const resolve = require('resolve/sync');

/**
* @typedef {import('@babel/core')} babel
*/

/**
* Normalize a file path to POSIX in order for it to be platform-agnostic.
* @param {string} importPath
* @returns {string}
*/
function toPosixPath(importPath) {
return nodePath.normalize(importPath).split(nodePath.sep).join(nodePath.posix.sep);
}

/**
* Converts a file path to a node import specifier.
* @param {string} importPath
* @returns {string}
*/
function pathToNodeImportSpecifier(importPath) {
const normalized = toPosixPath(importPath);
return normalized.startsWith('/') || normalized.startsWith('.') ? normalized : `./${normalized}`;
}

/**
* @typedef {{ outExtension?: string }} Options
*/

/**
* @param {babel} file
* @param {Options} options
* @returns {babel.PluginObj}
*/
module.exports = function plugin({ types: t }, { outExtension }) {
/** @type {Map<string, string>} */
const cache = new Map();
const extensions = ['.ts', '.tsx', '.js', '.jsx'];
const extensionsSet = new Set(extensions);
return {
visitor: {
ImportOrExportDeclaration(path, state) {
if (path.isExportDefaultDeclaration()) {
// Can't export default from an import specifier
return;
}

if (
(path.isExportDeclaration() && path.node.exportKind === 'type') ||
(path.isImportDeclaration() && path.node.importKind === 'type')
) {
// Ignore type imports, they will get compiled away anyway
return;
}

const source =
/** @type {babel.NodePath<babel.types.StringLiteral | null | undefined> } */ (
path.get('source')
);

if (!source.node) {
// Ignore import without source
return;
}

const importedPath = source.node.value;

if (!importedPath.startsWith('.')) {
// Only handle relative imports
return;
}

if (!state.filename) {
throw new Error('filename is not defined');
}

const importerPath = state.filename;
const importerDir = nodePath.dirname(importerPath);
// start from fully resolved import path
const absoluteImportPath = nodePath.resolve(importerDir, importedPath);

let resolvedPath = cache.get(absoluteImportPath);

if (!resolvedPath) {
// resolve to actual file
resolvedPath = resolve(absoluteImportPath, { extensions });

if (!resolvedPath) {
throw new Error(`could not resolve "${importedPath}" from "${state.filename}"`);
}

const resolvedExtension = nodePath.extname(resolvedPath);
if (outExtension && extensionsSet.has(resolvedExtension)) {
// replace extension
resolvedPath = nodePath.resolve(
nodePath.dirname(resolvedPath),
nodePath.basename(resolvedPath, resolvedExtension) + outExtension,
);
}

cache.set(absoluteImportPath, resolvedPath);
}

const relativeResolvedPath = nodePath.relative(importerDir, resolvedPath);
const importSpecifier = pathToNodeImportSpecifier(relativeResolvedPath);

source.replaceWith(t.stringLiteral(importSpecifier));
},
},
};
};
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"
}
}
6 changes: 6 additions & 0 deletions packages-internal/babel-plugin-resolve-imports/resolve.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module 'resolve/sync' {
import { Opts } from 'resolve';

function resolve(id: string, options?: Opts): string;
export = resolve;
}
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

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

Loading