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 @@
registry = https://registry.npmmirror.com/

View File

@@ -0,0 +1,8 @@
{
"json.schemas": [
{
"fileMatch": ["/tsconfig.build.json", "/tsconfig.base.json"],
"schema": {}
}
]
}

View File

@@ -0,0 +1,46 @@
{
"name": "vite-react-lib",
"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": {
"@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"
},
"peerDependencies": {
"react": "^19",
"react-dom": "^19"
}
}

View File

@@ -0,0 +1,121 @@
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);
}

View File

@@ -0,0 +1,121 @@
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);
}

View File

@@ -0,0 +1,4 @@
declare module '*.css' {
const content: { [className: string]: string };
export default content;
}

View File

@@ -0,0 +1,51 @@
/// <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;
}

View File

@@ -0,0 +1,22 @@
{
// tsconfig.base.json用于被 tesconfig.json 和 tesconfig.build.json 继承
"compilerOptions": {
// 输出模块语法,使用版本号最新的那个,而不是实验性语法 ESNext
"module": "es2022",
// 模块解析策略,模拟 Vite / Rollup / webpack支持 exports / imports不强制 Node ESM 的严格规则
"moduleResolution": "bundler",
// 显式声明使用的类型包
"types": ["node", "react"],
// 开启所有严格类型检查,防止 any / 隐式 any 扩散
"strict": true,
// 允许 ESM 导入 CJS
"esModuleInterop": true,
// 跳过 node_modules 类型检查,加快构建,避免第三方类型污染
"skipLibCheck": true
}
}

View File

@@ -0,0 +1,115 @@
{
// 此文件仅用于类型检查,不用于类型检查,构建时会指定使用 tsconfig.build.json
"extends": "./tsconfig.base.json",
"compilerOptions": {
// Browser api需要加 "DOM""DOM.Iterable"
// Node api需要加 "ES2025",始终使用带版本号的最新版本
// NextJs api属于同构server 端会预处理 DOM计算url三个都需要 "ES2025", "DOM", "DOM.Iterable"
"lib": ["ES2025", "DOM", "DOM.Iterable"],
/**
* 使
* - nodeNode.js API
* - reactJSX / React
* - vite/clientimport.meta / env
*/
"types": ["node", "react"],
/**
* React JSX
* - 使 React 17+ JSX Transform
* - import React
*/
"jsx": "react-jsx",
/**
*
* - tsc / tsc -b
*/
"outDir": "./dist",
/**
*
* - dist src
* - declaration
*/
"rootDir": "./src",
/**
* .d.ts
* - / npm
* -
*/
"declaration": true,
/**
* JS
* - Vite / Next / Nuxt bundler
* - tsc emit
*/
"noEmit": true,
/**
*
* - esbuild / SWC / bundler
* - enum / namespace
*/
"isolatedModules": true
},
/**
*
* - src
* - exclude
*/
"include": ["src"],
/**
*
* -
* - dist / test / config
* -
*/
"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"
]
}

View File

@@ -0,0 +1,16 @@
{
// 此文件仅用于类型检查,不用于构建,构建时会指定使用 tsconfig.build.json
"extends": "./tsconfig.base.json",
"compilerOptions": {
// node api 使用最新版本号的 "ESxxxx"browser api 使用 "DOM" 和 "DOM.Iterable"
"lib": ["ES2025", "DOM", "DOM.Iterable"],
//显式声明使用的类型包,避免找不到模块
"types": ["node", "react"],
// React JSX 编译模式
"jsx": "react-jsx"
},
// 将类型检查范围扩大至整个子项目,而不只是 src 文件夹,
"include": ["**/*"]
}

View File

@@ -0,0 +1,39 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import dts from "vite-plugin-dts";
import path from "path";
export default defineConfig({
plugins: [
react(),
dts({
insertTypesEntry: true,
}),
],
build: {
lib: {
entry: path.resolve(__dirname, "src/index.ts"),
name: "DefgovUIWeb",
formats: ["es", "cjs"],
fileName: (format) => `index.${format}.js`,
},
rollupOptions: {
external: ["react", "react-dom", "react/jsx-runtime"],
output: {
globals: {
react: "React",
"react-dom": "ReactDOM",
},
},
},
sourcemap: true,
cssCodeSplit: true,
watch: {
exclude: ["node_modules", "dist", ".git"],
},
},
});