From 567463447178ba4e442120ca2c65006dd2f6731e Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 20 Apr 2023 18:56:01 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E5=90=8D?= =?UTF-8?q?=E6=9B=B4=E6=8D=A2=E4=B8=BAfite.config.js;=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E4=B8=BA=E4=BA=8C=E7=BA=A7=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 2 +- lib/dev.js | 63 ++++++++++++++++++++++++++++++++-------------------- package.json | 2 +- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/index.js b/index.js index 15e2798..46cbd26 100755 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ import compile from './lib/prod.js' const WORK_SPACE = process.cwd() const IS_WINDOWS = process.platform === 'win32' -const CONFIG_FILE = normalize(join(WORK_SPACE, 'vue.live.js')) +const CONFIG_FILE = normalize(join(WORK_SPACE, 'fite.config.js')) const PROTOCOL = IS_WINDOWS ? 'file://' : '' const NODE_VERSION = +process.versions.node.split('.').slice(0, 2).join('.') diff --git a/lib/dev.js b/lib/dev.js index a9a3eca..65b004e 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -72,32 +72,47 @@ export default async function createServer(root = '', conf = {}) { } if (pathname) { - pathname = pathname.split('/') - - if (pathname[0].endsWith('.html')) { - pageName = pathname.shift() - - let tmp = pageName.split('.') - - ext = tmp.pop() - pageName = tmp.join('.') - - currentPage = pageName - pagesDir = dirname(conf.pages[pageName].entry) - } else { - if (currentPage) { - let tmp = pathname.at(-1).split('.') - // 修正history路由时的访问 - ext = tmp.length > 1 ? tmp.pop() : 'html' - pageName = currentPage - } else { - pageName = Object.keys(conf.pages).pop() - currentPage = pageName - pagesDir = dirname(conf.pages[pageName].entry) + // 这种情况是, 页面是子目录的情况 + if (pathname.includes('/') && pathname.endsWith('.html')) { + pageName = pathname.slice(0, -5) + if (conf.pages[pageName]) { ext = 'html' + currentPage = pageName + pagesDir = dirname(conf.pages[pageName]?.entry) } + } else { + pathname = pathname.split('/') + + if (pathname[0].endsWith('.html')) { + pageName = pathname.shift() + + let tmp = pageName.split('.') + + ext = tmp.pop() + pageName = tmp.join('.') + // 页面不存在时输出404, 避免进程崩溃退出 + if (!conf.pages[pageName]) { + res.writeHead(404, 'Not Found') + return res.end(`Oops!!! 404 Not Found`) + } + + currentPage = pageName + pagesDir = dirname(conf.pages[pageName]?.entry) + } else { + if (currentPage) { + let tmp = pathname.at(-1).split('.') + // 修正history路由时的访问 + ext = tmp.length > 1 ? tmp.pop() : 'html' + pageName = currentPage + } else { + pageName = Object.keys(conf.pages).pop() + currentPage = pageName + pagesDir = dirname(conf.pages[pageName].entry) + ext = 'html' + } + } + pathname = pathname.join('/') } - pathname = pathname.join('/') } else { if (IS_MPA) { isIndex = true @@ -128,7 +143,7 @@ export default async function createServer(root = '', conf = {}) { res.setHeader('content-type', MIME_TYPES.html) let page = conf.pages[pageName] - let entry = fs.cat(page.entry).toString() + let entry = fs.cat(page.entry)?.toString() let html = fs.cat(join(process.cwd(), 'index.html')).toString() entry = parseJs(entry, conf.imports, { diff --git a/package.json b/package.json index efcef80..87a0164 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "fite", "type": "module", - "version": "0.5.0", + "version": "0.6.0", "bin": { "fite": "index.js" },