From ff5a225cc1113d0c020ac6e1abee084786f8b7ef Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 15 Jun 2023 16:54:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9A=82=E5=AD=986.15?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compile.js | 3 --- lib/prod.js | 50 ++++++++++++++++++++++++++++++++------------------ lib/thread.js | 14 ++++---------- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/lib/compile.js b/lib/compile.js index 6f45b21..cc9dffd 100644 --- a/lib/compile.js +++ b/lib/compile.js @@ -20,10 +20,7 @@ export function compileFiles( ) { options.currentPage = currentPage - console.log('正在生成 %s ...', `${currentPage}.html`) - for (let [k, it] of files) { - // let it = files[k] // 入口文件, 特殊处理 if (page && it.path === page.entry) { let entry = fs.cat(page.entry).toString() diff --git a/lib/prod.js b/lib/prod.js index fc5cfc2..637280d 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -2,15 +2,16 @@ import { join, dirname, parse, normalize } from 'node:path' import { Worker } from 'node:worker_threads' import os from 'node:os' import fs from 'iofs' -// import Es from 'esbuild' -// import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js' + import { isCustomElement } from './utils.js' -import { compileFiles } from './compile.js' const IS_WIN = process.platform === 'win32' const PREFIX = IS_WIN ? 'pages\\' : 'pages/' -const THREADS_NUM = os.cpus().length -const __dirname = normalize(dirname(import.meta.url.slice(IS_WIN ? 8 : 7))) +// 线程太多, 效率反而不高 +const THREADS_NUM = os.cpus().length > 4 ? 4 : os.cpus().length - 1 +// const THREADS_NUM = os.cpus().length +const __filename = normalize(import.meta.url.slice(IS_WIN ? 8 : 7)) +const __dirname = dirname(__filename) function readFile(file) { return (file && fs.cat(file)?.toString()) || '' @@ -59,10 +60,9 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { if (it.ext !== '') { if (IS_MPA && it.name.startsWith(PREFIX)) { if (PAGES_PREFIX.some(it => it.startsWith(it.name))) { - return list.set(path, it) - } else { - return + list.set(path, it) } + return } if (it.path === conf.inject.scss) { @@ -90,7 +90,6 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { } }) } - console.log('') if (IS_MPA) { // 电脑线程数比页面数量还多时, 取小 @@ -126,6 +125,7 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { chunk.set(currentPage, { page, files }) } + new Worker(join(__dirname, './thread.js'), { workerData: { options, @@ -133,15 +133,12 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { chunk, verbose, dist, - imports: conf.imports + imports: conf.imports, + timeStart: Date.now(), + title: '正在解析currentPage' } } }) - // compileFiles(currentPage, page, chunk, options, { - // verbose, - // dist, - // imports: conf.imports - // }) } } else { // 每个线程处理的文件数 @@ -154,14 +151,14 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { for (let i = 0; i < THREADS_NUM; i++) { let start = i * chunkSize let end = start + chunkSize - let chunk = [list.slice(start, end)] + let chunk = new Map() + + chunk.set(currentPage, { page, files: list.slice(start, end) }) new Worker(join(__dirname, './thread.js'), { workerData: { options, data: { - currentPage, - page, chunk, verbose, dist, @@ -175,6 +172,23 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { if (IS_MPA) { console.log('\n正在解析公共依赖 ...') // compileFiles('', null, list) + let chunk = new Map() + + chunk.set('', { page: null, files: [...list] }) + + new Worker(join(__dirname, './thread.js'), { + workerData: { + options, + data: { + chunk, + verbose, + dist, + imports: conf.imports, + timeStart: Date.now(), + title: '正在解析公共依赖' + } + } + }) } process.on('exit', _ => { diff --git a/lib/thread.js b/lib/thread.js index 6e84968..0058072 100644 --- a/lib/thread.js +++ b/lib/thread.js @@ -10,15 +10,9 @@ const { options, data } = workerData options.isCustomElement = Function('return ' + options.isCustomElement)() -// console.log(options) +console.log('data: ', data.title, Date.now() - data.timeStart) -let chunk = data.chunk - -delete data.chunk - -chunk.forEach(([a, b]) => { - console.log(a, b) -}) -// for (let [currentPage, { page, files }] of chunk.entries()) { -// compileFiles(currentPage, page, files, options, { ...data }) +// for (let [currentPage, { page, files }] of data.chunk.entries()) { +// currentPage && console.log('正在生成 %s ...', `${currentPage}.html`) +// compileFiles(currentPage, page, files, options, data) // }