fite/lib/prod.js

153 lines
3.8 KiB
JavaScript
Raw Normal View History

2022-10-11 19:31:04 +08:00
import fs from 'iofs'
import { join, resolve, dirname, parse } from 'path'
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'
const noc = Buffer.from('')
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-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
}))
.filter(it => fs.isfile(it.path))
2022-10-11 19:31:04 +08:00
let compileFiles = function (currentPage, page, files) {
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()
2023-02-12 23:01:57 +08:00
entry = parseJs(
entry,
conf.imports,
2023-02-19 16:52:55 +08:00
{ IS_MPA, currentPage, IS_ENTRY: true, DEPLOY_PATH },
2023-02-12 23:01:57 +08:00
true
)
2022-10-11 19:31:04 +08:00
let code = parseHtml(
template,
{ page, imports: conf.imports, entry },
true
)
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
2022-10-11 19:31:04 +08:00
switch (it.ext) {
case '.vue':
{
let code = compileVue(
it.path,
conf.imports,
{ IS_MPA, currentPage, SOURCE_DIR, DEPLOY_PATH },
2022-10-11 19:31:04 +08:00
true
)
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,
join(
dist,
'assets/js/',
IS_MPA ? currentPage : '',
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)
2023-01-13 11:40:53 +08:00
code = parseJs(
code + '',
conf.imports,
2023-02-19 16:52:55 +08:00
{ IS_MPA, currentPage, DEPLOY_PATH },
2023-01-13 11:40:53 +08:00
true
)
2022-11-03 18:22:42 +08:00
Es.transform(code, { minify: true }).then(r => {
fs.echo(
r.code,
join(dist, 'assets/js/', IS_MPA ? currentPage : '', 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 code = compileScss(it.path)
2023-01-31 19:17:38 +08:00
if (!it.name.startsWith('assets')) {
it.name = 'assets/js/' + it.name
}
fs.echo(code, join(dist, it.name.replace(/\.scss$/, '.css')))
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
}
}
}
for (let currentPage in conf.pages) {
let page = conf.pages[currentPage]
let dir = dirname(page.entry)
let files = []
fs.ls(dir, true).forEach(it => {
if (fs.isdir(it)) {
return
}
let idx = list.findIndex(_ => _.path === it)
list.splice(idx, 1)
files.push({
name: it.slice(dir.length + 1),
path: it,
ext: parse(it).ext
})
})
2023-02-16 15:32:17 +08:00
console.log('正在生成 %s ...', `${currentPage}.html`)
compileFiles(currentPage, page, files)
}
console.log('正在解析公共依赖 ...')
compileFiles('', null, list)
//
if (fs.isdir(PUBLIC_DIR)) {
console.log('正在处理静态资源 ...')
2023-02-16 15:32:17 +08:00
fs.ls(PUBLIC_DIR, true).forEach(it => {
2023-02-19 20:34:15 +08:00
if (fs.isfile(it)) {
let name = it.slice(PUBLIC_DIR.length + 1)
console.log(' 正在复制 %s ...', name)
2023-02-19 20:34:15 +08:00
fs.cp(it, join(dist, name))
}
2023-02-16 15:32:17 +08:00
})
2022-10-11 19:31:04 +08:00
}
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
}