mirror of
https://github.com/electron-vite/electron-vite-vue
synced 2025-08-27 19:34:13 +08:00
chore: script 部分改成 ts
This commit is contained in:
@@ -1,70 +0,0 @@
|
||||
/**
|
||||
* electron 打包
|
||||
*/
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const electron = require('electron');
|
||||
const cp = require('child_process');
|
||||
const rollup = require('rollup');
|
||||
const argv = require('minimist')(process.argv.slice(2));
|
||||
const chalk = require('chalk');
|
||||
const ora = require('ora');
|
||||
const waitOn = require('wait-on');
|
||||
const options = require('./rollup.config');
|
||||
const pkg = require('../package.json');
|
||||
|
||||
const opt = options(argv.env);
|
||||
const TAG = '[script/build.js]';
|
||||
const spinner = ora(`${TAG} Electron build...`);
|
||||
const env = {};
|
||||
|
||||
try {
|
||||
// 解析 .env
|
||||
let env_str = fs.readFileSync(path.join(__dirname, '../.env'), 'utf-8');
|
||||
env_str = env_str.replace(/#.*\n/g, '') // 去掉注释
|
||||
for (const line of env_str.split('\n')) {
|
||||
const tmp = line.split('=');
|
||||
env[tmp[0]] = tmp[1];
|
||||
}
|
||||
} catch (error) { }
|
||||
|
||||
if (argv.watch) {
|
||||
let child = null;
|
||||
|
||||
waitOn({
|
||||
resources: [`http://localhost:${env.PORT}`],
|
||||
log: false,
|
||||
}, err => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// once here, all resources are available
|
||||
const watcher = rollup.watch(opt);
|
||||
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();
|
||||
child = cp.spawn(electron, [path.join(__dirname, '..', pkg.main)], { stdio: 'inherit', });
|
||||
} else if (ev.code === 'ERROR') {
|
||||
console.log(ev.error)
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
spinner.start();
|
||||
rollup.rollup(opt)
|
||||
.then(build => {
|
||||
spinner.stop();
|
||||
console.log(TAG, chalk.green('Electron build successed.'));
|
||||
build.write(opt.output);
|
||||
})
|
||||
.catch(error => {
|
||||
spinner.stop();
|
||||
console.log(`\n${TAG} ${chalk.red('构建报错')}\n`, error, '\n');
|
||||
});
|
||||
}
|
61
script/build.ts
Normal file
61
script/build.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* electron 打包
|
||||
*/
|
||||
import { join } from 'path'
|
||||
import { spawn, ChildProcess } from 'child_process'
|
||||
import { watch, rollup, OutputOptions } from 'rollup'
|
||||
import minimist from 'minimist'
|
||||
import chalk from 'chalk'
|
||||
import ora from 'ora'
|
||||
import waitOn from 'wait-on'
|
||||
import electron from 'electron'
|
||||
import dotenv from 'dotenv'
|
||||
import options from './rollup.config'
|
||||
import { main } from '../package.json'
|
||||
|
||||
dotenv.config({ path: join(__dirname, '../.env') })
|
||||
|
||||
const argv = minimist(process.argv.slice(2))
|
||||
const opt = options(argv.env)
|
||||
const TAG = '[script/build.ts]'
|
||||
const spinner = ora(`${TAG} Electron build...`)
|
||||
|
||||
if (argv.watch) {
|
||||
waitOn({
|
||||
resources: [`http://localhost:${process.env.PORT}`],
|
||||
log: false,
|
||||
}, err => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
// once here, all resources are available
|
||||
const watcher = watch(opt)
|
||||
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()
|
||||
child = spawn(electron as any, [join(__dirname, `../${main}`)], { stdio: 'inherit' })
|
||||
} else if (ev.code === 'ERROR') {
|
||||
console.log(ev.error)
|
||||
}
|
||||
})
|
||||
})
|
||||
} else {
|
||||
spinner.start()
|
||||
rollup(opt)
|
||||
.then(build => {
|
||||
spinner.stop()
|
||||
console.log(TAG, chalk.green('Electron build successed.'))
|
||||
build.write(opt.output as OutputOptions)
|
||||
})
|
||||
.catch(error => {
|
||||
spinner.stop()
|
||||
console.log(`\n${TAG} ${chalk.red('构建报错')}\n`, error, '\n')
|
||||
})
|
||||
}
|
@@ -1,21 +1,22 @@
|
||||
const path = require('path')
|
||||
const { nodeResolve } = require('@rollup/plugin-node-resolve')
|
||||
const commonjs = require('@rollup/plugin-commonjs')
|
||||
const esbuild = require('rollup-plugin-esbuild')
|
||||
const alias = require('@rollup/plugin-alias')
|
||||
const json = require('@rollup/plugin-json')
|
||||
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'
|
||||
|
||||
module.exports = (env = 'production') => {
|
||||
return {
|
||||
input: path.join(__dirname, '../src/main/index.ts'),
|
||||
export default (env = 'production') => {
|
||||
const options: RollupOptions = {
|
||||
input: join(__dirname, '../src/main/index.ts'),
|
||||
output: {
|
||||
file: path.join(__dirname, '../dist/main/_.js'),
|
||||
file: join(__dirname, '../dist/main/_.js'),
|
||||
format: 'cjs',
|
||||
name: 'ElectronMainBundle',
|
||||
sourcemap: true,
|
||||
},
|
||||
plugins: [
|
||||
nodeResolve({ jsnext: true, preferBuiltins: true, browser: true }), // 消除碰到 node.js 模块时⚠警告
|
||||
nodeResolve({ preferBuiltins: true, browser: true }), // 消除碰到 node.js 模块时⚠警告
|
||||
commonjs(),
|
||||
json(),
|
||||
esbuild({
|
||||
@@ -43,7 +44,7 @@ module.exports = (env = 'production') => {
|
||||
}),
|
||||
alias({
|
||||
entries: [
|
||||
{ find: '@main', replacement: path.join(__dirname, '../src/main'), },
|
||||
{ find: '@main', replacement: join(__dirname, '../src/main'), },
|
||||
]
|
||||
})
|
||||
],
|
||||
@@ -61,4 +62,6 @@ module.exports = (env = 'production') => {
|
||||
'electron',
|
||||
],
|
||||
}
|
||||
|
||||
return options
|
||||
}
|
Reference in New Issue
Block a user