Skip to content

Commit

Permalink
[code-infra] Fully resolve imports in ESM target (#3975)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored Aug 23, 2024
1 parent 0fec340 commit e4044bc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
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.

0 comments on commit e4044bc

Please sign in to comment.