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

47 lines
1.3 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, './public/iconfont/ali/iconfont.json')
const outPath = path.resolve(__dirname, './src/components/iconfont')
export default {
process(data: VitePluginTypes.FileWatcherProcessParam) {
const json = JSON.parse(data.getContent()) as IconfontJson
const names = json.glyphs.map(glyph => glyph.font_class)
const dtsFile = outPath + '/iconfont.d.ts'
console.log('正在生成文件:', dtsFile)
const dts = `export {}
declare global {
namespace IconfontTypes {
type name = ${names.map(name => `'${name}'`).join('\n | ')}
}
}
`
fs.writeFileSync(dtsFile, dts, {encoding: 'utf-8'})
const tsFile = outPath + '/icons.ts'
const ts = `export default reactive([${'\n ' + names.map(name => `{name: '${name}'}`).join(',\n ') + '\n'}])`
console.log('正在生成文件:', tsFile)
fs.writeFileSync(tsFile, ts, {encoding: 'utf-8'})
console.log('文件生成完成')
},
isAccept(data: VitePluginTypes.FileWatcherAcceptParam) {
return data.event === 'change' && !data.isDir && data.filePath === targetFile
},
} as VitePluginTypes.FileWatcherOptions