chore: script 部分改成 ts

This commit is contained in:
草鞋没号 2020-12-24 10:17:27 +08:00
parent 2094e722af
commit 3689e06d32
6 changed files with 131 additions and 86 deletions

View File

@ -8,9 +8,9 @@
"dev": "npm run dev:all", "dev": "npm run dev:all",
"dev:all": "concurrently -n=vue,ele -c=green,blue \"npm run dev:vue\" \"npm run dev:ele\"", "dev:all": "concurrently -n=vue,ele -c=green,blue \"npm run dev:vue\" \"npm run dev:ele\"",
"dev:vue": "vite", "dev:vue": "vite",
"dev:ele": "node script/build --env=development --watch", "dev:ele": "node -r ts-node/register script/build --env=development --watch",
"build:vue": "vite build", "build:vue": "vite build",
"build:ele": "node script/build --env=production", "build:ele": "node -r ts-node/register script/build --env=production",
"build": "rm -rf dist && npm run build:vue && npm run build:ele && electron-builder" "build": "rm -rf dist && npm run build:vue && npm run build:ele && electron-builder"
}, },
"build": { "build": {
@ -51,6 +51,8 @@
"@rollup/plugin-commonjs": "^15.0.0", "@rollup/plugin-commonjs": "^15.0.0",
"@rollup/plugin-json": "^4.1.0", "@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^9.0.0", "@rollup/plugin-node-resolve": "^9.0.0",
"@types/minimist": "^1.2.1",
"@types/wait-on": "^5.2.0",
"@vue/compiler-sfc": "^3.0.0", "@vue/compiler-sfc": "^3.0.0",
"chalk": "^4.1.0", "chalk": "^4.1.0",
"concurrently": "^5.3.0", "concurrently": "^5.3.0",
@ -61,6 +63,7 @@
"minimist": "^1.2.5", "minimist": "^1.2.5",
"ora": "^5.0.0", "ora": "^5.0.0",
"rollup-plugin-esbuild": "^2.4.2", "rollup-plugin-esbuild": "^2.4.2",
"ts-node": "^9.1.1",
"typescript": "^4.0.3", "typescript": "^4.0.3",
"vite": "^1.0.0-rc.4", "vite": "^1.0.0-rc.4",
"wait-on": "^5.2.0" "wait-on": "^5.2.0"

View File

@ -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
View 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')
})
}

View File

