njzscloud-dispose-web/plugin/iconfont-process.ts

48 lines
1.1 KiB
TypeScript

import fs from 'fs'
import path from 'node:path'
/*
interface IconfontJson {
font_family: string;
css_prefix_text: string;
glyphs: {
icon_id: string
font_class: string
unicode: string
name: string
}[];
}
*/
const targetFile = path.resolve(__dirname, '../src/components/a-icon/iconfont.json')
const outPath = path.resolve(__dirname, '../src/components/a-icon')
/**
* 阿里图标处理器
*/
export default {
process(data: VitePluginTypes.FileWatcherProcessParam) {
const text = data.getContent()
const tsFile = path.resolve(outPath, 'iconfont.ts')
console.log('正在生成文件:', tsFile)
const tsContent = `export const icons = ${text.trim()} as const
export type IconName = (typeof icons.glyphs)[number]['font_class']
export interface IconGlyphs {
icon_id: string
name: string
font_class: IconName
unicode: string
unicode_decimal: number
}
`
fs.writeFileSync(tsFile, tsContent, {encoding: 'utf-8'})
},
isAccept(data: VitePluginTypes.FileWatcherAcceptParam) {
return data.event === 'change' && !data.isDir && data.filePath === targetFile
},
} as VitePluginTypes.FileWatcherOptions