diff --git a/scripts/build.ts b/scripts/build.ts index 087751b..2ededb7 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -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 { try { diff --git a/scripts/dev.ts b/scripts/dev.ts index e69de29..0a2ba81 100644 --- a/scripts/dev.ts +++ b/scripts/dev.ts @@ -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) + } +})(); diff --git a/scripts/utils.ts b/scripts/utils.ts index 076aafb..4d1e5af 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -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, +} +