diff --git a/script/build-main.ts b/script/build-main.ts index cbca964..20dd593 100644 --- a/script/build-main.ts +++ b/script/build-main.ts @@ -1,22 +1,28 @@ /** - * electron 打包 + * Electron main process package script */ import { join } from 'path' import { spawn, ChildProcess } from 'child_process' -import { watch, rollup, OutputOptions } from 'rollup' +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' import minimist from 'minimist' import chalk from 'chalk' import ora from 'ora' import electron from 'electron' import dotenv from 'dotenv' import { waitOn } from './utils' -import options from './rollup.config' import { main } from '../package.json' +// Inject some environment variables from ".env" dotenv.config({ path: join(__dirname, '../.env') }) const argv = minimist(process.argv.slice(2)) -const opts = options(argv.env) +const opts = configFactory(argv.env) const TAG = '[build-main.ts]' const spinner = ora(`${TAG} Electron build...`) @@ -56,3 +62,35 @@ if (argv.watch) { console.log(`\n${TAG} ${chalk.red('构建报错')}\n`, error, '\n') }) } + +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 +} diff --git a/script/build-preload.ts b/script/build-preload.ts new file mode 100644 index 0000000..710d429 --- /dev/null +++ b/script/build-preload.ts @@ -0,0 +1,46 @@ +/** + * Electron main preload package script + */ +import { join } from 'path' +import { watch, rollup, RollupOptions, OutputOptions } from 'rollup' +import { builtins } from './utils' +import minimist from 'minimist' +import chalk from 'chalk' + +const argv = minimist(process.argv.slice(2)) +const opts = configFactory(argv.env) +const TAG = '[build-preload.ts]' + +if (argv.watch) { + const watcher = watch(opts) + watcher.on('change', filename => { + const log = chalk.yellow(`change -- ${filename}`) + console.log(TAG, log) + }) +} else { + rollup(opts) + .then(build => { + console.log(TAG, chalk.yellow('"preload/index.js" built.')) + build.write(opts.output as OutputOptions) + }) + .catch(error => { + console.log(`\n${TAG} ${chalk.red('构建报错')}\n`, error, '\n') + }) +} + +function configFactory(env = 'production') { + const options: RollupOptions = { + input: join(__dirname, '../src/preload/index.js'), + output: { + file: join(__dirname, '../dist/preload/index.js'), + format: 'iife', + }, + plugins: [], + external: [ + ...builtins(), + 'electron', + ], + } + + return options +} diff --git a/script/rollup.config.ts b/script/rollup.config.ts deleted file mode 100644 index 293b141..0000000 --- a/script/rollup.config.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { join } from 'path' -import { RollupOptions } 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' - -export default (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 -}