Skip to content

Commit 34008bc

Browse files
authored
[core] Add exports field and build proper ES modules (no import extensions variant) (mui#821)
1 parent f0863a6 commit 34008bc

16 files changed

+434
-102
lines changed

.circleci/config.yml

+17
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,19 @@ jobs:
375375
name: Run danger on PRs
376376
command: pnpm danger ci --fail-on-errors
377377
# TODO test bundle size https://github.com/mui/base-ui/issues/201
378+
test_package:
379+
<<: *default-job
380+
steps:
381+
- checkout
382+
- install_js:
383+
react-version: << parameters.react-version >>
384+
- run:
385+
name: Build packages
386+
command: pnpm release:build
387+
- run:
388+
name: Are the types wrong?
389+
command: pnpm -r test:package-types
390+
378391
workflows:
379392
version: 2
380393
pipeline:
@@ -419,6 +432,10 @@ workflows:
419432
<<: *default-context
420433
requires:
421434
- checkout
435+
- test_package:
436+
<<: *default-context
437+
requires:
438+
- checkout
422439
profile:
423440
when:
424441
equal: [profile, << pipeline.parameters.workflow >>]

babel.config.js

+17-36
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@ function resolveAliasPath(relativeToBabelConf) {
88
return `./${resolvedPath.replace('\\', '/')}`;
99
}
1010

11-
const productionPlugins = [
12-
['babel-plugin-react-remove-properties', { properties: ['data-mui-test'] }],
13-
];
14-
1511
module.exports = function getBabelConfig(api) {
16-
const useESModules = api.env(['regressions', 'stable', 'rollup']);
12+
const useESModules = !api.env(['node']);
1713

1814
const defaultAlias = {
1915
'@base-ui-components/react': resolveAliasPath('./packages/react/src'),
@@ -70,9 +66,16 @@ module.exports = function getBabelConfig(api) {
7066
],
7167
];
7268

73-
if (process.env.NODE_ENV === 'production') {
74-
plugins.push(...productionPlugins);
75-
}
69+
const devPlugins = [
70+
[
71+
'babel-plugin-module-resolver',
72+
{
73+
root: ['./'],
74+
alias: defaultAlias,
75+
},
76+
],
77+
'babel-plugin-add-import-extension',
78+
];
7679

7780
return {
7881
assumptions: {
@@ -98,39 +101,17 @@ module.exports = function getBabelConfig(api) {
98101
],
99102
env: {
100103
coverage: {
101-
plugins: [
102-
'babel-plugin-istanbul',
103-
[
104-
'babel-plugin-module-resolver',
105-
{
106-
root: ['./'],
107-
alias: defaultAlias,
108-
},
109-
],
110-
],
104+
plugins: ['babel-plugin-istanbul', ...devPlugins],
111105
},
112106
development: {
113-
plugins: [
114-
[
115-
'babel-plugin-module-resolver',
116-
{
117-
root: ['./'],
118-
alias: defaultAlias,
119-
},
120-
],
121-
],
107+
plugins: devPlugins,
122108
},
123109
test: {
124110
sourceMaps: 'both',
125-
plugins: [
126-
[
127-
'babel-plugin-module-resolver',
128-
{
129-
root: ['./'],
130-
alias: defaultAlias,
131-
},
132-
],
133-
],
111+
plugins: devPlugins,
112+
},
113+
production: {
114+
plugins: ['babel-plugin-add-import-extension'],
134115
},
135116
},
136117
};

docs/vitest.config.ts docs/vitest.config.mts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mergeConfig, defineProject } from 'vitest/config';
2-
import sharedConfig from '../vitest.shared';
2+
import sharedConfig from '../vitest.shared.mts';
33

44
export default mergeConfig(
55
sharedConfig,

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"@vitest/coverage-v8": "^2.1.4",
9898
"@vitest/ui": "2.1.4",
9999
"babel-loader": "^9.2.1",
100+
"babel-plugin-add-import-extension": "1.6.0",
100101
"babel-plugin-istanbul": "^6.1.1",
101102
"babel-plugin-macros": "^3.1.0",
102103
"babel-plugin-module-resolver": "^5.0.2",
@@ -130,6 +131,7 @@
130131
"fast-glob": "^3.3.2",
131132
"fs-extra": "^11.2.0",
132133
"globby": "^14.0.2",
134+
"jsonc-parser": "^3.3.1",
133135
"karma": "^6.4.4",
134136
"karma-browserstack-launcher": "~1.6.0",
135137
"karma-chrome-launcher": "^3.2.0",
@@ -165,6 +167,7 @@
165167
"tailwindcss": "4.0.0-alpha.36",
166168
"terser": "^5.31.0",
167169
"terser-webpack-plugin": "^5.3.10",
170+
"tsc-alias": "^1.8.10",
168171
"tsx": "^4.8.2",
169172
"typescript": "^5.6.3",
170173
"unist-util-visit": "^5.0.0",

packages/react/package.json

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"private": false,
55
"author": "MUI Team",
66
"description": "Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.",
7-
"main": "./src/index.ts",
87
"keywords": [
98
"react",
109
"react-component",
@@ -26,15 +25,25 @@
2625
"type": "opencollective",
2726
"url": "https://opencollective.com/mui-org"
2827
},
28+
"exports": {
29+
".": {
30+
"import": "./src/index.ts"
31+
},
32+
"./*": {
33+
"import": "./src/*/index.ts"
34+
}
35+
},
2936
"imports": {
3037
"#test-utils": "./test/index.ts"
3138
},
39+
"type": "commonjs",
3240
"scripts": {
3341
"build": "pnpm build:node && pnpm build:stable && pnpm build:types && pnpm build:copy-files",
3442
"build:node": "node ../../scripts/build.mjs node",
3543
"build:stable": "node ../../scripts/build.mjs stable",
3644
"build:copy-files": "node ../../scripts/copyFiles.mjs",
37-
"build:types": "tsc -b tsconfig.build.json",
45+
"build:types": "tsx ../../scripts/buildTypes.mjs --project tsconfig.build.json --copy build/cjs",
46+
"test:package-types": "attw --pack ./build --include-entrypoints ./Accordion ./Menu ./Tooltip",
3847
"prebuild": "rimraf --glob build build-tests \"*.tsbuildinfo\"",
3948
"release": "pnpm build && pnpm publish",
4049
"test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/react/**/*.test.{js,ts,tsx}'",
@@ -52,6 +61,7 @@
5261
"use-sync-external-store": "^1.2.2"
5362
},
5463
"devDependencies": {
64+
"@arethetypeswrong/cli": "^0.16.4",
5565
"@mui/internal-test-utils": "https://pkg.csb.dev/mui/material-ui/commit/92c23999/@mui/internal-test-utils",
5666
"@testing-library/react": "^16.0.1",
5767
"@testing-library/user-event": "^14.5.2",
@@ -63,7 +73,6 @@
6373
"@types/sinon": "^17.0.3",
6474
"@types/use-sync-external-store": "^0.0.6",
6575
"chai": "^4.5.0",
66-
"fast-glob": "^3.3.2",
6776
"lodash": "^4.17.21",
6877
"react": "19.0.0-rc-fb9a90fa48-20240614",
6978
"react-dom": "19.0.0-rc-fb9a90fa48-20240614",

packages/react/tsconfig.build.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
"moduleResolution": "bundler",
1111
"noEmit": false,
1212
"rootDir": "./src",
13-
"outDir": "build"
13+
"outDir": "build/esm"
1414
},
1515
"include": ["src/**/*.ts*"],
16-
"exclude": ["src/**/*.spec.ts*", "src/**/*.test.ts*"]
16+
"exclude": ["src/**/*.spec.ts*", "src/**/*.test.ts*"],
17+
"tsc-alias": {
18+
"resolveFullPaths": true
19+
}
1720
}

packages/react/vitest.config.ts packages/react/vitest.config.mts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mergeConfig, defineProject } from 'vitest/config';
2-
import sharedConfig from '../../vitest.shared';
2+
import sharedConfig from '../../vitest.shared.mts';
33

44
export default mergeConfig(
55
sharedConfig,

0 commit comments

Comments
 (0)