fite/lib/prod.js

183 lines
4.6 KiB
JavaScript
Raw Permalink Normal View History

2023-06-14 19:36:20 +08:00
import { join, dirname, parse, normalize } from 'node:path'
import { Worker, parentPort } from 'node:worker_threads'
2023-06-14 19:36:20 +08:00
import os from 'node:os'
2022-10-11 19:31:04 +08:00
import fs from 'iofs'
2023-06-15 16:54:45 +08:00
2023-05-30 17:06:53 +08:00
const IS_WIN = process.platform === 'win32'
const PREFIX = IS_WIN ? 'pages\\' : 'pages/'
2023-06-15 16:54:45 +08:00
// 线程太多, 效率反而不高
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)
2023-06-15 19:43:21 +08:00
const WORKER_POOL = new Set() // 线程池
const JOBS_QUEUE = [] // 任务队列
2023-05-30 17:06:53 +08:00
2023-05-16 14:17:40 +08:00
function readFile(file) {
return (file && fs.cat(file)?.toString()) || ''
}
2023-06-15 19:43:21 +08:00
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()
}
2023-06-15 19:43:21 +08:00
})
2023-06-15 19:43:21 +08:00
worker.postMessage(job)
}
}
2023-05-29 15:45:21 +08:00
export default function compile(root = '', dist = '', conf = {}, verbose) {
2022-10-09 19:19:21 +08:00
//
2023-02-16 15:32:17 +08:00
const SOURCE_DIR = join(root, 'src')
const PUBLIC_DIR = join(root, 'public')
2023-02-19 16:52:55 +08:00
const DEPLOY_PATH = conf.base || '' // 部署目录, 默认是根目录部署
2023-06-14 19:36:20 +08:00
const PAGES_KEYS = Object.keys(conf.pages)
const IS_MPA = PAGES_KEYS.length > 1
const PAGES_PREFIX = PAGES_KEYS.map(it =>
2023-05-30 17:06:53 +08:00
IS_WIN ? `${PREFIX + it}\\` : `${PREFIX + it}/`
)
2023-05-16 14:17:40 +08:00
const INJECT_SCSS = readFile(conf.inject?.scss)
const LEGACY_MODE = !!conf.legacy
2023-06-16 14:19:32 +08:00
const { isCustomElement } = conf.compileOptions || {}
2023-02-16 15:32:17 +08:00
2023-05-29 15:24:20 +08:00
conf.inject = conf.inject || { scss: '' }
2022-10-14 12:06:33 +08:00
let timeStart = Date.now()
2023-05-22 19:29:02 +08:00
let template = fs.cat(join(process.cwd(), 'index.html')).toString()
2023-06-14 19:36:20 +08:00
let list = new Map()
let options = {
IS_MPA,
SOURCE_DIR,
DEPLOY_PATH,
INJECT_SCSS,
LEGACY_MODE,
// 线程通讯无法传递函数类型, 需要转为字符串, 之后再转回来
2023-06-16 14:19:32 +08:00
isCustomElement: isCustomElement ? isCustomElement.toString() : null
}
// 创建线程池
for (let i = 0; i < THREADS_NUM; i++) {
WORKER_POOL.add(
new Worker(join(__dirname, './thread.js'), {
workerData: {
options,
verbose,
dist,
imports: conf.imports
}
})
)
2023-06-14 19:36:20 +08:00
}
2022-10-11 19:31:04 +08:00
2023-05-29 15:24:20 +08:00
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
}
2023-06-16 14:19:32 +08:00
if (it.ext) {
2023-05-30 17:06:53 +08:00
if (IS_MPA && it.name.startsWith(PREFIX)) {
2023-05-29 15:24:20 +08:00
if (PAGES_PREFIX.some(it => it.startsWith(it.name))) {
2023-06-15 16:54:45 +08:00
list.set(path, it)
2023-05-15 15:17:55 +08:00
}
2023-06-15 16:54:45 +08:00
return
2023-05-29 15:24:20 +08:00
}
2023-05-15 15:17:55 +08:00
2023-05-29 15:24:20 +08:00
if (it.path === conf.inject.scss) {
return
2023-05-15 15:17:55 +08:00
}
2023-06-14 19:36:20 +08:00
list.set(path, it)
2023-05-29 15:24:20 +08:00
}
})
2023-03-28 15:41:01 +08:00
// 优先处理静态目录, 之后的源码目录中, 以便如果有产生相同的文件名, 则覆盖静态目录中的文件
if (fs.isdir(PUBLIC_DIR)) {
console.log('\n正在处理静态资源 ...')
fs.ls(PUBLIC_DIR, true).forEach(it => {
let ext = parse(it).ext
2023-06-16 14:19:32 +08:00
if (ext && fs.isfile(it)) {
let name = it.slice(PUBLIC_DIR.length + 1)
2023-05-29 15:45:21 +08:00
verbose && console.log(' 复制 %s ...', name)
fs.cp(it, join(dist, name))
}
})
}
2023-06-14 19:36:20 +08:00
if (IS_MPA) {
2023-06-15 19:43:21 +08:00
for (let currentPage of PAGES_KEYS) {
let page = conf.pages[currentPage]
let dir = dirname(page.entry)
let files = new Map()
2023-06-14 19:36:20 +08:00
let chunk = new Map()
2023-06-15 19:43:21 +08:00
fs.ls(dir, true).forEach(path => {
if (fs.isdir(path)) {
return
}
2023-05-29 15:24:20 +08:00
2023-06-15 19:43:21 +08:00
let name = path.slice(dir.length + 1)
let ext = parse(name).ext
2023-03-28 15:41:01 +08:00
2023-06-15 19:43:21 +08:00
if (ext === '') {
return
}
2023-06-14 19:36:20 +08:00
2023-06-15 19:43:21 +08:00
list.delete(path)
2023-06-16 14:19:32 +08:00
files.set(path, { name, ext })
2023-06-15 19:43:21 +08:00
})
chunk.set(currentPage, { page, files })
JOBS_QUEUE.push(chunk)
doJob()
}
2023-06-14 19:36:20 +08:00
2023-06-16 14:19:32 +08:00
// 公共依赖
2023-06-15 19:43:21 +08:00
{
let chunk = new Map()
chunk.set('', { page: null, files: list })
2023-06-16 14:19:32 +08:00
2023-06-15 19:43:21 +08:00
JOBS_QUEUE.push(chunk)
doJob()
2023-06-14 19:36:20 +08:00
}
} else {
// 每个线程处理的文件数
let chunkSize = Math.ceil(list.size / THREADS_NUM)
let currentPage = PAGES_KEYS[0]
let page = conf.pages[currentPage]
2023-03-28 15:41:01 +08:00
2023-06-14 19:36:20 +08:00
list = [...list]
2023-06-16 14:19:32 +08:00
console.log(`正在生成 ${currentPage}.html ...`)
2023-06-14 19:36:20 +08:00
for (let i = 0; i < THREADS_NUM; i++) {
let start = i * chunkSize
let end = start + chunkSize
2023-06-15 16:54:45 +08:00
let chunk = new Map()
chunk.set(currentPage, { page, files: list.slice(start, end) })
2023-06-14 19:36:20 +08:00
2023-06-16 14:19:32 +08:00
JOBS_QUEUE.push(chunk)
doJob()
2023-02-24 16:08:18 +08:00
}
}
2023-05-29 15:24:20 +08:00
process.on('exit', _ => {
console.log('\n页面处理完成, 耗时 %ss\n', (Date.now() - timeStart) / 1000)
})
2022-10-09 19:19:21 +08:00
}