mirror of
https://github.com/electron-vite/electron-vite-vue
synced 2025-03-03 23:40:09 +08:00
refactor: better build, watch script
This commit is contained in:
parent
caaa8c7f19
commit
3c6a739da3
@ -1,21 +1,40 @@
|
||||
process.env.NODE_ENV = 'production'
|
||||
|
||||
import { dirname, join } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
import { build as viteBuild } from 'vite'
|
||||
import chalk from 'chalk'
|
||||
|
||||
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.config.ts',
|
||||
preload: 'configs/vite-preload.config.ts',
|
||||
renderer: 'src/renderer/vite.config.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 viteBuild(config)
|
||||
console.log() // for beautiful log.
|
||||
}
|
||||
}
|
||||
|
29
scripts/vite.config.mjs
Normal file
29
scripts/vite.config.mjs
Normal file
@ -0,0 +1,29 @@
|
||||
import { builtinModules, createRequire } from 'module'
|
||||
import { defineConfig } from 'vite'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
const pkg = require('../package.json')
|
||||
|
||||
export default defineConfig({
|
||||
mode: process.env.NODE_ENV,
|
||||
// root: [path],
|
||||
build: {
|
||||
// outDir: [path],
|
||||
lib: {
|
||||
entry: 'index.ts',
|
||||
formats: ['cjs'],
|
||||
},
|
||||
minify: process.env.NODE_ENV === 'production',
|
||||
emptyOutDir: true,
|
||||
rollupOptions: {
|
||||
external: [
|
||||
'electron',
|
||||
...builtinModules,
|
||||
...Object.keys(pkg.dependencies || {}),
|
||||
],
|
||||
output: {
|
||||
entryFileNames: '[name].cjs',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
@ -1,80 +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 chalk from 'chalk'
|
||||
import { spawn } from 'child_process'
|
||||
import { createServer, build } from 'vite'
|
||||
import electron from 'electron'
|
||||
|
||||
const TAG = chalk.bgGreen(' dev.mjs ')
|
||||
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({
|
||||
// Ensure `vite-main.config.ts` and `vite-preload.config.ts` correct `process.env.NODE_ENV`
|
||||
mode: process.env.NODE_ENV,
|
||||
// Options here precedence over configFile
|
||||
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.config.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.config.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: 'src/renderer/vite.config.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…
x
Reference in New Issue
Block a user