fite/lib/compile.js

95 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-06-14 19:36:20 +08:00
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @date 2023/06/14 18:36:15
*/
import { join, dirname, parse, normalize } from 'node:path'
import fs from 'iofs'
import Es from 'esbuild'
import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js'
const template = fs.cat(join(process.cwd(), 'index.html')).toString()
export async function compileFiles(
2023-06-14 19:36:20 +08:00
currentPage,
page,
files,
options,
{ verbose, imports, dist } = {}
) {
options.currentPage = currentPage
for (let [k, it] of files) {
// 入口文件, 特殊处理
if (page && it.path === page.entry) {
let entry = fs.cat(page.entry).toString()
entry = parseJs(entry, imports, { ...options, IS_ENTRY: true })
let code = parseHtml(template, {
page,
imports,
entry,
LEGACY_MODE: options.LEGACY_MODE
})
fs.echo(code, join(dist, `${currentPage}.html`))
continue
}
verbose && console.log(' 解析 %s ...', it.name)
let pageDir = options.IS_MPA && currentPage ? `pages/${currentPage}` : ''
switch (it.ext) {
case '.vue':
{
let code = compileVue(it.path, imports, options)
await Es.transform(code, { minify: true }).then(r => {
2023-06-14 19:36:20 +08:00
fs.echo(
r.code,
join(dist, 'assets/', pageDir, it.name.replace(/\.vue$/, '.js'))
)
})
}
break
case '.js':
{
let code = fs.cat(it.path)
code = parseJs(code + '', imports, options)
await Es.transform(code, { minify: true }).then(r => {
2023-06-14 19:36:20 +08:00
fs.echo(r.code, join(dist, 'assets/', pageDir, it.name))
})
}
break
case '.scss':
case '.css':
{
let target = join(
dist,
'assets/',
pageDir,
it.name.replace(/\.scss$/, '.css')
)
2023-06-14 19:36:20 +08:00
if (it.ext === '.css') {
fs.cp(it.path, target)
} else {
let code = compileScss(it.path)
fs.echo(code, target)
}
}
break
default:
fs.cp(it.path, join(dist, 'assets/', pageDir, it.name))
2023-06-14 19:36:20 +08:00
break
}
}
}