|
| 1 | +import path from 'node:path' |
| 2 | +import process from 'node:process' |
| 3 | +import fs from 'node:fs' |
| 4 | +import dayjs from 'dayjs' |
| 5 | +import type { PluginOption } from 'vite' |
| 6 | +import vue from '@vitejs/plugin-vue' |
| 7 | +import vueJsx from '@vitejs/plugin-vue-jsx' |
| 8 | +import autoImport from 'unplugin-auto-import/vite' |
| 9 | +import components from 'unplugin-vue-components/vite' |
| 10 | +import Unocss from 'unocss/vite' |
| 11 | +import { createSvgIconsPlugin } from 'vite-plugin-svg-icons' |
| 12 | +import { vitePluginFakeServer } from 'vite-plugin-fake-server' |
| 13 | +import { compression } from 'vite-plugin-compression2' |
| 14 | +import archiver from 'archiver' |
| 15 | +import TurboConsole from 'unplugin-turbo-console/vite' |
| 16 | +import banner from 'vite-plugin-banner' |
| 17 | +import boxen from 'boxen' |
| 18 | +import picocolors from 'picocolors' |
| 19 | + |
| 20 | +function sleep(ms) { |
| 21 | + return new Promise(resolve => setTimeout(resolve, ms)) |
| 22 | +} |
| 23 | + |
| 24 | +export default function createVitePlugins(viteEnv, isBuild = false) { |
| 25 | + const vitePlugins: (PluginOption | PluginOption[])[] = [ |
| 26 | + vue(), |
| 27 | + vueJsx(), |
| 28 | + |
| 29 | + // https://github.com/unplugin/unplugin-auto-import |
| 30 | + autoImport({ |
| 31 | + imports: [ |
| 32 | + 'vue', |
| 33 | + 'vue-router', |
| 34 | + 'pinia', |
| 35 | + ], |
| 36 | + dts: './src/types/auto-imports.d.ts', |
| 37 | + dirs: [ |
| 38 | + './src/utils/composables/**', |
| 39 | + ], |
| 40 | + }), |
| 41 | + |
| 42 | + // https://github.com/unplugin/unplugin-vue-components |
| 43 | + components({ |
| 44 | + dirs: [ |
| 45 | + 'src/components', |
| 46 | + 'src/views/ui-kit', |
| 47 | + ], |
| 48 | + dts: './src/types/components.d.ts', |
| 49 | + }), |
| 50 | + |
| 51 | + Unocss(), |
| 52 | + |
| 53 | + // https://github.com/vbenjs/vite-plugin-svg-icons |
| 54 | + createSvgIconsPlugin({ |
| 55 | + iconDirs: [path.resolve(process.cwd(), 'src/assets/icons/')], |
| 56 | + symbolId: 'icon-[dir]-[name]', |
| 57 | + svgoOptions: isBuild, |
| 58 | + }), |
| 59 | + |
| 60 | + // https://github.com/condorheroblog/vite-plugin-fake-server |
| 61 | + vitePluginFakeServer({ |
| 62 | + logger: !isBuild, |
| 63 | + include: 'src/mock', |
| 64 | + infixName: false, |
| 65 | + enableProd: isBuild && viteEnv.VITE_BUILD_MOCK === 'true', |
| 66 | + }), |
| 67 | + |
| 68 | + // https://github.com/nonzzz/vite-plugin-compression |
| 69 | + isBuild && viteEnv.VITE_BUILD_COMPRESS.split(',').includes('gzip') && compression(), |
| 70 | + isBuild && viteEnv.VITE_BUILD_COMPRESS.split(',').includes('brotli') && compression({ |
| 71 | + exclude: [/\.(br)$/, /\.(gz)$/], |
| 72 | + algorithm: 'brotliCompress', |
| 73 | + }), |
| 74 | + |
| 75 | + (function () { |
| 76 | + let outDir: string |
| 77 | + return { |
| 78 | + name: 'vite-plugin-archiver', |
| 79 | + apply: 'build', |
| 80 | + configResolved(resolvedConfig) { |
| 81 | + outDir = resolvedConfig.build.outDir |
| 82 | + }, |
| 83 | + async closeBundle() { |
| 84 | + if (['zip', 'tar'].includes(viteEnv.VITE_BUILD_ARCHIVE)) { |
| 85 | + await sleep(1000) |
| 86 | + const archive = archiver(viteEnv.VITE_BUILD_ARCHIVE, { |
| 87 | + ...(viteEnv.VITE_BUILD_ARCHIVE === 'zip' && { zlib: { level: 9 } }), |
| 88 | + ...(viteEnv.VITE_BUILD_ARCHIVE === 'tar' && { gzip: true, gzipOptions: { level: 9 } }), |
| 89 | + }) |
| 90 | + const output = fs.createWriteStream(`${outDir}.${dayjs().format('YYYY-MM-DD-HH-mm-ss')}.${viteEnv.VITE_BUILD_ARCHIVE === 'zip' ? 'zip' : 'tar.gz'}`) |
| 91 | + archive.pipe(output) |
| 92 | + archive.directory(outDir, false) |
| 93 | + archive.finalize() |
| 94 | + } |
| 95 | + }, |
| 96 | + } |
| 97 | + })(), |
| 98 | + |
| 99 | + // https://github.com/unplugin/unplugin-turbo-console |
| 100 | + TurboConsole(), |
| 101 | + |
| 102 | + // https://github.com/chengpeiquan/vite-plugin-banner |
| 103 | + banner(` |
| 104 | +/** |
| 105 | + * 由 One-step-admin 提供技术支持 |
| 106 | + * Powered by One-step-admin |
| 107 | + * https://one-step-admin.github.io |
| 108 | + */ |
| 109 | + `), |
| 110 | + |
| 111 | + { |
| 112 | + name: 'vite-plugin-debug-plugin', |
| 113 | + transform: (code, id) => { |
| 114 | + if (/src\/main.ts$/.test(id)) { |
| 115 | + if (viteEnv.VITE_APP_DEBUG_TOOL === 'eruda') { |
| 116 | + code = code.concat(` |
| 117 | + import eruda from 'eruda' |
| 118 | + eruda.init() |
| 119 | + `) |
| 120 | + } |
| 121 | + else if (viteEnv.VITE_APP_DEBUG_TOOL === 'vconsole') { |
| 122 | + code = code.concat(` |
| 123 | + import VConsole from 'vconsole' |
| 124 | + new VConsole() |
| 125 | + `) |
| 126 | + } |
| 127 | + return { |
| 128 | + code, |
| 129 | + map: null, |
| 130 | + } |
| 131 | + } |
| 132 | + }, |
| 133 | + }, |
| 134 | + |
| 135 | + { |
| 136 | + name: 'vite-plugin-disable-devtool', |
| 137 | + transform: (code, id) => { |
| 138 | + if (/src\/main.ts$/.test(id)) { |
| 139 | + if (viteEnv.VITE_APP_DISABLE_DEVTOOL === 'true') { |
| 140 | + code = code.concat(` |
| 141 | + import DisableDevtool from 'disable-devtool' |
| 142 | + DisableDevtool() |
| 143 | + `) |
| 144 | + } |
| 145 | + return { |
| 146 | + code, |
| 147 | + map: null, |
| 148 | + } |
| 149 | + } |
| 150 | + }, |
| 151 | + }, |
| 152 | + |
| 153 | + { |
| 154 | + name: 'vite-plugin-terminal-info', |
| 155 | + apply: 'serve', |
| 156 | + async buildStart() { |
| 157 | + const { bold, green, cyan, bgGreen, underline } = picocolors |
| 158 | + // eslint-disable-next-line no-console |
| 159 | + console.log( |
| 160 | + boxen( |
| 161 | + `${bold(green(`由 ${bgGreen('One-step-admin')} 驱动`))}\n\n${underline('https://one-step-admin.github.io')}\n\n当前使用:${cyan('基础版')}`, |
| 162 | + { |
| 163 | + padding: 1, |
| 164 | + margin: 1, |
| 165 | + borderStyle: 'double', |
| 166 | + textAlignment: 'center', |
| 167 | + }, |
| 168 | + ), |
| 169 | + ) |
| 170 | + }, |
| 171 | + }, |
| 172 | + ] |
| 173 | + return vitePlugins |
| 174 | +} |
0 commit comments