fite/lib/prod.js

184 lines
4.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 } from 'node:worker_threads'
import os from 'node:os'
2022-10-11 19:31:04 +08:00
import fs from 'iofs'
2023-06-14 19:36:20 +08:00
// import Es from 'esbuild'
// import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js'
2023-05-22 15:06:33 +08:00
import { isCustomElement } from './utils.js'
2023-06-14 19:36:20 +08:00
import { compileFiles } from './compile.js'
2022-10-11 19:31:04 +08:00
2023-05-30 17:06:53 +08:00
const IS_WIN = process.platform === 'win32'
const PREFIX = IS_WIN ? 'pages\\' : 'pages/'
2023-06-14 19:36:20 +08:00
const THREADS_NUM = os.cpus().length
const __dirname = normalize(dirname(import.meta.url.slice(IS_WIN ? 8 : 7)))
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-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-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,
// 线程通讯无法传递函数类型, 需要转为字符串, 之后再转回来
// isCustomElement: conf.isCustomElement || isCustomElement
isCustomElement: (conf.isCustomElement || isCustomElement).toString()
}
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 = {
path,
name,
ext: parse(name).ext
}
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-14 19:36:20 +08:00
return list.set(path, it)
2023-05-29 15:24:20 +08:00
} else {
return
2023-05-15 15:17:55 +08:00
}
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
if (ext === '') {
return
}
if (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-05-29 15:45:21 +08:00
console.log('')
2023-06-14 19:36:20 +08:00
if (IS_MPA) {
// 电脑线程数比页面数量还多时, 取小
let num = Math.min(PAGES_KEYS.length, THREADS_NUM)
let chunkSize = Math.ceil(PAGES_KEYS.length / num)
for (let i = 0; i < num; i++) {
let start = i * chunkSize
let end = start + chunkSize
let pages = PAGES_KEYS.slice(start, end)
let chunk = new Map()
for (let currentPage of pages) {
let page = conf.pages[currentPage]
let dir = dirname(page.entry)
let files = new Map()
fs.ls(dir, true).forEach(path => {
if (fs.isdir(path)) {
return
}
2023-05-29 15:24:20 +08:00
2023-06-14 19:36:20 +08:00
let name = path.slice(dir.length + 1)
let ext = parse(name).ext
2023-03-28 15:41:01 +08:00
2023-06-14 19:36:20 +08:00
if (ext === '') {
return
}
list.delete(path)
files.set(path, { name, path, ext })
})
chunk.set(currentPage, { page, files })
}
new Worker(join(__dirname, './thread.js'), {
workerData: {
options,
data: {
chunk,
verbose,
dist,
imports: conf.imports
}
2023-03-28 15:41:01 +08:00
}
2023-06-14 19:36:20 +08:00
})
// compileFiles(currentPage, page, chunk, 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]
2023-03-28 15:41:01 +08:00
2023-06-14 19:36:20 +08:00
list = [...list]
for (let i = 0; i < THREADS_NUM; i++) {
let start = i * chunkSize
let end = start + chunkSize
let chunk = [list.slice(start, end)]
new Worker(join(__dirname, './thread.js'), {
workerData: {
options,
data: {
currentPage,
page,
chunk,
verbose,
dist,
imports: conf.imports
}
}
})
2023-02-24 16:08:18 +08:00
}
}
2023-02-24 16:08:18 +08:00
if (IS_MPA) {
console.log('\n正在解析公共依赖 ...')
2023-06-14 19:36:20 +08:00
// compileFiles('', null, list)
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
}