From 45f4750eb8b27f9e3aba9a3b7c4b6c89c0cbdc69 Mon Sep 17 00:00:00 2001 From: tonglx Date: Sat, 21 May 2022 08:39:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=201=E3=80=81=E5=8F=AF=E4=BB=8E=E6=AF=8F?= =?UTF-8?q?=E4=B8=AA=E6=A8=A1=E5=9D=97=E9=87=8C=E7=9A=84icons/svg=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9=E5=8A=A0=E8=BD=BDsvg=E6=96=87=E4=BB=B6?= =?UTF-8?q?=EF=BC=9B=202=E3=80=81=E5=B1=8F=E8=94=BD=E7=9B=B8=E5=90=8C?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E7=9A=84svg=E6=96=87=E4=BB=B6=EF=BC=9B=203?= =?UTF-8?q?=E3=80=81=E6=9F=A5=E6=89=BEicons/svg=E7=BB=93=E6=9E=84=E7=9A=84?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/svg/index.ts | 47 +++++++++++++++++++++++++++++++++++++++------- vite.config.ts | 4 ++-- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/build/svg/index.ts b/build/svg/index.ts index 9640753..5e4ddcc 100644 --- a/build/svg/index.ts +++ b/build/svg/index.ts @@ -1,5 +1,6 @@ import { Plugin } from "vite"; -import { readFileSync, readdirSync } from "fs"; +import { readFileSync, readdirSync, accessSync } from "fs"; +import path from "path"; let idPerfix = ""; const svgTitle = /+].*?)>/; @@ -9,16 +10,19 @@ const hasViewBox = /(viewBox="[^>+].*?")/g; const clearReturn = /(\r)|(\n)/g; -function findSvgFile(dir: string): string[] { +function findSvgFile(dir: string, uniqueNames: Record): string[] { const svgRes = []; const dirents = readdirSync(dir, { withFileTypes: true }); for (const dirent of dirents) { if (dirent.isDirectory()) { - svgRes.push(...findSvgFile(dir + dirent.name + "/")); + svgRes.push(...findSvgFile(path.join(dir, dirent.name), uniqueNames)); + } else if (uniqueNames[dirent.name]) { + continue; } else { - const svg = readFileSync(dir + dirent.name) + uniqueNames[dirent.name] = true; + const svg = readFileSync(path.join(dir, dirent.name)) .toString() .replace(clearReturn, "") .replace(svgTitle, (_: any, $2: any) => { @@ -47,10 +51,15 @@ function findSvgFile(dir: string): string[] { return svgRes; } -export const svgBuilder = (path: string, perfix = "icon"): Plugin | null => { - if (path !== "") { +export const svgBuilder = (paths: string[], perfix = "icon"): Plugin | null => { + if (paths.length > 0) { idPerfix = perfix; - const res = findSvgFile(path); + const uniqueNames: Record = {}; + const res = paths.reduce( + (previousValue, currentValue) => + previousValue.concat(findSvgFile(currentValue, uniqueNames)), + [] + ); return { name: "svg-transform", transformIndexHtml(html): string { @@ -69,3 +78,27 @@ export const svgBuilder = (path: string, perfix = "icon"): Plugin | null => { return null; } }; + +export const findSvgFolders = (dir: string): string[] => { + const svgFolders = []; + const dirents = readdirSync(dir, { + withFileTypes: true + }); + + // 找到结构为icons/svg的文件夹 + for (const dirent of dirents) { + if (dirent.isDirectory()) { + const testPath = + dirent.name === "icons" + ? path.join(dir, "icons/svg") + : path.join(dir, dirent.name, "icons/svg"); + try { + accessSync(testPath); + svgFolders.push(testPath); + } catch (e) { + continue; + } + } + } + return svgFolders; +}; diff --git a/vite.config.ts b/vite.config.ts index 9dd394b..2f5a2ae 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -8,7 +8,7 @@ import Unocss from "unocss/vite"; import { presetUno } from "unocss"; import { proxy } from "./src/cool/config/proxy"; import { cool } from "./build/cool"; -import { svgBuilder } from "./build/svg"; +import { svgBuilder, findSvgFolders } from "./build/svg"; function resolve(dir: string) { return path.resolve(__dirname, ".", dir); @@ -27,7 +27,7 @@ export default (): UserConfig => { Unocss({ presets: [presetUno()] }), - svgBuilder("./src/icons/svg/"), + svgBuilder(["./src/icons/svg/",...findSvgFolders("./src/modules/")]), cool() ], css: {