diff --git a/scripts/build.js b/scripts/build.js index 7d52f8c7b519f895f5958b488ee210229c27d0a7..641ee3658211e9852e792a3c77bb2cdcd12a4633 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -2,16 +2,18 @@ const fs = require('fs-extra') const path = require('path') const chalk = require('chalk') const execa = require('execa') +const { spawn } = require('child_process') const { extract } = require('./apiExtractor') -const { targets: allTargets, fuzzyMatchTarget } = require('./utils') +const { targets: allTargets, fuzzyMatchTarget, priority } = require('./utils') const args = require('minimist')(process.argv.slice(2)) const targets = args._ // const formats = args.formats || args.f const devOnly = args.devOnly || args.d const isRelease = args.release +const multiProcess = args.m // const buildTypes = args.t || args.types || isRelease const buildAllMatching = args.all || args.a @@ -25,14 +27,68 @@ async function run() { } } +function buildWithChildProcess(target) { + const args = [__filename, target] + devOnly && args.push('-d') + isRelease && args.push('--release') + const child = spawn('node', args, { + cwd: process.cwd(), + stdio: 'inherit', + }) + return new Promise((resolve, reject) => { + child.on('exit', () => { + const exitCode = child.exitCode + if (exitCode === 0) { + resolve() + } else { + reject(new Error(`build ${target} failed with error code ${exitCode}`)) + } + }) + }) +} + +function getTargetGroup(targets) { + const group = {} + for (let i = 0; i < targets.length; i++) { + const target = targets[i] + const targetPriority = priority[target] + if (!group[targetPriority]) { + group[targetPriority] = [target] + } else { + group[targetPriority].push(target) + } + } + return group +} + async function buildAll(targets) { - for (const target of targets) { - console.log(`\n${chalk.blueBright(chalk.bold(target))}:`) - await build(target) + if (!multiProcess) { + for (const target of targets) { + await build(target) + } + return + } + if (targets.length === 1) { + // child process or single target + await build(targets[0]) + return + } + console.warn('compiling with multi process') + const group = getTargetGroup(targets) + const keys = Object.keys(group).sort((a, b) => { + return b - a + }) + for (let i = 0; i < keys.length; i++) { + const key = keys[i] + const groupTargets = group[key] + await Promise.all( + groupTargets.map((target) => buildWithChildProcess(target)) + ) } } async function build(target) { + console.log(`\n${chalk.blueBright(chalk.bold(target))}:`) const pkgDir = path.resolve(`packages/${target}`) const pkg = require(`${pkgDir}/package.json`) diff --git a/scripts/utils.js b/scripts/utils.js index f7409bb6bc4db38481216b70f1e32dd05762658a..65f76252d7e73d5bc64e244e91a08b7aca5af7a1 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -7,28 +7,29 @@ const priority = { 'uni-app': 90, 'uni-stat': 90, 'uni-i18n': 90, - 'uni-mp-vue': 80, - 'uni-mp-alipay': 70, - 'uni-mp-baidu': 70, - 'uni-mp-kuaishou': 70, - 'uni-mp-qq': 70, - 'uni-mp-toutiao': 70, + 'uni-mp-vue': 90, + 'uni-cli-shared': 80, + 'uni-h5': 75, + 'uni-mp-vite': 75, 'uni-mp-weixin': 70, - 'uni-quickapp-webview': 70, - 'uni-cli-shared': 60, - 'uni-cli-nvue': 55, - 'uni-h5': 50, - 'uni-h5-vite': 40, - 'uni-app-vue': 35, - 'uni-app-plus': 30, - 'uni-app-vite': 30, - 'uni-mp-vite': 30, - 'uni-mp-compiler': 30, - 'vite-plugin-uni': 20, - 'uni-cloud': 10, - 'uni-automator': 10, - 'size-check': 1, + 'uni-cli-nvue': 70, + 'uni-mp-compiler': 70, + 'uni-mp-alipay': 60, + 'uni-mp-baidu': 60, + 'uni-mp-kuaishou': 60, + 'uni-mp-qq': 60, + 'uni-mp-toutiao': 60, + 'uni-quickapp-webview': 60, + 'uni-h5-vite': 70, + 'uni-app-vue': 70, + 'uni-app-plus': 70, + 'uni-app-vite': 70, + 'vite-plugin-uni': 70, + 'uni-cloud': 70, + 'uni-automator': 70, + 'size-check': 60, } +exports.priority = priority const targets = (exports.targets = fs.readdirSync('packages').filter((f) => { if (!fs.statSync(`packages/${f}`).isDirectory()) {