import { join, dirname, parse, normalize } from 'node:path' 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/' // 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() // 线程池 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 const { ABS_CONFIG_FILEPATH, compileOptions = {}, define = {}, plugin = [] } = conf const { isCustomElement = defaultCustomElement } = compileOptions 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, ABS_CONFIG_FILEPATH, define } fs.ls(SOURCE_DIR, true).forEach(path => { if (fs.isdir(path)) { return } let name = path.slice(SOURCE_DIR.length + 1) let it = { 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 (path === conf.inject.scss) { return } list.set(path, it) } }) // 创建线程池 if (THREADS_NUM > 0 && (IS_MPA || list.size > THREADS_NUM * 10)) { // 页面数过少时, 线程数量不能比页面数多 let max = Math.min(THREADS_NUM, PAGES_KEYS.length) for (let i = 0; i < max; i++) { WORKER_POOL.add( new Worker(join(__dirname, './thread.js'), { workerData: { options, verbose, dist, imports: conf.imports } }) ) } } else { options.isCustomElement = isCustomElement } // 优先处理静态目录, 之后的源码目录中, 以便如果有产生相同的文件名, 则覆盖静态目录中的文件 if (fs.isdir(PUBLIC_DIR)) { console.log('\n正在处理静态资源 ...') fs.ls(PUBLIC_DIR, true).forEach(it => { let ext = parse(it).ext if (ext && fs.isfile(it)) { let name = it.slice(PUBLIC_DIR.length + 1) verbose && console.log(' 复制 %s ...', name) fs.cp(it, join(dist, name)) } }) } 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, ext }) }) 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 { // 每个线程处理的文件数 let chunkSize = Math.ceil(list.size / THREADS_NUM) let currentPage = PAGES_KEYS[0] let page = conf.pages[currentPage] console.log(`正在生成 ${currentPage}.html ...`) if (THREADS_NUM > 0 && list.size > THREADS_NUM * 10) { 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) }) JOBS_QUEUE.push(chunk) doJob() } } else { options.plugin = plugin options.isCustomElement = isCustomElement compileFiles(currentPage, page, list, options, { verbose, dist, imports: conf.imports }) } } process.on('exit', _ => { console.log('\n页面处理完成, 耗时 %ss\n', (Date.now() - timeStart) / 1000) }) }