|
|
|
import path from 'node:path'
|
|
|
|
import process from 'node:process'
|
|
|
|
import type { ConfigEnv } from 'vite'
|
|
|
|
import { defineConfig, loadEnv } from 'vite'
|
|
|
|
import Vue from '@vitejs/plugin-vue'
|
|
|
|
|
|
|
|
// banner
|
|
|
|
import Banner from 'vite-plugin-banner'
|
|
|
|
|
|
|
|
// auto import
|
|
|
|
import AutoImport from 'unplugin-auto-import/vite'
|
|
|
|
import { transformShortVmodel } from '@vue-macros/short-vmodel'
|
|
|
|
import { unheadVueComposablesImports } from '@unhead/vue'
|
|
|
|
|
|
|
|
// vue macros
|
|
|
|
import VueMacros from 'unplugin-vue-macros/vite'
|
|
|
|
|
|
|
|
// // pages
|
|
|
|
import Pages from 'vite-plugin-pages'
|
|
|
|
|
|
|
|
// layout
|
|
|
|
import Layouts from 'vite-plugin-vue-layouts'
|
|
|
|
|
|
|
|
import WebFontDownload from 'vite-plugin-webfont-dl'
|
|
|
|
import VueDevTools from 'vite-plugin-vue-devtools'
|
|
|
|
|
|
|
|
// unocss
|
|
|
|
import Unocss from 'unocss/vite'
|
|
|
|
import VueI18n from '@intlify/unplugin-vue-i18n/vite'
|
|
|
|
import Components from 'unplugin-vue-components/vite'
|
|
|
|
import { visualizer } from 'rollup-plugin-visualizer'
|
|
|
|
|
|
|
|
// svg
|
|
|
|
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
|
|
|
|
|
|
|
|
// compress
|
|
|
|
import ViteCompression from 'vite-plugin-compression'
|
|
|
|
|
|
|
|
// pwa
|
|
|
|
import { VitePWA } from 'vite-plugin-pwa'
|
|
|
|
|
|
|
|
// markdown
|
|
|
|
import Markdown from 'vite-plugin-vue-markdown'
|
|
|
|
import LinkAttributes from 'markdown-it-link-attributes'
|
|
|
|
import Shiki from 'markdown-it-shiki'
|
|
|
|
|
|
|
|
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
|
|
|
|
|
|
|
|
import pkg from './package.json'
|
|
|
|
|
|
|
|
export default defineConfig(({ command, mode }: ConfigEnv) => {
|
|
|
|
console.log('command', command)
|
|
|
|
const viteEnv = loadEnv(mode, process.cwd()) as unknown as ImportMetaEnv
|
|
|
|
const plugins = [
|
|
|
|
Banner({ outDir: '', content: `/**\n * name: ${pkg.name}\n * version: v${pkg.version}\n * description: ${pkg.description}\n * author: ${pkg.author.name}\n */` }),
|
|
|
|
// https://github.com/JohnCampionJr/vite-plugin-vue-layouts
|
|
|
|
Layouts(),
|
|
|
|
// https://github.com/hannoeru/vite-plugin-pages
|
|
|
|
Pages({
|
|
|
|
extensions: ['vue', 'md'],
|
|
|
|
}),
|
|
|
|
// https://github.com/antfu/unplugin-auto-import
|
|
|
|
AutoImport({
|
|
|
|
imports: [
|
|
|
|
'vue-router',
|
|
|
|
'vue',
|
|
|
|
'vue-i18n',
|
|
|
|
'vue/macros',
|
|
|
|
'@vueuse/core',
|
|
|
|
unheadVueComposablesImports,
|
|
|
|
{
|
|
|
|
'naive-ui': [
|
|
|
|
'useDialog',
|
|
|
|
'useMessage',
|
|
|
|
'useNotification',
|
|
|
|
'useLoadingBar',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
dts: 'src/auto-imports.d.ts',
|
|
|
|
dirs: [
|
|
|
|
'src/composables/**',
|
|
|
|
'src/stores',
|
|
|
|
'src/types',
|
|
|
|
'src/api/**',
|
|
|
|
],
|
|
|
|
vueTemplate: true,
|
|
|
|
resolvers: [
|
|
|
|
NaiveUiResolver(),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
VueMacros({
|
|
|
|
plugins: {
|
|
|
|
vue: Vue({
|
|
|
|
include: [/\.vue$/, /\.md$/],
|
|
|
|
reactivityTransform: true,
|
|
|
|
template: {
|
|
|
|
compilerOptions: {
|
|
|
|
nodeTransforms: [
|
|
|
|
transformShortVmodel({ prefix: '$' }),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
// https://github.com/feat-agency/vite-plugin-webfont-dl
|
|
|
|
WebFontDownload(),
|
|
|
|
// https://github.com/webfansplz/vite-plugin-vue-devtools
|
|
|
|
VueDevTools(),
|
|
|
|
// https://github.com/vbenjs/vite-plugin-svg-icons/blob/main/README.md
|
|
|
|
createSvgIconsPlugin({
|
|
|
|
iconDirs: [path.resolve(process.cwd(), 'assets/svg')],
|
|
|
|
symbolId: 'nl-[dir]-[name]',
|
|
|
|
}),
|
|
|
|
// https://github.com/antfu/unocss
|
|
|
|
// see uno.config.ts for config
|
|
|
|
Unocss(),
|
|
|
|
// https://github.com/intlify/bundle-tools/tree/main/packages/unplugin-vue-i18n
|
|
|
|
VueI18n({
|
|
|
|
runtimeOnly: true,
|
|
|
|
compositionOnly: true,
|
|
|
|
fullInstall: true,
|
|
|
|
include: [path.resolve(__dirname, 'locales/**')],
|
|
|
|
}),
|
|
|
|
// https://github.com/antfu/unplugin-vue-components
|
|
|
|
Components({
|
|
|
|
// allow auto load markdown components under `./src/components/`
|
|
|
|
extensions: ['vue', 'md', 'ts'],
|
|
|
|
// allow auto import and register components used in markdown
|
|
|
|
include: [/\.vue$/, /\.vue\?vue/, /\.md$/, /\.ts$/],
|
|
|
|
directoryAsNamespace: true,
|
|
|
|
collapseSamePrefixes: true,
|
|
|
|
dts: 'src/components.d.ts',
|
|
|
|
resolvers: [
|
|
|
|
NaiveUiResolver(),
|
|
|
|
(name) => {
|
|
|
|
if (name.startsWith('L')) {
|
|
|
|
const importName = name.slice(2)
|
|
|
|
const from = 'src/components'
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
from: `${from}/${importName}`,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
if (viteEnv.VITE_VISUALIZER) {
|
|
|
|
plugins.push(visualizer({
|
|
|
|
gzipSize: true,
|
|
|
|
brotliSize: true,
|
|
|
|
open: true,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// compress
|
|
|
|
if (viteEnv.VITE_COMPRESS !== 'none') {
|
|
|
|
plugins.push(ViteCompression({ algorithm: viteEnv.VITE_COMPRESS }))
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/antfu/vite-plugin-pwa
|
|
|
|
if (viteEnv.VITE_PWA) {
|
|
|
|
plugins.push(VitePWA({
|
|
|
|
registerType: 'autoUpdate',
|
|
|
|
includeAssets: ['favicon.svg', 'safari-pinned-tab.svg'],
|
|
|
|
manifest: {
|
|
|
|
name: 'N-Admin',
|
|
|
|
short_name: 'N-Admin',
|
|
|
|
theme_color: '#ffffff',
|
|
|
|
icons: [
|
|
|
|
{
|
|
|
|
src: '/pwa-192x192.png',
|
|
|
|
sizes: '192x192',
|
|
|
|
type: 'image/png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: '/pwa-512x512.png',
|
|
|
|
sizes: '512x512',
|
|
|
|
type: 'image/png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: '/pwa-512x512.png',
|
|
|
|
sizes: '512x512',
|
|
|
|
type: 'image/png',
|
|
|
|
purpose: 'any maskable',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/antfu/vite-plugin-vue-markdown
|
|
|
|
// Don't need this? Try vitesse-lite: https://github.com/antfu/vitesse-lite
|
|
|
|
if (viteEnv.VITE_MARKDOWN) {
|
|
|
|
plugins.push(Markdown({
|
|
|
|
wrapperClasses: 'prose prose-sm m-auto text-left',
|
|
|
|
markdownItSetup(md) {
|
|
|
|
// https://prismjs.com/
|
|
|
|
md.use(Shiki, {
|
|
|
|
theme: {
|
|
|
|
light: 'vitesse-light',
|
|
|
|
dark: 'vitesse-dark',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
md.use(LinkAttributes, {
|
|
|
|
matcher: (link: string) => /^https?:\/\//.test(link),
|
|
|
|
attrs: {
|
|
|
|
target: '_blank',
|
|
|
|
rel: 'noopener',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
base: viteEnv.VITE_BASE_URL,
|
|
|
|
plugins,
|
|
|
|
resolve: {
|
|
|
|
alias: {
|
|
|
|
'~/': `${path.resolve(__dirname, 'src')}/`,
|
|
|
|
'#/': `${path.resolve(__dirname, 'src/types')}/`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
server: {
|
|
|
|
host: '0.0.0.0',
|
|
|
|
port: 3200,
|
|
|
|
proxy: {
|
|
|
|
'^/api': {
|
|
|
|
target: 'http://127.0.0.1:19999',
|
|
|
|
changeOrigin: true,
|
|
|
|
// rewrite: path => path.replace(/^\/api/, '')
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
optimizeDeps: {
|
|
|
|
include: [],
|
|
|
|
},
|
|
|
|
|
|
|
|
// https://github.com/vitest-dev/vitest
|
|
|
|
test: {
|
|
|
|
include: ['test/**/*.test.ts'],
|
|
|
|
environment: 'jsdom',
|
|
|
|
deps: {
|
|
|
|
inline: ['@vue', '@vueuse', 'vue-demi'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
ssr: {
|
|
|
|
// TODO: workaround until they support native ESM
|
|
|
|
noExternal: ['workbox-window', /vue-i18n/],
|
|
|
|
},
|
|
|
|
|
|
|
|
build: {
|
|
|
|
reportCompressedSize: true,
|
|
|
|
sourcemap: false,
|
|
|
|
commonjsOptions: {
|
|
|
|
ignoreTryCatch: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|