fite/lib/prod.js

198 lines
4.9 KiB
JavaScript
Raw Permalink Normal View History

2023-05-22 15:06:33 +08:00
import { join, dirname, parse } from 'node:path'
2022-10-11 19:31:04 +08:00
import fs from 'iofs'
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'
2023-05-22 15:06:33 +08:00
import { isCustomElement } from './utils.js'
2022-10-11 19:31:04 +08:00
2023-05-30 17:06:53 +08:00
const IS_WIN = process.platform === 'win32'
const PREFIX = IS_WIN ? 'pages\\' : 'pages/'
2023-05-16 14:17:40 +08:00
function readFile(file) {
return (file && fs.cat(file)?.toString()) || ''
}
2023-05-29 15:45:21 +08:00
export default function compile(root = '', dist = '', conf = {}, verbose) {
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-05-30 17:06:53 +08:00
const PAGES_PREFIX = Object.keys(conf.pages).map(it =>
IS_WIN ? `${PREFIX + it}\\` : `${PREFIX + it}/`
)
2023-05-16 14:17:40 +08:00
const INJECT_SCSS = readFile(conf.inject?.scss)
const LEGACY_MODE = !!conf.legacy
2023-02-16 15:32:17 +08:00
2023-05-29 15:24:20 +08:00
conf.inject = conf.inject || { scss: '' }
2022-10-14 12:06:33 +08:00
let timeStart = Date.now()
2023-05-22 19:29:02 +08:00
let template = fs.cat(join(process.cwd(), 'index.html')).toString()
2023-05-29 15:24:20 +08:00
let list = {}
2022-10-11 19:31:04 +08:00
2023-05-29 15:24:20 +08:00
fs.ls(SOURCE_DIR, true).forEach(path => {
if (fs.isdir(path)) {
return
}
let name = path.slice(SOURCE_DIR.length + 1)
let it = {
path,
name,
ext: parse(name).ext
}
if (it.ext !== '') {
2023-05-30 17:06:53 +08:00
if (IS_MPA && it.name.startsWith(PREFIX)) {
2023-05-29 15:24:20 +08:00
if (PAGES_PREFIX.some(it => it.startsWith(it.name))) {
return (list[path] = it)
} else {
return
2023-05-15 15:17:55 +08:00
}
2023-05-29 15:24:20 +08:00
}
2023-05-15 15:17:55 +08:00
2023-05-29 15:24:20 +08:00
if (it.path === conf.inject.scss) {
return
2023-05-15 15:17:55 +08:00
}
2023-05-29 15:24:20 +08:00
list[path] = it
}
})
2023-03-28 15:41:01 +08:00
let compileFiles = function (currentPage, page, files) {
let options = {
IS_MPA,
currentPage,
SOURCE_DIR,
DEPLOY_PATH,
INJECT_SCSS,
2023-05-22 15:06:33 +08:00
LEGACY_MODE,
isCustomElement: conf.isCustomElement || isCustomElement
}
2023-05-29 15:24:20 +08:00
for (let k in files) {
let it = files[k]
2022-10-11 19:31:04 +08:00
// 入口文件, 特殊处理
if (page && it.path === page.entry) {
2022-10-11 19:31:04 +08:00
let entry = fs.cat(page.entry).toString()
entry = parseJs(entry, conf.imports, { ...options, IS_ENTRY: true })
2022-10-11 19:31:04 +08:00
let code = parseHtml(template, {
page,
imports: conf.imports,
entry,
LEGACY_MODE
})
2022-10-11 19:31:04 +08:00
fs.echo(code, join(dist, `${currentPage}.html`))
continue
}
2023-05-29 15:45:21 +08:00
verbose && 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':
{
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)
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 target = join(
dist,
'assets/',
it.name.replace(/\.scss$/, '.css')
2023-05-22 17:25:36 +08:00
)
if (it.ext === '.css') {
fs.cp(it.path, target)
} else {
let code = compileScss(it.path)
fs.echo(code, target)
}
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
}
}
}
// 优先处理静态目录, 之后的源码目录中, 以便如果有产生相同的文件名, 则覆盖静态目录中的文件
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)
2023-05-29 15:45:21 +08:00
verbose && console.log(' 复制 %s ...', name)
fs.cp(it, join(dist, name))
}
})
}
2023-05-29 15:45:21 +08:00
console.log('')
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) {
2023-05-29 15:24:20 +08:00
files = {}
fs.ls(dir, true).forEach(path => {
if (fs.isdir(path)) {
2023-02-24 16:08:18 +08:00
return
}
2023-05-29 15:24:20 +08:00
let name = path.slice(dir.length + 1)
2023-03-28 15:41:01 +08:00
let ext = parse(name).ext
if (ext === '') {
return
}
2023-05-29 15:24:20 +08:00
delete list[path]
files[path] = { name, path, ext }
})
2023-02-24 16:08:18 +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-05-29 15:24:20 +08:00
process.on('exit', _ => {
console.log('\n页面处理完成, 耗时 %ss\n', (Date.now() - timeStart) / 1000)
})
2022-10-09 19:19:21 +08:00
}