暂存6.15

threads
yutent 2023-06-15 16:54:45 +08:00
parent c675b73f25
commit ff5a225cc1
3 changed files with 36 additions and 31 deletions

View File

@ -20,10 +20,7 @@ export function compileFiles(
) { ) {
options.currentPage = currentPage options.currentPage = currentPage
console.log('正在生成 %s ...', `${currentPage}.html`)
for (let [k, it] of files) { for (let [k, it] of files) {
// let it = files[k]
// 入口文件, 特殊处理 // 入口文件, 特殊处理
if (page && it.path === page.entry) { if (page && it.path === page.entry) {
let entry = fs.cat(page.entry).toString() let entry = fs.cat(page.entry).toString()

View File

@ -2,15 +2,16 @@ import { join, dirname, parse, normalize } from 'node:path'
import { Worker } from 'node:worker_threads' import { Worker } from 'node:worker_threads'
import os from 'node:os' import os from 'node:os'
import fs from 'iofs' import fs from 'iofs'
// import Es from 'esbuild'
// import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js'
import { isCustomElement } from './utils.js' import { isCustomElement } from './utils.js'
import { compileFiles } from './compile.js'
const IS_WIN = process.platform === 'win32' const IS_WIN = process.platform === 'win32'
const PREFIX = IS_WIN ? 'pages\\' : 'pages/' 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) { function readFile(file) {
return (file && fs.cat(file)?.toString()) || '' return (file && fs.cat(file)?.toString()) || ''
@ -59,10 +60,9 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
if (it.ext !== '') { if (it.ext !== '') {
if (IS_MPA && it.name.startsWith(PREFIX)) { if (IS_MPA && it.name.startsWith(PREFIX)) {
if (PAGES_PREFIX.some(it => it.startsWith(it.name))) { if (PAGES_PREFIX.some(it => it.startsWith(it.name))) {
return list.set(path, it) list.set(path, it)
} else {
return
} }
return
} }
if (it.path === conf.inject.scss) { if (it.path === conf.inject.scss) {
@ -90,7 +90,6 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
} }
}) })
} }
console.log('')
if (IS_MPA) { if (IS_MPA) {
// 电脑线程数比页面数量还多时, 取小 // 电脑线程数比页面数量还多时, 取小
@ -126,6 +125,7 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
chunk.set(currentPage, { page, files }) chunk.set(currentPage, { page, files })
} }
new Worker(join(__dirname, './thread.js'), { new Worker(join(__dirname, './thread.js'), {
workerData: { workerData: {
options, options,
@ -133,15 +133,12 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
chunk, chunk,
verbose, verbose,
dist, dist,
imports: conf.imports imports: conf.imports,
timeStart: Date.now(),
title: '正在解析currentPage'
} }
} }
}) })
// compileFiles(currentPage, page, chunk, options, {
// verbose,
// dist,
// imports: conf.imports
// })
} }
} else { } else {
// 每个线程处理的文件数 // 每个线程处理的文件数
@ -154,14 +151,14 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
for (let i = 0; i < THREADS_NUM; i++) { for (let i = 0; i < THREADS_NUM; i++) {
let start = i * chunkSize let start = i * chunkSize
let end = start + 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'), { new Worker(join(__dirname, './thread.js'), {
workerData: { workerData: {
options, options,
data: { data: {
currentPage,
page,
chunk, chunk,
verbose, verbose,
dist, dist,
@ -175,6 +172,23 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
if (IS_MPA) { if (IS_MPA) {
console.log('\n正在解析公共依赖 ...') console.log('\n正在解析公共依赖 ...')
// compileFiles('', null, list) // 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', _ => { process.on('exit', _ => {

View File

@ -10,15 +10,9 @@ const { options, data } = workerData
options.isCustomElement = Function('return ' + options.isCustomElement)() options.isCustomElement = Function('return ' + options.isCustomElement)()
// console.log(options) console.log('data: ', data.title, Date.now() - data.timeStart)
let chunk = data.chunk // for (let [currentPage, { page, files }] of data.chunk.entries()) {
// currentPage && console.log('正在生成 %s ...', `${currentPage}.html`)
delete data.chunk // compileFiles(currentPage, page, files, options, data)
chunk.forEach(([a, b]) => {
console.log(a, b)
})
// for (let [currentPage, { page, files }] of chunk.entries()) {
// compileFiles(currentPage, page, files, options, { ...data })
// } // }