build-webfont.mjs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { webfont } from "webfont";
  2. import * as fs from 'fs'
  3. import template from 'lodash.template'
  4. import { getPackageDir, getPackageJson, getAliases, types, asyncForEach, toPascalCase } from '../../../.build/helpers.mjs'
  5. const formats = ['ttf', 'woff', 'woff2']
  6. const p = getPackageJson()
  7. const DIR = getPackageDir('icons-webfont')
  8. const fontHeight = 1000
  9. const aliases = getAliases(true)
  10. fs.mkdirSync(`${DIR}/dist/fonts`, { recursive: true })
  11. types.push('all')
  12. const getAlliasesFlat = () => {
  13. let allAliases = {}
  14. Object.entries(aliases).forEach(([type, aliases]) => {
  15. Object.entries(aliases).forEach(([from, to]) => {
  16. allAliases[`${from}${type !== 'outline' ? `-${type}` : ''}`] = `${to}${type !== 'outline' ? `-${type}` : ''}`
  17. })
  18. })
  19. return allAliases
  20. }
  21. asyncForEach(types, async type => {
  22. console.log(`Building webfont for ${type} icons`)
  23. await webfont({
  24. files: `icons-outlined/${type}/*.svg`,
  25. fontName: 'tabler-icons',
  26. prependUnicode: true,
  27. formats,
  28. normalize: true,
  29. fontHeight,
  30. descent: 100,
  31. ascent: 900,
  32. fixedWidth: false
  33. })
  34. .then((result) => {
  35. formats.forEach(format => {
  36. fs.writeFileSync(`${DIR}/dist/fonts/tabler-icons${type !== 'all' ? `-${type}` : ''}.${format}`, result[format])
  37. })
  38. const glyphs = result.glyphsData
  39. .map(icon => icon.metadata)
  40. .sort(function (a, b) {
  41. return ('' + a.name).localeCompare(b.name)
  42. })
  43. const options = {
  44. name: `Tabler Icons${type !== 'all' ? ` ${toPascalCase(type)}` : ''}`,
  45. fileName: `tabler-icons${type !== 'all' ? `-${type}` : ''}`,
  46. glyphs,
  47. v: p.version,
  48. aliases: (type === 'all' ? getAlliasesFlat() : aliases[type]) || {}
  49. }
  50. //scss
  51. const compiled = template(fs.readFileSync(`${DIR}/.build/iconfont.scss`).toString())
  52. const resultSCSS = compiled(options)
  53. fs.writeFileSync(`${DIR}/dist/tabler-icons${type !== 'all' ? `-${type}` : ''}.scss`, resultSCSS)
  54. //html
  55. const compiledHtml = template(fs.readFileSync(`${DIR}/.build/iconfont.html`).toString())
  56. const resultHtml = compiledHtml(options)
  57. fs.writeFileSync(`${DIR}/dist/tabler-icons${type !== 'all' ? `-${type}` : ''}.html`, resultHtml)
  58. })
  59. .catch((error) => {
  60. throw error;
  61. });
  62. })