refactor: 环境变量 VITE_APP_DISABLE_DEVTOOL 改成用 vite 插件的方式注入代码中

This commit is contained in:
Hooray Hu
2024-07-03 23:25:36 +08:00
parent 647edd7207
commit d00fc26616
5 changed files with 27 additions and 6 deletions
-3
View File
@@ -1,5 +1,4 @@
<script setup lang="ts">
import DisableDevtool from 'disable-devtool'
import hotkeys from 'hotkeys-js'
import eventBus from './utils/eventBus'
import Provider from './ui-provider/index.vue'
@@ -65,8 +64,6 @@ onMounted(() => {
eventBus.emit('global-system-info-toggle')
})
})
import.meta.env.VITE_APP_DISABLE_DEVTOOL === 'true' && DisableDevtool()
</script>
<template>
+2 -2
View File
@@ -17,7 +17,7 @@ export default async ({ mode, command }) => {
}
})
return defineConfig({
// 开发服务器选项 https://cn.vitejs.dev/config/#server-options
// 开发服务器选项 https://cn.vitejs.dev/config/server-options
server: {
open: true,
port: 9000,
@@ -29,7 +29,7 @@ export default async ({ 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',
+1 -1
View File
@@ -3,7 +3,7 @@ import type { Plugin } from 'vite'
export default function createDebugTool(env): Plugin {
const { VITE_APP_DEBUG_TOOL } = env
return {
name: 'debug-plugin',
name: 'debug-tool',
transform: (code, id) => {
if (/src\/main.ts$/.test(id)) {
if (VITE_APP_DEBUG_TOOL === 'eruda') {
+22
View File
@@ -0,0 +1,22 @@
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,
}
}
},
}
}
+2
View File
@@ -17,6 +17,7 @@ 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[])[] = [
@@ -44,5 +45,6 @@ export default function createVitePlugins(viteEnv, isBuild = false) {
vitePlugins.push(createConsole())
vitePlugins.push(createBanner())
vitePlugins.push(createDebugTool(viteEnv))
vitePlugins.push(createDisableDevtool(viteEnv))
return vitePlugins
}