wangzhibo
6 天以前 7bd866831780abcf5a59c4cbb7d1be456eea7c99
packages/vite-plugin/src/svg/index.ts
@@ -2,7 +2,6 @@
import { basename, extname } from "path";
import { rootDir } from "../utils";
import svgo from "svgo";
import { config } from "../config";
let svgIcons: string[] = [];
@@ -11,28 +10,12 @@
   const dirs = readdirSync(dir, {
      withFileTypes: true,
   });
   // 获取当前目录的模块名
   const moduleName = dir.match(/[/\\](?:src[/\\](?:plugins|modules)[/\\])([^/\\]+)/)?.[1] || "";
   for (const d of dirs) {
      if (d.isDirectory()) {
         arr.push(...findSvg(dir + d.name + "/"));
      } else {
         if (extname(d.name) == ".svg") {
            const baseName = basename(d.name, ".svg");
            // 判断是否需要跳过拼接模块名
            let shouldSkip = config.svg.skipNames?.includes(moduleName);
            // 跳过包含icon-
            if (baseName.includes("icon-")) {
               shouldSkip = true;
            }
            const iconName = shouldSkip ? baseName : `${moduleName}-${baseName}`;
            svgIcons.push(iconName);
            svgIcons.push(basename(d.name, ".svg"));
            const svg = readFileSync(dir + d.name)
               .toString()
@@ -54,7 +37,7 @@
                  if (!/(viewBox="[^>+].*?")/g.test($2)) {
                     content += `viewBox="0 0 ${width} ${height}"`;
                  }
                  return `<symbol id="icon-${iconName}" ${content}>`;
                  return `<symbol id="icon-${d.name.replace(".svg", "")}" ${content}>`;
               })
               .replace("</svg>", "</symbol>");
@@ -62,7 +45,6 @@
         }
      }
   }
   return arr;
}