From 5497bd18fae37bfd3913edb27554f3ca87c93289 Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 2 Mar 2023 16:55:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E6=B3=A2bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compile-vue.js | 32 ++++++++++++++++++++++---------- lib/dev.js | 16 +++++++--------- package.json | 2 +- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/lib/compile-vue.js b/lib/compile-vue.js index 20f56c9..40c0d03 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -72,6 +72,7 @@ export function compileScss(file, mini = true) { return scss.compileString(file, { style, ...OPTIONS }).css.trim() } } catch (err) { + console.log('compile scss: ', file) console.error(err) } } @@ -88,6 +89,14 @@ export function parseJs( isBuild ) { let fixedStyle = '\n\n' + let ASSETS_JS = '/$ASSETS_JS/' + let ASSETS_CSS = '/$ASSETS_CSS/' + + if (isBuild) { + ASSETS_JS = '/assets/js/' + ASSETS_CSS = '/assets/css/' + } + code = Es.transformSync(code).code || '' return ( code @@ -96,7 +105,7 @@ export function parseJs( /import ([\w\W]*?) from (["'])(.*?)\2/g, function (m, alias, q, name) { if (name.startsWith('@/')) { - name = name.replace('@/', urlJoin(DEPLOY_PATH, '/assets/js/')) + name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_JS)) } if (!imports[name]) { @@ -105,14 +114,14 @@ export function parseJs( if (IS_MPA) { name = `${currentPage}/` + name } - name = urlJoin(DEPLOY_PATH, '/assets/js/', name) + name = urlJoin(DEPLOY_PATH, ASSETS_JS, name) } } else if ( name.startsWith('/') && !name.startsWith('//') && - !name.startsWith(urlJoin(DEPLOY_PATH, '/assets/js/')) + !name.startsWith(urlJoin(DEPLOY_PATH, ASSETS_JS)) ) { - name = name.replace(/^\//, urlJoin(DEPLOY_PATH, '/assets/js/')) + name = name.replace(/^\//, urlJoin(DEPLOY_PATH, ASSETS_JS)) } if (!name.endsWith('.js') && !name.endsWith('.vue')) { @@ -133,6 +142,9 @@ export function parseJs( if (isBuild) { name = name.replace(/\.vue$/, '.js') } + if (name.startsWith('@/')) { + name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_JS)) + } return `import('${name}')` }) .replace(/import (["'])(.*?)\1/g, function (m, q, name) { @@ -154,7 +166,7 @@ export function parseJs( )}'` } else { if (name.startsWith('@/')) { - name = name.replace('@/', urlJoin(DEPLOY_PATH, '/assets/js/')) + name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_JS)) } // if (!imports[name]) { @@ -163,14 +175,14 @@ export function parseJs( if (IS_MPA) { name = `${currentPage}/` + name } - name = urlJoin(DEPLOY_PATH, '/assets/js/', name) + name = urlJoin(DEPLOY_PATH, ASSETS_JS, name) } } else if ( name.startsWith('/') && !name.startsWith('//') && - !name.startsWith(urlJoin(DEPLOY_PATH, '/assets/js/')) + !name.startsWith(urlJoin(DEPLOY_PATH, ASSETS_JS)) ) { - name = name.replace(/^\//, urlJoin(DEPLOY_PATH, '/assets/js/')) + name = name.replace(/^\//, urlJoin(DEPLOY_PATH, ASSETS_JS)) } if (!name.endsWith('.js') && !name.endsWith('.vue')) { @@ -198,7 +210,7 @@ export function compileVue(file, imports, options = {}, isBuild) { let js = code.match(JS_EXP) let scss = [...code.matchAll(STYLE_EXP)] - let html = code.match(HTML_EXP) + let html = code.match(HTML_EXP) || ['', ''] let hash = md5(file) let scopeId = 'data-' + hash @@ -232,7 +244,7 @@ export function compileVue(file, imports, options = {}, isBuild) { isCustomElement: tag => tag.startsWith('wc-') }).code.replace('export function render', 'function render') - output += html + output += html + '\n\n' if (CACHE[file]) { CACHE[file] = { diff --git a/lib/dev.js b/lib/dev.js index 4159624..679de8f 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -84,7 +84,9 @@ export default async function createServer(root = '', conf = {}) { pagesDir = dirname(conf.pages[pageName].entry) } else { if (currentPage) { - ext = pathname.at(-1).split('.').pop() + let tmp = pathname.at(-1).split('.') + // 修正history路由时的访问 + ext = tmp.length > 1 ? tmp.pop() : 'html' pageName = currentPage } else { pageName = Object.keys(conf.pages).pop() @@ -104,10 +106,6 @@ export default async function createServer(root = '', conf = {}) { ext = 'html' } } - // 修正history路由时的访问 - if (pathname === ext) { - ext = 'html' - } for (let k in COMMON_HEADERS) { res.setHeader(k, COMMON_HEADERS[k]) @@ -145,7 +143,7 @@ export default async function createServer(root = '', conf = {}) { case 'vue': { - let rpath = pathname.replace(/^assets\/js\//, '') + let rpath = pathname.replace(/\$ASSETS_JS\//, '') let file if (IS_MPA) { @@ -194,8 +192,8 @@ export default async function createServer(root = '', conf = {}) { let file = join( SOURCE_DIR, pathname - .replace(/^assets\/css\//, '') - .replace(/^assets\/js\//, '') + .replace(/\$ASSETS_CSS\//, '') + .replace(/\$ASSETS_JS\//, '') ) code = compileScss(file) res.setHeader('content-type', MIME_TYPES.css) @@ -204,7 +202,7 @@ export default async function createServer(root = '', conf = {}) { case 'js': { - pathname = pathname.replace(/^assets\/js\//, '') + pathname = pathname.replace(/\$ASSETS_JS\//, '') let file if (IS_MPA) { if (pathname.startsWith(currentPage)) { diff --git a/package.json b/package.json index 622cd76..bf34c86 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "fite", "type": "module", - "version": "0.3.2", + "version": "0.3.3", "bin": { "fite": "index.js" },