mirror of
https://github.com/electron-vite/electron-vite-vue
synced 2025-08-30 19:01:47 +08:00
chore(files): script -> scripts
This commit is contained in:
55
scripts/build-main.ts
Normal file
55
scripts/build-main.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import path from 'path'
|
||||
import { watch, rollup, OutputOptions } from 'rollup'
|
||||
import { spawn, ChildProcess } from 'child_process'
|
||||
import electron from 'electron'
|
||||
import minimist from 'minimist'
|
||||
import chalk from 'chalk'
|
||||
import ora from 'ora'
|
||||
import { waitOn } from './utils'
|
||||
import options from './rollup.config'
|
||||
import { main, env } from '../package.json'
|
||||
|
||||
const argv = minimist(process.argv.slice(2))
|
||||
const opt = options({ proc: 'main', env: argv.env })
|
||||
const TAG = '[build-main.ts]'
|
||||
const spinner = ora(`${TAG} Electron main build...`)
|
||||
|
||||
; (async () => {
|
||||
if (argv.watch) {
|
||||
// Wait on vite server launched
|
||||
const waitOnState = waitOn({ port: env.PORT })
|
||||
|
||||
const watcher = watch(opt)
|
||||
let child: ChildProcess
|
||||
watcher.on('change', filename => {
|
||||
const log = chalk.green(`change -- ${filename}`)
|
||||
console.log(TAG, log)
|
||||
})
|
||||
watcher.on('event', async ev => {
|
||||
await waitOnState
|
||||
|
||||
if (ev.code === 'END') {
|
||||
if (child) child.kill()
|
||||
child = spawn(
|
||||
electron as unknown as string,
|
||||
[path.join(__dirname, `../${main}`)],
|
||||
{ env: Object.assign(process.env, env), stdio: 'inherit' },
|
||||
)
|
||||
} else if (ev.code === 'ERROR') {
|
||||
console.log(ev.error)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
spinner.start()
|
||||
try {
|
||||
const build = await rollup(opt)
|
||||
await build.write(opt.output as OutputOptions)
|
||||
spinner.succeed()
|
||||
process.exit()
|
||||
} catch (error) {
|
||||
console.log(`\n${TAG} ${chalk.red('构建报错')}\n`, error, '\n')
|
||||
spinner.fail()
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
})();
|
42
scripts/build-preload.ts
Normal file
42
scripts/build-preload.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { watch, rollup, OutputOptions } from 'rollup'
|
||||
import minimist from 'minimist'
|
||||
import chalk from 'chalk'
|
||||
import ora from 'ora'
|
||||
import WebSocket from 'ws'
|
||||
import options from './rollup.config'
|
||||
import { createWsServer, formatWsSendData } from './ws'
|
||||
|
||||
const argv = minimist(process.argv.slice(2))
|
||||
const opt = options({ proc: 'preload', env: argv.env })
|
||||
const TAG = '[build-preload.ts]'
|
||||
const spinner = ora(`${TAG} Electron preload build...`)
|
||||
|
||||
; (async () => {
|
||||
if (argv.watch) {
|
||||
const watcher = watch(opt)
|
||||
const wssObj = createWsServer({ TAG })
|
||||
|
||||
watcher.on('change', filename => {
|
||||
const log = chalk.yellow(`change -- ${filename}`)
|
||||
console.log(TAG, log)
|
||||
|
||||
// Hot reload renderer process !!!
|
||||
if (wssObj.instance?.readyState === WebSocket.OPEN) {
|
||||
console.log(TAG, 'Hot reload renderer process')
|
||||
wssObj.instance.send(formatWsSendData({ cmd: 'reload', data: Date.now() }))
|
||||
}
|
||||
})
|
||||
} else {
|
||||
spinner.start()
|
||||
try {
|
||||
const build = await rollup(opt)
|
||||
await build.write(opt.output as OutputOptions)
|
||||
spinner.succeed()
|
||||
process.exit()
|
||||
} catch (error) {
|
||||
console.log(`\n${TAG} ${chalk.red('构建报错')}\n`, error, '\n')
|
||||
spinner.fail()
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
})();
|
64
scripts/rollup.config.ts
Normal file
64
scripts/rollup.config.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import path from 'path'
|
||||
import { RollupOptions } from 'rollup'
|
||||
import nodeResolve from '@rollup/plugin-node-resolve'
|
||||
import typescript from '@rollup/plugin-typescript'
|
||||
import commonjs from '@rollup/plugin-commonjs'
|
||||
import replace from '@rollup/plugin-replace'
|
||||
import alias from '@rollup/plugin-alias'
|
||||
import json from '@rollup/plugin-json'
|
||||
import { builtins } from './utils'
|
||||
|
||||
export interface ConfigOptions {
|
||||
env?: typeof process.env.NODE_ENV
|
||||
proc: 'main' | 'render' | 'preload'
|
||||
}
|
||||
|
||||
export default function (opts: ConfigOptions) {
|
||||
const sourcemap = opts.proc === 'render'
|
||||
const options: RollupOptions = {
|
||||
input: path.join(__dirname, `../src/${opts.proc}/index.ts`),
|
||||
output: {
|
||||
dir: path.join(__dirname, `../dist/${opts.proc}`),
|
||||
format: 'cjs',
|
||||
sourcemap,
|
||||
},
|
||||
plugins: [
|
||||
nodeResolve({
|
||||
extensions: ['.ts', '.js', 'json'],
|
||||
}),
|
||||
commonjs(),
|
||||
json(),
|
||||
typescript({
|
||||
sourceMap: sourcemap,
|
||||
noEmitOnError: true,
|
||||
}),
|
||||
alias({
|
||||
entries: {
|
||||
'@root': path.join(__dirname, '..'),
|
||||
'@': path.join(__dirname, '../src'),
|
||||
},
|
||||
}),
|
||||
replace({
|
||||
...Object
|
||||
.entries({ NODE_ENV: opts.env })
|
||||
.reduce(
|
||||
(acc, [k, v]) => Object.assign(acc, { [`process.env.${k}`]: JSON.stringify(v) }),
|
||||
{},
|
||||
),
|
||||
preventAssignment: true,
|
||||
}),
|
||||
],
|
||||
external: [
|
||||
...builtins(),
|
||||
'electron',
|
||||
],
|
||||
onwarn: warning => {
|
||||
// https://github.com/rollup/rollup/issues/1089#issuecomment-365395213
|
||||
if (warning.code !== 'CIRCULAR_DEPENDENCY') {
|
||||
console.error(`(!) ${warning.message}`)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
return options
|
||||
}
|
35
scripts/utils.ts
Normal file
35
scripts/utils.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { builtinModules } from 'module'
|
||||
import { get } from 'http'
|
||||
import { green } from 'chalk'
|
||||
import { Plugin } from 'rollup'
|
||||
|
||||
/** 轮询监听 vite 启动 */
|
||||
export function waitOn(arg0: { port: string | number; interval?: number; }) {
|
||||
return new Promise<number | undefined>(resolve => {
|
||||
const { port, interval = 149 } = arg0
|
||||
const url = `http://localhost:${port}`
|
||||
let counter = 0
|
||||
const timer: NodeJS.Timer = setInterval(() => {
|
||||
get(url, res => {
|
||||
clearInterval(timer)
|
||||
console.log('[waitOn]', green(`"${url}" are already responsive.`), `(${res.statusCode}: ${res.statusMessage})`)
|
||||
resolve(res.statusCode)
|
||||
}).on('error', err => {
|
||||
console.log('[waitOn]', `counter: ${counter++}`)
|
||||
})
|
||||
}, interval)
|
||||
})
|
||||
}
|
||||
|
||||
/** node.js builtins module */
|
||||
export const builtins = () => builtinModules.filter(x => !/^_|^(internal|v8|node-inspect)\/|\//.test(x))
|
||||
|
||||
/**
|
||||
* @todo
|
||||
* typescript with esbuild
|
||||
*/
|
||||
export function typescript(): Plugin {
|
||||
return {
|
||||
name: 'cxmh:rollup-typescript-esbuild',
|
||||
}
|
||||
}
|
43
scripts/ws.ts
Normal file
43
scripts/ws.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Hot reload from preload script during development
|
||||
*/
|
||||
import WebSocket from 'ws'
|
||||
import chalk from 'chalk'
|
||||
import pkg from '../package.json'
|
||||
|
||||
export interface CreateWsServerOptions {
|
||||
TAG: string
|
||||
}
|
||||
|
||||
export function createWsServer(options: CreateWsServerOptions) {
|
||||
const { TAG } = options
|
||||
const port = pkg.env.PORT_WS
|
||||
const host = '127.0.0.1'
|
||||
const wss = new WebSocket.Server({ host, port })
|
||||
const wssObj: { wss: WebSocket.Server; instance: WebSocket | null } = { wss, instance: null }
|
||||
|
||||
console.log(TAG, 'Wss run at - ' + chalk.yellow(`ws://${host}:${port}`))
|
||||
|
||||
wss.on('connection', ws => {
|
||||
console.log(TAG, chalk.yellow(`wss.on('connection')`))
|
||||
|
||||
wssObj.instance = ws
|
||||
ws.on('message', message => {
|
||||
console.log(TAG, `ws.on('message'):`, message.toString())
|
||||
})
|
||||
|
||||
ws.send(formatWsSendData({ cmd: 'message', data: 'connected.' }))
|
||||
})
|
||||
|
||||
wss.on('close', () => {
|
||||
console.log(TAG, chalk.gray(`wss.on('close')`))
|
||||
|
||||
wssObj.instance = null
|
||||
})
|
||||
|
||||
return wssObj
|
||||
}
|
||||
|
||||
export function formatWsSendData(json: { cmd: string, data?: any }) {
|
||||
return JSON.stringify(json)
|
||||
}
|
Reference in New Issue
Block a user