From cca39d915619693ca5577bdbc896db5ee0d23d30 Mon Sep 17 00:00:00 2001 From: yutent Date: Fri, 16 Jun 2023 12:11:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=A4=9A=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E7=BC=96=E8=AF=91,=20=E4=BF=AE=E5=A4=8D=E9=9D=99=E6=80=81?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=8D=E5=88=B6=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compile.js | 15 ++++++++++----- lib/prod.js | 32 +++++++++----------------------- lib/thread.js | 13 ++++++++++--- lib/utils.js | 14 -------------- lib/ws.js | 2 +- 5 files changed, 30 insertions(+), 46 deletions(-) diff --git a/lib/compile.js b/lib/compile.js index cc9dffd..5c5b560 100644 --- a/lib/compile.js +++ b/lib/compile.js @@ -11,7 +11,7 @@ import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js' const template = fs.cat(join(process.cwd(), 'index.html')).toString() -export function compileFiles( +export async function compileFiles( currentPage, page, files, @@ -47,7 +47,7 @@ export function compileFiles( { let code = compileVue(it.path, imports, options) - Es.transform(code, { minify: true }).then(r => { + await Es.transform(code, { minify: true }).then(r => { fs.echo( r.code, join(dist, 'assets/', pageDir, it.name.replace(/\.vue$/, '.js')) @@ -62,7 +62,7 @@ export function compileFiles( code = parseJs(code + '', imports, options) - Es.transform(code, { minify: true }).then(r => { + await Es.transform(code, { minify: true }).then(r => { fs.echo(r.code, join(dist, 'assets/', pageDir, it.name)) }) } @@ -71,7 +71,12 @@ export function compileFiles( case '.scss': case '.css': { - let target = join(dist, 'assets/', it.name.replace(/\.scss$/, '.css')) + let target = join( + dist, + 'assets/', + pageDir, + it.name.replace(/\.scss$/, '.css') + ) if (it.ext === '.css') { fs.cp(it.path, target) } else { @@ -82,7 +87,7 @@ export function compileFiles( break default: - fs.cp(it.path, join(dist, it.name)) + fs.cp(it.path, join(dist, 'assets/', pageDir, it.name)) break } } diff --git a/lib/prod.js b/lib/prod.js index 2dd88a4..48bbae0 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -1,10 +1,8 @@ import { join, dirname, parse, normalize } from 'node:path' -import { Worker } from 'node:worker_threads' +import { Worker, parentPort } from 'node:worker_threads' import os from 'node:os' import fs from 'iofs' -import { isCustomElement } from './utils.js' - const IS_WIN = process.platform === 'win32' const PREFIX = IS_WIN ? 'pages\\' : 'pages/' // 线程太多, 效率反而不高 @@ -19,11 +17,6 @@ function readFile(file) { } function doJob() { - // console.log('<><><>', JOBS_QUEUE.length, WORKER_POOL.size) - // if (JOBS_QUEUE.length === 0 && WORKER_POOL.size === THREADS_NUM) { - // process.exit() - // } - while (JOBS_QUEUE.length && WORKER_POOL.size) { let job = JOBS_QUEUE.shift() let worker = WORKER_POOL.values().next().value @@ -31,10 +24,14 @@ function doJob() { WORKER_POOL.delete(worker) worker.once('message', _ => { - WORKER_POOL.add(worker) - - doJob() + if (JOBS_QUEUE.length) { + WORKER_POOL.add(worker) + doJob() + } else { + worker.terminate() + } }) + worker.postMessage(job) } } @@ -64,8 +61,7 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { INJECT_SCSS, LEGACY_MODE, // 线程通讯无法传递函数类型, 需要转为字符串, 之后再转回来 - // isCustomElement: conf.isCustomElement || isCustomElement - isCustomElement: (conf.isCustomElement || isCustomElement).toString() + isCustomElement: conf.isCustomElement } fs.ls(SOURCE_DIR, true).forEach(path => { @@ -128,16 +124,6 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { } if (IS_MPA) { - // 电脑线程数比页面数量还多时, 取小 - // let num = Math.min(PAGES_KEYS.length, THREADS_NUM) - // let chunkSize = Math.ceil(PAGES_KEYS.length / num) - - // for (let i = 0; i < num; i++) { - // let start = i * chunkSize - // let end = start + chunkSize - // let pages = PAGES_KEYS.slice(start, end) - // let chunk = new Map() - for (let currentPage of PAGES_KEYS) { let page = conf.pages[currentPage] let dir = dirname(page.entry) diff --git a/lib/thread.js b/lib/thread.js index 318a3a3..43f35e4 100644 --- a/lib/thread.js +++ b/lib/thread.js @@ -5,17 +5,24 @@ */ import { parentPort, workerData } from 'node:worker_threads' import { compileFiles } from './compile.js' +import { isCustomElement } from './utils.js' const { options, verbose, dist, imports } = workerData options.isCustomElement = Function('return ' + options.isCustomElement)() -parentPort.on('message', job => { +async function doJob(job) { let [currentPage, { page, files }] = job.entries().next().value console.log( currentPage ? `正在生成 ${currentPage}.html ...` : '\n正在解析公共依赖 ...' ) - compileFiles(currentPage, page, files, options, { verbose, dist, imports }) + await compileFiles(currentPage, page, files, options, { + verbose, + dist, + imports + }) parentPort.postMessage('ok') -}) +} + +parentPort.on('message', doJob) diff --git a/lib/utils.js b/lib/utils.js index 68f2f1c..9919451 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -7,7 +7,6 @@ import { createHash, randomUUID } from 'node:crypto' import { join } from 'node:path' import { gzipSync } from 'node:zlib' -import { Worker } from 'node:worker_threads' import { red, cyan, blue } from 'kolorist' // 修正路径合并 避免在windows下被转义 @@ -98,16 +97,3 @@ export function createHmrScript(legacy, session = '') { export function isCustomElement(tag) { return tag.startsWith('wc-') } - -export function startWorker(file) { - return new Promise((resolve, reject) => { - let worker = new Worker(file) - worker.on('message', resolve) - worker.on('error', reject) - worker.on('exit', code => { - if (code !== 0) { - reject(new Error(`Worker stopped with exit code ${code}`)) - } - }) - }) -} diff --git a/lib/ws.js b/lib/ws.js index a495f48..208f45f 100644 --- a/lib/ws.js +++ b/lib/ws.js @@ -32,7 +32,7 @@ class WebSocket { send(msg = {}) { if (this.#clients.size) { - for (let [key, client] of this.#clients.entries()) { + for (let [key, client] of this.#clients) { client.send(JSON.stringify(msg)) } } else {