79 lines
2.3 KiB
TypeScript
79 lines
2.3 KiB
TypeScript
import { defineConfig } from "vite";
|
||
import react from "@vitejs/plugin-react";
|
||
import dts from "vite-plugin-dts";
|
||
import { resolve } from "node:path";
|
||
import tailwindcss from "@tailwindcss/vite";
|
||
|
||
export default defineConfig(({ mode }) => {
|
||
const isProduction = mode === "production";
|
||
|
||
return {
|
||
// React 核心插件
|
||
plugins: [
|
||
tailwindcss(),
|
||
react(),
|
||
dts({
|
||
include: ["src/**/*"],
|
||
exclude: ["src/**/*.test.ts", "src/**/*.stories.tsx"],
|
||
outDir: "./dist",
|
||
rollupTypes: true, // 合并类型声明文件
|
||
}),
|
||
],
|
||
|
||
// 路径别名与后缀配置
|
||
resolve: {
|
||
alias: {
|
||
"@": resolve(__dirname, "./src"),
|
||
},
|
||
extensions: [".ts", ".tsx", ".js", ".jsx", ".json"],
|
||
},
|
||
|
||
// 纯 ES 模式打包配置(移除 UMD 相关)
|
||
build: {
|
||
lib: {
|
||
entry: resolve(__dirname, "./src/index.ts"),
|
||
formats: ["es"], // 仅保留 ES 模块格式
|
||
fileName: () => "index.es.js", // 固定 ES 模式文件名
|
||
},
|
||
rollupOptions: {
|
||
// 排除 React 相关依赖(用户项目自行引入)
|
||
external: ["react", "react-dom"],
|
||
output: {
|
||
compact: isProduction, // 生产环境压缩代码格式
|
||
globals: {
|
||
react: "React",
|
||
"react-dom": "ReactDOM",
|
||
},
|
||
assetFileNames: (assetInfo) => {
|
||
if (assetInfo.name && assetInfo.name.endsWith(".css")) {
|
||
return "index.css";
|
||
}
|
||
return assetInfo.name || "[name].[ext]";
|
||
},
|
||
},
|
||
// 移除:rollupOptions 下无效的 exclude 配置
|
||
// exclude: ["example/**/*", "scripts/**/*"],
|
||
},
|
||
outDir: "./dist",
|
||
cssCodeSplit: false,
|
||
sourcemap: true,
|
||
minify: isProduction ? "esbuild" : false,
|
||
emptyOutDir: true,
|
||
},
|
||
|
||
// TS 兼容配置
|
||
esbuild: {
|
||
ignoreAnnotations: true,
|
||
// 移除:esbuild 下无效的 exclude 配置
|
||
// exclude: ["example/**/*", "scripts/**/*"],
|
||
},
|
||
|
||
// 新增:使用 Vite 官方支持的方式排除文件
|
||
// 通过 optimizeDeps.exclude 排除依赖,通过 build.assetsInclude 反向控制,
|
||
// 同时结合 tsconfig.json 的 exclude 确保 TS 编译也排除目标文件
|
||
optimizeDeps: {
|
||
exclude: ["example", "scripts"],
|
||
},
|
||
};
|
||
});
|