From 18072c4887a01576504c4c8534d785070ae0144f Mon Sep 17 00:00:00 2001 From: yutent Date: Fri, 17 Mar 2023 17:56:05 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=89=93=E5=8C=85=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compile-vue.js | 26 ++++++++++++-------------- lib/dev.js | 23 ++++++++++------------- lib/prod.js | 16 +++++----------- package.json | 2 +- 4 files changed, 28 insertions(+), 39 deletions(-) diff --git a/lib/compile-vue.js b/lib/compile-vue.js index df90b61..d2611ba 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -89,12 +89,10 @@ export function parseJs( isBuild ) { let fixedStyle = '\n\n' - let ASSETS_JS = '/@/' - let ASSETS_CSS = '/@/' + let ASSETS_DIR = '/@/' if (isBuild) { - ASSETS_JS = '/assets/js/' - ASSETS_CSS = '/assets/css/' + ASSETS_DIR = '/assets/' // + (IS_MPA ? 'pages/' : '') } code = Es.transformSync(code).code || '' @@ -105,23 +103,23 @@ 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_DIR)) } if (!imports[name]) { if (name.startsWith('./') || name.startsWith('../')) { if (IS_ENTRY) { if (IS_MPA) { - name = `${currentPage}/` + name + name = `pages/${currentPage}/` + name } - name = urlJoin(DEPLOY_PATH, ASSETS_JS, name) + name = urlJoin(DEPLOY_PATH, ASSETS_DIR, name) } } else if ( name.startsWith('/') && !name.startsWith('//') && - !name.startsWith(urlJoin(DEPLOY_PATH, ASSETS_JS)) + !name.startsWith(urlJoin(DEPLOY_PATH, ASSETS_DIR)) ) { - name = name.replace(/^\//, urlJoin(DEPLOY_PATH, ASSETS_JS)) + name = name.replace(/^\//, urlJoin(DEPLOY_PATH, ASSETS_DIR)) } if (!name.endsWith('.js') && !name.endsWith('.vue')) { @@ -143,7 +141,7 @@ export function parseJs( name = name.replace(/\.vue$/, '.js') } if (name.startsWith('@/')) { - name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_JS)) + name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR)) } return `import('${name}')` }) @@ -166,7 +164,7 @@ export function parseJs( )}'` } else { if (name.startsWith('@/')) { - name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_JS)) + name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR)) } // if (!imports[name]) { @@ -175,14 +173,14 @@ export function parseJs( if (IS_MPA) { name = `${currentPage}/` + name } - name = urlJoin(DEPLOY_PATH, ASSETS_JS, name) + name = urlJoin(DEPLOY_PATH, ASSETS_DIR, name) } } else if ( name.startsWith('/') && !name.startsWith('//') && - !name.startsWith(urlJoin(DEPLOY_PATH, ASSETS_JS)) + !name.startsWith(urlJoin(DEPLOY_PATH, ASSETS_DIR)) ) { - name = name.replace(/^\//, urlJoin(DEPLOY_PATH, ASSETS_JS)) + name = name.replace(/^\//, urlJoin(DEPLOY_PATH, ASSETS_DIR)) } if (!name.endsWith('.js') && !name.endsWith('.vue')) { diff --git a/lib/dev.js b/lib/dev.js index 1f228cc..a9a3eca 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -150,8 +150,7 @@ export default async function createServer(root = '', conf = {}) { if (IS_MPA) { if (rpath.startsWith(currentPage)) { - rpath = rpath.slice(currentPage.length) - file = join(pagesDir, rpath) + file = join(pagesDir, rpath.slice(currentPage.length)) } else { file = join(SOURCE_DIR, rpath) } @@ -199,27 +198,25 @@ export default async function createServer(root = '', conf = {}) { case 'js': { - pathname = pathname.replace(/@\//, '') + let rpath = pathname.replace(/@\//, '') let file + if (IS_MPA) { - if (pathname.startsWith(currentPage)) { - file = join(pagesDir, pathname.slice(currentPage.length)) + if (rpath.startsWith(currentPage)) { + file = join(pagesDir, rpath.slice(currentPage.length)) } else { - file = join(pagesDir, pathname) - } - if (!fs.isfile(file)) { - file = join(SOURCE_DIR, pathname) + file = join(SOURCE_DIR, rpath) } } else { - file = join(SOURCE_DIR, pathname) + file = join(SOURCE_DIR, rpath) } if (fs.isfile(file)) { code = fs.cat(file) - } else if (fs.isfile(join(PUBLIC_DIR, pathname))) { - code = fs.cat(join(PUBLIC_DIR, pathname)) + } else if (fs.isfile(join(PUBLIC_DIR, rpath))) { + code = fs.cat(join(PUBLIC_DIR, rpath)) } else { - friendlyErrors(pathname, ext) + friendlyErrors(rpath, ext) res.writeHead(404, 'Not Found') res.end('') return diff --git a/lib/prod.js b/lib/prod.js index 612295c..2b6bb03 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -49,6 +49,8 @@ export default function compile(root = '', dist = '', conf = {}) { console.log(' 解析 %s ...', it.name) + let pageDir = IS_MPA && currentPage ? `pages/${currentPage}` : '' + switch (it.ext) { case '.vue': { @@ -62,12 +64,7 @@ export default function compile(root = '', dist = '', conf = {}) { Es.transform(code, { minify: true }).then(r => { fs.echo( r.code, - join( - dist, - 'assets/js/', - IS_MPA ? currentPage : '', - it.name.replace(/\.vue$/, '.js') - ) + join(dist, 'assets/', pageDir, it.name.replace(/\.vue$/, '.js')) ) }) } @@ -84,10 +81,7 @@ export default function compile(root = '', dist = '', conf = {}) { true ) Es.transform(code, { minify: true }).then(r => { - fs.echo( - r.code, - join(dist, 'assets/js/', IS_MPA ? currentPage : '', it.name) - ) + fs.echo(r.code, join(dist, 'assets/', pageDir, it.name)) }) } break @@ -97,7 +91,7 @@ export default function compile(root = '', dist = '', conf = {}) { { let code = compileScss(it.path) if (!it.name.startsWith('assets')) { - it.name = 'assets/js/' + it.name + it.name = 'assets/' + it.name } fs.echo(code, join(dist, it.name.replace(/\.scss$/, '.css'))) } diff --git a/package.json b/package.json index 6ffad3d..593da87 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "fite", "type": "module", - "version": "0.3.5", + "version": "0.4.0", "bin": { "fite": "index.js" },