From 74a72143ea9f3d2eb0ffd76735be32dc5b5719ff Mon Sep 17 00:00:00 2001 From: Hooray Hu <304327508@qq.com> Date: Wed, 3 Jul 2024 22:55:45 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=20`VITE=5F?= =?UTF-8?q?APP=5FDEBUG=5FTOOL`=20=E6=94=B9=E6=88=90=E7=94=A8=20vite=20?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E7=9A=84=E6=96=B9=E5=BC=8F=E6=B3=A8=E5=85=A5?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 5 ----- vite.config.ts | 4 ++-- vite/plugins/debug-tool.ts | 28 ++++++++++++++++++++++++++++ vite/plugins/index.ts | 2 ++ 4 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 vite/plugins/debug-tool.ts diff --git a/src/App.vue b/src/App.vue index 57d3e16..02c233c 100755 --- a/src/App.vue +++ b/src/App.vue @@ -1,6 +1,4 @@ diff --git a/vite.config.ts b/vite.config.ts index 1b51fd4..4ace21e 100755 --- a/vite.config.ts +++ b/vite.config.ts @@ -16,7 +16,7 @@ export default ({ mode, command }) => { }) return defineConfig({ base: './', - // 开发服务器选项 https://cn.vitejs.dev/config/#server-options + // 开发服务器选项 https://cn.vitejs.dev/config/server-options server: { open: true, port: 9000, @@ -28,7 +28,7 @@ export default ({ mode, command }) => { }, }, }, - // 构建选项 https://cn.vitejs.dev/config/#server-fsserve-root + // 构建选项 https://cn.vitejs.dev/config/build-options build: { outDir: mode === 'production' ? 'dist' : `dist-${mode}`, sourcemap: env.VITE_BUILD_SOURCEMAP === 'true', diff --git a/vite/plugins/debug-tool.ts b/vite/plugins/debug-tool.ts new file mode 100644 index 0000000..aa41c52 --- /dev/null +++ b/vite/plugins/debug-tool.ts @@ -0,0 +1,28 @@ +import type { Plugin } from 'vite' + +export default function createDebugTool(env): Plugin { + const { VITE_APP_DEBUG_TOOL } = env + return { + name: 'debug-plugin', + 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/index.ts b/vite/plugins/index.ts index e171a26..22005af 100755 --- a/vite/plugins/index.ts +++ b/vite/plugins/index.ts @@ -14,6 +14,7 @@ import createCompression from './compression' import createArchiver from './archiver' import createConsole from './console' import createBanner from './banner' +import createDebugTool from './debug-tool' export default function createVitePlugins(viteEnv, isBuild = false) { const vitePlugins: (PluginOption | PluginOption[])[] = [ @@ -36,5 +37,6 @@ export default function createVitePlugins(viteEnv, isBuild = false) { vitePlugins.push(createArchiver(viteEnv)) vitePlugins.push(createConsole()) vitePlugins.push(createBanner()) + vitePlugins.push(createDebugTool(viteEnv)) return vitePlugins }