@ -1,21 +1,22 @@
const path = require('path') import { join } from 'path'
const { nodeResolve } = require('@rollup/plugin-node-resolve') import { RollupOptions } from 'rollup'
const commonjs = require('@rollup/plugin-commonjs') import { nodeResolve } from '@rollup/plugin-node-resolve'
const esbuild = require('rollup-plugin-esbuild') import commonjs from '@rollup/plugin-commonjs'
const alias = require('@rollup/plugin-alias') import esbuild from 'rollup-plugin-esbuild'
const json = require('@rollup/plugin-json') import alias from '@rollup/plugin-alias'
import json from '@rollup/plugin-json'
module.exports = (env = 'production') => { export default (env = 'production') => {
return { const options: RollupOptions = {
input: path.join(__dirname, '../src/main/index.ts'), input: join(__dirname, '../src/main/index.ts'),
output: { output: {
file: path.join(__dirname, '../dist/main/_.js'), file: join(__dirname, '../dist/main/_.js'),
format: 'cjs', format: 'cjs',
name: 'ElectronMainBundle', name: 'ElectronMainBundle',
sourcemap: true, sourcemap: true,
}, },
plugins: [ plugins: [
nodeResolve({ jsnext: true, preferBuiltins: true, browser: true }), // 消除碰到 node.js 模块时⚠警告 nodeResolve({ preferBuiltins: true, browser: true }), // 消除碰到 node.js 模块时⚠警告
commonjs(), commonjs(),
json(), json(),
esbuild({ esbuild({
@ -43,7 +44,7 @@ module.exports = (env = 'production') => {
}), }),
alias({ alias({
entries: [ 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', 'electron',
], ],
} }
return options
} }

View File

@ -1,11 +1,12 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "esnext", "target": "esnext",
"module": "esnext", "module": "UMD",
"moduleResolution": "node", "moduleResolution": "node",
"importHelpers": true, "importHelpers": true,
"jsx": "react", "jsx": "react",
"esModuleInterop": true, "esModuleInterop": true,
"resolveJsonModule": true,
"sourceMap": true, "sourceMap": true,
"baseUrl": "./", "baseUrl": "./",
"strict": true, "strict": true,

View File

@ -339,6 +339,11 @@
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.3.tgz#c893b73721db73699943bfc3653b1deb7faa4a3a" resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.3.tgz#c893b73721db73699943bfc3653b1deb7faa4a3a"
integrity sha512-Jus9s4CDbqwocc5pOAnh8ShfrnMcPHuJYzVcSUU7lrh8Ni5HuIqX3oilL86p3dlTrk0LzHRCgA/GQ7uNCw6l2Q== integrity sha512-Jus9s4CDbqwocc5pOAnh8ShfrnMcPHuJYzVcSUU7lrh8Ni5HuIqX3oilL86p3dlTrk0LzHRCgA/GQ7uNCw6l2Q==
"@types/minimist@^1.2.1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.1.tgz#283f669ff76d7b8260df8ab7a4262cc83d988256"
integrity sha512-fZQQafSREFyuZcdWFAExYjBiCL7AUCdgsk80iO0q4yihYYdcIiH28CcuPTGFgLOCC8RlW49GSQxdHwZP+I7CNg==
"@types/node@*": "@types/node@*":
version "14.0.27" version "14.0.27"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.27.tgz#a151873af5a5e851b51b3b065c9e63390a9e0eb1" resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.27.tgz#a151873af5a5e851b51b3b065c9e63390a9e0eb1"
@ -374,6 +379,11 @@
"@types/express-serve-static-core" "*" "@types/express-serve-static-core" "*"
"@types/mime" "*" "@types/mime" "*"
"@types/wait-on@^5.2.0":
version "5.2.0"
resolved "https://registry.yarnpkg.com/@types/wait-on/-/wait-on-5.2.0.tgz#f9096b7bd0c9c03052d6d402ae5cd51714480b2d"
integrity sha512-3+jsMyPm8aot1mqDUDLOl+dejPvpysUUoUXD6CCRY20MNNhcjEfvdcBnGdnk7DEYs9Hr16ubGJA/9/QW0Df/9g==
"@types/yargs-parser@*": "@types/yargs-parser@*":
version "15.0.0" version "15.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz#cb3f9f741869e20cce330ffbeb9271590483882d" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz#cb3f9f741869e20cce330ffbeb9271590483882d"
@ -643,6 +653,11 @@ app-builder-lib@22.8.0:
semver "^7.3.2" semver "^7.3.2"
temp-file "^3.3.7" temp-file "^3.3.7"
arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
argparse@^1.0.7: argparse@^1.0.7:
version "1.0.10" version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911" resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
@ -1106,6 +1121,11 @@ cosmiconfig@^5.0.0:
js-yaml "^3.13.1" js-yaml "^3.13.1"
parse-json "^4.0.0" parse-json "^4.0.0"
create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
cross-spawn@^7.0.0: cross-spawn@^7.0.0:
version "7.0.3" version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
@ -1246,6 +1266,11 @@ detect-node@^2.0.4:
resolved "https://registry.yarnpkg.com/detect-node/-/detect-node-2.0.4.tgz#014ee8f8f669c5c58023da64b8179c083a28c46c" resolved "https://registry.yarnpkg.com/detect-node/-/detect-node-2.0.4.tgz#014ee8f8f669c5c58023da64b8179c083a28c46c"
integrity sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw== integrity sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==
diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
dir-glob@^3.0.1: dir-glob@^3.0.1:
version "3.0.1" version "3.0.1"
resolved "https://registry.yarnpkg.com/dir-glob/-/dir-glob-3.0.1.tgz#56dbf73d992a4a93ba1584f4534063fd2e41717f" resolved "https://registry.yarnpkg.com/dir-glob/-/dir-glob-3.0.1.tgz#56dbf73d992a4a93ba1584f4534063fd2e41717f"
@ -2355,6 +2380,11 @@ make-dir@^3.0.0, make-dir@^3.1.0:
dependencies: dependencies:
semver "^6.0.0" semver "^6.0.0"
make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
matcher@^3.0.0: matcher@^3.0.0:
version "3.0.0" version "3.0.0"
resolved "https://registry.yarnpkg.com/matcher/-/matcher-3.0.0.tgz#bd9060f4c5b70aa8041ccc6f80368760994f30ca" resolved "https://registry.yarnpkg.com/matcher/-/matcher-3.0.0.tgz#bd9060f4c5b70aa8041ccc6f80368760994f30ca"
@ -3184,7 +3214,7 @@ slash@^3.0.0:
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634" resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q== integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==
source-map-support@^0.5.19, source-map-support@~0.5.12: source-map-support@^0.5.17, source-map-support@^0.5.19, source-map-support@~0.5.12:
version "0.5.19" version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw== integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
@ -3410,6 +3440,18 @@ truncate-utf8-bytes@^1.0.0:
dependencies: dependencies:
utf8-byte-length "^1.0.1" utf8-byte-length "^1.0.1"
ts-node@^9.1.1:
version "9.1.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.1.1.tgz#51a9a450a3e959401bda5f004a72d54b936d376d"
integrity sha512-hPlt7ZACERQGf03M253ytLY3dHbGNGrAq9qIHWUY9XHYl1z7wYngSr3OQ5xmui8o2AaxsONxIzjafLUiWBo1Fg==
dependencies:
arg "^4.1.0"
create-require "^1.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"
tslib@^1.9.0: tslib@^1.9.0:
version "1.13.0" version "1.13.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.13.0.tgz#c881e13cc7015894ed914862d276436fa9a47043" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.13.0.tgz#c881e13cc7015894ed914862d276436fa9a47043"
@ -3773,3 +3815,8 @@ ylru@^1.2.0:
version "1.2.1" version "1.2.1"
resolved "https://registry.yarnpkg.com/ylru/-/ylru-1.2.1.tgz#f576b63341547989c1de7ba288760923b27fe84f" resolved "https://registry.yarnpkg.com/ylru/-/ylru-1.2.1.tgz#f576b63341547989c1de7ba288760923b27fe84f"
integrity sha512-faQrqNMzcPCHGVC2aaOINk13K+aaBDUPjGWl0teOXywElLjyVAB6Oe2jj62jHYtwsU49jXhScYbvPENK+6zAvQ== integrity sha512-faQrqNMzcPCHGVC2aaOINk13K+aaBDUPjGWl0teOXywElLjyVAB6Oe2jj62jHYtwsU49jXhScYbvPENK+6zAvQ==
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==