From c4c1a7ecd0cad3bf82abb91fea7e9eb253fd706e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dudak?= Date: Wed, 2 Oct 2024 08:56:36 +0200 Subject: [PATCH] Change the output directory layout --- packages/mui-base/package.json | 7 ++++++- packages/mui-base/tsconfig.build.json | 2 +- scripts/build.mjs | 24 ++++++------------------ scripts/copyFiles.mjs | 3 --- scripts/copyFilesUtils.mjs | 19 ++++++++----------- 5 files changed, 21 insertions(+), 34 deletions(-) diff --git a/packages/mui-base/package.json b/packages/mui-base/package.json index eb9bf5135..a98474bc1 100644 --- a/packages/mui-base/package.json +++ b/packages/mui-base/package.json @@ -4,7 +4,6 @@ "private": false, "author": "MUI Team", "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.", - "main": "./src/index.ts", "keywords": [ "react", "react-component", @@ -26,9 +25,15 @@ "type": "opencollective", "url": "https://opencollective.com/mui-org" }, + "exports": { + "./*": { + "import": "./src/*/index.ts" + } + }, "imports": { "#test-utils": "./test/index.ts" }, + "type": "commonjs", "scripts": { "build": "pnpm build:node && pnpm build:stable && pnpm build:types && pnpm build:copy-files", "build:node": "node ../../scripts/build.mjs node", diff --git a/packages/mui-base/tsconfig.build.json b/packages/mui-base/tsconfig.build.json index 0ea89a8bd..8b13c86ad 100644 --- a/packages/mui-base/tsconfig.build.json +++ b/packages/mui-base/tsconfig.build.json @@ -10,7 +10,7 @@ "moduleResolution": "bundler", "noEmit": false, "rootDir": "./src", - "outDir": "build" + "outDir": "build/types" }, "include": ["src/**/*.ts*"], "exclude": ["src/**/*.spec.ts*", "src/**/*.test.ts*"] diff --git a/scripts/build.mjs b/scripts/build.mjs index 4310c1638..7e1c3cfa3 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -1,5 +1,4 @@ import childProcess from 'child_process'; -import glob from 'fast-glob'; import path from 'path'; import { promisify } from 'util'; import yargs from 'yargs'; @@ -40,26 +39,11 @@ async function run(argv) { '**/*.d.ts', ]; - const topLevelNonIndexFiles = glob - .sync(`*{${extensions.join(',')}}`, { cwd: srcDir, ignore }) - .filter((file) => { - return path.basename(file, path.extname(file)) !== 'index'; - }); - const topLevelPathImportsCanBePackages = topLevelNonIndexFiles.length === 0; - const outDir = path.resolve( relativeOutDir, - // We generally support top level path imports e.g. - // 1. `import ArrowDownIcon from '@mui/icons-material/ArrowDown'`. - // 2. `import Typography from '@mui/material/Typography'`. - // The first case resolves to a file while the second case resolves to a package first i.e. a package.json - // This means that only in the second case the bundler can decide whether it uses ES modules or CommonJS modules. - // Different extensions are not viable yet since they require additional bundler config for users and additional transpilation steps in our repo. - // - // TODO v6: Switch to `exports` field. { - node: topLevelPathImportsCanBePackages ? './node' : './', - stable: topLevelPathImportsCanBePackages ? './' : './esm', + node: './cjs', + stable: './esm', }[bundle], ); @@ -91,6 +75,10 @@ async function run(argv) { throw new Error(`'${command}' failed with \n${stderr}`); } + if (bundle === 'stable') { + await exec(`echo { "type": "module" } > ${path.join(outDir, 'package.json')}`); + } + if (verbose) { // eslint-disable-next-line no-console console.log(stdout); diff --git a/scripts/copyFiles.mjs b/scripts/copyFiles.mjs index 20bd9d2dd..a59613526 100644 --- a/scripts/copyFiles.mjs +++ b/scripts/copyFiles.mjs @@ -1,7 +1,6 @@ /* eslint-disable no-console */ import path from 'path'; import { - createModulePackages, createPackageFile, includeFileInBuild, prepend, @@ -52,8 +51,6 @@ async function run() { ); await addLicense(packageData); - - await createModulePackages({ from: srcPath, to: buildPath }); } catch (err) { console.error(err); process.exit(1); diff --git a/scripts/copyFilesUtils.mjs b/scripts/copyFilesUtils.mjs index 40b055426..2f0713267 100644 --- a/scripts/copyFilesUtils.mjs +++ b/scripts/copyFilesUtils.mjs @@ -93,22 +93,19 @@ export async function typescriptCopy({ from, to }) { export async function createPackageFile() { const packageData = await fse.readFile(path.resolve(packagePath, './package.json'), 'utf8'); - const { nyc, scripts, devDependencies, workspaces, ...packageDataOther } = + const { imports, exports, nyc, scripts, devDependencies, workspaces, ...packageDataOther } = JSON.parse(packageData); const newPackageData = { ...packageDataOther, private: false, - ...(packageDataOther.main - ? { - main: fse.existsSync(path.resolve(buildPath, './node/index.js')) - ? './node/index.js' - : './index.js', - module: fse.existsSync(path.resolve(buildPath, './esm/index.js')) - ? './esm/index.js' - : './index.js', - } - : {}), + exports: { + './*': { + types: './types/*/index.d.ts', + import: './esm/*/index.js', + require: './cjs/*/index.js', + }, + }, }; const typeDefinitionsFilePath = path.resolve(buildPath, './index.d.ts');