refactory: better script
This commit is contained in:
parent
2fd327d49c
commit
d417c65f63
|
@ -1,24 +1,40 @@
|
|||
process.env.NODE_ENV = 'production'
|
||||
|
||||
import { build as viteBuild } from 'vite'
|
||||
import { dirname, join } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
import { build } from 'vite'
|
||||
import chalk from 'chalk'
|
||||
|
||||
const TAG = chalk.bgBlue('[build.mjs]')
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const TAG = chalk.bgBlue(' build.mjs ')
|
||||
|
||||
/**
|
||||
* @type {Record<string, import('vite').InlineConfig>}
|
||||
*/
|
||||
const viteConfigs = {
|
||||
main: 'configs/vite.main.ts',
|
||||
preload: 'configs/vite.preload.ts',
|
||||
renderer: 'configs/vite.renderer.ts',
|
||||
main: {
|
||||
configFile: 'scripts/vite.config.mjs',
|
||||
root: join(__dirname, '../src/main'),
|
||||
build: {
|
||||
outDir: '../../dist/main',
|
||||
},
|
||||
},
|
||||
preload: {
|
||||
configFile: 'scripts/vite.config.mjs',
|
||||
root: join(__dirname, '../src/preload'),
|
||||
build: {
|
||||
outDir: '../../dist/preload',
|
||||
},
|
||||
},
|
||||
renderer: {
|
||||
configFile: 'src/renderer/vite.config.ts',
|
||||
},
|
||||
}
|
||||
|
||||
async function buildElectron() {
|
||||
for (const [name, configPath] of Object.entries(viteConfigs)) {
|
||||
console.group(TAG, name)
|
||||
await viteBuild({
|
||||
configFile: configPath,
|
||||
mode: process.env.NODE_ENV,
|
||||
})
|
||||
console.groupEnd()
|
||||
for (const [name, config] of Object.entries(viteConfigs)) {
|
||||
console.log(TAG, name)
|
||||
await build(config)
|
||||
console.log() // for beautiful log.
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,77 +1,66 @@
|
|||
process.env.NODE_ENV = 'development'
|
||||
|
||||
import electron from 'electron'
|
||||
import { spawn } from 'child_process'
|
||||
import { fileURLToPath } from 'url'
|
||||
import { join, dirname } from 'path'
|
||||
import { createRequire } from 'module'
|
||||
import { createServer, build as viteBuild } from 'vite'
|
||||
import { spawn } from 'child_process'
|
||||
import { createServer, build } from 'vite'
|
||||
import electron from 'electron'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const require = createRequire(import.meta.url)
|
||||
const pkg = require('../package.json')
|
||||
|
||||
/**
|
||||
* @param {{ name: string; configFile: string; writeBundle: import('rollup').OutputPlugin['writeBundle'] }} param0
|
||||
* @returns {import('rollup').RollupWatcher}
|
||||
* @type {() => Promise<import('rollup').RollupWatcher>}
|
||||
*/
|
||||
function getWatcher({ name, configFile, writeBundle }) {
|
||||
return viteBuild({
|
||||
// Options here precedence over configFile
|
||||
mode: process.env.NODE_ENV,
|
||||
build: {
|
||||
watch: {},
|
||||
},
|
||||
configFile,
|
||||
plugins: [{ name, writeBundle }],
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Promise<import('rollup').RollupWatcher>}
|
||||
*/
|
||||
async function watchMain() {
|
||||
function watchMain() {
|
||||
/**
|
||||
* @type {import('child_process').ChildProcessWithoutNullStreams | null}
|
||||
*/
|
||||
let electronProcess = null
|
||||
|
||||
/**
|
||||
* @type {import('rollup').RollupWatcher}
|
||||
*/
|
||||
const watcher = await getWatcher({
|
||||
name: 'electron-main-watcher',
|
||||
configFile: 'configs/vite.main.ts',
|
||||
writeBundle() {
|
||||
electronProcess && electronProcess.kill()
|
||||
electronProcess = spawn(electron, ['.'], {
|
||||
stdio: 'inherit',
|
||||
env: Object.assign(process.env, pkg.env),
|
||||
})
|
||||
return build({
|
||||
configFile: 'scripts/vite.config.mjs',
|
||||
root: join(__dirname, '../src/main'),
|
||||
build: {
|
||||
outDir: '../../dist/main',
|
||||
},
|
||||
plugins: [{
|
||||
name: 'electron-main-watcher',
|
||||
writeBundle() {
|
||||
electronProcess && electronProcess.kill()
|
||||
electronProcess = spawn(electron, ['.'], {
|
||||
stdio: 'inherit',
|
||||
env: Object.assign(process.env, pkg.env),
|
||||
})
|
||||
},
|
||||
}],
|
||||
})
|
||||
|
||||
return watcher
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('vite').ViteDevServer} viteDevServer
|
||||
* @returns {Promise<import('rollup').RollupWatcher>}
|
||||
* @type {(server: import('vite').ViteDevServer) => Promise<import('rollup').RollupWatcher>}
|
||||
*/
|
||||
async function watchPreload(viteDevServer) {
|
||||
return getWatcher({
|
||||
name: 'electron-preload-watcher',
|
||||
configFile: 'configs/vite.preload.ts',
|
||||
writeBundle() {
|
||||
viteDevServer.ws.send({
|
||||
type: 'full-reload',
|
||||
})
|
||||
function watchPreload(server) {
|
||||
return build({
|
||||
configFile: 'scripts/vite.config.mjs',
|
||||
root: join(__dirname, '../src/preload'),
|
||||
build: {
|
||||
outDir: '../../dist/preload',
|
||||
},
|
||||
plugins: [{
|
||||
name: 'electron-preload-watcher',
|
||||
writeBundle() {
|
||||
server.ws.send({ type: 'full-reload' })
|
||||
},
|
||||
}],
|
||||
})
|
||||
}
|
||||
|
||||
// bootstrap
|
||||
const viteDevServer = await createServer({
|
||||
configFile: 'configs/vite.renderer.ts',
|
||||
})
|
||||
const server = await createServer({ configFile: 'src/renderer/vite.config.ts' })
|
||||
|
||||
await viteDevServer.listen()
|
||||
await watchPreload(viteDevServer)
|
||||
await server.listen()
|
||||
await watchPreload(server)
|
||||
await watchMain()
|
||||
|
|
Loading…
Reference in New Issue