fite/lib/prod.js

188 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

2023-05-22 15:06:33 +08:00
import { join, dirname, parse } from 'node:path'
2022-10-11 19:31:04 +08:00
import fs from 'iofs'
2022-11-03 18:22:42 +08:00
import Es from 'esbuild'
2022-10-11 19:31:04 +08:00
import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js'
2023-05-22 15:06:33 +08:00
import { isCustomElement } from './utils.js'
2022-10-11 19:31:04 +08:00
2023-05-16 14:17:40 +08:00
function readFile(file) {
return (file && fs.cat(file)?.toString()) || ''
}
2022-10-11 19:31:04 +08:00
export default function compile(root = '', dist = '', conf = {}) {
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 || '' // 部署目录, 默认是根目录部署
2022-10-11 19:31:04 +08:00
const IS_MPA = Object.keys(conf.pages).length > 1
2023-05-15 15:17:55 +08:00
const PAGES_PREFIX = Object.keys(conf.pages).map(it => `pages/${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
2022-10-14 12:06:33 +08:00
let timeStart = Date.now()
let template = fs.cat(join(process.env.PWD, 'index.html')).toString()
2022-10-11 19:31:04 +08:00
let list = fs
.ls(SOURCE_DIR, true)
.map(it => ({
2023-02-16 15:32:17 +08:00
name: it.slice(SOURCE_DIR.length + 1),
2022-10-11 19:31:04 +08:00
path: it,
ext: parse(it).ext
}))
2023-05-15 15:17:55 +08:00
.filter(it => {
if (fs.isfile(it.path) && it.ext !== '') {
if (IS_MPA && it.name.startsWith('pages/')) {
if (PAGES_PREFIX.some(it => it.startsWith(it.name))) {
return true
} else {
return false
}
}
2023-05-16 14:17:40 +08:00
return it.path !== conf.inject?.scss
2023-05-15 15:17:55 +08:00
}
return false
})
2023-03-28 15:41:01 +08:00
let compileFiles = function (currentPage, page, files) {
let options = {
IS_MPA,
currentPage,
SOURCE_DIR,
DEPLOY_PATH,
INJECT_SCSS,
2023-05-22 15:06:33 +08:00
LEGACY_MODE,
isCustomElement: conf.isCustomElement || isCustomElement
}
for (let it of files) {
2022-10-11 19:31:04 +08:00
// 入口文件, 特殊处理
if (page && it.path === page.entry) {
2022-10-11 19:31:04 +08:00
let entry = fs.cat(page.entry).toString()
entry = parseJs(entry, conf.imports, { ...options, IS_ENTRY: true })
2022-10-11 19:31:04 +08:00
let code = parseHtml(template, {
page,
imports: conf.imports,
entry,
LEGACY_MODE
})
2022-10-11 19:31:04 +08:00
fs.echo(code, join(dist, `${currentPage}.html`))
continue
}
2022-11-03 18:22:42 +08:00
console.log(' 解析 %s ...', it.name)
2022-10-14 12:06:33 +08:00
2023-03-17 17:56:05 +08:00
let pageDir = IS_MPA && currentPage ? `pages/${currentPage}` : ''
2022-10-11 19:31:04 +08:00
switch (it.ext) {
case '.vue':
{
let code = compileVue(it.path, conf.imports, options)
2022-10-11 19:31:04 +08:00
2022-11-03 18:22:42 +08:00
Es.transform(code, { minify: true }).then(r => {
2023-01-13 11:40:53 +08:00
fs.echo(
r.code,
2023-03-17 17:56:05 +08:00
join(dist, 'assets/', pageDir, it.name.replace(/\.vue$/, '.js'))
2023-01-13 11:40:53 +08:00
)
2022-11-03 18:22:42 +08:00
})
2022-10-11 19:31:04 +08:00
}
break
case '.js':
{
let code = fs.cat(it.path)
code = parseJs(code + '', conf.imports, options)
2022-11-03 18:22:42 +08:00
Es.transform(code, { minify: true }).then(r => {
2023-03-17 17:56:05 +08:00
fs.echo(r.code, join(dist, 'assets/', pageDir, it.name))
2022-11-03 18:22:42 +08:00
})
2022-10-11 19:31:04 +08:00
}
break
2023-01-31 19:17:38 +08:00
case '.scss':
case '.css':
2022-10-12 18:55:02 +08:00
{
let target = join(
dist,
'assets/',
it.name.replace(/\.scss$/, '.css')
2023-05-22 17:25:36 +08:00
)
if (it.ext === '.css') {
fs.cp(it.path, target)
} else {
let code = compileScss(it.path)
fs.echo(code, target)
}
2022-10-12 18:55:02 +08:00
}
break
2022-10-14 12:06:33 +08:00
default:
fs.cp(it.path, join(dist, it.name))
break
2022-10-11 19:31:04 +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)
console.log(' 复制 %s ...', name)
fs.cp(it, join(dist, name))
}
})
}
for (let currentPage in conf.pages) {
let page = conf.pages[currentPage]
let dir = dirname(page.entry)
2023-02-24 16:08:18 +08:00
let files = list
if (IS_MPA) {
files = []
fs.ls(dir, true).forEach(it => {
if (fs.isdir(it)) {
return
}
2023-05-15 15:17:55 +08:00
let idx = list.findIndex(_ => _?.path === it)
2023-03-28 15:41:01 +08:00
let name = it.slice(dir.length + 1)
let ext = parse(name).ext
if (ext === '') {
return
}
2023-05-15 15:17:55 +08:00
list[idx] = null
2023-02-24 16:08:18 +08:00
files.push({
2023-03-28 15:41:01 +08:00
name,
2023-02-24 16:08:18 +08:00
path: it,
2023-03-28 15:41:01 +08:00
ext
2023-02-24 16:08:18 +08:00
})
})
2023-02-24 16:08:18 +08:00
}
console.log('正在生成 %s ...', `${currentPage}.html`)
compileFiles(currentPage, page, files)
}
2023-05-15 15:17:55 +08:00
list = list.filter(it => it)
2023-02-24 16:08:18 +08:00
if (IS_MPA) {
console.log('\n正在解析公共依赖 ...')
compileFiles('', null, list)
}
2022-10-14 12:06:33 +08:00
console.log('\n页面处理完成, 耗时 %ss\n', (Date.now() - timeStart) / 1000)
2022-10-09 19:19:21 +08:00
}