From 66cb0b6abd4e0c14f2e5a3dd9981c5a44c0fa8e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Sat, 10 Jul 2021 08:51:15 +0800 Subject: [PATCH] refactor: inject process.env.NODE_ENV --- script/build-main.ts | 9 +++++++-- typings/global.d.ts | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/script/build-main.ts b/script/build-main.ts index 372b024..cbca964 100644 --- a/script/build-main.ts +++ b/script/build-main.ts @@ -19,7 +19,6 @@ const argv = minimist(process.argv.slice(2)) const opts = options(argv.env) const TAG = '[build-main.ts]' const spinner = ora(`${TAG} Electron build...`) -process.env.NODE_ENV = argv.env if (argv.watch) { waitOn({ port: process.env.PORT as string }).then(msg => { @@ -32,7 +31,13 @@ if (argv.watch) { watcher.on('event', ev => { if (ev.code === 'END') { if (child) child.kill() - child = spawn(electron as any, [join(__dirname, `../${main}`)], { stdio: 'inherit' }) + child = spawn( + electron as any, + [join(__dirname, `../${main}`)], + { + stdio: 'inherit', + env: Object.assign(process.env, { NODE_ENV: argv.env }), + }) } else if (ev.code === 'ERROR') { console.log(ev.error) } diff --git a/typings/global.d.ts b/typings/global.d.ts index 42be2a3..22db89b 100644 --- a/typings/global.d.ts +++ b/typings/global.d.ts @@ -1,7 +1,7 @@ declare namespace NodeJS { interface ProcessEnv { - NODE_ENV: 'development' | 'production' + readonly NODE_ENV: 'development' | 'production' readonly PORT: string } }