diff --git a/lib/compile-vue.js b/lib/compile-vue.js index 5a1afe2..debc363 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -72,39 +72,51 @@ export function compileScss(file, mini = true) { * 主要是处理js的依赖引用 * @param code js代码 */ -export function parseJs(code = '', imports, { IS_MPA, currentPage } = {}, isBuild) { +export function parseJs( + code = '', + imports, + { IS_MPA, currentPage } = {}, + isBuild +) { let fixedStyle = '\n\n' return ( code - .replace(/import (.*?) from (["'])(.*?)\2/g, function (m, alias, q, name) { - if (name.startsWith('@/')) { - name = name.replace('@/', '/assets/js/') - } - - if (!imports[name]) { - if (name.startsWith('./')) { - name = name.replace('./', '/assets/js/') - if (IS_MPA) { - name += `${currentPage}/` - } - } else if (name.startsWith('/') && !name.startsWith('/assets/js/')) { - name = name.replace(/^\//, '/assets/js/') + .replace( + /import (.*?) from (["'])(.*?)\2/g, + function (m, alias, q, name) { + if (name.startsWith('@/')) { + name = name.replace('@/', '/assets/js/') } - if (!name.endsWith('.js') && !name.endsWith('.vue')) { - if (name.includes('components')) { - name += '.vue' - } else { - name += '.js' + if (!imports[name]) { + if (name.startsWith('./')) { + name = name.replace('./', '/assets/js/') + if (IS_MPA) { + name += `${currentPage}/` + } + } else if ( + name.startsWith('/') && + !name.startsWith('//') && + !name.startsWith('/assets/js/') + ) { + name = name.replace(/^\//, '/assets/js/') + } + + if (!name.endsWith('.js') && !name.endsWith('.vue')) { + if (name.includes('components')) { + name += '.vue' + } else { + name += '.js' + } } } + if (isBuild) { + name = name.replace(/\.vue$/, '.js') + } + return `import ${alias} from '${name}'` } - if (isBuild) { - name = name.replace(/\.vue$/, '.js') - } - return `import ${alias} from '${name}'` - }) + ) .replace(/import\((['"])(.*?)\1\)/g, function (m, q, name) { if (isBuild) { name = name.replace(/\.vue$/, '.js') @@ -178,7 +190,9 @@ export function compileVue(file, imports, options = {}, isBuild) { js += ` let stylesheet = new CSSStyleSheet() -stylesheet.path = '${file.slice(options.IS_MPA ? options.pagesDir.length : options.root.length)}' +stylesheet.path = '${file.slice( + options.IS_MPA ? options.pagesDir.length : options.root.length + )}' stylesheet.replaceSync(\`${scss.join(' ')}\`) document.adoptedStyleSheets.push(stylesheet) ` @@ -200,5 +214,8 @@ export function parseHtml(html, { page, imports, entry }) { .replace('{{keywords}}', page.keywords || '') .replace('{{description}}', page.description || '') .replace('{{importmap}}', JSON.stringify({ imports })) - .replace('', ``) + .replace( + '', + `` + ) } diff --git a/lib/constants.js b/lib/constants.js index d77fafe..f1a275f 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -8,7 +8,8 @@ export const JS_EXP = /]*?>([\w\W]*?)<\/script>/ export const STYLE_EXP = /]*?>([\w\W]*?)<\/style>/g export const HTML_EXP = /]*?>([\w\W]*?)<\/template>/ -export const CSS_SHEET_EXP = /([\w\.,#\-:>\+\~\|\(\)\[\]"'\=\s]+)\{([^\{\}]*?)\}/g +export const CSS_SHEET_EXP = + /([\w\.,#\-:>\+\~\|\(\)\[\]"'\=\s]+)\{([^\{\}]*?)\}/g export const COMMON_HEADERS = { 'Cache-Control': 'no-store' diff --git a/lib/dev.js b/lib/dev.js index 99938ec..a3d67ca 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -127,7 +127,12 @@ export default function createServer(root = '', conf = {}) { } // console.log('>>>>', file) - code = compileVue(file, conf.imports, { IS_MPA, currentPage, root, pagesDir }) + code = compileVue(file, conf.imports, { + IS_MPA, + currentPage, + root, + pagesDir + }) res.setHeader('content-type', MIME_TYPES.js) } break @@ -198,7 +203,17 @@ export default function createServer(root = '', conf = {}) { }) .on('listening', _ => { console.log('启动成功, 可通过以下地址访问') - console.log(' 本地: %s://%s:%d', USE_HTTPS ? 'https' : 'http', '127.0.0.1', PORT) - console.log(' 外网: %s://%s:%d\n', USE_HTTPS ? 'https' : 'http', DOMAIN, PORT) + console.log( + ' 本地: %s://%s:%d', + USE_HTTPS ? 'https' : 'http', + '127.0.0.1', + PORT + ) + console.log( + ' 外网: %s://%s:%d\n', + USE_HTTPS ? 'https' : 'http', + DOMAIN, + PORT + ) }) } diff --git a/lib/prod.js b/lib/prod.js index ae425d0..bcd3783 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -56,7 +56,10 @@ export default function compile(root = '', dist = '', conf = {}) { ) Es.transform(code, { minify: true }).then(r => { - fs.echo(r.code, join(dist, `assets/js/${it.name.split('.').shift()}.js`)) + fs.echo( + r.code, + join(dist, `assets/js/${it.name.split('.').shift()}.js`) + ) }) } break @@ -65,7 +68,12 @@ export default function compile(root = '', dist = '', conf = {}) { { let code = fs.cat(it.path) - code = parseJs(code + '', conf.imports, { IS_MPA, currentPage }, true) + code = parseJs( + code + '', + conf.imports, + { IS_MPA, currentPage }, + true + ) Es.transform(code, { minify: true }).then(r => { fs.echo(r.code, join(dist, `assets/js/${it.name}`)) }) diff --git a/package.json b/package.json index fa7c1db..c7ad3ae 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@bytedo/vue-live", "type": "module", - "version": "0.0.14", + "version": "0.0.15", "bin": { "vue-live": "index.js" },