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()
|
|
|
|
|
2023-06-16 12:11:43 +08:00
|
|
|
export async function compileFiles(
|
2023-06-14 19:36:20 +08:00
|
|
|
currentPage,
|
|
|
|
page,
|
|
|
|
files,
|
|
|
|
options,
|
|
|
|
{ verbose, imports, dist } = {}
|
|
|
|
) {
|
2023-06-25 12:27:51 +08:00
|
|
|
let pageDir = options.IS_MPA && currentPage ? `pages/${currentPage}` : ''
|
2023-06-14 19:36:20 +08:00
|
|
|
options.currentPage = currentPage
|
|
|
|
|
2023-06-16 14:19:32 +08:00
|
|
|
for (let [path, it] of files) {
|
2023-06-14 19:36:20 +08:00
|
|
|
// 入口文件, 特殊处理
|
2023-06-16 14:19:32 +08:00
|
|
|
if (page && path === page.entry) {
|
2023-06-14 19:36:20 +08:00
|
|
|
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':
|
|
|
|
{
|
2024-07-25 15:24:06 +08:00
|
|
|
let code = await compileVue(path, imports, options)
|
2023-06-14 19:36:20 +08:00
|
|
|
|
2024-07-25 16:30:30 +08:00
|
|
|
await Es.transform(code, { minify: true }).then(async ({ code }) => {
|
|
|
|
for (let fn of options.plugin) {
|
|
|
|
code = await fn('js', code)
|
|
|
|
}
|
2023-06-14 19:36:20 +08:00
|
|
|
fs.echo(
|
2024-07-25 16:30:30 +08:00
|
|
|
code,
|
2023-06-14 19:36:20 +08:00
|
|
|
join(dist, 'assets/', pageDir, it.name.replace(/\.vue$/, '.js'))
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case '.js':
|
|
|
|
{
|
2023-06-16 14:19:32 +08:00
|
|
|
let code = fs.cat(path)
|
2023-06-14 19:36:20 +08:00
|
|
|
|
|
|
|
code = parseJs(code + '', imports, options)
|
|
|
|
|
2024-07-25 15:24:06 +08:00
|
|
|
await Es.transform(code, { minify: true }).then(async ({ code }) => {
|
|
|
|
for (let fn of options.plugin) {
|
|
|
|
code = await fn('js', code)
|
|
|
|
}
|
|
|
|
fs.echo(code, join(dist, 'assets/', pageDir, it.name))
|
2023-06-14 19:36:20 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
break
|
2024-04-01 18:26:00 +08:00
|
|
|
// es2024之后esm的语法的assets 变成了with, 对构建工具来说无法适配到具体的浏览器
|
|
|
|
// 故把json文件改成js文件
|
2024-03-01 18:39:39 +08:00
|
|
|
case '.json':
|
|
|
|
{
|
|
|
|
let code = fs.cat(path)
|
|
|
|
|
2024-04-01 18:26:00 +08:00
|
|
|
code = 'export default ' + JSON.stringify(JSON.parse(code + ''))
|
2024-03-01 18:39:39 +08:00
|
|
|
|
2024-04-01 18:26:00 +08:00
|
|
|
fs.echo(code, join(dist, 'assets/', pageDir, it.name + '.js'))
|
2024-03-01 18:39:39 +08:00
|
|
|
}
|
|
|
|
break
|
|
|
|
|
2023-06-14 19:36:20 +08:00
|
|
|
case '.scss':
|
|
|
|
case '.css':
|
|
|
|
{
|
2023-06-16 12:11:43 +08:00
|
|
|
let target = join(
|
|
|
|
dist,
|
|
|
|
'assets/',
|
|
|
|
pageDir,
|
|
|
|
it.name.replace(/\.scss$/, '.css')
|
|
|
|
)
|
2023-06-14 19:36:20 +08:00
|
|
|
if (it.ext === '.css') {
|
2023-06-16 14:19:32 +08:00
|
|
|
fs.cp(path, target)
|
2023-06-14 19:36:20 +08:00
|
|
|
} else {
|
2023-06-16 14:19:32 +08:00
|
|
|
let code = compileScss(path)
|
2024-07-25 15:24:06 +08:00
|
|
|
for (let fn of options.plugin) {
|
|
|
|
code = await fn('css', code)
|
|
|
|
}
|
2023-06-14 19:36:20 +08:00
|
|
|
fs.echo(code, target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
default:
|
2023-06-16 14:19:32 +08:00
|
|
|
fs.cp(path, join(dist, 'assets/', pageDir, it.name))
|
2023-06-14 19:36:20 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|