From f4a3ff6355954744a68c51e314412e5aa42622f7 Mon Sep 17 00:00:00 2001 From: yutent Date: Tue, 20 Jun 2023 09:53:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=BA=BF=E7=A8=8B=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E8=87=AA=E9=80=82=E5=BA=94(4=E6=A0=B8=E5=8F=8A=E4=BB=A5?= =?UTF-8?q?=E4=B8=8A=E5=BC=80=E5=90=AF)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/prod.js | 85 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 25 deletions(-) diff --git a/lib/prod.js b/lib/prod.js index 1c1151b..6991cdc 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -3,10 +3,14 @@ import { Worker, parentPort } from 'node:worker_threads' import os from 'node:os' import fs from 'iofs' +import { compileFiles } from './compile.js' +import { defaultCustomElement } from './utils.js' + const IS_WIN = process.platform === 'win32' const PREFIX = IS_WIN ? 'pages\\' : 'pages/' -// 线程太多, 效率反而不高 -const THREADS_NUM = os.cpus().length > 4 ? 4 : os.cpus().length - 1 +// 4核(或4线程)以上的CPU, 才开启多线程编译。且线程开销太高, 开太多线程效率反而不高。 +const CPU_CORES = os.cpus().length > 5 ? 6 : os.cpus().length +const THREADS_NUM = CPU_CORES > 3 ? CPU_CORES - 1 : 0 const __filename = normalize(import.meta.url.slice(IS_WIN ? 8 : 7)) const __dirname = dirname(__filename) const WORKER_POOL = new Set() // 线程池 @@ -62,21 +66,28 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { INJECT_SCSS, LEGACY_MODE, // 线程通讯无法传递函数类型, 需要转为字符串, 之后再转回来 - isCustomElement: isCustomElement ? isCustomElement.toString() : null + isCustomElement: + THREADS_NUM > 0 + ? isCustomElement + ? isCustomElement.toString() + : null + : isCustomElement || defaultCustomElement } // 创建线程池 - for (let i = 0; i < THREADS_NUM; i++) { - WORKER_POOL.add( - new Worker(join(__dirname, './thread.js'), { - workerData: { - options, - verbose, - dist, - imports: conf.imports - } - }) - ) + if (THREADS_NUM > 0) { + for (let i = 0; i < THREADS_NUM; i++) { + WORKER_POOL.add( + new Worker(join(__dirname, './thread.js'), { + workerData: { + options, + verbose, + dist, + imports: conf.imports + } + }) + ) + } } fs.ls(SOURCE_DIR, true).forEach(path => { @@ -141,18 +152,34 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { list.delete(path) files.set(path, { name, ext }) }) - chunk.set(currentPage, { page, files }) - JOBS_QUEUE.push(chunk) - doJob() + if (THREADS_NUM > 0) { + chunk.set(currentPage, { page, files }) + JOBS_QUEUE.push(chunk) + doJob() + } else { + console.log(`正在生成 ${currentPage}.html ...`) + compileFiles(currentPage, page, files, options, { + verbose, + dist, + imports: conf.imports + }) + } } // 公共依赖 - { + if (THREADS_NUM > 0) { let chunk = new Map() chunk.set('', { page: null, files: list }) JOBS_QUEUE.push(chunk) doJob() + } else { + console.log('\n正在解析公共依赖 ...') + compileFiles('', null, list, options, { + verbose, + dist, + imports: conf.imports + }) } } else { // 每个线程处理的文件数 @@ -164,15 +191,23 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { console.log(`正在生成 ${currentPage}.html ...`) - for (let i = 0; i < THREADS_NUM; i++) { - let start = i * chunkSize - let end = start + chunkSize - let chunk = new Map() + if (THREADS_NUM > 0) { + for (let i = 0; i < THREADS_NUM; i++) { + let start = i * chunkSize + let end = start + chunkSize + let chunk = new Map() - chunk.set(currentPage, { page, files: list.slice(start, end) }) + chunk.set(currentPage, { page, files: list.slice(start, end) }) - JOBS_QUEUE.push(chunk) - doJob() + JOBS_QUEUE.push(chunk) + doJob() + } + } else { + compileFiles(currentPage, page, list, options, { + verbose, + dist, + imports: conf.imports + }) } } -- 2.40.1