From c1ccba61037d17c7f7eacdd9c28ece684e8e62da Mon Sep 17 00:00:00 2001 From: yutent Date: Sun, 12 Feb 2023 23:01:57 +0800 Subject: [PATCH] fixed bug --- lib/compile-vue.js | 28 ++++++++++++++++++++++++---- lib/dev.js | 6 +++++- lib/prod.js | 7 ++++++- package.json | 2 +- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/compile-vue.js b/lib/compile-vue.js index c708547..14670c6 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -8,6 +8,7 @@ import fs from 'iofs' import scss from '@bytedo/sass' import { createHash } from 'crypto' import Es from 'esbuild' +import { resolve } from 'path' import { JS_EXP, @@ -82,7 +83,7 @@ export function compileScss(file, mini = true) { export function parseJs( code = '', imports, - { IS_MPA, currentPage } = {}, + { IS_MPA, currentPage, IS_ENTRY } = {}, isBuild ) { let fixedStyle = '\n\n' @@ -97,9 +98,12 @@ export function parseJs( } if (!imports[name]) { - if (name.startsWith('./')) { - if (IS_MPA) { - name += `${currentPage}/` + if (name.startsWith('./') || name.startsWith('../')) { + if (IS_ENTRY) { + if (IS_MPA) { + name = `${currentPage}/` + name + } + name = resolve('/assets/js/', name) } } else if ( name.startsWith('/') && @@ -152,10 +156,26 @@ export function parseJs( } // if (!imports[name]) { + if (name.startsWith('./') || name.startsWith('../')) { + if (IS_ENTRY) { + if (IS_MPA) { + name = `${currentPage}/` + name + } + name = resolve('/assets/js/', name) + } + } else if ( + name.startsWith('/') && + !name.startsWith('//') && + !name.startsWith('/assets/js/') + ) { + name = name.replace(/^\//, '/assets/js/') + } + if (!name.endsWith('.js') && !name.endsWith('.vue')) { name += '.js' } } + return `import '${name}'` } }) + fixedStyle diff --git a/lib/dev.js b/lib/dev.js index fbfd380..abf1385 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -117,7 +117,11 @@ export default async function createServer(root = '', conf = {}) { let entry = fs.cat(page.entry).toString() let html = fs.cat(join(process.cwd(), 'index.html')).toString() - entry = parseJs(entry, conf.imports, { IS_MPA, currentPage }) + entry = parseJs(entry, conf.imports, { + IS_MPA, + currentPage, + IS_ENTRY: true + }) code = parseHtml(html, { page, imports: conf.imports, entry }) } diff --git a/lib/prod.js b/lib/prod.js index 52d921a..a08e24f 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -35,7 +35,12 @@ export default function compile(root = '', dist = '', conf = {}) { // 入口文件, 特殊处理 if (it.path === page.entry) { let entry = fs.cat(page.entry).toString() - entry = parseJs(entry, conf.imports, { IS_MPA, currentPage }, true) + entry = parseJs( + entry, + conf.imports, + { IS_MPA, currentPage, IS_ENTRY: true }, + true + ) let code = parseHtml(html, { page, imports: conf.imports, entry }, true) diff --git a/package.json b/package.json index 26e482b..4238628 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@bytedo/vue-live", "type": "module", - "version": "0.1.6", + "version": "0.1.7", "bin": { "vue-live": "index.js" },