mirror of
https://github.com/cool-team-official/cool-admin-vue.git
synced 2024-11-01 06:02:38 +08:00
feat:
1、可从每个模块里的icons/svg文件夹加载svg文件; 2、屏蔽相同名称的svg文件; 3、查找icons/svg结构的文件夹;
This commit is contained in:
parent
59b6a2f73b
commit
45f4750eb8
@ -1,5 +1,6 @@
|
|||||||
import { Plugin } from "vite";
|
import { Plugin } from "vite";
|
||||||
import { readFileSync, readdirSync } from "fs";
|
import { readFileSync, readdirSync, accessSync } from "fs";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
let idPerfix = "";
|
let idPerfix = "";
|
||||||
const svgTitle = /<svg([^>+].*?)>/;
|
const svgTitle = /<svg([^>+].*?)>/;
|
||||||
@ -9,16 +10,19 @@ const hasViewBox = /(viewBox="[^>+].*?")/g;
|
|||||||
|
|
||||||
const clearReturn = /(\r)|(\n)/g;
|
const clearReturn = /(\r)|(\n)/g;
|
||||||
|
|
||||||
function findSvgFile(dir: string): string[] {
|
function findSvgFile(dir: string, uniqueNames: Record<string, boolean>): string[] {
|
||||||
const svgRes = [];
|
const svgRes = [];
|
||||||
const dirents = readdirSync(dir, {
|
const dirents = readdirSync(dir, {
|
||||||
withFileTypes: true
|
withFileTypes: true
|
||||||
});
|
});
|
||||||
for (const dirent of dirents) {
|
for (const dirent of dirents) {
|
||||||
if (dirent.isDirectory()) {
|
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 {
|
} else {
|
||||||
const svg = readFileSync(dir + dirent.name)
|
uniqueNames[dirent.name] = true;
|
||||||
|
const svg = readFileSync(path.join(dir, dirent.name))
|
||||||
.toString()
|
.toString()
|
||||||
.replace(clearReturn, "")
|
.replace(clearReturn, "")
|
||||||
.replace(svgTitle, (_: any, $2: any) => {
|
.replace(svgTitle, (_: any, $2: any) => {
|
||||||
@ -47,10 +51,15 @@ function findSvgFile(dir: string): string[] {
|
|||||||
return svgRes;
|
return svgRes;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const svgBuilder = (path: string, perfix = "icon"): Plugin | null => {
|
export const svgBuilder = (paths: string[], perfix = "icon"): Plugin | null => {
|
||||||
if (path !== "") {
|
if (paths.length > 0) {
|
||||||
idPerfix = perfix;
|
idPerfix = perfix;
|
||||||
const res = findSvgFile(path);
|
const uniqueNames: Record<string, boolean> = {};
|
||||||
|
const res = paths.reduce(
|
||||||
|
(previousValue, currentValue) =>
|
||||||
|
previousValue.concat(findSvgFile(currentValue, uniqueNames)),
|
||||||
|
[]
|
||||||
|
);
|
||||||
return {
|
return {
|
||||||
name: "svg-transform",
|
name: "svg-transform",
|
||||||
transformIndexHtml(html): string {
|
transformIndexHtml(html): string {
|
||||||
@ -69,3 +78,27 @@ export const svgBuilder = (path: string, perfix = "icon"): Plugin | null => {
|
|||||||
return 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;
|
||||||
|
};
|
||||||
|
@ -8,7 +8,7 @@ import Unocss from "unocss/vite";
|
|||||||
import { presetUno } from "unocss";
|
import { presetUno } from "unocss";
|
||||||
import { proxy } from "./src/cool/config/proxy";
|
import { proxy } from "./src/cool/config/proxy";
|
||||||
import { cool } from "./build/cool";
|
import { cool } from "./build/cool";
|
||||||
import { svgBuilder } from "./build/svg";
|
import { svgBuilder, findSvgFolders } from "./build/svg";
|
||||||
|
|
||||||
function resolve(dir: string) {
|
function resolve(dir: string) {
|
||||||
return path.resolve(__dirname, ".", dir);
|
return path.resolve(__dirname, ".", dir);
|
||||||
@ -27,7 +27,7 @@ export default (): UserConfig => {
|
|||||||
Unocss({
|
Unocss({
|
||||||
presets: [presetUno()]
|
presets: [presetUno()]
|
||||||
}),
|
}),
|
||||||
svgBuilder("./src/icons/svg/"),
|
svgBuilder(["./src/icons/svg/",...findSvgFolders("./src/modules/")]),
|
||||||
cool()
|
cool()
|
||||||
],
|
],
|
||||||
css: {
|
css: {
|
||||||
|
Loading…
Reference in New Issue
Block a user