feat: supported preload rollup package

This commit is contained in:
草鞋没号 2021-08-05 09:25:34 +08:00
parent 0d281de679
commit 5e0bb24fb8
3 changed files with 88 additions and 44 deletions

View File

@ -1,22 +1,28 @@
/** /**
* electron * Electron main process package script
*/ */
import { join } from 'path' import { join } from 'path'
import { spawn, ChildProcess } from 'child_process' 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 minimist from 'minimist'
import chalk from 'chalk' import chalk from 'chalk'
import ora from 'ora' import ora from 'ora'
import electron from 'electron' import electron from 'electron'
import dotenv from 'dotenv' import dotenv from 'dotenv'
import { waitOn } from './utils' import { waitOn } from './utils'
import options from './rollup.config'
import { main } from '../package.json' import { main } from '../package.json'
// Inject some environment variables from ".env"
dotenv.config({ path: join(__dirname, '../.env') }) dotenv.config({ path: join(__dirname, '../.env') })
const argv = minimist(process.argv.slice(2)) const argv = minimist(process.argv.slice(2))
const opts = options(argv.env) const opts = configFactory(argv.env)
const TAG = '[build-main.ts]' const TAG = '[build-main.ts]'
const spinner = ora(`${TAG} Electron build...`) const spinner = ora(`${TAG} Electron build...`)
@ -56,3 +62,35 @@ if (argv.watch) {
console.log(`\n${TAG} ${chalk.red('构建报错')}\n`, error, '\n') 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
}

46
script/build-preload.ts Normal file
View File

@ -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
}

View File

@ -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
}