|
| 1 | +import { relative } from 'node:path' |
| 2 | +import { readFileSync } from 'node:fs' |
| 3 | +import glob from 'fast-glob' |
| 4 | +import { normalizePath } from 'vite' |
| 5 | + |
| 6 | +import type { Plugin } from 'vite' |
| 7 | + |
| 8 | +export interface IconPluginOptions { |
| 9 | + dir?: string, |
| 10 | + domId?: string |
| 11 | +} |
| 12 | + |
| 13 | +interface IconFileState { |
| 14 | + id: string, |
| 15 | + code: string, |
| 16 | + mtimeMs?: number |
| 17 | +} |
| 18 | + |
| 19 | +const ICON_REGISTER = 'virtual:icon-register' |
| 20 | +const XMLNS = 'http://www.w3.org/2000/svg' |
| 21 | +const XMLNS_LINK = 'http://www.w3.org/1999/xlink' |
| 22 | +const DEFAULT_DOM_ID = '__icon_symbols__' |
| 23 | + |
| 24 | +const svgRE = /\.svg$/ |
| 25 | +const svgTagRE = /<svg([\s\S]*?)>([\s\S]*?)<\/svg>/ |
| 26 | +const idAttrRE = /id="[\s\S]*?"/g |
| 27 | + |
| 28 | +export function createIconPlugin(options: IconPluginOptions = {}): Plugin { |
| 29 | + const { dir = '', domId = DEFAULT_DOM_ID } = options |
| 30 | + |
| 31 | + const cache = new Map<string, IconFileState>() |
| 32 | + |
| 33 | + let isBuild = false |
| 34 | + |
| 35 | + async function renderModuleCode() { |
| 36 | + const entries = await glob('**/*.svg', { |
| 37 | + cwd: dir, |
| 38 | + stats: true, |
| 39 | + absolute: true |
| 40 | + }) |
| 41 | + |
| 42 | + let html = '' |
| 43 | + |
| 44 | + for (const entry of entries) { |
| 45 | + const { path, stats: { mtimeMs } = {} } = entry |
| 46 | + const cachedStat = cache.get(path) |
| 47 | + |
| 48 | + let id: string |
| 49 | + let code: string |
| 50 | + |
| 51 | + if (cachedStat && cachedStat.mtimeMs === mtimeMs) { |
| 52 | + id = cachedStat.id |
| 53 | + code = cachedStat.code |
| 54 | + } else { |
| 55 | + const content = readFileSync(path, 'utf-8') |
| 56 | + const svg = content.match(svgTagRE)?.[0] |
| 57 | + |
| 58 | + id = normalizePath(relative(dir, path).replace(svgRE, '')) |
| 59 | + code = (svg || '<svg></svg>') |
| 60 | + .replace(idAttrRE, '') |
| 61 | + .replace(svgTagRE, `<symbol id="${id}" $1>$2</symbol>`) |
| 62 | + |
| 63 | + cache.set(path, { id, code, mtimeMs }) |
| 64 | + } |
| 65 | + |
| 66 | + html += code |
| 67 | + } |
| 68 | + |
| 69 | + html = ` |
| 70 | + if (typeof window !== 'undefined') { |
| 71 | + function loadSvg() { |
| 72 | + const body = document.body |
| 73 | + let svgDom = document.getElementById('${domId}') |
| 74 | + if(!svgDom) { |
| 75 | + svgDom = document.createElementNS('${XMLNS}', 'svg') |
| 76 | + svgDom.style.position = 'absolute' |
| 77 | + svgDom.style.width = '0' |
| 78 | + svgDom.style.height = '0' |
| 79 | + svgDom.style.pointerEvents = 'none' |
| 80 | + svgDom.id = '${domId}' |
| 81 | + svgDom.setAttribute('xmlns','${XMLNS}') |
| 82 | + svgDom.setAttribute('xmlns:link','${XMLNS_LINK}') |
| 83 | + } |
| 84 | + svgDom.innerHTML = ${JSON.stringify(html)} |
| 85 | + body.insertBefore(svgDom, body.firstChild) |
| 86 | + } |
| 87 | + if(document.readyState === 'loading') { |
| 88 | + document.addEventListener('DOMContentLoaded', loadSvg); |
| 89 | + } else { |
| 90 | + loadSvg() |
| 91 | + } |
| 92 | + } |
| 93 | + ` |
| 94 | + |
| 95 | + return html |
| 96 | + } |
| 97 | + |
| 98 | + return { |
| 99 | + name: 'plugin:icon', |
| 100 | + configResolved(config) { |
| 101 | + isBuild = config.command === 'build' |
| 102 | + }, |
| 103 | + resolveId(id) { |
| 104 | + return ICON_REGISTER === id ? id : null |
| 105 | + }, |
| 106 | + async load(id, ssr) { |
| 107 | + if (!isBuild || ICON_REGISTER !== id) return null |
| 108 | + if (ssr && !isBuild) return 'export default {}' |
| 109 | + |
| 110 | + return await renderModuleCode() |
| 111 | + }, |
| 112 | + configureServer: ({ middlewares }) => { |
| 113 | + // middlewares.use(cors({ origin: '*' })) |
| 114 | + middlewares.use(async (req, res, next) => { |
| 115 | + const url = normalizePath(req.url!) |
| 116 | + const registerId = `/@id/${ICON_REGISTER}` |
| 117 | + |
| 118 | + if (url.endsWith(registerId)) { |
| 119 | + res.setHeader('Content-Type', 'application/javascript') |
| 120 | + res.setHeader('Cache-Control', 'no-cache') |
| 121 | + |
| 122 | + // res.setHeader('Etag', getEtag(content, { weak: true })) |
| 123 | + res.statusCode = 200 |
| 124 | + res.end(await renderModuleCode()) |
| 125 | + } else { |
| 126 | + next() |
| 127 | + } |
| 128 | + }) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments