fite/lib/prod.js

96 lines
2.3 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-10-09 19:19:21 +08:00
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
//
2022-10-11 19:31:04 +08:00
const IS_MPA = Object.keys(conf.pages).length > 1
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
let list = fs.ls(root, true).map(it => ({
name: it.slice(root.length + 1),
path: it,
ext: parse(it).ext
}))
currentPage = Object.keys(conf.pages)[0]
page = conf.pages[currentPage]
2022-10-14 12:06:33 +08:00
console.log('正在生成 %s...', `${currentPage}.html`)
2022-10-11 19:31:04 +08:00
for (let it of list) {
// 入口文件, 特殊处理
if (it.path === page.entry) {
let entry = fs.cat(page.entry).toString()
entry = parseJs(entry, conf.imports, { IS_MPA, currentPage }, true)
let code = parseHtml(html, { page, imports: conf.imports, entry })
fs.echo(code, join(dist, `${currentPage}.html`))
continue
}
2022-10-14 12:06:33 +08:00
console.log(' 解析 %s...', it.name)
2022-10-11 19:31:04 +08:00
switch (it.ext) {
case '.vue':
{
let code = compileVue(
it.path,
conf.imports,
{ IS_MPA, currentPage, root, pagesDir },
true
)
fs.echo(code, join(dist, `assets/js/${it.name.split('.').shift()}.js`))
}
break
case '.js':
{
let code = fs.cat(it.path)
code = parseJs(code + '', conf.imports, { IS_MPA, currentPage }, true)
fs.echo(code, join(dist, `assets/js/${it.name}`))
}
break
case '.png':
case '.jpg':
case '.jpeg':
case '.webp':
case '.gif':
case '.svg':
case '.ico':
case '.bmp':
fs.cp(it.path, join(dist, it.name))
break
2022-10-12 18:55:02 +08:00
case 'scss':
case 'css':
{
let code = compileScss(it.path)
fs.echo(code, join(dist, it.name))
}
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
}
}
}
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
}