Skip to content

Commit fdf52bb

Browse files
committed
refactor: 环境变量 VITE_APP_DISABLE_DEVTOOL 改成用 vite 插件的方式注入代码中
1 parent 6d35fd3 commit fdf52bb

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

src/App.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script setup lang="ts">
2-
import DisableDevtool from 'disable-devtool'
32
import hotkeys from 'hotkeys-js'
43
import eventBus from './utils/eventBus'
54
import Provider from './ui-provider/index.vue'
@@ -36,8 +35,6 @@ onMounted(() => {
3635
eventBus.emit('global-system-info-toggle')
3736
})
3837
})
39-
40-
import.meta.env.VITE_APP_DISABLE_DEVTOOL === 'true' && DisableDevtool()
4138
</script>
4239

4340
<template>

vite.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default ({ mode, command }) => {
1717
}
1818
})
1919
return defineConfig({
20-
// 开发服务器选项 https://cn.vitejs.dev/config/#server-options
20+
// 开发服务器选项 https://cn.vitejs.dev/config/server-options
2121
server: {
2222
open: true,
2323
port: 9000,
@@ -29,7 +29,7 @@ export default ({ mode, command }) => {
2929
},
3030
},
3131
},
32-
// 构建选项 https://cn.vitejs.dev/config/#server-fsserve-root
32+
// 构建选项 https://cn.vitejs.dev/config/build-options
3333
build: {
3434
outDir: mode === 'production' ? 'dist' : `dist-${mode}`,
3535
sourcemap: env.VITE_BUILD_SOURCEMAP === 'true',

vite/plugins/debug-tool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Plugin } from 'vite'
33
export default function createDebugTool(env): Plugin {
44
const { VITE_APP_DEBUG_TOOL } = env
55
return {
6-
name: 'debug-plugin',
6+
name: 'debug-tool',
77
transform: (code, id) => {
88
if (/src\/main.ts$/.test(id)) {
99
if (VITE_APP_DEBUG_TOOL === 'eruda') {

vite/plugins/disable-devtool.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { Plugin } from 'vite'
2+
3+
export default function createDisableDevtool(env): Plugin {
4+
const { VITE_APP_DISABLE_DEVTOOL } = env
5+
return {
6+
name: 'disable-devtool',
7+
transform: (code, id) => {
8+
if (/src\/main.ts$/.test(id)) {
9+
if (VITE_APP_DISABLE_DEVTOOL === 'true') {
10+
code = code.concat(`
11+
import DisableDevtool from 'disable-devtool'
12+
DisableDevtool()
13+
`)
14+
}
15+
return {
16+
code,
17+
map: null,
18+
}
19+
}
20+
},
21+
}
22+
}

vite/plugins/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import createArchiver from './archiver'
1313
import createConsole from './console'
1414
import createBanner from './banner'
1515
import createDebugTool from './debug-tool'
16+
import createDisableDevtool from './disable-devtool'
1617

1718
export default function createVitePlugins(viteEnv, isBuild = false) {
1819
const vitePlugins: (PluginOption | PluginOption[])[] = [
@@ -30,5 +31,6 @@ export default function createVitePlugins(viteEnv, isBuild = false) {
3031
vitePlugins.push(createConsole())
3132
vitePlugins.push(createBanner())
3233
vitePlugins.push(createDebugTool(viteEnv))
34+
vitePlugins.push(createDisableDevtool(viteEnv))
3335
return vitePlugins
3436
}

0 commit comments

Comments
 (0)