mm
This commit is contained in:
14
packages/ui-web-tw/.vscode/settings.json
vendored
14
packages/ui-web-tw/.vscode/settings.json
vendored
@@ -1,14 +0,0 @@
|
||||
{
|
||||
"json.schemas": [
|
||||
{
|
||||
"fileMatch": ["/tsconfig.build.json", "/tsconfig.base.json"],
|
||||
"schema": {}
|
||||
}
|
||||
],
|
||||
|
||||
"files.associations": {
|
||||
"*.css": "tailwindcss"
|
||||
},
|
||||
|
||||
"extensions.disabledExtensions": ["vunguyentuan.vscode-css-variables"]
|
||||
}
|
||||
@@ -22,30 +22,27 @@
|
||||
"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",
|
||||
"@tailwindcss/vite": "^4.2.4",
|
||||
"@types/node": "^25.6.0",
|
||||
"@tailwindcss/vite": "^4.3.0",
|
||||
"@types/node": "^25.6.2",
|
||||
"@types/react": "^19.2.14",
|
||||
"@types/react-dom": "^19.2.3",
|
||||
"@vitejs/plugin-react": "~5.2.0",
|
||||
"@vitejs/plugin-react": "^6.0.1",
|
||||
"tinyglobby": "^0.2.16",
|
||||
"ts-node": "^10.9.2",
|
||||
"typescript": "~6.0.3",
|
||||
"vite": "~7.3.2",
|
||||
"vite-plugin-dts": "^5.0.0"
|
||||
"typescript": "^6.0.3",
|
||||
"vite": "^8.0.12"
|
||||
},
|
||||
"dependencies": {
|
||||
"@base-ui/react": "^1.4.1",
|
||||
"tailwind-merge": "^3.5.0",
|
||||
"tailwind-merge": "^3.6.0",
|
||||
"tailwind-variants": "^3.2.2",
|
||||
"tailwindcss": "^4.2.4"
|
||||
"tailwindcss": "^4.3.0"
|
||||
},
|
||||
"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
packages/ui-web-tw/scripts/gen-index.ts
Normal file
147
packages/ui-web-tw/scripts/gen-index.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
// 1. 引入 tinyglobby
|
||||
import { globSync } from "tinyglobby";
|
||||
|
||||
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: ['@import "tailwindcss";'],
|
||||
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) => {
|
||||
// 2. 路径模式保持不变,tinyglobby 能够正确处理
|
||||
const scanPattern = path.resolve(currentPath, dir, "**", "*.*");
|
||||
|
||||
const allFilePath = globSync(scanPattern, {
|
||||
absolute: true,
|
||||
// 3. 移除了 windowsPathsNoEscape,tinyglobby 默认处理路径更智能
|
||||
});
|
||||
|
||||
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,8 +4,9 @@
|
||||
"noEmit": false,
|
||||
"emitDeclarationOnly": true,
|
||||
"declaration": true,
|
||||
"outDir": "dist",
|
||||
"declarationDir": "dist/types"
|
||||
"declarationDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"outDir": "./dist"
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": [
|
||||
|
||||
@@ -22,5 +22,5 @@
|
||||
"erasableSyntaxOnly": true,
|
||||
"noFallthroughCasesInSwitch": true
|
||||
},
|
||||
"include": ["."]
|
||||
"include": ["src", "scripts", "vite.config.ts"]
|
||||
}
|
||||
|
||||
@@ -1,29 +1,40 @@
|
||||
import { defineConfig } from "vite";
|
||||
import react from "@vitejs/plugin-react";
|
||||
import dts from "vite-plugin-dts";
|
||||
import path from "path";
|
||||
import tailwindcss from "@tailwindcss/vite";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
react(),
|
||||
tailwindcss(),
|
||||
dts({
|
||||
insertTypesEntry: true,
|
||||
}),
|
||||
],
|
||||
|
||||
esbuild: false,
|
||||
// d.ts 由 tsc 完成,使用专门配置 tsconfig.build.json
|
||||
// 转换由 oxc 完成
|
||||
// 打包由 rolldown 完成
|
||||
plugins: [react(), tailwindcss()],
|
||||
|
||||
build: {
|
||||
cssMinify: false,
|
||||
lib: {
|
||||
entry: path.resolve(__dirname, "src/index.ts"),
|
||||
name: "DefgovUIWebTw",
|
||||
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`,
|
||||
},
|
||||
rolldownOptions: {
|
||||
// 避免这些被打包,peerDependencies 对 rolldown 是无效的,不会自动 external
|
||||
external: ["react", "react-dom", "react/jsx-runtime"],
|
||||
// 专门给 UMD / IIFE 的映射表
|
||||
output: {
|
||||
globals: {
|
||||
react: "React",
|
||||
"react-dom": "ReactDOM",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
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