diff --git a/vite/plugins.ts b/vite/plugins.ts new file mode 100644 index 0000000..f144fd9 --- /dev/null +++ b/vite/plugins.ts @@ -0,0 +1,202 @@ +import path from 'node:path' +import process from 'node:process' +import fs from 'node:fs' +import dayjs from 'dayjs' +import type { PluginOption } from 'vite' +import vue from '@vitejs/plugin-vue' +import vueJsx from '@vitejs/plugin-vue-jsx' +import vueLegacy from '@vitejs/plugin-legacy' +import VueDevTools from 'vite-plugin-vue-devtools' +import autoImport from 'unplugin-auto-import/vite' +import components from 'unplugin-vue-components/vite' +import Unocss from 'unocss/vite' +import { createSvgIconsPlugin } from 'vite-plugin-svg-icons' +import { vitePluginFakeServer } from 'vite-plugin-fake-server' +import Layouts from 'vite-plugin-vue-meta-layouts' +import Pages from 'vite-plugin-pages' +import { compression } from 'vite-plugin-compression2' +import archiver from 'archiver' +import TurboConsole from 'unplugin-turbo-console/vite' +import banner from 'vite-plugin-banner' +import boxen from 'boxen' +import picocolors from 'picocolors' + +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)) +} + +export default function createVitePlugins(viteEnv, isBuild = false) { + const vitePlugins: (PluginOption | PluginOption[])[] = [ + vue(), + vueJsx(), + vueLegacy({ + renderLegacyChunks: false, + modernPolyfills: [ + 'es.array.at', + 'es.array.find-last', + ], + }), + + // https://github.com/vuejs/devtools-next + viteEnv.VITE_OPEN_DEVTOOLS === 'true' && VueDevTools(), + + // https://github.com/unplugin/unplugin-auto-import + autoImport({ + imports: [ + 'vue', + 'vue-router', + 'pinia', + ], + dts: './src/types/auto-imports.d.ts', + dirs: [ + './src/utils/composables/**', + ], + }), + + // https://github.com/unplugin/unplugin-vue-components + components({ + dirs: [ + 'src/components', + 'src/layouts/ui-kit', + ], + include: [/\.vue$/, /\.vue\?vue/, /\.tsx$/], + dts: './src/types/components.d.ts', + }), + + Unocss(), + + // https://github.com/vbenjs/vite-plugin-svg-icons + createSvgIconsPlugin({ + iconDirs: [path.resolve(process.cwd(), 'src/assets/icons/')], + symbolId: 'icon-[dir]-[name]', + svgoOptions: isBuild, + }), + + // https://github.com/condorheroblog/vite-plugin-fake-server + vitePluginFakeServer({ + logger: !isBuild, + include: 'src/mock', + infixName: false, + enableProd: isBuild && viteEnv.VITE_BUILD_MOCK === 'true', + }), + + // https://github.com/dishait/vite-plugin-vue-meta-layouts + Layouts({ + defaultLayout: 'index', + }), + + // https://github.com/hannoeru/vite-plugin-pages + Pages({ + dirs: 'src/views', + exclude: [ + '**/components/**/*.vue', + ], + }), + + // https://github.com/nonzzz/vite-plugin-compression + isBuild && viteEnv.VITE_BUILD_COMPRESS.split(',').includes('gzip') && compression(), + isBuild && viteEnv.VITE_BUILD_COMPRESS.split(',').includes('brotli') && compression({ + exclude: [/\.(br)$/, /\.(gz)$/], + algorithm: 'brotliCompress', + }), + + (function () { + let outDir: string + return { + name: 'vite-plugin-archiver', + apply: 'build', + configResolved(resolvedConfig) { + outDir = resolvedConfig.build.outDir + }, + async closeBundle() { + if (['zip', 'tar'].includes(viteEnv.VITE_BUILD_ARCHIVE)) { + await sleep(1000) + const archive = archiver(viteEnv.VITE_BUILD_ARCHIVE, { + ...(viteEnv.VITE_BUILD_ARCHIVE === 'zip' && { zlib: { level: 9 } }), + ...(viteEnv.VITE_BUILD_ARCHIVE === 'tar' && { gzip: true, gzipOptions: { level: 9 } }), + }) + const output = fs.createWriteStream(`${outDir}.${dayjs().format('YYYY-MM-DD-HH-mm-ss')}.${viteEnv.VITE_BUILD_ARCHIVE === 'zip' ? 'zip' : 'tar.gz'}`) + archive.pipe(output) + archive.directory(outDir, false) + archive.finalize() + } + }, + } + })(), + + // https://github.com/unplugin/unplugin-turbo-console + TurboConsole(), + + // https://github.com/chengpeiquan/vite-plugin-banner + banner(` +/** + * 由 Fantastic-admin 提供技术支持 + * Powered by Fantastic-admin + * https://fantastic-admin.github.io + */ + `), + + { + name: 'vite-plugin-debug-plugin', + transform: (code, id) => { + if (/src\/main.ts$/.test(id)) { + if (viteEnv.VITE_APP_DEBUG_TOOL === 'eruda') { + code = code.concat(` + import eruda from 'eruda' + eruda.init() + `) + } + else if (viteEnv.VITE_APP_DEBUG_TOOL === 'vconsole') { + code = code.concat(` + import VConsole from 'vconsole' + new VConsole() + `) + } + return { + code, + map: null, + } + } + }, + }, + + { + name: 'vite-plugin-disable-devtool', + transform: (code, id) => { + if (/src\/main.ts$/.test(id)) { + if (viteEnv.VITE_APP_DISABLE_DEVTOOL === 'true') { + code = code.concat(` + import DisableDevtool from 'disable-devtool' + DisableDevtool() + `) + } + return { + code, + map: null, + } + } + }, + }, + + { + name: 'vite-plugin-terminal-info', + apply: 'serve', + async buildStart() { + const { bold, green, cyan, bgGreen, underline } = picocolors + // eslint-disable-next-line no-console + console.log( + boxen( + `${bold(green(`由 ${bgGreen('Fantastic-admin')} 驱动`))}\n\n${underline('https://fantastic-admin.github.io')}\n\n当前使用:${cyan('基础版')}`, + { + padding: 1, + margin: 1, + borderStyle: 'double', + textAlignment: 'center', + }, + ), + ) + }, + }, + ] + return vitePlugins +} diff --git a/vite/plugins/app-info.ts b/vite/plugins/app-info.ts deleted file mode 100644 index a6294d3..0000000 --- a/vite/plugins/app-info.ts +++ /dev/null @@ -1,25 +0,0 @@ -import boxen from 'boxen' -import picocolors from 'picocolors' -import type { Plugin } from 'vite' - -export default function appInfo(): Plugin { - return { - name: 'appInfo', - apply: 'serve', - async buildStart() { - const { bold, green, cyan, bgGreen, underline } = picocolors - // eslint-disable-next-line no-console - console.log( - boxen( - `${bold(green(`由 ${bgGreen('Fantastic-admin')} 驱动`))}\n\n${underline('https://fantastic-admin.github.io')}\n\n当前使用:${cyan('基础版')}`, - { - padding: 1, - margin: 1, - borderStyle: 'double', - textAlignment: 'center', - }, - ), - ) - }, - } -} diff --git a/vite/plugins/archiver.ts b/vite/plugins/archiver.ts deleted file mode 100644 index 2cc973e..0000000 --- a/vite/plugins/archiver.ts +++ /dev/null @@ -1,33 +0,0 @@ -import fs from 'node:fs' -import dayjs from 'dayjs' -import archiver from 'archiver' -import type { Plugin } from 'vite' - -function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)) -} - -export default function createArchiver(env): Plugin { - const { VITE_BUILD_ARCHIVE } = env - let outDir: string - return { - name: 'vite-plugin-archiver', - apply: 'build', - configResolved(resolvedConfig) { - outDir = resolvedConfig.build.outDir - }, - async closeBundle() { - if (['zip', 'tar'].includes(VITE_BUILD_ARCHIVE)) { - await sleep(1000) - const archive = archiver(VITE_BUILD_ARCHIVE, { - ...(VITE_BUILD_ARCHIVE === 'zip' && { zlib: { level: 9 } }), - ...(VITE_BUILD_ARCHIVE === 'tar' && { gzip: true, gzipOptions: { level: 9 } }), - }) - const output = fs.createWriteStream(`${outDir}.${dayjs().format('YYYY-MM-DD-HH-mm-ss')}.${VITE_BUILD_ARCHIVE === 'zip' ? 'zip' : 'tar.gz'}`) - archive.pipe(output) - archive.directory(outDir, false) - archive.finalize() - } - }, - } -} diff --git a/vite/plugins/auto-import.ts b/vite/plugins/auto-import.ts deleted file mode 100755 index b27bdd5..0000000 --- a/vite/plugins/auto-import.ts +++ /dev/null @@ -1,15 +0,0 @@ -import autoImport from 'unplugin-auto-import/vite' - -export default function createAutoImport() { - return autoImport({ - imports: [ - 'vue', - 'vue-router', - 'pinia', - ], - dts: './src/types/auto-imports.d.ts', - dirs: [ - './src/utils/composables/**', - ], - }) -} diff --git a/vite/plugins/banner.ts b/vite/plugins/banner.ts deleted file mode 100755 index b68274b..0000000 --- a/vite/plugins/banner.ts +++ /dev/null @@ -1,11 +0,0 @@ -import banner from 'vite-plugin-banner' - -export default function createBanner() { - return banner(` -/** - * 由 Fantastic-admin 提供技术支持 - * Powered by Fantastic-admin - * https://fantastic-admin.github.io - */ -`) -} diff --git a/vite/plugins/components.ts b/vite/plugins/components.ts deleted file mode 100755 index 204d700..0000000 --- a/vite/plugins/components.ts +++ /dev/null @@ -1,12 +0,0 @@ -import components from 'unplugin-vue-components/vite' - -export default function createComponents() { - return components({ - dirs: [ - 'src/components', - 'src/layouts/ui-kit', - ], - include: [/\.vue$/, /\.vue\?vue/, /\.tsx$/], - dts: './src/types/components.d.ts', - }) -} diff --git a/vite/plugins/compression.ts b/vite/plugins/compression.ts deleted file mode 100755 index 76b3c26..0000000 --- a/vite/plugins/compression.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { compression } from 'vite-plugin-compression2' -import type { PluginOption } from 'vite' - -export default function createCompression(env, isBuild) { - const plugin: (PluginOption | PluginOption[])[] = [] - if (isBuild) { - const { VITE_BUILD_COMPRESS } = env - const compressList = VITE_BUILD_COMPRESS.split(',') - if (compressList.includes('gzip')) { - plugin.push( - compression(), - ) - } - if (compressList.includes('brotli')) { - plugin.push( - compression({ - exclude: [/\.(br)$/, /\.(gz)$/], - algorithm: 'brotliCompress', - }), - ) - } - } - return plugin -} diff --git a/vite/plugins/console.ts b/vite/plugins/console.ts deleted file mode 100644 index 71705ca..0000000 --- a/vite/plugins/console.ts +++ /dev/null @@ -1,5 +0,0 @@ -import TurboConsole from 'unplugin-turbo-console/vite' - -export default function createConsole() { - return TurboConsole() -} diff --git a/vite/plugins/debug-tool.ts b/vite/plugins/debug-tool.ts deleted file mode 100644 index 4421df8..0000000 --- a/vite/plugins/debug-tool.ts +++ /dev/null @@ -1,28 +0,0 @@ -import type { Plugin } from 'vite' - -export default function createDebugTool(env): Plugin { - const { VITE_APP_DEBUG_TOOL } = env - return { - name: 'debug-tool', - transform: (code, id) => { - if (/src\/main.ts$/.test(id)) { - if (VITE_APP_DEBUG_TOOL === 'eruda') { - code = code.concat(` - import eruda from 'eruda' - eruda.init() - `) - } - else if (VITE_APP_DEBUG_TOOL === 'vconsole') { - code = code.concat(` - import VConsole from 'vconsole' - new VConsole() - `) - } - return { - code, - map: null, - } - } - }, - } -} diff --git a/vite/plugins/devtools.ts b/vite/plugins/devtools.ts deleted file mode 100755 index 599c747..0000000 --- a/vite/plugins/devtools.ts +++ /dev/null @@ -1,6 +0,0 @@ -import VueDevTools from 'vite-plugin-vue-devtools' - -export default function createDevtools(env) { - const { VITE_OPEN_DEVTOOLS } = env - return VITE_OPEN_DEVTOOLS === 'true' && VueDevTools() -} diff --git a/vite/plugins/disable-devtool.ts b/vite/plugins/disable-devtool.ts deleted file mode 100644 index d3bb560..0000000 --- a/vite/plugins/disable-devtool.ts +++ /dev/null @@ -1,22 +0,0 @@ -import type { Plugin } from 'vite' - -export default function createDisableDevtool(env): Plugin { - const { VITE_APP_DISABLE_DEVTOOL } = env - return { - name: 'disable-devtool', - transform: (code, id) => { - if (/src\/main.ts$/.test(id)) { - if (VITE_APP_DISABLE_DEVTOOL === 'true') { - code = code.concat(` - import DisableDevtool from 'disable-devtool' - DisableDevtool() - `) - } - return { - code, - map: null, - } - } - }, - } -} diff --git a/vite/plugins/index.ts b/vite/plugins/index.ts deleted file mode 100755 index 984efe1..0000000 --- a/vite/plugins/index.ts +++ /dev/null @@ -1,50 +0,0 @@ -import type { PluginOption } from 'vite' -import vue from '@vitejs/plugin-vue' -import vueJsx from '@vitejs/plugin-vue-jsx' -import vueLegacy from '@vitejs/plugin-legacy' -import appInfo from './app-info' - -import createDevtools from './devtools' -import createAutoImport from './auto-import' -import createComponents from './components' -import createUnocss from './unocss' -import createSvgIcon from './svg-icon' -import createMock from './mock' -import createLayouts from './layouts' -import createPages from './pages' -import createCompression from './compression' -import createArchiver from './archiver' -import createConsole from './console' -import createBanner from './banner' -import createDebugTool from './debug-tool' -import createDisableDevtool from './disable-devtool' - -export default function createVitePlugins(viteEnv, isBuild = false) { - const vitePlugins: (PluginOption | PluginOption[])[] = [ - appInfo(), - vue(), - vueJsx(), - vueLegacy({ - renderLegacyChunks: false, - modernPolyfills: [ - 'es.array.at', - 'es.array.find-last', - ], - }), - ] - vitePlugins.push(createDevtools(viteEnv)) - vitePlugins.push(createAutoImport()) - vitePlugins.push(createComponents()) - vitePlugins.push(createUnocss()) - vitePlugins.push(createSvgIcon(isBuild)) - vitePlugins.push(createMock(viteEnv, isBuild)) - vitePlugins.push(createLayouts()) - vitePlugins.push(createPages()) - vitePlugins.push(...createCompression(viteEnv, isBuild)) - vitePlugins.push(createArchiver(viteEnv)) - vitePlugins.push(createConsole()) - vitePlugins.push(createBanner()) - vitePlugins.push(createDebugTool(viteEnv)) - vitePlugins.push(createDisableDevtool(viteEnv)) - return vitePlugins -} diff --git a/vite/plugins/layouts.ts b/vite/plugins/layouts.ts deleted file mode 100755 index 8698b09..0000000 --- a/vite/plugins/layouts.ts +++ /dev/null @@ -1,7 +0,0 @@ -import Layouts from 'vite-plugin-vue-meta-layouts' - -export default function createLayouts() { - return Layouts({ - defaultLayout: 'index', - }) -} diff --git a/vite/plugins/mock.ts b/vite/plugins/mock.ts deleted file mode 100755 index 380370b..0000000 --- a/vite/plugins/mock.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { vitePluginFakeServer } from 'vite-plugin-fake-server' - -export default function createMock(env, isBuild) { - const { VITE_BUILD_MOCK } = env - return vitePluginFakeServer({ - logger: !isBuild, - include: 'src/mock', - infixName: false, - enableProd: isBuild && VITE_BUILD_MOCK === 'true', - }) -} diff --git a/vite/plugins/pages.ts b/vite/plugins/pages.ts deleted file mode 100755 index 470bcca..0000000 --- a/vite/plugins/pages.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Pages from 'vite-plugin-pages' - -export default function createPages() { - return Pages({ - dirs: 'src/views', - exclude: [ - '**/components/**/*.vue', - ], - }) -} diff --git a/vite/plugins/svg-icon.ts b/vite/plugins/svg-icon.ts deleted file mode 100755 index f76753d..0000000 --- a/vite/plugins/svg-icon.ts +++ /dev/null @@ -1,11 +0,0 @@ -import path from 'node:path' -import process from 'node:process' -import { createSvgIconsPlugin } from 'vite-plugin-svg-icons' - -export default function createSvgIcon(isBuild) { - return createSvgIconsPlugin({ - iconDirs: [path.resolve(process.cwd(), 'src/assets/icons/')], - symbolId: 'icon-[dir]-[name]', - svgoOptions: isBuild, - }) -} diff --git a/vite/plugins/unocss.ts b/vite/plugins/unocss.ts deleted file mode 100755 index 8160e63..0000000 --- a/vite/plugins/unocss.ts +++ /dev/null @@ -1,5 +0,0 @@ -import Unocss from 'unocss/vite' - -export default function createUnocss() { - return Unocss() -}