feature: main,preload build
This commit is contained in:
parent
93fa203620
commit
7b92b312d6
|
@ -0,0 +1,24 @@
|
||||||
|
import { app, BrowserWindow } from 'electron'
|
||||||
|
|
||||||
|
const WINDOWS: Record<string, BrowserWindow | null> = {
|
||||||
|
main: null,
|
||||||
|
win1: null,
|
||||||
|
}
|
||||||
|
|
||||||
|
function mainWin() {
|
||||||
|
WINDOWS.main = new BrowserWindow({
|
||||||
|
title: 'Main window',
|
||||||
|
})
|
||||||
|
|
||||||
|
if (app.isPackaged) {
|
||||||
|
WINDOWS.main.loadFile('')
|
||||||
|
} else {
|
||||||
|
WINDOWS.main.loadURL(`http://127.0.0.1:${process.env.PORT}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
app.whenReady().then(mainWin)
|
||||||
|
app.on('window-all-closed', () => {
|
||||||
|
Object.keys(WINDOWS).forEach((key) => WINDOWS[key] = null)
|
||||||
|
})
|
|
@ -0,0 +1 @@
|
||||||
|
console.log('index.main.ts')
|
|
@ -0,0 +1 @@
|
||||||
|
console.log('index.win1.ts')
|
|
@ -0,0 +1,52 @@
|
||||||
|
import { join } from 'path'
|
||||||
|
import { readdirSync } from 'fs'
|
||||||
|
import { OutputOptions, rollup, RollupOptions, RollupOutput } from 'rollup'
|
||||||
|
import { optionsFactory } from './rollup.config'
|
||||||
|
|
||||||
|
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',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async function doBuild(options: RollupOptions): Promise<[Error | null, RollupOutput | null]> {
|
||||||
|
try {
|
||||||
|
const build = await rollup(options)
|
||||||
|
const output = await build.write(options.output as OutputOptions)
|
||||||
|
|
||||||
|
return [null, output]
|
||||||
|
} catch (error: any) {
|
||||||
|
return [error, null]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// build
|
||||||
|
[mainOptions(), preloadOptions()].forEach(options => {
|
||||||
|
doBuild(options)
|
||||||
|
.then(([err, output]) => {
|
||||||
|
if (err) {
|
||||||
|
console.error(err)
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
output?.output.forEach(out => {
|
||||||
|
console.log(TAG, `Build successed -- ${(out as any).facadeModuleId}`)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
|
@ -0,0 +1,29 @@
|
||||||
|
import { builtinModules } from 'module'
|
||||||
|
import { RollupOptions } from 'rollup'
|
||||||
|
import commonjs from '@rollup/plugin-commonjs'
|
||||||
|
import { nodeResolve } from '@rollup/plugin-node-resolve'
|
||||||
|
// import typescript from '@rollup/plugin-typescript'
|
||||||
|
import swc from 'rollup-plugin-swc'
|
||||||
|
|
||||||
|
function optionsFactory(options: RollupOptions): RollupOptions {
|
||||||
|
return {
|
||||||
|
input: options.input,
|
||||||
|
output: {
|
||||||
|
name: '[name].js',
|
||||||
|
format: 'cjs',
|
||||||
|
...options.output,
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
commonjs(),
|
||||||
|
nodeResolve(),
|
||||||
|
// typescript(),
|
||||||
|
swc(),
|
||||||
|
],
|
||||||
|
external: [
|
||||||
|
'electron',
|
||||||
|
...builtinModules,
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { optionsFactory }
|
|
@ -0,0 +1 @@
|
||||||
|
import { join } from 'path'
|
Loading…
Reference in New Issue