This commit is contained in:
2026-05-06 02:59:19 +08:00
parent 015c4fb91e
commit da0a6d0f81
60 changed files with 2406 additions and 323 deletions

View File

@@ -0,0 +1,137 @@
// plugins/vite-plugin-gen-index-css.ts
import type { Plugin } from "vite";
import fs from "fs";
import path from "path";
import { globSync } from "glob";
interface Config {
targetDirs: string[];
includeExtensions: string[];
importSyntax: "@import";
importSyntaxTail?: string;
excludeFilePattern: RegExp[];
excludeDirs: string[];
warnDuplicateTailwindImport: boolean;
indexFileName: string;
}
const CONFIG: Config = {
targetDirs: ["src"],
includeExtensions: [".css"],
importSyntax: "@import",
excludeFilePattern: [/index\.css/, /index\.scss/, /\.(test|spec)\./],
excludeDirs: [
"__tests__",
"tests",
"story",
"stories",
"types",
"node_modules",
"dist",
"build",
],
warnDuplicateTailwindImport: true,
indexFileName: "index.css",
};
const normalizePath = (p: string) => p.replace(/\\/g, "/");
function isValidFile(filePath: string, config: Config): boolean {
const filenameWithExt = filePath.split(/[\\/]/).pop()!;
const shouldExcludeFile = config.excludeFilePattern.some((p) =>
p.test(filenameWithExt),
);
if (shouldExcludeFile) return false;
const normalized = normalizePath(filePath);
const shouldExcludeDir = config.excludeDirs.some((dir) =>
normalized.includes(`/${dir}/`),
);
if (shouldExcludeDir) return false;
const ext = path.extname(filePath);
if (!config.includeExtensions.includes(ext)) return false;
if (config.warnDuplicateTailwindImport) {
try {
const content = fs.readFileSync(filePath, "utf-8");
if (
content.includes('@import "tailwindcss"') ||
content.includes("@import 'tailwindcss'")
) {
console.warn(
`[gen-index-css] ${filePath} 含有重复的 @import "tailwindcss",建议删除`,
);
}
} catch {
// ignore
}
}
return true;
}
function generateIndexFile(config: Config) {
const [targetDir] = config.targetDirs;
const dirPath = path.resolve(process.cwd(), targetDir);
const searchPattern = path.resolve(dirPath, "**", "*.*");
const allFiles = globSync(searchPattern, {
nodir: true,
absolute: true,
windowsPathsNoEscape: true,
dot: false,
follow: true,
});
const validFiles = allFiles.filter((f) => isValidFile(f, config));
console.log(`✅ 有效 CSS 文件数量: ${validFiles.length}`);
validFiles.sort();
const importStatements = validFiles.map((file) => {
const relPath = path.relative(dirPath, file);
const importPath = "./" + relPath.replace(/\\/g, "/");
return `${config.importSyntax} '${importPath}';`;
});
const indexContent = `
@import "tailwindcss";
${importStatements.join("\n")}
`.trim();
const indexFilePath = path.resolve(dirPath, config.indexFileName);
// ✅ 内容比对,防止无限 rebuild
if (fs.existsSync(indexFilePath)) {
const old = fs.readFileSync(indexFilePath, "utf8");
if (old === indexContent) return;
}
fs.writeFileSync(indexFilePath, indexContent, "utf8");
console.log(`✅ 成功生成 ${config.indexFileName}: ${indexFilePath}`);
}
export function genIndexCssPlugin(): Plugin {
return {
name: "vite-plugin-gen-index-css",
apply: "build",
buildStart() {
try {
generateIndexFile(CONFIG);
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
this.error(`[gen-index-css] failed: ${msg}`);
throw err;
}
},
handleHotUpdate({ file }) {
if (normalizePath(file).endsWith(`/src/${CONFIG.indexFileName}`)) {
return [];
}
},
};
}

View File

@@ -0,0 +1,129 @@
// plugins/vite-plugin-gen-index-ts.ts
import type { Plugin } from "vite";
import fs from "fs";
import path from "path";
import { globSync } from "glob";
interface Config {
targetDirs: string[];
includeExtensions: string[];
excludeKeywords: {
dirs: string[];
fileSuffixes: string[];
filePatterns: RegExp[];
};
}
const CONFIG: Config = {
targetDirs: ["src"],
includeExtensions: [".ts", ".tsx", ".vue"],
excludeKeywords: {
dirs: ["__tests__", "tests", "story", "stories", "types"],
fileSuffixes: [".d.ts"],
filePatterns: [
/^index\.(ts|tsx|js|jsx)$/,
/\.(test|spec)\./,
/\.(story|stories)\./,
],
},
};
const normalizePath = (p: string) => p.replace(/\\/g, "/");
const isInExcludeDir = (filePath: string) => {
const normalized = normalizePath(filePath);
return CONFIG.excludeKeywords.dirs.some((dir) =>
normalized.includes(`/${dir}/`),
);
};
const isExcludeSuffix = (filePath: string) =>
CONFIG.excludeKeywords.fileSuffixes.some((suffix) =>
filePath.endsWith(suffix),
);
const isMatchExcludePattern = (fileName: string) =>
CONFIG.excludeKeywords.filePatterns.some((pattern) => pattern.test(fileName));
function isValidFile(filePath: string): boolean {
const fileName = filePath.split(/[\\/]/).pop()!;
if (isInExcludeDir(filePath)) return false;
if (isExcludeSuffix(filePath)) return false;
if (isMatchExcludePattern(fileName)) return false;
const ext = path.extname(filePath);
return CONFIG.includeExtensions.includes(ext);
}
function generateIndexFile(dirPath: string) {
const searchPattern = path.resolve(dirPath, "**", "*.*");
const allFiles = globSync(searchPattern, {
nodir: true,
absolute: true,
windowsPathsNoEscape: true,
dot: false,
follow: true,
});
const validFiles = allFiles.filter(isValidFile);
if (validFiles.length === 0) return;
validFiles.sort();
const exportStatements = validFiles.map((file) => {
const relPath = path.relative(dirPath, file);
const importPath = `./${relPath
.replace(/\.[^.]+$/, "")
.replace(/\\/g, "/")}`;
return `export * from '${importPath}';`;
});
const indexContent = `
import './index.css';
${exportStatements.join("\n")}
`.trim();
const indexFilePath = path.resolve(dirPath, "index.ts");
// ✅ 内容比对,避免无限 rebuild
if (fs.existsSync(indexFilePath)) {
const old = fs.readFileSync(indexFilePath, "utf8");
if (old === indexContent) return;
}
fs.writeFileSync(indexFilePath, indexContent, "utf8");
}
export function genIndexTsPlugin(): Plugin {
return {
name: "vite-plugin-gen-index-ts",
apply: "build",
buildStart() {
const [targetDir] = CONFIG.targetDirs;
if (!targetDir) {
this.error("CONFIG.targetDirs is empty");
return;
}
const absTargetDir = path.resolve(process.cwd(), targetDir);
try {
generateIndexFile(absTargetDir);
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
this.error(`[gen-index-ts] failed: ${msg}`);
throw err;
}
},
handleHotUpdate({ file }) {
if (file.replace(/\\/g, "/").endsWith("/src/index.ts")) {
return [];
}
},
};
}