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" },