mirror of
https://github.com/electron-vite/electron-vite-vue
synced 2025-04-07 20:20:12 +08:00
refactor: package.json.env alternative .env
This commit is contained in:
parent
e8d61ca160
commit
d05338ca1b
@ -75,6 +75,11 @@
|
||||
"vite": "^2.5.6",
|
||||
"ws": "^8.2.3"
|
||||
},
|
||||
"env": {
|
||||
"//": "Will be auto inject to main,renderer,preload process.env",
|
||||
"PORT": 3344,
|
||||
"PORT_WS": 3355
|
||||
},
|
||||
"keywords": [
|
||||
"vite",
|
||||
"electron",
|
||||
|
@ -5,11 +5,10 @@ import electron from 'electron'
|
||||
import minimist from 'minimist'
|
||||
import chalk from 'chalk'
|
||||
import ora from 'ora'
|
||||
import { waitOn, getEnv } from './utils'
|
||||
import { waitOn } from './utils'
|
||||
import options from './rollup.config'
|
||||
import { main } from '../package.json'
|
||||
import { main, env } from '../package.json'
|
||||
|
||||
const env = getEnv()
|
||||
const argv = minimist(process.argv.slice(2))
|
||||
const opt = options({ proc: 'main', env: argv.env })
|
||||
const TAG = '[build-main.ts]'
|
||||
@ -18,7 +17,7 @@ const spinner = ora(`${TAG} Electron main build...`)
|
||||
; (async () => {
|
||||
if (argv.watch) {
|
||||
// Wait on vite server launched
|
||||
const waitOnState = waitOn({ port: env.PORT as string })
|
||||
const waitOnState = waitOn({ port: env.PORT })
|
||||
|
||||
const watcher = watch(opt)
|
||||
let child: ChildProcess
|
||||
@ -34,7 +33,7 @@ const spinner = ora(`${TAG} Electron main build...`)
|
||||
child = spawn(
|
||||
electron as unknown as string,
|
||||
[path.join(__dirname, `../${main}`)],
|
||||
{ stdio: 'inherit' },
|
||||
{ env: Object.assign(process.env, env), stdio: 'inherit' },
|
||||
)
|
||||
} else if (ev.code === 'ERROR') {
|
||||
console.log(ev.error)
|
||||
|
@ -6,7 +6,7 @@ import commonjs from '@rollup/plugin-commonjs'
|
||||
import replace from '@rollup/plugin-replace'
|
||||
import alias from '@rollup/plugin-alias'
|
||||
import json from '@rollup/plugin-json'
|
||||
import { builtins, getEnv } from './utils'
|
||||
import { builtins } from './utils'
|
||||
|
||||
export interface ConfigOptions {
|
||||
env?: typeof process.env.NODE_ENV
|
||||
@ -40,7 +40,7 @@ export default function (opts: ConfigOptions) {
|
||||
}),
|
||||
replace({
|
||||
...Object
|
||||
.entries({ ...getEnv(), NODE_ENV: opts.env })
|
||||
.entries({ NODE_ENV: opts.env })
|
||||
.reduce(
|
||||
(acc, [k, v]) => Object.assign(acc, { [`process.env.${k}`]: JSON.stringify(v) }),
|
||||
{},
|
||||
|
@ -1,10 +1,7 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { builtinModules } from 'module'
|
||||
import { get } from 'http'
|
||||
import { green } from 'chalk'
|
||||
import { Plugin } from 'rollup'
|
||||
import { parse as parseEnv } from 'dotenv'
|
||||
|
||||
/** 轮询监听 vite 启动 */
|
||||
export function waitOn(arg0: { port: string | number; interval?: number; }) {
|
||||
@ -36,16 +33,3 @@ export function typescript(): Plugin {
|
||||
name: 'cxmh:rollup-typescript-esbuild',
|
||||
}
|
||||
}
|
||||
|
||||
export function getEnv(): Record<string, string> {
|
||||
try {
|
||||
if (getEnv.env) {
|
||||
return getEnv.env
|
||||
}
|
||||
const env = parseEnv(fs.readFileSync(path.join(process.cwd(), '.env')))
|
||||
return getEnv.env = env
|
||||
} catch (error) {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
getEnv.env = undefined as (Record<string, string> | undefined) // Just fix ts check
|
||||
|
@ -1,8 +1,8 @@
|
||||
require('dotenv').config({ path: join(__dirname, '.env') })
|
||||
|
||||
// require('dotenv').config({ path: join(__dirname, '.env') })
|
||||
import { join } from 'path'
|
||||
import { defineConfig } from 'vite'
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
import pkg from './package.json'
|
||||
|
||||
export default defineConfig(env => {
|
||||
return {
|
||||
@ -12,7 +12,7 @@ export default defineConfig(env => {
|
||||
root: join(__dirname, 'src/render'),
|
||||
base: './',
|
||||
server: {
|
||||
port: +process.env.PORT,
|
||||
port: pkg.env.PORT,
|
||||
},
|
||||
resolve: {
|
||||
alias: {
|
||||
|
Loading…
x
Reference in New Issue
Block a user