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] Fully resolve imports in ESM target #3975

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 23 additions & 3 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// @ts-check
/**
* @typedef {import('@babel/core')} babel
*/

const productionPlugins = [
['babel-plugin-react-remove-properties', { properties: ['data-mui-test'] }],
];

/** @type {babel.ConfigFunction} */
module.exports = function getBabelConfig(api) {
const useCommonjs = api.env(['node']);
const useESModules = !api.env(['node']);

const presets = [
[
Expand All @@ -12,7 +18,7 @@ module.exports = function getBabelConfig(api) {
bugfixes: true,
browserslistEnv: process.env.BABEL_ENV || process.env.NODE_ENV,
debug: process.env.MUI_BUILD_VERBOSE === 'true',
modules: useCommonjs ? 'commonjs' : false,
modules: useESModules ? false : 'commonjs',
},
],
[
Expand All @@ -24,11 +30,14 @@ module.exports = function getBabelConfig(api) {
'@babel/preset-typescript',
];

const outFileExtension = '.js';

/** @type {babel.PluginItem[]} */
const plugins = [
[
'@babel/plugin-transform-runtime',
{
useESModules: !useCommonjs,
useESModules,
// any package needs to declare 7.4.4 as a runtime dependency. default is ^7.0.0
version: '^7.4.4',
},
Expand All @@ -45,6 +54,17 @@ module.exports = function getBabelConfig(api) {
plugins.push(...productionPlugins);
}

if (useESModules) {
plugins.push([
'@mui/internal-babel-plugin-resolve-imports',
{
// Don't replace the extension when we're using aliases.
// Essentially only replace in production builds.
outExtension: outFileExtension,
},
]);
}

return {
assumptions: {
noDocumentAll: true,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@babel/preset-env": "7.25.3",
"@babel/preset-react": "7.24.7",
"@babel/preset-typescript": "7.24.7",
"@mui/internal-babel-plugin-resolve-imports": "1.0.16-dev.20240822-123416-f4a704707a",
"@mui/internal-docs-utils": "1.0.11",
"@mui/internal-markdown": "1.0.11",
"@mui/internal-scripts": "1.0.17",
Expand Down
17 changes: 15 additions & 2 deletions pnpm-lock.yaml

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

Loading