109 lines
2.9 KiB
TypeScript
109 lines
2.9 KiB
TypeScript
import { defineConfig } from 'vite'
|
|
import vue from '@vitejs/plugin-vue'
|
|
import vueJsx from '@vitejs/plugin-vue-jsx'
|
|
import { resolve, dirname } from 'path'
|
|
import { fileURLToPath } from 'url'
|
|
|
|
// ES 模块中获取 __dirname
|
|
const __filename = fileURLToPath(import.meta.url)
|
|
const __dirname = dirname(__filename)
|
|
|
|
/**
|
|
* ES Module 构建配置
|
|
* 用于 Import Maps 方案
|
|
*
|
|
* 策略:将 Vue、Element Plus 等依赖打包进共享模块
|
|
* 业务应用只需引入 @shared/*,无需关心底层依赖
|
|
*/
|
|
export default defineConfig({
|
|
plugins: [vue(), vueJsx()],
|
|
define: {
|
|
__VUE_PROD_DEVTOOLS__: true,
|
|
// __VUE_OPTIONS_API__: true, // 确保启用 Options API
|
|
// __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: true
|
|
},
|
|
resolve: {
|
|
alias: [
|
|
{ find: '@', replacement: resolve(__dirname, 'src') }
|
|
],
|
|
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
|
|
},
|
|
|
|
build: {
|
|
lib: {
|
|
entry: {
|
|
components: resolve(__dirname, 'src/components/index.ts'),
|
|
utils: resolve(__dirname, 'src/utils/index.ts'),
|
|
api: resolve(__dirname, 'src/api/index.ts'),
|
|
composables: resolve(__dirname, 'src/composables/index.ts'),
|
|
types: resolve(__dirname, 'src/types/index.ts')
|
|
},
|
|
formats: ['es'], // 仅构建 ES Module
|
|
fileName: (format, entryName) => `${entryName}.js`
|
|
},
|
|
|
|
rollupOptions: {
|
|
// ⚠️ 不外部化依赖,将它们打包进共享模块
|
|
// 这样业务应用只需引入 @shared/* 即可
|
|
external: [],
|
|
|
|
output: {
|
|
// 保持 ES Module 格式
|
|
format: 'es',
|
|
// 导出命名导出
|
|
exports: 'named',
|
|
// 生成 sourcemap
|
|
sourcemap: true,
|
|
// 分块策略:将大的依赖分离出来
|
|
manualChunks(id) {
|
|
// Vue 核心
|
|
if (id.includes('node_modules/vue/') ||
|
|
id.includes('node_modules/@vue/')) {
|
|
return 'vue-core'
|
|
}
|
|
// Vue Router
|
|
if (id.includes('node_modules/vue-router/')) {
|
|
return 'vue-router'
|
|
}
|
|
// Pinia
|
|
if (id.includes('node_modules/pinia/')) {
|
|
return 'pinia'
|
|
}
|
|
// Element Plus
|
|
if (id.includes('node_modules/element-plus/')) {
|
|
return 'element-plus'
|
|
}
|
|
// VueUse
|
|
if (id.includes('node_modules/@vueuse/')) {
|
|
return 'vueuse'
|
|
}
|
|
}
|
|
}
|
|
},
|
|
|
|
// 输出目录
|
|
outDir: 'dist/esm',
|
|
emptyOutDir: true,
|
|
|
|
// 目标浏览器
|
|
target: 'esnext',
|
|
|
|
// 不压缩(开发环境)
|
|
minify: false,
|
|
|
|
// 启用代码分割
|
|
cssCodeSplit: true
|
|
},
|
|
// 开发服务器配置
|
|
server: {
|
|
port: 5000,
|
|
host: true,
|
|
cors: true,
|
|
headers: {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
// 'Content-Type': 'application/javascript; charset=utf-8'
|
|
}
|
|
}
|
|
})
|