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