From 01ce8c996d631752d7b97838b1701513320b08cb Mon Sep 17 00:00:00 2001 From: geekgeekrun Date: Thu, 12 Feb 2026 07:00:30 +0800 Subject: [PATCH] fix in production build `process.env.NODE_ENV` is still `development` --- packages/ui/electron.vite.config.ts | 85 ++++++++++++++++++----------- packages/ui/src/main/index.ts | 5 +- 2 files changed, 56 insertions(+), 34 deletions(-) diff --git a/packages/ui/electron.vite.config.ts b/packages/ui/electron.vite.config.ts index eb529c4..2883f15 100644 --- a/packages/ui/electron.vite.config.ts +++ b/packages/ui/electron.vite.config.ts @@ -7,6 +7,56 @@ import transformerDirective from '@unocss/transformer-directives' import Replace from 'unplugin-replace/vite' process.env = { ...process.env, ...loadEnv(process.env.NODE_ENV!, process.cwd()) } +const mainPlugins = [ + externalizeDepsPlugin({ + exclude: [ + '@geekgeekrun/utils', + 'find-chrome-bin', + '@geekgeekrun/launch-bosszhipin-login-page-with-preload-extension' + ] + }), + Replace({ + delimiters: ['', ''], + sourcemap: true, + include: ['**/src/main/utils/gtag/Analytics.ts'], + values: [ + { + find: //g, + replacement: process.env.VITE_APP_GTAG_MEASUREMENT_ID as string + }, + { + find: //g, + replacement: process.env.VITE_APP_GTAG_API_SECRET as string + } + ] + }) +] +const preloadPlugins = [externalizeDepsPlugin()] +const rendererPlugins = [ + vue(), + UnoCSS({ + presets: [presetUno(), presetAttributify(), presetIcons()], + transformers: [transformerDirective()] + }) +] +if (process.env.NODE_ENV) { + ;[mainPlugins, preloadPlugins, rendererPlugins].forEach((pluginList) => { + pluginList.push( + Replace({ + delimiters: ['', ''], + sourcemap: true, + include: ['**'], + values: [ + { + find: /process.env.NODE_ENV/g, + replacement: `'${process.env.NODE_ENV}'` as string + } + ] + }) + ) + }) +} + export default defineConfig({ main: { build: { @@ -15,33 +65,10 @@ export default defineConfig({ }, minify: process.env.NODE_ENV === 'development' ? undefined : 'terser' }, - plugins: [ - externalizeDepsPlugin({ - exclude: [ - '@geekgeekrun/utils', - 'find-chrome-bin', - '@geekgeekrun/launch-bosszhipin-login-page-with-preload-extension' - ] - }), - Replace({ - delimiters: ['', ''], - sourcemap: true, - include: ['**/src/main/utils/gtag/Analytics.ts'], - values: [ - { - find: //g, - replacement: process.env.VITE_APP_GTAG_MEASUREMENT_ID as string - }, - { - find: //g, - replacement: process.env.VITE_APP_GTAG_API_SECRET as string - } - ] - }) - ] + plugins: mainPlugins }, preload: { - plugins: [externalizeDepsPlugin()], + plugins: preloadPlugins, build: { minify: process.env.NODE_ENV === 'development' ? undefined : 'terser' } @@ -52,13 +79,7 @@ export default defineConfig({ '@renderer': resolve('src/renderer/src') } }, - plugins: [ - vue(), - UnoCSS({ - presets: [presetUno(), presetAttributify(), presetIcons()], - transformers: [transformerDirective()] - }) - ], + plugins: rendererPlugins, build: { minify: process.env.NODE_ENV === 'development' ? undefined : 'terser' } diff --git a/packages/ui/src/main/index.ts b/packages/ui/src/main/index.ts index d9592e6..56c0e1c 100644 --- a/packages/ui/src/main/index.ts +++ b/packages/ui/src/main/index.ts @@ -9,6 +9,7 @@ const enableLogToFile = process.env.GEEKGEEKRUN_ENABLE_LOG_TO_FILE === String(1) if (isUiDev || enableLogToFile) { overrideConsole() } +console.log('NODE_ENV:', process.env.NODE_ENV) // 捕获未处理的 EPIPE 错误 process.on('uncaughtException', (err) => { @@ -18,9 +19,9 @@ process.on('uncaughtException', (err) => { throw err }) -console.log(process.argv) +console.log('argv:', process.argv) const commandlineArgs = minimist(isUiDev ? process.argv.slice(2) : process.argv.slice(1)) -console.log(commandlineArgs) +console.log('parsed commandline args:', commandlineArgs) const runMode = commandlineArgs['mode']