2022-10-11 19:31:04 +08:00
|
|
|
import fs from 'iofs'
|
2023-05-10 21:22:02 +08:00
|
|
|
import { join, 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'
|
|
|
|
|
|
|
|
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()
|
2023-02-24 15:45:55 +08:00
|
|
|
let template = fs.cat(join(process.env.PWD, 'index.html')).toString()
|
2022-10-11 19:31:04 +08:00
|
|
|
|
2023-02-24 15:45:55 +08:00
|
|
|
let list = fs
|
|
|
|
.ls(SOURCE_DIR, true)
|
|
|
|
.map(it => ({
|
2023-02-16 15:32:17 +08:00
|
|
|
name: it.slice(SOURCE_DIR.length + 1),
|
2022-10-11 19:31:04 +08:00
|
|
|
path: it,
|
|
|
|
ext: parse(it).ext
|
|
|
|
}))
|
2023-03-28 15:41:01 +08:00
|
|
|
.filter(it => fs.isfile(it.path) && it.ext !== '')
|
|
|
|
|
2023-02-24 15:45:55 +08:00
|
|
|
let compileFiles = function (currentPage, page, files) {
|
2023-05-11 12:25:07 +08:00
|
|
|
let options = { IS_MPA, currentPage, SOURCE_DIR, DEPLOY_PATH }
|
|
|
|
|
2023-02-24 15:45:55 +08:00
|
|
|
for (let it of files) {
|
2022-10-11 19:31:04 +08:00
|
|
|
// 入口文件, 特殊处理
|
2023-02-24 15:45:55 +08:00
|
|
|
if (page && it.path === page.entry) {
|
2022-10-11 19:31:04 +08:00
|
|
|
let entry = fs.cat(page.entry).toString()
|
2023-02-24 15:45:55 +08:00
|
|
|
|
2023-05-11 12:25:07 +08:00
|
|
|
entry = parseJs(entry, conf.imports, { ...options, IS_ENTRY: true })
|
2022-10-11 19:31:04 +08:00
|
|
|
|
2023-05-11 12:25:07 +08:00
|
|
|
let code = parseHtml(template, { page, imports: conf.imports, entry })
|
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
|
|
|
|
2023-03-17 17:56:05 +08:00
|
|
|
let pageDir = IS_MPA && currentPage ? `pages/${currentPage}` : ''
|
|
|
|
|
2022-10-11 19:31:04 +08:00
|
|
|
switch (it.ext) {
|
|
|
|
case '.vue':
|
|
|
|
{
|
2023-05-11 12:25:07 +08:00
|
|
|
let code = compileVue(it.path, conf.imports, options)
|
2022-10-11 19:31:04 +08:00
|
|
|
|
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,
|
2023-03-17 17:56:05 +08:00
|
|
|
join(dist, 'assets/', pageDir, it.name.replace(/\.vue$/, '.js'))
|
2023-01-13 11:40:53 +08:00
|
|
|
)
|
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-05-11 12:25:07 +08:00
|
|
|
code = parseJs(code + '', conf.imports, options)
|
2022-11-03 18:22:42 +08:00
|
|
|
Es.transform(code, { minify: true }).then(r => {
|
2023-03-17 17:56:05 +08:00
|
|
|
fs.echo(r.code, join(dist, 'assets/', pageDir, it.name))
|
2022-11-03 18:22:42 +08:00
|
|
|
})
|
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')) {
|
2023-03-17 17:56:05 +08:00
|
|
|
it.name = 'assets/' + it.name
|
2023-01-31 19:17:38 +08:00
|
|
|
}
|
|
|
|
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-24 15:45:55 +08:00
|
|
|
}
|
|
|
|
|
2023-05-10 21:22:02 +08:00
|
|
|
// 优先处理静态目录, 之后的源码目录中, 以便如果有产生相同的文件名, 则覆盖静态目录中的文件
|
|
|
|
if (fs.isdir(PUBLIC_DIR)) {
|
|
|
|
console.log('\n正在处理静态资源 ...')
|
|
|
|
fs.ls(PUBLIC_DIR, true).forEach(it => {
|
|
|
|
let ext = parse(it).ext
|
|
|
|
|
|
|
|
if (ext === '') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.isfile(it)) {
|
|
|
|
let name = it.slice(PUBLIC_DIR.length + 1)
|
|
|
|
console.log(' 复制 %s ...', name)
|
|
|
|
fs.cp(it, join(dist, name))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-24 15:45:55 +08:00
|
|
|
for (let currentPage in conf.pages) {
|
|
|
|
let page = conf.pages[currentPage]
|
|
|
|
let dir = dirname(page.entry)
|
2023-02-24 16:08:18 +08:00
|
|
|
let files = list
|
|
|
|
if (IS_MPA) {
|
|
|
|
files = []
|
|
|
|
fs.ls(dir, true).forEach(it => {
|
|
|
|
if (fs.isdir(it)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let idx = list.findIndex(_ => _.path === it)
|
2023-03-28 15:41:01 +08:00
|
|
|
let name = it.slice(dir.length + 1)
|
|
|
|
let ext = parse(name).ext
|
|
|
|
|
|
|
|
if (ext === '') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-24 16:08:18 +08:00
|
|
|
list.splice(idx, 1)
|
|
|
|
|
|
|
|
files.push({
|
2023-03-28 15:41:01 +08:00
|
|
|
name,
|
2023-02-24 16:08:18 +08:00
|
|
|
path: it,
|
2023-03-28 15:41:01 +08:00
|
|
|
ext
|
2023-02-24 16:08:18 +08:00
|
|
|
})
|
2023-02-24 15:45:55 +08:00
|
|
|
})
|
2023-02-24 16:08:18 +08:00
|
|
|
}
|
2023-02-24 15:45:55 +08:00
|
|
|
console.log('正在生成 %s ...', `${currentPage}.html`)
|
|
|
|
compileFiles(currentPage, page, files)
|
|
|
|
}
|
|
|
|
|
2023-02-24 16:08:18 +08:00
|
|
|
if (IS_MPA) {
|
|
|
|
console.log('\n正在解析公共依赖 ...')
|
|
|
|
compileFiles('', null, list)
|
|
|
|
}
|
2023-02-24 15:45:55 +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
|
|
|
}
|