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