Skip to content

Commit 1d174b6

Browse files
author
Brijesh Bittu
committed
Rename all d.ts files to d.mts in esm output dir
1 parent f72cd7a commit 1d174b6

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

packages/mui-utils/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
"scripts": {
2828
"build": "pnpm build:node && pnpm build:stable && pnpm build:types && pnpm build:copy-files",
2929
"build:node": "node ../../scripts/build.mjs node",
30-
"build:stable": "node ../../scripts/build.mjs stable --verbose",
31-
"build:copy-files": "node ../../scripts/copyFiles.mjs",
32-
"build:types": "tsx ../../scripts/buildTypes.mts",
30+
"build:stable": "MUI_EXPERIMENTAL_MJS=true node ../../scripts/build.mjs stable --verbose",
31+
"build:copy-files": "MUI_EXPERIMENTAL_MJS=true node ../../scripts/copyFiles.mjs",
32+
"build:types": "MUI_EXPERIMENTAL_MJS=true tsx ../../scripts/buildTypes.mts",
3333
"prebuild": "rimraf build tsconfig.build.tsbuildinfo",
3434
"release": "pnpm build && pnpm publish",
3535
"test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/mui-utils/**/*.test.?(c|m)[jt]s?(x)'",

scripts/build.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
import childProcess from 'child_process';
23
import path from 'path';
34
import { promisify } from 'util';
@@ -58,11 +59,10 @@ async function run(argv) {
5859

5960
let outFileExtension = '.js';
6061

61-
if (EXPERIMENTAL_MJS && (bundle === 'stable')) {
62+
if (EXPERIMENTAL_MJS && bundle === 'stable') {
6263
outFileExtension = '.mjs';
6364
}
6465

65-
console.log(EXPERIMENTAL_MJS)
6666
console.log(`Building ${bundle} bundle with outFileExtension: ${outFileExtension}`);
6767

6868
const relativeOutDir = {

scripts/buildTypes.mts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { parse } from 'jsonc-parser';
88

99
const $$ = $({ stdio: 'inherit' });
1010

11+
// Use .mjs extension for ESM output files if the MUI_EXPERIMENTAL_MJS environment variable is set.
12+
const EXPERIMENTAL_MJS = !!process.env.MUI_EXPERIMENTAL_MJS;
13+
1114
async function emitDeclarations(tsconfig: string, outDir: string) {
1215
// eslint-disable-next-line no-console
1316
console.log(`Building types for ${path.resolve(tsconfig)}`);
@@ -70,6 +73,22 @@ async function copyDeclarations(sourceDirectory: string, destinationDirectory: s
7073
});
7174
}
7275

76+
async function renameDtsFilesToDmts(sourceDirectory: string) {
77+
const dtsFiles = await glob('**/*.d.ts', { absolute: true, cwd: sourceDirectory });
78+
if (dtsFiles.length === 0) {
79+
console.warn('No .d.ts files found in the directory. Skipping renaming to .d.mts');
80+
return;
81+
}
82+
83+
console.log('Renaming .d.ts files to .d.mts files in', sourceDirectory);
84+
await Promise.all(
85+
dtsFiles.map(async (dtsFile) => {
86+
const mtsFile = dtsFile.replace(/\.d\.ts$/, '.d.mts');
87+
await fs.rename(dtsFile, mtsFile);
88+
}),
89+
);
90+
}
91+
7392
interface HandlerArgv {
7493
skipTsc: boolean;
7594
cjsDir: string;
@@ -84,10 +103,6 @@ async function main(argv: HandlerArgv) {
84103
() => false,
85104
);
86105

87-
const tsConfig = tsconfigExists
88-
? (parse(await fs.readFile(tsconfigPath, 'utf-8')) as { compilerOptions: { outDir: string } })
89-
: null;
90-
91106
const srcPath = path.join(packageRoot, 'src');
92107
const buildFolder = path.join(packageRoot, 'build');
93108
const esmDir = path.join(buildFolder, 'esm');
@@ -107,11 +122,15 @@ async function main(argv: HandlerArgv) {
107122
await emitDeclarations(tsconfigPath, esmDir);
108123
}
109124

110-
await copyDeclarations(esmDir, cjsDir)
125+
await copyDeclarations(esmDir, cjsDir);
111126

112127
await postProcessImports(esmDir, argv.removeCss);
113128
await postProcessImports(cjsDir, argv.removeCss);
114129

130+
if (EXPERIMENTAL_MJS) {
131+
await renameDtsFilesToDmts(esmDir);
132+
}
133+
115134
const tsbuildinfo = await glob('**/*.tsbuildinfo', { absolute: true, cwd: buildFolder });
116135
await Promise.all(tsbuildinfo.map(async (file) => fs.rm(file)));
117136
}

scripts/copyFiles.mjs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ async function addLicense(packageData) {
1616
*/
1717
`;
1818
await Promise.all(
19-
['./index.js', './esm/index.js', './modern/index.js', './node/index.js'].map(async (file) => {
20-
try {
21-
await prepend(path.resolve(buildPath, file), license);
22-
} catch (err) {
23-
if (err.code === 'ENOENT') {
24-
console.log(`Skipped license for ${file}`);
25-
} else {
26-
throw err;
19+
['./index.js', './esm/index.js', './esm/index.mjs', './modern/index.js', './node/index.js'].map(
20+
async (file) => {
21+
try {
22+
await prepend(path.resolve(buildPath, file), license);
23+
} catch (err) {
24+
if (err.code === 'ENOENT') {
25+
console.log(`Skipped license for ${file}`);
26+
} else {
27+
throw err;
28+
}
2729
}
28-
}
29-
}),
30+
},
31+
),
3032
);
3133
}
3234

0 commit comments

Comments
 (0)