feat: script/dev.ts

This commit is contained in:
草鞋没号 2021-11-01 20:57:17 +08:00
parent a889da2dba
commit 3210954fac
3 changed files with 107 additions and 26 deletions

View File

@ -1,34 +1,14 @@
import { join, relative } from 'path'
import { readdirSync } from 'fs'
import { OutputOptions, rollup, RollupOptions, RollupOutput, RollupError } from 'rollup'
import { rollup, RollupOptions, OutputOptions, RollupOutput } from 'rollup'
import { build as viteBuild2 } from 'vite'
import { optionsFactory } from './rollup.config'
export type BuildResult = [RollupError | null, RollupOutput | null]
import {
mainOptions,
preloadOptions,
BuildResult,
} from './utils'
const TAG = '[build.ts]'
function mainOptions() {
return optionsFactory({
input: join(__dirname, '../main/index.ts'),
output: {
dir: 'dist/main',
},
})
}
function preloadOptions() {
const dirs = readdirSync(join(__dirname, '../preload'))
const inputs = dirs.filter(name => /^index\..+\.ts$/.test(name))
return optionsFactory({
input: inputs.map(input => join(__dirname, '../preload', input)),
output: {
dir: 'dist/preload',
},
})
}
// build main、preload
async function rollupBuild(options: RollupOptions): Promise<BuildResult> {
try {

View File

@ -0,0 +1,69 @@
import { join } from 'path'
import { spawn, ChildProcess } from 'child_process'
import electron from 'electron'
import { RollupWatcher, RollupWatcherEvent, watch } from 'rollup'
import { createServer as createViteServer } from 'vite'
import pkg from '../package.json'
import {
mainOptions,
preloadOptions,
} from './utils'
const TAG = '[dev.ts]'
function eventHandle(ev: RollupWatcherEvent) {
if (ev.code === 'ERROR') {
console.error(TAG, ev.error)
} else if (ev.code === 'BUNDLE_START') {
console.log(TAG, `Rebuild start - ${ev.output}`)
}
}
function watchMain(): RollupWatcher {
let electronProcess: ChildProcess | null = null
return watch(mainOptions())
.on('event', ev => {
if (ev.code === 'END') {
electronProcess && electronProcess.kill()
electronProcess = spawn(
electron as unknown as string,
[join(__dirname, '..', pkg.main)],
{ stdio: 'inherit', env: Object.assign(process.env, pkg.env) },
)
} else if (ev.code === 'ERROR') {
electronProcess && electronProcess.kill()
electronProcess = null
}
eventHandle(ev)
})
}
function watchPreload(): RollupWatcher {
return watch(preloadOptions())
.on('event', ev => {
if (ev.code === 'END') {
// TODO Hot reload
}
eventHandle(ev)
})
}
; (async () => {
try {
const server = await (await createViteServer({
root: join(__dirname, '../react-ts'),
configFile: join(__dirname, '../react-ts/vite.config.ts'),
})).listen()
const { host = '127.0.0.1', port = 3000 } = server.config.server
console.log(TAG, `Server run at - http://${host}:${port}`)
watchPreload()
watchMain()
} catch (error) {
console.error(TAG, error)
}
})();

View File

@ -1 +1,33 @@
import { join } from 'path'
import { readdirSync } from 'fs'
import { OutputOptions, rollup, RollupOptions, RollupOutput, RollupError } from 'rollup'
import { optionsFactory } from './rollup.config'
export type BuildResult = [RollupError | null, RollupOutput | null]
function mainOptions(): RollupOptions {
return optionsFactory({
input: join(__dirname, '../main/index.ts'),
output: {
dir: 'dist/main',
},
})
}
function preloadOptions(): RollupOptions {
const dirs = readdirSync(join(__dirname, '../preload'))
const inputs = dirs.filter(name => /^index\..+\.ts$/.test(name))
return optionsFactory({
input: inputs.map(input => join(__dirname, '../preload', input)),
output: {
dir: 'dist/preload',
},
})
}
export {
mainOptions,
preloadOptions,
}