import { join, dirname, parse, normalize } from 'node:path' import { Worker, parentPort } from 'node:worker_threads' import os from 'node:os' import fs from 'iofs' const IS_WIN = process.platform === 'win32' const PREFIX = IS_WIN ? 'pages\\' : 'pages/' // 线程太多, 效率反而不高 const THREADS_NUM = os.cpus().length > 4 ? 4 : os.cpus().length - 1 const __filename = normalize(import.meta.url.slice(IS_WIN ? 8 : 7)) const __dirname = dirname(__filename) const WORKER_POOL = new Set() // 线程池 const JOBS_QUEUE = [] // 任务队列 function readFile(file) { return (file && fs.cat(file)?.toString()) || '' } function doJob() { while (JOBS_QUEUE.length && WORKER_POOL.size) { let job = JOBS_QUEUE.shift() let worker = WORKER_POOL.values().next().value WORKER_POOL.delete(worker) worker.once('message', _ => { if (JOBS_QUEUE.length) { WORKER_POOL.add(worker) doJob() } else { worker.terminate() } }) worker.postMessage(job) } } export default function compile(root = '', dist = '', conf = {}, verbose) { // const SOURCE_DIR = join(root, 'src') const PUBLIC_DIR = join(root, 'public') const DEPLOY_PATH = conf.base || '' // 部署目录, 默认是根目录部署 const PAGES_KEYS = Object.keys(conf.pages) const IS_MPA = PAGES_KEYS.length > 1 const PAGES_PREFIX = PAGES_KEYS.map(it => IS_WIN ? `${PREFIX + it}\\` : `${PREFIX + it}/` ) const INJECT_SCSS = readFile(conf.inject?.scss) const LEGACY_MODE = !!conf.legacy conf.inject = conf.inject || { scss: '' } let timeStart = Date.now() let template = fs.cat(join(process.cwd(), 'index.html')).toString() let list = new Map() let options = { IS_MPA, SOURCE_DIR, DEPLOY_PATH, INJECT_SCSS, LEGACY_MODE, // 线程通讯无法传递函数类型, 需要转为字符串, 之后再转回来 isCustomElement: conf.isCustomElement } fs.ls(SOURCE_DIR, true).forEach(path => { if (fs.isdir(path)) { return } let name = path.slice(SOURCE_DIR.length + 1) let it = { path, name, ext: parse(name).ext } if (it.ext !== '') { if (IS_MPA && it.name.startsWith(PREFIX)) { if (PAGES_PREFIX.some(it => it.startsWith(it.name))) { list.set(path, it) } return } if (it.path === conf.inject.scss) { return } list.set(path, it) } }) // 优先处理静态目录, 之后的源码目录中, 以便如果有产生相同的文件名, 则覆盖静态目录中的文件 if (fs.isdir(PUBLIC_DIR)) { console.log('\n正在处理静态资源 ...') fs.ls(PUBLIC_DIR, true).forEach(it => { let ext = parse(it).ext if (ext === '') { return } if (fs.isfile(it)) { let name = it.slice(PUBLIC_DIR.length + 1) verbose && console.log(' 复制 %s ...', name) fs.cp(it, join(dist, name)) } }) } // 创建线程池 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 (IS_MPA) { for (let currentPage of PAGES_KEYS) { let page = conf.pages[currentPage] let dir = dirname(page.entry) let files = new Map() let chunk = new Map() fs.ls(dir, true).forEach(path => { if (fs.isdir(path)) { return } let name = path.slice(dir.length + 1) let ext = parse(name).ext if (ext === '') { return } list.delete(path) files.set(path, { name, path, ext }) }) chunk.set(currentPage, { page, files }) JOBS_QUEUE.push(chunk) doJob() } // { let chunk = new Map() chunk.set('', { page: null, files: list }) JOBS_QUEUE.push(chunk) doJob() } } else { // 每个线程处理的文件数 let chunkSize = Math.ceil(list.size / THREADS_NUM) let currentPage = PAGES_KEYS[0] let page = conf.pages[currentPage] list = [...list] 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) }) new Worker(join(__dirname, './thread.js'), { workerData: { options, data: { chunk, verbose, dist, imports: conf.imports } } }) } } process.on('exit', _ => { console.log('\n页面处理完成, 耗时 %ss\n', (Date.now() - timeStart) / 1000) }) }