计时改为进程退出时

pull/1/head
yutent 2023-05-29 15:24:20 +08:00
parent 28838bb848
commit 45ac8e8b3d
1 changed files with 40 additions and 36 deletions

View File

@ -18,31 +18,39 @@ export default function compile(root = '', dist = '', conf = {}) {
const INJECT_SCSS = readFile(conf.inject?.scss)
const LEGACY_MODE = !!conf.legacy
conf.inject = conf.inject || { scss: '' }
let timeStart = Date.now()
let template = fs.cat(join(process.cwd(), 'index.html')).toString()
let list = {}
let list = fs
.ls(SOURCE_DIR, true)
.map(it => ({
name: it.slice(SOURCE_DIR.length + 1),
path: it,
ext: parse(it).ext
}))
.filter(it => {
if (fs.isfile(it.path) && it.ext !== '') {
if (IS_MPA && it.name.startsWith('pages/')) {
if (PAGES_PREFIX.some(it => it.startsWith(it.name))) {
return true
} else {
return false
}
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 !== '') {
if (IS_MPA && it.name.startsWith('pages/')) {
if (PAGES_PREFIX.some(it => it.startsWith(it.name))) {
return (list[path] = it)
} else {
return
}
return it.path !== conf.inject?.scss
}
return false
})
if (it.path === conf.inject.scss) {
return
}
list[path] = it
}
})
let compileFiles = function (currentPage, page, files) {
let options = {
@ -55,7 +63,8 @@ export default function compile(root = '', dist = '', conf = {}) {
isCustomElement: conf.isCustomElement || isCustomElement
}
for (let it of files) {
for (let k in files) {
let it = files[k]
// 入口文件, 特殊处理
if (page && it.path === page.entry) {
let entry = fs.cat(page.entry).toString()
@ -73,7 +82,7 @@ export default function compile(root = '', dist = '', conf = {}) {
continue
}
console.log(' 解析 %s ...', it.name)
page === null && console.log(' 解析 %s ...', it.name)
let pageDir = IS_MPA && currentPage ? `pages/${currentPage}` : ''
@ -150,38 +159,33 @@ export default function compile(root = '', dist = '', conf = {}) {
let dir = dirname(page.entry)
let files = list
if (IS_MPA) {
files = []
fs.ls(dir, true).forEach(it => {
if (fs.isdir(it)) {
files = {}
fs.ls(dir, true).forEach(path => {
if (fs.isdir(path)) {
return
}
let idx = list.findIndex(_ => _?.path === it)
let name = it.slice(dir.length + 1)
let name = path.slice(dir.length + 1)
let ext = parse(name).ext
if (ext === '') {
return
}
list[idx] = null
files.push({
name,
path: it,
ext
})
delete list[path]
files[path] = { name, path, ext }
})
}
console.log('正在生成 %s ...', `${currentPage}.html`)
compileFiles(currentPage, page, files)
}
list = list.filter(it => it)
if (IS_MPA) {
console.log('\n正在解析公共依赖 ...')
compileFiles('', null, list)
}
console.log('\n页面处理完成, 耗时 %ss\n', (Date.now() - timeStart) / 1000)
process.on('exit', _ => {
console.log('\n页面处理完成, 耗时 %ss\n', (Date.now() - timeStart) / 1000)
})
}