fite/lib/prod.js

225 lines
5.8 KiB
JavaScript
Raw 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
import { compileFiles } from './compile.js'
import { defaultCustomElement } from './utils.js'
2023-05-30 17:06:53 +08:00
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
2023-06-15 16:54:45 +08:00
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
2024-07-31 14:15:29 +08:00
const {
ABS_CONFIG_FILEPATH,
compileOptions = {},
define = {},
plugin = []
} = conf
2024-07-25 16:30:30 +08:00
const { isCustomElement = defaultCustomElement } = 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,
2024-07-25 16:30:30 +08:00
ABS_CONFIG_FILEPATH,
define
2023-06-16 14:19:32 +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-06-25 12:27:51 +08:00
if (path === conf.inject.scss) {
2023-05-29 15:24:20 +08:00
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
2023-06-25 12:27:51 +08:00
// 创建线程池
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++) {
2023-06-25 12:27:51 +08:00
WORKER_POOL.add(
new Worker(join(__dirname, './thread.js'), {
workerData: {
options,
verbose,
dist,
imports: conf.imports
}
})
)
}
2024-07-25 16:30:30 +08:00
} else {
options.isCustomElement = isCustomElement
2023-06-25 12:27:51 +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
})
2023-06-25 12:27:51 +08:00
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
})
}
2023-06-15 19:43:21 +08:00
}
2023-06-14 19:36:20 +08:00
2023-06-16 14:19:32 +08:00
// 公共依赖
if (THREADS_NUM > 0) {
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()
} else {
console.log('\n正在解析公共依赖 ...')
compileFiles('', null, list, options, {
verbose,
dist,
imports: conf.imports
})
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-16 14:19:32 +08:00
console.log(`正在生成 ${currentPage}.html ...`)
2023-06-25 12:27:51 +08:00
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()
2023-06-15 16:54:45 +08:00
chunk.set(currentPage, { page, files: list.slice(start, end) })
2023-06-14 19:36:20 +08:00
JOBS_QUEUE.push(chunk)
doJob()
}
} else {
2024-07-31 14:15:29 +08:00
options.plugin = plugin
options.isCustomElement = isCustomElement
compileFiles(currentPage, page, list, options, {
verbose,
dist,
imports: conf.imports
})
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
}