143 lines
2.9 KiB
TypeScript
143 lines
2.9 KiB
TypeScript
import { existsSync, readdirSync, statSync } from "fs";
|
|
import { relative, resolve } from "path";
|
|
import { Alias, defineConfig } from "vite";
|
|
import vue from "@vitejs/plugin-vue";
|
|
import VueSetupExtend from "vite-plugin-vue-setup-extend";
|
|
import { prismjsPlugin } from "vite-plugin-prismjs";
|
|
import AutoImport from "unplugin-auto-import/vite";
|
|
import Components from "unplugin-vue-components/vite";
|
|
import dts from "vite-plugin-dts";
|
|
|
|
const alias: Alias[] = [
|
|
{
|
|
find: /^@timi\/web(\/(es|lib))?$/,
|
|
replacement: resolve(__dirname, "./src/index.ts")
|
|
}
|
|
];
|
|
|
|
/**
|
|
* 递归收集组件入口,用于按组件导出。
|
|
* 例如 src/components/icon/index.ts -> components/icon/index
|
|
*/
|
|
function collectComponentEntries(dir: string, root: string): Record<string, string> {
|
|
const entries: Record<string, string> = {};
|
|
|
|
for (const name of readdirSync(dir)) {
|
|
const fullPath = resolve(dir, name);
|
|
|
|
if (!statSync(fullPath).isDirectory()) {
|
|
continue;
|
|
}
|
|
|
|
const indexPath = resolve(fullPath, "index.ts");
|
|
|
|
if (existsSync(indexPath)) {
|
|
const entryName = relative(root, indexPath)
|
|
.replace(/\\/g, "/")
|
|
.replace(/\.ts$/, "");
|
|
entries[entryName] = indexPath;
|
|
} else {
|
|
Object.assign(entries, collectComponentEntries(fullPath, root));
|
|
}
|
|
}
|
|
|
|
return entries;
|
|
}
|
|
|
|
const srcRoot = resolve(__dirname, "src");
|
|
const componentEntries = collectComponentEntries(resolve(srcRoot, "components"), srcRoot);
|
|
|
|
export default defineConfig({
|
|
server: {
|
|
port: 3003,
|
|
host: true
|
|
},
|
|
resolve: {
|
|
alias
|
|
},
|
|
build: {
|
|
target: "es2017",
|
|
outDir: "dist",
|
|
lib: {
|
|
entry: {
|
|
index: resolve(srcRoot, "index.ts"),
|
|
"components/index": resolve(srcRoot, "components/index.ts"),
|
|
"editor/index": resolve(srcRoot, "editor/index.ts"),
|
|
"utils/index": resolve(srcRoot, "utils/index.ts"),
|
|
...componentEntries
|
|
},
|
|
formats: ["es"],
|
|
fileName: (_format, entryName) => `${entryName}.js`,
|
|
cssFileName: "timi-web"
|
|
},
|
|
rollupOptions: {
|
|
external: [
|
|
"vue",
|
|
"@timi/ts",
|
|
"marked",
|
|
/^marked-/,
|
|
"prismjs",
|
|
/^prismjs\//,
|
|
/^@tiptap\//,
|
|
"@marsio/vue-split-pane"
|
|
],
|
|
output: {
|
|
chunkFileNames: "chunks/[name]-[hash].js"
|
|
}
|
|
},
|
|
cssCodeSplit: false,
|
|
minify: "terser"
|
|
},
|
|
plugins: [
|
|
vue({
|
|
include: [/\.vue$/, /\.md$/]
|
|
}),
|
|
VueSetupExtend(),
|
|
dts({
|
|
// 按组件导出后,声明文件直接落在 dist/**,与 exports 的路径保持一致。
|
|
entryRoot: "src"
|
|
}),
|
|
prismjsPlugin({
|
|
languages: [
|
|
"ini",
|
|
"php",
|
|
"sql",
|
|
"xml",
|
|
"css",
|
|
"less",
|
|
"html",
|
|
"json",
|
|
"yaml",
|
|
"java",
|
|
"nginx",
|
|
"groovy",
|
|
"javascript",
|
|
"typescript",
|
|
"apacheconf",
|
|
"properties",
|
|
"bash"
|
|
],
|
|
plugins: [
|
|
"line-numbers"
|
|
],
|
|
theme: "default",
|
|
css: true
|
|
}),
|
|
AutoImport({
|
|
imports: [
|
|
"vue"
|
|
],
|
|
dts: "auto-imports.d.ts",
|
|
eslintrc: {
|
|
enabled: true,
|
|
globalsPropValue: true
|
|
}
|
|
}),
|
|
Components({
|
|
dirs: [
|
|
"src/components"
|
|
]
|
|
})
|
|
]
|
|
});
|