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

Generate dynamic imports for icons-react #1081

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
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
136 changes: 87 additions & 49 deletions .build/build-icons.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs-extra'
import path from 'path'
import { PACKAGES_DIR, getAliases, toPascalCase, getAllIcons } from './helpers.mjs'
import { stringify } from 'svgson'
import fs from 'fs-extra';
import path from 'path';
import { PACKAGES_DIR, getAliases, toPascalCase, getAllIcons } from './helpers.mjs';
import { stringify } from 'svgson';

/**
* Build icons
Expand All @@ -22,62 +22,67 @@ export const buildJsIcons = ({
key = true,
pascalCase = false,
pascalName = true,
indexFile = 'icons.ts'
indexFile = 'icons.ts',
}) => {
const DIST_DIR = path.resolve(PACKAGES_DIR, name);
const aliases = getAliases(),
allIcons = getAllIcons(false, true)
allIcons = getAllIcons(false, true);

let index = []
let index = [];
Object.entries(allIcons).forEach(([type, icons]) => {
icons.forEach((icon, i) => {
process.stdout.write(`Building \`${name}\` ${type} ${i}/${icons.length}: ${icon.name.padEnd(42)}\r`)
process.stdout.write(
`Building \`${name}\` ${type} ${i}/${icons.length}: ${icon.name.padEnd(42)}\r`,
);

const children = icon.obj.children
.map(({
name,
attributes
}, i) => {
.map(({ name, attributes }, i) => {
if (key) {
attributes.key = `svg-${i}`
attributes.key = `svg-${i}`;
}

if (pascalCase) {
attributes.strokeWidth = attributes['stroke-width']
delete attributes['stroke-width']
attributes.strokeWidth = attributes['stroke-width'];
delete attributes['stroke-width'];
}

return [name, attributes]
return [name, attributes];
})
.filter((i) => {
const [name, attributes] = i
return !attributes.d || attributes.d !== 'M0 0h24v24H0z'
})
const [name, attributes] = i;
return !attributes.d || attributes.d !== 'M0 0h24v24H0z';
});

const iconName = `${icon.name}${type !== 'outline' ? `-${type}` : ''}`,
iconNamePascal = `${icon.namePascal}${type !== 'outline' ? toPascalCase(type) : ''}`
iconNamePascal = `${icon.namePascal}${type !== 'outline' ? toPascalCase(type) : ''}`;

let component = componentTemplate({
type,
name: iconName,
namePascal: iconNamePascal,
children,
stringify,
svg: icon.content
})

let filePath = path.resolve(DIST_DIR, 'src/icons', `${pascalName ? iconNamePascal : iconName}.${extension}`)
fs.writeFileSync(filePath, component, 'utf-8')

index.push(indexItemTemplate({
type,
name: iconName,
namePascal: iconNamePascal
}))
})
})

fs.writeFileSync(path.resolve(DIST_DIR, `src/icons/${indexFile}`), index.join('\n'), 'utf-8')
svg: icon.content,
});

let filePath = path.resolve(
DIST_DIR,
'src/icons',
`${pascalName ? iconNamePascal : iconName}.${extension}`,
);
fs.writeFileSync(filePath, component, 'utf-8');

index.push(
indexItemTemplate({
type,
name: iconName,
namePascal: iconNamePascal,
}),
);
});
});

fs.writeFileSync(path.resolve(DIST_DIR, `src/icons/${indexFile}`), index.join('\n'), 'utf-8');

// Write aliases
let aliasesStr = '';
Expand All @@ -87,28 +92,61 @@ export const buildJsIcons = ({
from,
to,
fromPascal: toPascalCase(from),
toPascal: toPascalCase(to)
})
})
toPascal: toPascalCase(to),
});
});
}

fs.writeFileSync(path.resolve(DIST_DIR, `./src/aliases.ts`), aliasesStr || `export {};`, 'utf-8')
}
fs.writeFileSync(path.resolve(DIST_DIR, `./src/aliases.ts`), aliasesStr || `export {};`, 'utf-8');
};

export const buildIconsList = (name) => {
const DIST_DIR = path.resolve(PACKAGES_DIR, name);
const allIcons = getAllIcons(false, true)
const allIcons = getAllIcons(false, true);

let index = [];
Object.entries(allIcons).forEach(([type, icons]) => {
icons.forEach((icon, i) => {
process.stdout.write(
`Building \`${name}\` ${type} ${i}/${icons.length}: ${icon.name.padEnd(42)}\r`,
);

const iconName = `${icon.name}${type !== 'outline' ? `-${type}` : ''}`;

index.push(iconName);
});
});

fs.writeFileSync(
path.resolve(DIST_DIR, `./src/icons-list.ts`),
`export default ${JSON.stringify(index, null, 2)};`,
'utf-8',
);
};

export const buildIconsDynamicImport = (name) => {
const DIST_DIR = path.resolve(PACKAGES_DIR, name);
const allIcons = getAllIcons(false, true);

let index = []
let dynamicImportString = 'export default {';
Object.entries(allIcons).forEach(([type, icons]) => {
icons.forEach((icon, i) => {
process.stdout.write(`Building \`${name}\` ${type} ${i}/${icons.length}: ${icon.name.padEnd(42)}\r`)
process.stdout.write(
`Building \`${name}\` ${type} ${i}/${icons.length}: ${icon.name.padEnd(42)}\r`,
);

const iconName = `${icon.name}${type !== 'outline' ? `-${type}` : ''}`,
iconNamePascal = `${icon.namePascal}${type !== 'outline' ? toPascalCase(type) : ''}`;

const iconName = `${icon.name}${type !== 'outline' ? `-${type}` : ''}`
dynamicImportString += ` '${iconName}': () => import('./icons/${iconNamePascal}'),\n`;
});
});

index.push(iconName)
})
})
dynamicImportString += '};\n';

fs.writeFileSync(path.resolve(DIST_DIR, `./src/icons-list.ts`), `export default ${JSON.stringify(index, null, 2)};`, 'utf-8')
}
fs.writeFileSync(
path.resolve(DIST_DIR, `./src/dynamic-imports.ts`),
dynamicImportString,
'utf-8',
);
};
Loading
Loading