diff --git a/.codesandbox/ci.json b/.codesandbox/ci.json
index ec72fd1039c4d5..e360d73477e088 100644
--- a/.codesandbox/ci.json
+++ b/.codesandbox/ci.json
@@ -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"
@@ -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",
diff --git a/babel.config.js b/babel.config.js
index 5940d3b7bb9542..54f27f79d69dd4 100644
--- a/babel.config.js
+++ b/babel.config.js
@@ -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']);
@@ -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',
@@ -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') {
@@ -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: {
diff --git a/package.json b/package.json
index 95f5a49d317b00..a2288b5c911b70 100644
--- a/package.json
+++ b/package.json
@@ -121,6 +121,7 @@
"@octokit/rest": "^21.0.2",
"@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",
diff --git a/packages-internal/babel-plugin-resolve-imports/README.md b/packages-internal/babel-plugin-resolve-imports/README.md
new file mode 100644
index 00000000000000..87964bf6f1f8b2
--- /dev/null
+++ b/packages-internal/babel-plugin-resolve-imports/README.md
@@ -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.
diff --git a/packages-internal/babel-plugin-resolve-imports/index.js b/packages-internal/babel-plugin-resolve-imports/index.js
new file mode 100644
index 00000000000000..68ac472f19c54e
--- /dev/null
+++ b/packages-internal/babel-plugin-resolve-imports/index.js
@@ -0,0 +1,115 @@
+// @ts-check
+///
+
+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} */
+ 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 } */ (
+ 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));
+ },
+ },
+ };
+};
diff --git a/packages-internal/babel-plugin-resolve-imports/package.json b/packages-internal/babel-plugin-resolve-imports/package.json
new file mode 100644
index 00000000000000..2959bfe315a75c
--- /dev/null
+++ b/packages-internal/babel-plugin-resolve-imports/package.json
@@ -0,0 +1,30 @@
+{
+ "name": "@mui/internal-babel-plugin-resolve-imports",
+ "version": "1.0.16",
+ "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"
+ }
+}
diff --git a/packages-internal/babel-plugin-resolve-imports/resolve.d.ts b/packages-internal/babel-plugin-resolve-imports/resolve.d.ts
new file mode 100644
index 00000000000000..7a911b17ce4bea
--- /dev/null
+++ b/packages-internal/babel-plugin-resolve-imports/resolve.d.ts
@@ -0,0 +1,6 @@
+declare module 'resolve/sync' {
+ import { Opts } from 'resolve';
+
+ function resolve(id: string, options?: Opts): string;
+ export = resolve;
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index f5694791e7cd7d..d65a5f1a0dcba3 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -116,6 +116,9 @@ importers:
'@playwright/test':
specifier: 1.46.1
version: 1.46.1
+ '@types/babel__core':
+ specifier: ^7.20.5
+ version: 7.20.5
'@types/fs-extra':
specifier: ^11.0.4
version: 11.0.4
@@ -900,6 +903,22 @@ importers:
specifier: ^17.7.2
version: 17.7.2
+ packages-internal/babel-plugin-resolve-imports:
+ dependencies:
+ '@babel/core':
+ specifier: ^7.25.2
+ version: 7.25.2
+ resolve:
+ specifier: ^1.22.8
+ version: 1.22.8
+ devDependencies:
+ '@types/babel__core':
+ specifier: ^7.20.5
+ version: 7.20.5
+ '@types/resolve':
+ specifier: ^1.20.6
+ version: 1.20.6
+
packages-internal/docs-utils:
dependencies:
rimraf:
@@ -5510,6 +5529,9 @@ packages:
'@types/react@18.3.3':
resolution: {integrity: sha512-hti/R0pS0q1/xx+TsI73XIqk26eBsISZ2R0wUijXIngRK9R/e7Xw/cXVxQK7R5JjW+SV4zGcn5hXjudkN/pLIw==}
+ '@types/resolve@1.20.6':
+ resolution: {integrity: sha512-A4STmOXPhMUtHH+S6ymgE2GiBSMqf4oTvcQZMcHzokuTLVYzXTB8ttjcgxOVaAp2lGwEdzZ0J+cRbbeevQj1UQ==}
+
'@types/retry@0.12.0':
resolution: {integrity: sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==}
@@ -16549,6 +16571,8 @@ snapshots:
'@types/prop-types': 15.7.12
csstype: 3.1.3
+ '@types/resolve@1.20.6': {}
+
'@types/retry@0.12.0': {}
'@types/send@0.17.1':