From 3689e06d3288688216513547c81eacf5f6aa7db5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Thu, 24 Dec 2020 10:17:27 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20script=20=E9=83=A8=E5=88=86=E6=94=B9?= =?UTF-8?q?=E6=88=90=20ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 7 +- script/build.js | 70 ------------------- script/build.ts | 61 ++++++++++++++++ script/{rollup.config.js => rollup.config.ts} | 27 +++---- tsconfig.json | 3 +- yarn.lock | 49 ++++++++++++- 6 files changed, 131 insertions(+), 86 deletions(-) delete mode 100644 script/build.js create mode 100644 script/build.ts rename script/{rollup.config.js => rollup.config.ts} (64%) diff --git a/package.json b/package.json index b47b33d..6bb7b29 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,9 @@ "dev": "npm run dev:all", "dev:all": "concurrently -n=vue,ele -c=green,blue \"npm run dev:vue\" \"npm run dev:ele\"", "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: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": { @@ -51,6 +51,8 @@ "@rollup/plugin-commonjs": "^15.0.0", "@rollup/plugin-json": "^4.1.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", "chalk": "^4.1.0", "concurrently": "^5.3.0", @@ -61,6 +63,7 @@ "minimist": "^1.2.5", "ora": "^5.0.0", "rollup-plugin-esbuild": "^2.4.2", + "ts-node": "^9.1.1", "typescript": "^4.0.3", "vite": "^1.0.0-rc.4", "wait-on": "^5.2.0" diff --git a/script/build.js b/script/build.js deleted file mode 100644 index d46c8be..0000000 --- a/script/build.js +++ /dev/null @@ -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'); - }); -} diff --git a/script/build.ts b/script/build.ts new file mode 100644 index 0000000..c785a31 --- /dev/null +++ b/script/build.ts @@ -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') + }) +} diff --git a/script/rollup.config.js b/script/rollup.config.ts similarity index 64% rename from script/rollup.config.js rename to script/rollup.config.ts index 76c3c4a..ca63c0c 100644 --- a/script/rollup.config.js +++ b/script/rollup.config.ts @@ -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 } diff --git a/tsconfig.json b/tsconfig.json index b7d1edb..e61f5b3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,12 @@ { "compilerOptions": { "target": "esnext", - "module": "esnext", + "module": "UMD", "moduleResolution": "node", "importHelpers": true, "jsx": "react", "esModuleInterop": true, + "resolveJsonModule": true, "sourceMap": true, "baseUrl": "./", "strict": true, diff --git a/yarn.lock b/yarn.lock index 9ea078e..6173f75 100644 --- a/yarn.lock +++ b/yarn.lock @@ -339,6 +339,11 @@ resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.3.tgz#c893b73721db73699943bfc3653b1deb7faa4a3a" 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@*": version "14.0.27" resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.27.tgz#a151873af5a5e851b51b3b065c9e63390a9e0eb1" @@ -374,6 +379,11 @@ "@types/express-serve-static-core" "*" "@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@*": version "15.0.0" 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" 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: version "1.0.10" 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" 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: version "7.0.3" 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" 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: version "3.0.1" 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: 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: version "3.0.0" 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" 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" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61" integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw== @@ -3410,6 +3440,18 @@ truncate-utf8-bytes@^1.0.0: dependencies: 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: version "1.13.0" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.13.0.tgz#c881e13cc7015894ed914862d276436fa9a47043" @@ -3773,3 +3815,8 @@ ylru@^1.2.0: version "1.2.1" resolved "https://registry.yarnpkg.com/ylru/-/ylru-1.2.1.tgz#f576b63341547989c1de7ba288760923b27fe84f" 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==