From d5dcd44fec70919e28b57b86f1fd6db843e5c7ab Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 27 Apr 2023 17:13:58 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84style[scoped]=E7=9A=84?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compile-vue.js | 39 ++++++++++++++++++++++++--------------- lib/constants.js | 2 +- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/compile-vue.js b/lib/compile-vue.js index af013f3..74bfc43 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -38,6 +38,16 @@ function md5(str = '') { return sum.digest('hex').slice(0, 8) } +function parseVDeep(curr, val, scoped) { + let res = V_DEEP.exec(curr) + if (res) { + scoped && (val = val.replace(/\[data\-[^\]]+\]/g, '')) + return `${res[1] + res[2]} ${val}` + } else { + return `${curr} ${val}` + } +} + function scopeCss(css = '', hash) { return css.replace(CSS_SHEET_EXP, (m, selector) => { if (!selector.startsWith('@')) { @@ -50,29 +60,28 @@ function scopeCss(css = '', hash) { return s } let tmp = s.split(' ') - let last = tmp.pop() - if (last.includes(':')) { - let res = V_DEEP.exec(last) - if (res) { - if (tmp.length) { - last = tmp.pop() - last += `[data-${hash}] ` + let output = '' + let last + let scoped = false + + while ((last = tmp.pop())) { + if (scoped) { + if (last.startsWith(':')) { + output = parseVDeep(last, output, true) } else { - last = res[1] + output = `${last} ${output}` } } else { if (last.startsWith(':')) { - let _prev = tmp.pop() - last = `${_prev}[data-${hash}] ` + last + output = parseVDeep(last, output) } else { - last = last.replace(':', `[data-${hash}]:`) + scoped = true + output = `${last}[data-${hash}] ${output}` } } - } else { - last += `[data-${hash}]` } - tmp.push(last) - return tmp.join(' ') + + return output }) .join(', ') } diff --git a/lib/constants.js b/lib/constants.js index 1fed485..301b85c 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -7,7 +7,7 @@ export const JS_EXP = /]*?>([\w\W]*?)<\/script>/ export const STYLE_EXP = /]*?)>([\w\W]*?)<\/style>/g export const HTML_EXP = /]*?>([\w\W]*?)\n<\/template>/ -export const V_DEEP = /:deep\(([^)]*?)\)/ +export const V_DEEP = /:deep\(([^)]*?)\)(.*)/ export const CSS_SHEET_EXP = /([%@\w\.,#\-:>\+\~\|\(\)\[\]"'\=\s]+)\{/g export const PERCENT_EXP = /^\d+%$/