mm
This commit is contained in:
@@ -1 +0,0 @@
|
||||
registry = https://registry.npmmirror.com/
|
||||
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"json.schemas": [
|
||||
{
|
||||
"fileMatch": ["/tsconfig.build.json", "/tsconfig.base.json"],
|
||||
"schema": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>DefGov UI Web</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,47 +0,0 @@
|
||||
{
|
||||
"name": "vite-react-app",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"sideEffects": [
|
||||
"*.css"
|
||||
],
|
||||
"module": "./dist/index.es.js",
|
||||
"main": "./dist/index.cjs.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"style": "./dist/index.css",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/index.es.js",
|
||||
"require": "./dist/index.cjs.js",
|
||||
"types": "./dist/index.d.ts"
|
||||
},
|
||||
"./index.css": "./dist/index.css"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"gen-css": "ts-node scripts/gen-index-css.ts",
|
||||
"gen-ts": "ts-node scripts/gen-index-ts.ts",
|
||||
"gen-index": "pnpm run gen-css && pnpm run gen-ts",
|
||||
"dev": "pnpm run gen-index && vite build --watch",
|
||||
"build": "pnpm run gen-index && tsc -p tsconfig.build.json && vite build"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-typescript": "^12.3.0",
|
||||
"@types/node": "^25.6.0",
|
||||
"@types/react": "^19.2.14",
|
||||
"@types/react-dom": "^19.2.3",
|
||||
"@vitejs/plugin-react": "~5.2.0",
|
||||
"glob": "^13.0.6",
|
||||
"typescript": "~6.0.3",
|
||||
"vite": "~7.3.2",
|
||||
"vite-plugin-dts": "^4.5.4",
|
||||
"ts-node": "^10.9.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "^19.2.5",
|
||||
"react-dom": "^19.2.5"
|
||||
}
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
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) {
|
||||
console.log("⚠️ 未找到符合条件的文件,跳过生成 index.ts");
|
||||
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");
|
||||
|
||||
// ✅ 内容比对,避免重复写入
|
||||
if (fs.existsSync(indexFilePath)) {
|
||||
const old = fs.readFileSync(indexFilePath, "utf8");
|
||||
if (old === indexContent) {
|
||||
console.log("✅ index.ts 内容无变化,无需重新生成");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(indexFilePath, indexContent, "utf8");
|
||||
console.log(`✅ 成功生成 index.ts: ${indexFilePath}`);
|
||||
}
|
||||
|
||||
// 脚本入口
|
||||
const [targetDir] = CONFIG.targetDirs;
|
||||
if (!targetDir) {
|
||||
console.error("❌ CONFIG.targetDirs 不能为空");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const absTargetDir = path.resolve(process.cwd(), targetDir);
|
||||
|
||||
try {
|
||||
console.log(`🚀 开始扫描目录: ${absTargetDir}`);
|
||||
generateIndexFile(absTargetDir);
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
console.error(`❌ [gen-index-ts] 执行失败: ${msg}`);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
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) {
|
||||
console.log("⚠️ 未找到符合条件的文件,跳过生成 index.ts");
|
||||
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");
|
||||
|
||||
// ✅ 内容比对,避免重复写入
|
||||
if (fs.existsSync(indexFilePath)) {
|
||||
const old = fs.readFileSync(indexFilePath, "utf8");
|
||||
if (old === indexContent) {
|
||||
console.log("✅ index.ts 内容无变化,无需重新生成");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(indexFilePath, indexContent, "utf8");
|
||||
console.log(`✅ 成功生成 index.ts: ${indexFilePath}`);
|
||||
}
|
||||
|
||||
// 脚本入口
|
||||
const [targetDir] = CONFIG.targetDirs;
|
||||
if (!targetDir) {
|
||||
console.error("❌ CONFIG.targetDirs 不能为空");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const absTargetDir = path.resolve(process.cwd(), targetDir);
|
||||
|
||||
try {
|
||||
console.log(`🚀 开始扫描目录: ${absTargetDir}`);
|
||||
generateIndexFile(absTargetDir);
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
console.error(`❌ [gen-index-ts] 执行失败: ${msg}`);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export default function App() {
|
||||
return <></>;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
import { createRoot } from "react-dom/client";
|
||||
import App from "./App";
|
||||
|
||||
const container = document.getElementById("root")!;
|
||||
const root = createRoot(container);
|
||||
|
||||
root.render(<App />);
|
||||
4
templates/vite-react-app/src/types/css.d.ts
vendored
4
templates/vite-react-app/src/types/css.d.ts
vendored
@@ -1,4 +0,0 @@
|
||||
declare module '*.css' {
|
||||
const content: { [className: string]: string };
|
||||
export default content;
|
||||
}
|
||||
51
templates/vite-react-app/src/types/env.d.ts
vendored
51
templates/vite-react-app/src/types/env.d.ts
vendored
@@ -1,51 +0,0 @@
|
||||
/// <reference types="vite/client" />
|
||||
|
||||
declare module "*.svg" {
|
||||
import * as React from "react";
|
||||
export const ReactComponent: React.FC<React.SVGProps<SVGSVGElement>>;
|
||||
const src: string;
|
||||
export default src;
|
||||
}
|
||||
|
||||
declare module "*.png" {
|
||||
const src: string;
|
||||
export default src;
|
||||
}
|
||||
|
||||
declare module "*.jpg" {
|
||||
const src: string;
|
||||
export default src;
|
||||
}
|
||||
|
||||
declare module "*.jpeg" {
|
||||
const src: string;
|
||||
export default src;
|
||||
}
|
||||
|
||||
declare module "*.gif" {
|
||||
const src: string;
|
||||
export default src;
|
||||
}
|
||||
|
||||
declare module "*.webp" {
|
||||
const src: string;
|
||||
export default src;
|
||||
}
|
||||
|
||||
interface ImportMetaEnv {
|
||||
readonly MODE: "development" | "production" | "test";
|
||||
readonly BASE_URL: string;
|
||||
readonly PROD: boolean;
|
||||
readonly DEV: boolean;
|
||||
readonly SSR: boolean;
|
||||
|
||||
// ===== 业务环境变量 =====
|
||||
readonly VITE_API_BASE: string;
|
||||
readonly VITE_UPLOAD_URL?: string;
|
||||
readonly VITE_ENABLE_MOCK?: "true" | "false";
|
||||
readonly VITE_SENTRY_DSN?: string;
|
||||
}
|
||||
|
||||
interface ImportMeta {
|
||||
readonly env: ImportMetaEnv;
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "es2022",
|
||||
"moduleResolution": "bundler",
|
||||
"lib": ["ES2025", "DOM", "DOM.Iterable"],
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"isolatedModules": true,
|
||||
"verbatimModuleSyntax": true
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
{
|
||||
"extends": "./tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"jsx": "react-jsx",
|
||||
"declaration": true
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"dist",
|
||||
|
||||
// ---------- build / cache ----------
|
||||
".turbo/**/*",
|
||||
".cache/**/*",
|
||||
".vite/**/*",
|
||||
|
||||
// ---------- 配置文件 ----------
|
||||
"vite.config.ts",
|
||||
"*.config.ts",
|
||||
"*.config.js",
|
||||
"tsconfig.*.json",
|
||||
|
||||
// ---------- 测试相关 ----------
|
||||
"__tests__/**/*",
|
||||
"test/**/*",
|
||||
"tests/**/*",
|
||||
"**/*.test.ts",
|
||||
"**/*.test.tsx",
|
||||
"**/*.spec.ts",
|
||||
"**/*.spec.tsx",
|
||||
|
||||
// ---------- Storybook ----------
|
||||
".storybook/**/*",
|
||||
"stories/**/*",
|
||||
|
||||
// ---------- 示例 / 脚本 ----------
|
||||
"example/**/*",
|
||||
"examples/**/*",
|
||||
"scripts/**/*",
|
||||
|
||||
// ---------- 环境与静态资源 ----------
|
||||
".env",
|
||||
".env.*",
|
||||
"public/**/*",
|
||||
|
||||
// ---------- 文档 ----------
|
||||
"docs/**/*",
|
||||
"README.md",
|
||||
"LICENSE"
|
||||
]
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"extends": "./tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx",
|
||||
"noEmit": true
|
||||
},
|
||||
"include": ["."]
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import { defineConfig } from "vite";
|
||||
import react from "@vitejs/plugin-react";
|
||||
import dts from "vite-plugin-dts";
|
||||
import typescript from "@rollup/plugin-typescript";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
react(),
|
||||
dts({
|
||||
insertTypesEntry: true,
|
||||
}),
|
||||
],
|
||||
|
||||
esbuild: false,
|
||||
|
||||
build: {
|
||||
rollupOptions: {
|
||||
plugins: [typescript({ tsconfig: "./tsconfig.build.json" })],
|
||||
external: ["react", "react-dom", "react/jsx-runtime"],
|
||||
output: {
|
||||
globals: {
|
||||
react: "React",
|
||||
"react-dom": "ReactDOM",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
sourcemap: true,
|
||||
cssCodeSplit: true,
|
||||
},
|
||||
});
|
||||
@@ -1 +0,0 @@
|
||||
registry = https://registry.npmmirror.com/
|
||||
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"json.schemas": [
|
||||
{
|
||||
"fileMatch": ["/tsconfig.build.json", "/tsconfig.base.json"],
|
||||
"schema": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "vite-react-lib",
|
||||
"name": "@defgov/vite-react-lib",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
@@ -22,23 +22,18 @@
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"gen-css": "ts-node scripts/gen-index-css.ts",
|
||||
"gen-ts": "ts-node scripts/gen-index-ts.ts",
|
||||
"gen-index": "pnpm run gen-css && pnpm run gen-ts",
|
||||
"dev": "pnpm run gen-index && vite build --watch",
|
||||
"build": "pnpm run gen-index && tsc -p tsconfig.build.json && vite build"
|
||||
"gen-index": "ts-node scripts/gen-index.ts",
|
||||
"gen-dts": "tsc -p tsconfig.build.json",
|
||||
"dev": "pnpm gen-index && vite build --watch",
|
||||
"build": "pnpm gen-index && vite build && pnpm gen-dts"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-typescript": "^12.3.0",
|
||||
"@types/node": "^25.6.0",
|
||||
"@types/react": "^19.2.14",
|
||||
"@types/react-dom": "^19.2.3",
|
||||
"@vitejs/plugin-react": "~5.2.0",
|
||||
"@vitejs/plugin-react": "^6.0.1",
|
||||
"glob": "^13.0.6",
|
||||
"typescript": "~6.0.3",
|
||||
"vite": "~7.3.2",
|
||||
"vite-plugin-dts": "^4.5.4",
|
||||
"ts-node": "^10.9.2"
|
||||
"ts-node": "^10.9.2",
|
||||
"typescript": "^6.0.3",
|
||||
"vite": "^8.0.10"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": "^19",
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
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) {
|
||||
console.log("⚠️ 未找到符合条件的文件,跳过生成 index.ts");
|
||||
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");
|
||||
|
||||
// ✅ 内容比对,避免重复写入
|
||||
if (fs.existsSync(indexFilePath)) {
|
||||
const old = fs.readFileSync(indexFilePath, "utf8");
|
||||
if (old === indexContent) {
|
||||
console.log("✅ index.ts 内容无变化,无需重新生成");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(indexFilePath, indexContent, "utf8");
|
||||
console.log(`✅ 成功生成 index.ts: ${indexFilePath}`);
|
||||
}
|
||||
|
||||
// 脚本入口
|
||||
const [targetDir] = CONFIG.targetDirs;
|
||||
if (!targetDir) {
|
||||
console.error("❌ CONFIG.targetDirs 不能为空");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const absTargetDir = path.resolve(process.cwd(), targetDir);
|
||||
|
||||
try {
|
||||
console.log(`🚀 开始扫描目录: ${absTargetDir}`);
|
||||
generateIndexFile(absTargetDir);
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
console.error(`❌ [gen-index-ts] 执行失败: ${msg}`);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
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) {
|
||||
console.log("⚠️ 未找到符合条件的文件,跳过生成 index.ts");
|
||||
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");
|
||||
|
||||
// ✅ 内容比对,避免重复写入
|
||||
if (fs.existsSync(indexFilePath)) {
|
||||
const old = fs.readFileSync(indexFilePath, "utf8");
|
||||
if (old === indexContent) {
|
||||
console.log("✅ index.ts 内容无变化,无需重新生成");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(indexFilePath, indexContent, "utf8");
|
||||
console.log(`✅ 成功生成 index.ts: ${indexFilePath}`);
|
||||
}
|
||||
|
||||
// 脚本入口
|
||||
const [targetDir] = CONFIG.targetDirs;
|
||||
if (!targetDir) {
|
||||
console.error("❌ CONFIG.targetDirs 不能为空");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const absTargetDir = path.resolve(process.cwd(), targetDir);
|
||||
|
||||
try {
|
||||
console.log(`🚀 开始扫描目录: ${absTargetDir}`);
|
||||
generateIndexFile(absTargetDir);
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
console.error(`❌ [gen-index-ts] 执行失败: ${msg}`);
|
||||
process.exit(1);
|
||||
}
|
||||
147
templates/vite-react-lib/scripts/gen-index.ts
Normal file
147
templates/vite-react-lib/scripts/gen-index.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { globSync } from "glob";
|
||||
|
||||
interface Config {
|
||||
outputDir: string;
|
||||
outputFilenameWithExt: string;
|
||||
scanDirs: string[];
|
||||
importPrefix: string;
|
||||
predefineStatements: string[];
|
||||
includeExtensions: string[];
|
||||
excludeDirs: string[];
|
||||
excludeFileExtensions: string[];
|
||||
excludePatterns: RegExp[];
|
||||
}
|
||||
|
||||
const cssConfig: Config = {
|
||||
outputDir: "src",
|
||||
outputFilenameWithExt: "index.css",
|
||||
scanDirs: ["src"],
|
||||
importPrefix: "@import",
|
||||
predefineStatements: [],
|
||||
includeExtensions: [".css"],
|
||||
excludeDirs: ["__tests__", "tests", "story", "stories", "types"],
|
||||
excludeFileExtensions: [],
|
||||
excludePatterns: [/^index\.(css)$/, /\.(test|spec)\./, /\.(story|stories)\./],
|
||||
};
|
||||
|
||||
const tsConfig: Config = {
|
||||
outputDir: "src",
|
||||
outputFilenameWithExt: "index.ts",
|
||||
scanDirs: ["src"],
|
||||
importPrefix: "export * from",
|
||||
predefineStatements: ["import './index.css'"],
|
||||
includeExtensions: [".ts", "tsx", "js", "jsx"],
|
||||
excludeDirs: ["__tests__", "tests", "story", "stories", "types"],
|
||||
excludeFileExtensions: [".d.ts"],
|
||||
excludePatterns: [
|
||||
/^index\.(ts|tsx|js|jsx)$/,
|
||||
/\.(test|spec)\./,
|
||||
/\.(story|stories)\./,
|
||||
],
|
||||
};
|
||||
|
||||
const normalizePath = (p: string) => p.replace(/\\/g, "/");
|
||||
|
||||
const isExcludeDir = (filePath: string, excludeDirs: string[]) => {
|
||||
const normalized = normalizePath(filePath);
|
||||
return excludeDirs.some((dir) => normalized.includes(`/${dir}/`));
|
||||
};
|
||||
|
||||
const isExcludeFileExtensions = (
|
||||
filePath: string,
|
||||
excludeFileExtensions: string[],
|
||||
) => excludeFileExtensions.some((ext) => filePath.endsWith(ext));
|
||||
|
||||
const isExcludePattern = (fileName: string, excludePatterns: RegExp[]) =>
|
||||
excludePatterns.some((pattern) => pattern.test(fileName));
|
||||
|
||||
// ----------------------------------------
|
||||
function isValidFile(filePath: string, config: Config): boolean {
|
||||
const fileName = filePath.split(/[\\/]/).pop()!;
|
||||
|
||||
if (isExcludeDir(filePath, config.excludeDirs)) return false;
|
||||
if (isExcludeFileExtensions(filePath, config.excludeFileExtensions))
|
||||
return false;
|
||||
if (isExcludePattern(fileName, config.excludePatterns)) return false;
|
||||
|
||||
const ext = path.extname(filePath);
|
||||
return config.includeExtensions.includes(ext);
|
||||
}
|
||||
// -----------------------------------------
|
||||
function generateIndexFile(config: Config) {
|
||||
const currentPath = process.cwd();
|
||||
const outputPath = path.resolve(currentPath, config.outputDir);
|
||||
let exportStatements: string[] = [];
|
||||
|
||||
// ------ scanDirs forEach start ------------------------
|
||||
config.scanDirs.forEach((dir) => {
|
||||
const scanPattern = path.resolve(currentPath, dir, "**", "*.*");
|
||||
|
||||
const allFilePath = globSync(scanPattern, {
|
||||
absolute: true,
|
||||
windowsPathsNoEscape: true,
|
||||
dot: false,
|
||||
follow: true,
|
||||
});
|
||||
|
||||
const validFiles = allFilePath.filter((filePath) => {
|
||||
return isValidFile(filePath, config);
|
||||
});
|
||||
|
||||
if (validFiles.length === 0) {
|
||||
console.log(
|
||||
`⚠️ 未找到符合条件的文件,跳过生成 ${config.outputFilenameWithExt}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
validFiles.sort();
|
||||
|
||||
validFiles.forEach((file) => {
|
||||
const relativePath = path.relative(outputPath, file);
|
||||
const importPath = `./${relativePath.replace(/\\/g, "/")}`;
|
||||
exportStatements.push(`${config.importPrefix} '${importPath}';`);
|
||||
});
|
||||
});
|
||||
|
||||
// --------- scanDirs forEach end ----------------
|
||||
|
||||
const indexFileContent = `
|
||||
${config.predefineStatements.join("\n")}
|
||||
${exportStatements.join("\n")}
|
||||
`.trim();
|
||||
|
||||
const indexFilePath = path.resolve(
|
||||
currentPath,
|
||||
config.outputDir,
|
||||
config.outputFilenameWithExt,
|
||||
);
|
||||
|
||||
// ✅ 内容比对,避免重复写入
|
||||
if (fs.existsSync(indexFilePath)) {
|
||||
const old = fs.readFileSync(indexFilePath, "utf8");
|
||||
if (old === indexFileContent) {
|
||||
console.log(
|
||||
`✅ ${config.outputFilenameWithExt} 内容无变化,无需重新生成`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(indexFilePath, indexFileContent, "utf8");
|
||||
console.log(`✅ 成功生成 ${config.outputFilenameWithExt}: ${indexFilePath}`);
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
|
||||
try {
|
||||
console.log(`🚀 [gen-index] 开始扫描`);
|
||||
generateIndexFile(cssConfig);
|
||||
generateIndexFile(tsConfig);
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
console.error(`❌ [gen-index] 执行失败: ${msg}`);
|
||||
process.exit(1);
|
||||
}
|
||||
4
templates/vite-react-lib/src/types/css.d.ts
vendored
4
templates/vite-react-lib/src/types/css.d.ts
vendored
@@ -1,4 +0,0 @@
|
||||
declare module '*.css' {
|
||||
const content: { [className: string]: string };
|
||||
export default content;
|
||||
}
|
||||
53
templates/vite-react-lib/src/types/env.d.ts
vendored
53
templates/vite-react-lib/src/types/env.d.ts
vendored
@@ -1,51 +1,4 @@
|
||||
/// <reference types="vite/client" />
|
||||
|
||||
declare module "*.svg" {
|
||||
import * as React from "react";
|
||||
export const ReactComponent: React.FC<React.SVGProps<SVGSVGElement>>;
|
||||
const src: string;
|
||||
export default src;
|
||||
}
|
||||
|
||||
declare module "*.png" {
|
||||
const src: string;
|
||||
export default src;
|
||||
}
|
||||
|
||||
declare module "*.jpg" {
|
||||
const src: string;
|
||||
export default src;
|
||||
}
|
||||
|
||||
declare module "*.jpeg" {
|
||||
const src: string;
|
||||
export default src;
|
||||
}
|
||||
|
||||
declare module "*.gif" {
|
||||
const src: string;
|
||||
export default src;
|
||||
}
|
||||
|
||||
declare module "*.webp" {
|
||||
const src: string;
|
||||
export default src;
|
||||
}
|
||||
|
||||
interface ImportMetaEnv {
|
||||
readonly MODE: "development" | "production" | "test";
|
||||
readonly BASE_URL: string;
|
||||
readonly PROD: boolean;
|
||||
readonly DEV: boolean;
|
||||
readonly SSR: boolean;
|
||||
|
||||
// ===== 业务环境变量 =====
|
||||
readonly VITE_API_BASE: string;
|
||||
readonly VITE_UPLOAD_URL?: string;
|
||||
readonly VITE_ENABLE_MOCK?: "true" | "false";
|
||||
readonly VITE_SENTRY_DSN?: string;
|
||||
}
|
||||
|
||||
interface ImportMeta {
|
||||
readonly env: ImportMetaEnv;
|
||||
declare module "*.css" {
|
||||
const content: string;
|
||||
export default content;
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "es2022",
|
||||
"moduleResolution": "bundler",
|
||||
"lib": ["ES2025", "DOM", "DOM.Iterable"],
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"isolatedModules": true,
|
||||
"verbatimModuleSyntax": true
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,24 @@
|
||||
{
|
||||
"extends": "./tsconfig.base.json",
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"noEmit": false,
|
||||
"emitDeclarationOnly": true,
|
||||
"declaration": true,
|
||||
"rootDir": "./src",
|
||||
"jsx": "react-jsx",
|
||||
"declaration": true
|
||||
"outDir": "./dist",
|
||||
"declarationDir": "./dist"
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"dist",
|
||||
|
||||
// ---------- build / cache ----------
|
||||
".turbo/**/*",
|
||||
".cache/**/*",
|
||||
".vite/**/*",
|
||||
|
||||
// ---------- 配置文件 ----------
|
||||
"vite.config.ts",
|
||||
"*.config.ts",
|
||||
"*.config.js",
|
||||
"tsconfig.*.json",
|
||||
|
||||
// ---------- 测试相关 ----------
|
||||
"__tests__/**/*",
|
||||
"test/**/*",
|
||||
"tests/**/*",
|
||||
@@ -30,22 +26,14 @@
|
||||
"**/*.test.tsx",
|
||||
"**/*.spec.ts",
|
||||
"**/*.spec.tsx",
|
||||
|
||||
// ---------- Storybook ----------
|
||||
".storybook/**/*",
|
||||
"stories/**/*",
|
||||
|
||||
// ---------- 示例 / 脚本 ----------
|
||||
"example/**/*",
|
||||
"examples/**/*",
|
||||
"scripts/**/*",
|
||||
|
||||
// ---------- 环境与静态资源 ----------
|
||||
".env",
|
||||
".env.*",
|
||||
"public/**/*",
|
||||
|
||||
// ---------- 文档 ----------
|
||||
"docs/**/*",
|
||||
"README.md",
|
||||
"LICENSE"
|
||||
|
||||
@@ -1,8 +1,26 @@
|
||||
{
|
||||
"extends": "./tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"target": "es2025",
|
||||
"lib": ["ES2025", "DOM", "DOM.Iterable"],
|
||||
"module": "ESNext",
|
||||
"types": ["node", "vite/client"],
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
|
||||
/* Bundler mode */
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"moduleDetection": "force",
|
||||
"noEmit": true,
|
||||
"jsx": "react-jsx",
|
||||
"noEmit": true
|
||||
|
||||
/* Linting */
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"erasableSyntaxOnly": true,
|
||||
"noFallthroughCasesInSwitch": true
|
||||
},
|
||||
"include": ["."]
|
||||
"include": ["src", "scripts", "vite.config.ts"]
|
||||
}
|
||||
|
||||
@@ -1,30 +1,28 @@
|
||||
import { defineConfig } from "vite";
|
||||
import react from "@vitejs/plugin-react";
|
||||
import dts from "vite-plugin-dts";
|
||||
import path from "path";
|
||||
import typescript from "@rollup/plugin-typescript";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
react(),
|
||||
dts({
|
||||
insertTypesEntry: true,
|
||||
}),
|
||||
],
|
||||
|
||||
esbuild: false,
|
||||
// d.ts 由 tsc 完成,使用专门配置 tsconfig.build.json
|
||||
// 转换由 oxc 完成
|
||||
// 打包由 rolldown 完成
|
||||
plugins: [react()],
|
||||
|
||||
build: {
|
||||
cssMinify: false,
|
||||
lib: {
|
||||
entry: path.resolve(__dirname, "src/index.ts"),
|
||||
name: "DefgovUIWeb",
|
||||
entry: path.resolve(import.meta.dirname, "src/index.ts"),
|
||||
// 不写 vite 默认是 index.mjs 和 index.js,不方便识别
|
||||
formats: ["es", "cjs"],
|
||||
// package.json 管别人怎么使用,
|
||||
// build.lib 管怎么打包,生成什么
|
||||
// 如果不一致,就会报错,指向文件不存在
|
||||
fileName: (format) => `index.${format}.js`,
|
||||
},
|
||||
|
||||
rollupOptions: {
|
||||
plugins: [typescript({ tsconfig: "./tsconfig.build.json" })],
|
||||
rolldownOptions: {
|
||||
// 避免这些被打包,peerDependencies 对 rolldown 是无效的,不会自动 external
|
||||
external: ["react", "react-dom", "react/jsx-runtime"],
|
||||
// 专门给 UMD / IIFE 的映射表
|
||||
output: {
|
||||
globals: {
|
||||
react: "React",
|
||||
@@ -33,7 +31,9 @@ export default defineConfig({
|
||||
},
|
||||
},
|
||||
|
||||
sourcemap: true,
|
||||
cssCodeSplit: true,
|
||||
emptyOutDir: true, // 防止旧产物残留
|
||||
sourcemap: true, // 方便调试
|
||||
cssCodeSplit: false, // 合并成一个css文件
|
||||
outDir: "dist", // 专管打包输出目录,tsconfig.build.json 中的 outDir 管的是 d.ts 输出目录
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user