mirror of
https://github.com/pengzhanbo/vuepress-theme-plume.git
synced 2026-04-23 10:58:13 +08:00
perf(plugin-md-power): 优化 icons css 输出
This commit is contained in:
parent
0ac4e70abc
commit
9a64ee07bf
@ -1,3 +1,4 @@
|
||||
import { constants, promises as fsp } from 'node:fs'
|
||||
import type { App } from 'vuepress/core'
|
||||
import { getIconContentCSS, getIconData } from '@iconify/utils'
|
||||
import { fs, logger } from 'vuepress/utils'
|
||||
@ -15,6 +16,7 @@ export interface IconCacheItem {
|
||||
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 8)
|
||||
const iconDataCache = new Map<string, any>()
|
||||
const URL_CONTENT_RE = /(url\([^]+?\))/
|
||||
const CSS_PATH = 'internal/md-power/icons.css'
|
||||
|
||||
function resolveOption(opt?: boolean | IconsOptions): Required<IconsOptions> {
|
||||
const options = typeof opt === 'object' ? opt : {}
|
||||
@ -27,8 +29,16 @@ function resolveOption(opt?: boolean | IconsOptions): Required<IconsOptions> {
|
||||
export function createIconCSSWriter(app: App, opt?: boolean | IconsOptions) {
|
||||
const cache = new Map<string, IconCacheItem>()
|
||||
const isInstalled = isPackageExists('@iconify/json')
|
||||
const currentPath = app.dir.temp(CSS_PATH)
|
||||
|
||||
const write = (content: string) => app.writeTemp('internal/md-power/icons.css', content)
|
||||
const write = async (content: string) => {
|
||||
if (!content && app.env.isDev) {
|
||||
if (existsSync(currentPath) && (await fsp.stat(currentPath)).isFile()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
await app.writeTemp(CSS_PATH, content)
|
||||
}
|
||||
let timer: NodeJS.Timeout | null = null
|
||||
|
||||
const options = resolveOption(opt)
|
||||
@ -42,10 +52,12 @@ export function createIconCSSWriter(app: App, opt?: boolean | IconsOptions) {
|
||||
timer = setTimeout(async () => {
|
||||
let css = defaultContent
|
||||
|
||||
for (const [, { content, className }] of cache)
|
||||
css += `.${className} {\n --svg: ${content};\n}\n`
|
||||
if (cache.size > 0) {
|
||||
for (const [, { content, className }] of cache)
|
||||
css += `.${className} {\n --svg: ${content};\n}\n`
|
||||
|
||||
await write(css)
|
||||
await write(css)
|
||||
}
|
||||
}, 100)
|
||||
}
|
||||
|
||||
@ -132,3 +144,13 @@ async function genIconContent(iconName: string, cb: (content: string) => void) {
|
||||
const match = content.match(URL_CONTENT_RE)
|
||||
return cb(match ? match[1] : '')
|
||||
}
|
||||
|
||||
function existsSync(fp: string) {
|
||||
try {
|
||||
fs.accessSync(fp, constants.R_OK)
|
||||
return true
|
||||
}
|
||||
catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user