npm run dev:all :)

This commit is contained in:
草鞋没号
2020-08-16 20:42:52 +08:00
parent 73071609b6
commit 8ceefba9a1
23 changed files with 1843 additions and 45 deletions

53
script/build.js Normal file
View File

@@ -0,0 +1,53 @@
/**
* electron 打包
*/
const path = require('path');
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 electron = require('electron-connect').server.create({ stopOnClose: true });
require('dotenv').config({ path: path.join(__dirname, '../.env') })
const options = require('./rollup.config');
const opt = options(argv.env);
const TAG = '[script/build.js]';
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 = 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') {
// init-未启动、started-第一次启动、restarted-重新启动
electron.electronState === 'init' ? electron.start() : electron.restart();
}
});
});
} 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');
});
}

27
script/rollup.config.js Normal file
View File

@@ -0,0 +1,27 @@
const path = require('path');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const typescript = require('@rollup/plugin-typescript');
module.exports = (env = 'production') => {
return {
input: path.join(__dirname, '../src/main/index.ts'),
output: {
file: path.join(__dirname, '../src/main/_.js'),
format: 'cjs',
name: 'ElectronMainBundle',
sourcemap: true,
},
plugins: [
nodeResolve({ jsnext: true, preferBuiltins: true, browser: true }), // 消除碰到 node.js 模块时⚠警告
commonjs(),
typescript(),
],
external: [
'fs',
'path',
'electron',
'electron-is-dev',
],
}
};