mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2026-11-20 02:03:35 +08:00
Share workspace packages between Ray Admin and the interactive docs. BREAKING CHANGE: v6 is a new major line. v5 stays on legacy/v5.
229 lines
6.5 KiB
TypeScript
229 lines
6.5 KiB
TypeScript
import { dirname, resolve } from 'node:path'
|
||
import { fileURLToPath } from 'node:url'
|
||
import viteVeI18nPlugin from '@intlify/unplugin-vue-i18n/vite'
|
||
import svgSpritemap from '@spiriit/vite-plugin-svg-spritemap'
|
||
import vue from '@vitejs/plugin-vue'
|
||
import viteVueJSX from '@vitejs/plugin-vue-jsx'
|
||
import gzipPlugin from 'rollup-plugin-gzip'
|
||
import unoCSS from 'unocss/vite'
|
||
import viteAutoImport from 'unplugin-auto-import/vite'
|
||
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
|
||
import unpluginViteComponents from 'unplugin-vue-components/vite'
|
||
import type { PluginOption } from 'vite'
|
||
import { analyzer } from 'vite-bundle-analyzer'
|
||
import { cdn as viteCDNPlugin } from 'vite-plugin-cdn2'
|
||
import { ViteEjsPlugin as viteEjsPlugin } from 'vite-plugin-ejs'
|
||
import viteInspect from 'vite-plugin-inspect'
|
||
import { mockDevServerPlugin } from 'vite-plugin-mock-dev-server'
|
||
import viteSvgLoader from 'vite-svg-loader'
|
||
import { iconNameFromPath, iconsRoot, scanIcons } from './scripts/icons.mjs'
|
||
import { iconManifestPlugin } from './vite-helper/icons.ts'
|
||
import { cdnResolve } from './vite-helper/index.ts'
|
||
import { siteInfo, siteSeoPlugin } from './vite-helper/site-seo.ts'
|
||
import config from './vite.custom.config.ts'
|
||
|
||
// 仅适用于报告模式(report)
|
||
function onlyReportOptions(mode: string): PluginOption[] {
|
||
if (mode !== 'report') {
|
||
return []
|
||
}
|
||
|
||
return [
|
||
analyzer({
|
||
// 输出静态报告,适合终端与 CI
|
||
analyzerMode: 'static',
|
||
// 不自动打开浏览器
|
||
openAnalyzer: false,
|
||
}),
|
||
]
|
||
}
|
||
|
||
// 仅适用于构建模式(任何构建模式:preview、build、report...)
|
||
function onlyBuildOptions(mode: string): PluginOption[] {
|
||
const { cdn } = config
|
||
|
||
return [
|
||
viteCDNPlugin({
|
||
// modules 顺序 vue, vue-demi 必须保持当前顺序加载,否则会出现加载错误问题
|
||
resolve: cdnResolve(),
|
||
modules: cdn
|
||
? [
|
||
{
|
||
name: 'vue',
|
||
global: 'Vue',
|
||
relativeModule: 'dist/vue.global.prod.js',
|
||
},
|
||
{
|
||
name: 'vue-demi',
|
||
global: 'VueDemi',
|
||
relativeModule: 'lib/index.iife.js',
|
||
},
|
||
{
|
||
name: 'naive-ui',
|
||
global: 'naive',
|
||
relativeModule: 'dist/index.prod.js',
|
||
},
|
||
{
|
||
name: 'pinia',
|
||
global: 'Pinia',
|
||
relativeModule: 'dist/pinia.iife.prod.js',
|
||
},
|
||
{
|
||
name: 'vue-router',
|
||
global: 'VueRouter',
|
||
relativeModule: 'dist/vue-router.global.prod.js',
|
||
},
|
||
{
|
||
name: 'vue-i18n',
|
||
global: 'VueI18n',
|
||
relativeModule: 'dist/vue-i18n.global.prod.js',
|
||
},
|
||
{
|
||
name: 'echarts',
|
||
global: 'echarts',
|
||
relativeModule: 'dist/echarts.min.js',
|
||
},
|
||
{
|
||
name: 'axios',
|
||
global: 'axios',
|
||
relativeModule: 'dist/axios.min.js',
|
||
},
|
||
{
|
||
name: 'jsbarcode',
|
||
global: 'JsBarcode',
|
||
relativeModule: 'dist/JsBarcode.all.min.js',
|
||
},
|
||
{
|
||
name: 'dayjs',
|
||
global: 'dayjs',
|
||
relativeModule: 'dayjs.min.js',
|
||
},
|
||
{
|
||
name: 'print-js',
|
||
global: 'printJS',
|
||
relativeModule: 'dist/print.js',
|
||
},
|
||
]
|
||
: [],
|
||
}),
|
||
]
|
||
}
|
||
|
||
// 仅适用于开发模式
|
||
function onlyDevOptions(mode: string): PluginOption[] {
|
||
if (mode !== 'development') {
|
||
return []
|
||
}
|
||
|
||
return []
|
||
}
|
||
|
||
// 开发、构建和单元测试共用 Vue/TSX 与自动导入转换。
|
||
function vueTransformPlugins(): PluginOption[] {
|
||
return [
|
||
vue(),
|
||
viteVueJSX(),
|
||
viteAutoImport({
|
||
eslintrc: {
|
||
enabled: true,
|
||
filepath: fileURLToPath(
|
||
new URL('./unplugin/.eslintrc-auto-import.json', import.meta.url),
|
||
),
|
||
},
|
||
include: [
|
||
/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
|
||
/\.vue$/,
|
||
/\.vue\?vue/, // .vue
|
||
/\.md$/, // .md
|
||
],
|
||
dts: fileURLToPath(
|
||
new URL('./unplugin/auto-imports.d.ts', import.meta.url),
|
||
),
|
||
imports: ['vue', 'vue-router', 'pinia'],
|
||
resolvers: [NaiveUiResolver()],
|
||
}),
|
||
unpluginViteComponents({
|
||
dts: fileURLToPath(
|
||
new URL('./unplugin/components.d.ts', import.meta.url),
|
||
),
|
||
resolvers: [NaiveUiResolver()],
|
||
types: [
|
||
{
|
||
from: 'vue-router',
|
||
names: ['RouterLink', 'RouterView'],
|
||
},
|
||
],
|
||
}),
|
||
]
|
||
}
|
||
|
||
// 基础插件配置
|
||
function baseOptions(mode: string): PluginOption[] {
|
||
const { appPrimaryColor, preloadingConfig } = config
|
||
const iconPackages = [
|
||
...new Set(scanIcons().map((name) => name.split('/')[0])),
|
||
]
|
||
|
||
return [
|
||
...vueTransformPlugins(),
|
||
iconManifestPlugin(iconPackages),
|
||
siteSeoPlugin(),
|
||
viteVeI18nPlugin({
|
||
include: [
|
||
resolve(
|
||
dirname(fileURLToPath(import.meta.url)),
|
||
'./apps/admin/src/locales/lang/**/*.json',
|
||
),
|
||
],
|
||
}),
|
||
gzipPlugin(),
|
||
viteSvgLoader({
|
||
defaultImport: 'url', // 默认以 url 形式导入 svg
|
||
}),
|
||
|
||
viteEjsPlugin({
|
||
preloadingConfig,
|
||
appPrimaryColor,
|
||
siteInfo,
|
||
}),
|
||
// 仅适用于开发模式(检查 `Vite` 插件的中间状态)
|
||
viteInspect({
|
||
dev: false,
|
||
}),
|
||
mockDevServerPlugin({
|
||
cwd: fileURLToPath(new URL('./apps/admin', import.meta.url)),
|
||
dir: 'mock',
|
||
include: ['**/*.mock.ts'],
|
||
prefix: '/api',
|
||
awaitInitialLoad: true,
|
||
reload: true,
|
||
build: true,
|
||
}),
|
||
...iconPackages.map((name) =>
|
||
svgSpritemap(`${iconsRoot.replaceAll('\\', '/')}/${name}/**/*.svg`, {
|
||
prefix: 'icon-',
|
||
idify: (_name, svg) => iconNameFromPath(svg.filePath),
|
||
route: { url: `/__spritemap-${name}` },
|
||
output: { name: `icons-${name}.svg`, hrefAttribute: 'href' },
|
||
svgo: false,
|
||
}),
|
||
),
|
||
unoCSS({
|
||
configFile: fileURLToPath(new URL('./uno.config.ts', import.meta.url)),
|
||
}),
|
||
]
|
||
}
|
||
|
||
export default function (mode: string): PluginOption[] {
|
||
// 单元测试无需启动 mock 服务、HTML 注入、图标扫描或压缩输出。
|
||
if (mode === 'test') {
|
||
return vueTransformPlugins()
|
||
}
|
||
|
||
const plugins =
|
||
mode === 'development' ? onlyDevOptions(mode) : onlyBuildOptions(mode)
|
||
const reportPlugins = mode === 'report' ? onlyReportOptions(mode) : []
|
||
|
||
return [...baseOptions(mode), ...plugins, ...reportPlugins]
|
||
}
|