2020-12-24 10:17:27 +08:00
|
|
|
/**
|
2021-08-05 09:25:34 +08:00
|
|
|
* Electron main process package script
|
2020-12-24 10:17:27 +08:00
|
|
|
*/
|
|
|
|
import { join } from 'path'
|
|
|
|
import { spawn, ChildProcess } from 'child_process'
|
2021-08-05 09:25:34 +08:00
|
|
|
import { watch, rollup, RollupOptions, OutputOptions } from 'rollup'
|
|
|
|
import nodeResolve from '@rollup/plugin-node-resolve'
|
|
|
|
import commonjs from '@rollup/plugin-commonjs'
|
|
|
|
import typescript from '@rollup/plugin-typescript'
|
|
|
|
import alias from '@rollup/plugin-alias'
|
|
|
|
import json from '@rollup/plugin-json'
|
|
|
|
import { builtins } from './utils'
|
2020-12-24 10:17:27 +08:00
|
|
|
import minimist from 'minimist'
|
|
|
|
import chalk from 'chalk'
|
|
|
|
import ora from 'ora'
|
|
|
|
import electron from 'electron'
|
|
|
|
import dotenv from 'dotenv'
|
2021-02-17 22:27:40 +08:00
|
|
|
import { waitOn } from './utils'
|
2020-12-24 10:17:27 +08:00
|
|
|
import { main } from '../package.json'
|
|
|
|
|
2021-08-05 09:25:34 +08:00
|
|
|
// Inject some environment variables from ".env"
|
2020-12-24 10:17:27 +08:00
|
|
|
dotenv.config({ path: join(__dirname, '../.env') })
|
|
|
|
|
|
|
|
const argv = minimist(process.argv.slice(2))
|
2021-08-05 09:25:34 +08:00
|
|
|
const opts = configFactory(argv.env)
|
2021-04-01 11:32:55 +08:00
|
|
|
const TAG = '[build-main.ts]'
|
2020-12-24 10:17:27 +08:00
|
|
|
const spinner = ora(`${TAG} Electron build...`)
|
|
|
|
|
|
|
|
if (argv.watch) {
|
2021-02-17 22:27:40 +08:00
|
|
|
waitOn({ port: process.env.PORT as string }).then(msg => {
|
2021-05-15 14:53:43 +08:00
|
|
|
const watcher = watch(opts)
|
2020-12-24 10:17:27 +08:00
|
|
|
let child: ChildProcess
|
|
|
|
watcher.on('change', filename => {
|
|
|
|
const log = chalk.green(`change -- ${filename}`)
|
|
|
|
console.log(TAG, log)
|
|
|
|
})
|
|
|
|
watcher.on('event', ev => {
|
|
|
|
if (ev.code === 'END') {
|
|
|
|
if (child) child.kill()
|
2021-07-10 08:51:15 +08:00
|
|
|
child = spawn(
|
|
|
|
electron as any,
|
|
|
|
[join(__dirname, `../${main}`)],
|
|
|
|
{
|
|
|
|
stdio: 'inherit',
|
|
|
|
env: Object.assign(process.env, { NODE_ENV: argv.env }),
|
|
|
|
})
|
2020-12-24 10:17:27 +08:00
|
|
|
} else if (ev.code === 'ERROR') {
|
|
|
|
console.log(ev.error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
spinner.start()
|
2021-05-15 14:53:43 +08:00
|
|
|
rollup(opts)
|
2020-12-24 10:17:27 +08:00
|
|
|
.then(build => {
|
|
|
|
spinner.stop()
|
|
|
|
console.log(TAG, chalk.green('Electron build successed.'))
|
2021-05-15 14:53:43 +08:00
|
|
|
build.write(opts.output as OutputOptions)
|
2020-12-24 10:17:27 +08:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
spinner.stop()
|
|
|
|
console.log(`\n${TAG} ${chalk.red('构建报错')}\n`, error, '\n')
|
|
|
|
})
|
|
|
|
}
|
2021-08-05 09:25:34 +08:00
|
|
|
|
|
|
|
function configFactory(env = 'production') {
|
|
|
|
const options: RollupOptions = {
|
|
|
|
input: join(__dirname, '../src/main/index.ts'),
|
|
|
|
output: {
|
|
|
|
file: join(__dirname, '../dist/main/index.js'),
|
|
|
|
format: 'cjs',
|
|
|
|
name: 'ElectronMainBundle',
|
|
|
|
sourcemap: true,
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
nodeResolve(),
|
|
|
|
commonjs(),
|
|
|
|
json(),
|
|
|
|
typescript(),
|
|
|
|
alias({
|
|
|
|
entries: [
|
|
|
|
{ find: '@render', replacement: join(__dirname, '../src/render') },
|
|
|
|
{ find: '@main', replacement: join(__dirname, '../src/main') },
|
|
|
|
{ find: '@src', replacement: join(__dirname, '../src') },
|
|
|
|
{ find: '@root', replacement: join(__dirname, '..') },
|
|
|
|
]
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
external: [
|
|
|
|
...builtins(),
|
|
|
|
'electron',
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|