105 lines
2.5 KiB
JavaScript
105 lines
2.5 KiB
JavaScript
/**
|
|
* {}
|
|
* @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(
|
|
currentPage,
|
|
page,
|
|
files,
|
|
options,
|
|
{ verbose, imports, dist } = {}
|
|
) {
|
|
let pageDir = options.IS_MPA && currentPage ? `pages/${currentPage}` : ''
|
|
options.currentPage = currentPage
|
|
|
|
for (let [path, it] of files) {
|
|
// 入口文件, 特殊处理
|
|
if (page && 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)
|
|
|
|
switch (it.ext) {
|
|
case '.vue':
|
|
{
|
|
let code = compileVue(path, imports, options)
|
|
|
|
await Es.transform(code, { minify: true }).then(r => {
|
|
fs.echo(
|
|
r.code,
|
|
join(dist, 'assets/', pageDir, it.name.replace(/\.vue$/, '.js'))
|
|
)
|
|
})
|
|
}
|
|
break
|
|
|
|
case '.js':
|
|
{
|
|
let code = fs.cat(path)
|
|
|
|
code = parseJs(code + '', imports, options)
|
|
|
|
await Es.transform(code, { minify: true }).then(r => {
|
|
fs.echo(r.code, join(dist, 'assets/', pageDir, it.name))
|
|
})
|
|
}
|
|
break
|
|
// es2024之后esm的语法的assets 变成了with, 对构建工具来说无法适配到具体的浏览器
|
|
// 故把json文件改成js文件
|
|
case '.json':
|
|
{
|
|
let code = fs.cat(path)
|
|
|
|
code = 'export default ' + JSON.stringify(JSON.parse(code + ''))
|
|
|
|
fs.echo(code, join(dist, 'assets/', pageDir, it.name + '.js'))
|
|
}
|
|
break
|
|
|
|
case '.scss':
|
|
case '.css':
|
|
{
|
|
let target = join(
|
|
dist,
|
|
'assets/',
|
|
pageDir,
|
|
it.name.replace(/\.scss$/, '.css')
|
|
)
|
|
if (it.ext === '.css') {
|
|
fs.cp(path, target)
|
|
} else {
|
|
let code = compileScss(path)
|
|
fs.echo(code, target)
|
|
}
|
|
}
|
|
break
|
|
|
|
default:
|
|
fs.cp(path, join(dist, 'assets/', pageDir, it.name))
|
|
break
|
|
}
|
|
}
|
|
}
|