refactor: build with console.log electron

This commit is contained in:
草鞋没号
2021-05-15 14:53:43 +08:00
parent f19f1d1d4b
commit 030b7a5f3f
8 changed files with 112 additions and 48 deletions

View File

@@ -0,0 +1,60 @@
import { join } from 'path'
import { RollupOptions } from 'rollup'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import esbuild from 'rollup-plugin-esbuild'
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/_.js'),
format: 'cjs',
name: 'ElectronMainBundle',
sourcemap: true,
},
plugins: [
nodeResolve({ preferBuiltins: true, browser: true }), // 消除碰到 node.js 模块时⚠警告
commonjs(),
json(),
esbuild({
// All options are optional
include: /\.[jt]sx?$/, // default, inferred from `loaders` option
exclude: /node_modules/, // default
// watch: process.argv.includes('--watch'), // rollup 中有配置
sourceMap: false, // default
minify: env === 'production',
target: 'es2017', // default, or 'es20XX', 'esnext'
jsxFactory: 'React.createElement',
jsxFragment: 'React.Fragment',
// Like @rollup/plugin-replace
define: {
__VERSION__: '"x.y.z"'
},
tsconfig: 'tsconfig.json', // default
// Add extra loaders
loaders: {
// Add .json files support
// require @rollup/plugin-commonjs
'.json': 'json',
// Enable JSX in .js files too
'.js': 'jsx'
},
}),
alias({
entries: [
{ find: '@main', replacement: join(__dirname, '../src/main'), },
]
}),
],
external: [
...builtins(),
'electron',
],
}
return options
}

View File

@@ -16,13 +16,13 @@ import { main } from '../package.json'
dotenv.config({ path: join(__dirname, '../.env') })
const argv = minimist(process.argv.slice(2))
const opt = options(argv.env)
const opts = options(argv.env)
const TAG = '[build-main.ts]'
const spinner = ora(`${TAG} Electron build...`)
if (argv.watch) {
waitOn({ port: process.env.PORT as string }).then(msg => {
const watcher = watch(opt)
const watcher = watch(opts)
let child: ChildProcess
watcher.on('change', filename => {
const log = chalk.green(`change -- ${filename}`)
@@ -39,11 +39,11 @@ if (argv.watch) {
})
} else {
spinner.start()
rollup(opt)
rollup(opts)
.then(build => {
spinner.stop()
console.log(TAG, chalk.green('Electron build successed.'))
build.write(opt.output as OutputOptions)
build.write(opts.output as OutputOptions)
})
.catch(error => {
spinner.stop()

View File

@@ -1,8 +1,8 @@
import { join } from 'path'
import { RollupOptions } from 'rollup'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import nodeResolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import esbuild from 'rollup-plugin-esbuild'
import typescript from '@rollup/plugin-typescript'
import alias from '@rollup/plugin-alias'
import json from '@rollup/plugin-json'
import { builtins } from './utils'
@@ -11,42 +11,22 @@ export default (env = 'production') => {
const options: RollupOptions = {
input: join(__dirname, '../src/main/index.ts'),
output: {
file: join(__dirname, '../dist/main/_.js'),
file: join(__dirname, '../dist/main/index.js'),
format: 'cjs',
name: 'ElectronMainBundle',
sourcemap: true,
},
plugins: [
nodeResolve({ preferBuiltins: true, browser: true }), // 消除碰到 node.js 模块时⚠警告
nodeResolve(),
commonjs(),
json(),
esbuild({
// All options are optional
include: /\.[jt]sx?$/, // default, inferred from `loaders` option
exclude: /node_modules/, // default
// watch: process.argv.includes('--watch'), // rollup 中有配置
sourceMap: false, // default
minify: env === 'production',
target: 'es2017', // default, or 'es20XX', 'esnext'
jsxFactory: 'React.createElement',
jsxFragment: 'React.Fragment',
// Like @rollup/plugin-replace
define: {
__VERSION__: '"x.y.z"'
},
tsconfig: 'tsconfig.json', // default
// Add extra loaders
loaders: {
// Add .json files support
// require @rollup/plugin-commonjs
'.json': 'json',
// Enable JSX in .js files too
'.js': 'jsx'
},
typescript({
module: 'ESNext',
}),
alias({
entries: [
{ find: '@main', replacement: join(__dirname, '../src/main'), },
{ find: '@root', replacement: join(__dirname, '..'), },
]
}),
],