From 79873ce5c09d33e7e9bd4cfe0dcfaa7079796a6f Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Thu, 31 Aug 2023 09:14:02 -0400 Subject: [PATCH] [icons-material] Add types to ESM icons --- .../scripts/create-typings.mjs | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/mui-icons-material/scripts/create-typings.mjs b/packages/mui-icons-material/scripts/create-typings.mjs index 144c529dde5c24..bcecbd8e5cc458 100644 --- a/packages/mui-icons-material/scripts/create-typings.mjs +++ b/packages/mui-icons-material/scripts/create-typings.mjs @@ -8,19 +8,20 @@ import url from 'url'; const currentDirectory = url.fileURLToPath(new URL('.', import.meta.url)); const SRC_DIR = path.resolve(currentDirectory, '../lib/esm'); -const TARGET_DIR = path.resolve(currentDirectory, '../build'); +const CJS_TARGET_DIR = path.resolve(currentDirectory, '../build'); +const ESM_TARGET_DIR = path.resolve(currentDirectory, '../build/esm'); function normalizeFileName(file) { return path.parse(file).name; } -function createIconTyping(file) { +function createIconTyping(file, targetDir) { const name = normalizeFileName(file); const contents = `export { default } from '@mui/material/SvgIcon';`; - return fse.writeFile(path.resolve(TARGET_DIR, `${name}.d.ts`), contents, 'utf8'); + return fse.writeFile(path.resolve(targetDir, `${name}.d.ts`), contents, 'utf8'); } -function createIndexTyping(files) { +function createIndexTyping(files, targetDir) { const contents = ` import SvgIcon from '@mui/material/SvgIcon'; @@ -29,17 +30,26 @@ type SvgIconComponent = typeof SvgIcon; ${files.map((file) => `export const ${normalizeFileName(file)}: SvgIconComponent;`).join('\n')} `; - return fse.writeFile(path.resolve(TARGET_DIR, 'index.d.ts'), contents, 'utf8'); + return fse.writeFile(path.resolve(targetDir, 'index.d.ts'), contents, 'utf8'); } // Generate TypeScript. async function run() { - await fse.ensureDir(TARGET_DIR); + await fse.ensureDir(CJS_TARGET_DIR); + await fse.ensureDir(ESM_TARGET_DIR); console.log(`\u{1f52c} Searching for modules inside "${chalk.dim(SRC_DIR)}".`); const files = await glob('!(index)*.js', { cwd: SRC_DIR }); - const typings = files.map((file) => createIconTyping(file)); - await Promise.all([...typings, createIndexTyping(files)]); - console.log(`\u{1F5C4} Written typings to ${chalk.dim(TARGET_DIR)}.`); + const cjsTypings = files.map((file) => createIconTyping(file, CJS_TARGET_DIR)); + const esmTypings = files.map((file) => createIconTyping(file, ESM_TARGET_DIR)); + await Promise.all([ + ...cjsTypings, + ...esmTypings, + createIndexTyping(files, CJS_TARGET_DIR), + createIndexTyping(files, ESM_TARGET_DIR), + ]); + console.log( + `\u{1F5C4} Written typings to ${chalk.dim(CJS_TARGET_DIR)} and ${chalk.dim(ESM_TARGET_DIR)}.`, + ); } run();