build.mjs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env node
  2. import { buildJsIcons } from '../../.build/build-icons.mjs'
  3. import { readFileSync, writeFileSync } from 'fs'
  4. import { getAllIcons, toPascalCase } from '../../.build/helpers.mjs'
  5. const updatePackageJson = () => {
  6. let packageJson = JSON.parse(readFileSync('package.json', 'utf-8'))
  7. if (packageJson.exports) {
  8. for (const key in packageJson.exports) {
  9. if (key.match(/^\.\/[^\.]+\.svelte$/)) {
  10. delete packageJson.exports[key]
  11. }
  12. }
  13. }
  14. const allIcons = getAllIcons(false, false)
  15. Object.entries(allIcons).forEach(([type, icons]) => {
  16. icons.forEach((icon, i) => {
  17. const iconName = `${icon.name}${type !== 'outline' ? `-${type}` : ''}`,
  18. iconNamePascal = `${icon.namePascal}${type !== 'outline' ? toPascalCase(type) : ''}`
  19. packageJson.exports[`./${iconNamePascal}.svelte`] = {
  20. types: `./dist/icons/${iconName}.svelte.d.ts`,
  21. svelte: `./dist/icons/${iconName}.svelte`
  22. }
  23. })
  24. })
  25. writeFileSync('package.json', JSON.stringify(packageJson, null, 2))
  26. }
  27. const componentTemplate = ({
  28. type,
  29. name,
  30. children,
  31. stringify
  32. }) => {
  33. return `\
  34. <script lang="ts">
  35. import Icon from '../Icon.svelte';
  36. import type { IconNode, IconProps } from '../types.js';
  37. type $$Props = IconProps;
  38. const iconNode: IconNode = ${JSON.stringify(children)};
  39. </script>
  40. <Icon type="${type}" name="${name}" {...$$props} iconNode={iconNode}>
  41. <slot/>
  42. </Icon>
  43. `;
  44. };
  45. const aliasTemplate = ({ fromPascal, to }) => `export { default as Icon${fromPascal} } from './icons/${to}.svelte';\n`
  46. const indexItemTemplate = ({
  47. name,
  48. namePascal
  49. }) => `export { default as ${namePascal} } from './${name}.svelte';`
  50. buildJsIcons({
  51. name: 'icons-svelte',
  52. componentTemplate,
  53. indexItemTemplate,
  54. aliasTemplate,
  55. extension: 'svelte',
  56. key: false,
  57. indexFile: 'index.ts',
  58. pascalName: false,
  59. })
  60. updatePackageJson()