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'
|
process.env.NODE_ENV = 'production'
|
||||||
|
|
||||||
|
import { dirname, join } from 'path'
|
||||||
|
import { fileURLToPath } from 'url'
|
||||||
import { build as viteBuild } from 'vite'
|
import { build as viteBuild } from 'vite'
|
||||||
import chalk from 'chalk'
|
import chalk from 'chalk'
|
||||||
|
|
||||||
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||||
const TAG = chalk.bgBlue(' build.mjs ')
|
const TAG = chalk.bgBlue(' build.mjs ')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {Record<string, import('vite').InlineConfig>}
|
||||||
|
*/
|
||||||
const viteConfigs = {
|
const viteConfigs = {
|
||||||
main: 'configs/vite-main.config.ts',
|
main: {
|
||||||
preload: 'configs/vite-preload.config.ts',
|
configFile: 'scripts/vite.config.mjs',
|
||||||
renderer: 'src/renderer/vite.config.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({ configFile: configPath, mode: process.env.NODE_ENV })
|
await viteBuild(config)
|
||||||
console.groupEnd()
|
|
||||||
console.log() // for beautiful log.
|
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'
|
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 chalk from 'chalk'
|
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 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({
|
|
||||||
// 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() {
|
|
||||||
/**
|
/**
|
||||||
* @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.config.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.config.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({ configFile: 'src/renderer/vite.config.ts' })
|
const server = await createServer({ configFile: 'src/renderer/vite.config.ts' })
|
||||||
|
|
||||||
await viteDevServer.listen()
|
await server.listen()
|
||||||
await watchPreload(viteDevServer)
|
await watchPreload(server)
|
||||||
await watchMain()
|
await watchMain()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user