计时改为进程退出时

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