From 45ac8e8b3d58d7184f305d6ae018a5ce80b3c167 Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 29 May 2023 15:24:20 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A1=E6=97=B6=E6=94=B9=E4=B8=BA=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B=E9=80=80=E5=87=BA=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/prod.js | 76 ++++++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/lib/prod.js b/lib/prod.js index 60452e6..c239dd3 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -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) + }) }