diff --git a/lib/compile-vue.js b/lib/compile-vue.js index b1a5f08..aca6ecb 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -30,12 +30,8 @@ function md5(str = '') { } function scopeCss(css = '', hash) { - let rules = css.matchAll(CSS_SHEET_EXP) - - return [...rules] - .map(r => { - let selector = r[1] - let style = r[2] + return css.replace(CSS_SHEET_EXP, (m, selector) => { + if (!selector.startsWith('@')) { selector = selector.split(',') selector = selector @@ -51,10 +47,9 @@ function scopeCss(css = '', hash) { return tmp.join(' ') }) .join(', ') - - return selector + ` {${style}}` - }) - .join('\n') + } + return selector + '{' + }) } /** @@ -224,7 +219,7 @@ export function compileVue(file, imports, options = {}, isBuild) { let scoped = it[0].includes('scoped') let css = compileScss(it[1]) if (scoped) { - return scopeCss(css, hash) + css = scopeCss(css, hash) } return css }) diff --git a/lib/constants.js b/lib/constants.js index 013bf77..5cc139b 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -8,8 +8,7 @@ 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' @@ -20,10 +19,15 @@ export const HMR_SCRIPT = ` var ws = new WebSocket(\`ws\${location.protocol === 'https:' ? 's' : ''}://\${location.host}/ws-vue-live\`) ws.addEventListener('open', function (r) { + if(vue_live_hmr.closed){ + delete vue_live_hmr.closed + location.reload() + } console.log('vue-live hmr ready...') }) ws.addEventListener('close', function(){ + vue_live_hmr.closed = true setTimeout(vue_live_hmr, 2000) }) diff --git a/package.json b/package.json index c56b2e4..7590c57 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@bytedo/vue-live", "type": "module", - "version": "0.2.0", + "version": "0.2.1", "bin": { "vue-live": "index.js" },