Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[icons-material] Add types to ESM icons #38742

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions packages/mui-icons-material/scripts/create-typings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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();