fite/lib/prod.js

118 lines
3.1 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()
2022-10-11 19:31:04 +08:00
let html = fs.cat(join(process.env.PWD, 'index.html')).toString()
let pagesDir = '',
currentPage = ''
if (IS_MPA) {
} else {
let page
2023-02-16 15:32:17 +08:00
let list = fs.ls(SOURCE_DIR, true).map(it => ({
name: it.slice(SOURCE_DIR.length + 1),
2022-10-11 19:31:04 +08:00
path: it,
ext: parse(it).ext
}))
currentPage = Object.keys(conf.pages)[0]
page = conf.pages[currentPage]
2022-11-03 18:22:42 +08:00
console.log('正在生成 %s ...', `${currentPage}.html`)
2022-10-14 12:06:33 +08:00
2022-10-11 19:31:04 +08:00
for (let it of list) {
2022-11-03 18:22:42 +08:00
if (fs.isdir(it.path)) {
continue
}
2022-10-11 19:31:04 +08:00
// 入口文件, 特殊处理
if (it.path === page.entry) {
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
2023-01-31 19:17:38 +08:00
let code = parseHtml(html, { 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,
2023-02-19 16:52:55 +08:00
{ IS_MPA, currentPage, SOURCE_DIR, pagesDir, 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/${it.name.split('.').shift()}.js`)
)
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/${it.name}`))
})
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
}
}
2023-02-16 15:32:17 +08:00
fs.ls(PUBLIC_DIR, true).forEach(it => {
let name = it.slice(PUBLIC_DIR.length + 1)
console.log('正在复制静态文件 %s ...', name)
fs.cp(it, join(dist, name))
})
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
}