|
| 1 | +import { BindingMetadata } from './types' |
| 2 | +import { SFCDescriptor } from './parseComponent' |
| 3 | +import { PluginCreator } from 'postcss' |
| 4 | +import hash from 'hash-sum' |
| 5 | +import { prefixIdentifiers } from './prefixIdentifiers' |
| 6 | + |
| 7 | +export const CSS_VARS_HELPER = `useCssVars` |
| 8 | + |
| 9 | +export function genCssVarsFromList( |
| 10 | + vars: string[], |
| 11 | + id: string, |
| 12 | + isProd: boolean, |
| 13 | + isSSR = false |
| 14 | +): string { |
| 15 | + return `{\n ${vars |
| 16 | + .map( |
| 17 | + key => `"${isSSR ? `--` : ``}${genVarName(id, key, isProd)}": (${key})` |
| 18 | + ) |
| 19 | + .join(',\n ')}\n}` |
| 20 | +} |
| 21 | + |
| 22 | +function genVarName(id: string, raw: string, isProd: boolean): string { |
| 23 | + if (isProd) { |
| 24 | + return hash(id + raw) |
| 25 | + } else { |
| 26 | + return `${id}-${raw.replace(/([^\w-])/g, '_')}` |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function normalizeExpression(exp: string) { |
| 31 | + exp = exp.trim() |
| 32 | + if ( |
| 33 | + (exp[0] === `'` && exp[exp.length - 1] === `'`) || |
| 34 | + (exp[0] === `"` && exp[exp.length - 1] === `"`) |
| 35 | + ) { |
| 36 | + return exp.slice(1, -1) |
| 37 | + } |
| 38 | + return exp |
| 39 | +} |
| 40 | + |
| 41 | +const vBindRE = /v-bind\s*\(/g |
| 42 | + |
| 43 | +export function parseCssVars(sfc: SFCDescriptor): string[] { |
| 44 | + const vars: string[] = [] |
| 45 | + sfc.styles.forEach(style => { |
| 46 | + let match |
| 47 | + // ignore v-bind() in comments /* ... */ |
| 48 | + const content = style.content.replace(/\/\*([\s\S]*?)\*\//g, '') |
| 49 | + while ((match = vBindRE.exec(content))) { |
| 50 | + const start = match.index + match[0].length |
| 51 | + const end = lexBinding(content, start) |
| 52 | + if (end !== null) { |
| 53 | + const variable = normalizeExpression(content.slice(start, end)) |
| 54 | + if (!vars.includes(variable)) { |
| 55 | + vars.push(variable) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + }) |
| 60 | + return vars |
| 61 | +} |
| 62 | + |
| 63 | +const enum LexerState { |
| 64 | + inParens, |
| 65 | + inSingleQuoteString, |
| 66 | + inDoubleQuoteString |
| 67 | +} |
| 68 | + |
| 69 | +function lexBinding(content: string, start: number): number | null { |
| 70 | + let state: LexerState = LexerState.inParens |
| 71 | + let parenDepth = 0 |
| 72 | + |
| 73 | + for (let i = start; i < content.length; i++) { |
| 74 | + const char = content.charAt(i) |
| 75 | + switch (state) { |
| 76 | + case LexerState.inParens: |
| 77 | + if (char === `'`) { |
| 78 | + state = LexerState.inSingleQuoteString |
| 79 | + } else if (char === `"`) { |
| 80 | + state = LexerState.inDoubleQuoteString |
| 81 | + } else if (char === `(`) { |
| 82 | + parenDepth++ |
| 83 | + } else if (char === `)`) { |
| 84 | + if (parenDepth > 0) { |
| 85 | + parenDepth-- |
| 86 | + } else { |
| 87 | + return i |
| 88 | + } |
| 89 | + } |
| 90 | + break |
| 91 | + case LexerState.inSingleQuoteString: |
| 92 | + if (char === `'`) { |
| 93 | + state = LexerState.inParens |
| 94 | + } |
| 95 | + break |
| 96 | + case LexerState.inDoubleQuoteString: |
| 97 | + if (char === `"`) { |
| 98 | + state = LexerState.inParens |
| 99 | + } |
| 100 | + break |
| 101 | + } |
| 102 | + } |
| 103 | + return null |
| 104 | +} |
| 105 | + |
| 106 | +// for compileStyle |
| 107 | +export interface CssVarsPluginOptions { |
| 108 | + id: string |
| 109 | + isProd: boolean |
| 110 | +} |
| 111 | + |
| 112 | +export const cssVarsPlugin: PluginCreator<CssVarsPluginOptions> = opts => { |
| 113 | + const { id, isProd } = opts! |
| 114 | + return { |
| 115 | + postcssPlugin: 'vue-sfc-vars', |
| 116 | + Declaration(decl) { |
| 117 | + // rewrite CSS variables |
| 118 | + const value = decl.value |
| 119 | + if (vBindRE.test(value)) { |
| 120 | + vBindRE.lastIndex = 0 |
| 121 | + let transformed = '' |
| 122 | + let lastIndex = 0 |
| 123 | + let match |
| 124 | + while ((match = vBindRE.exec(value))) { |
| 125 | + const start = match.index + match[0].length |
| 126 | + const end = lexBinding(value, start) |
| 127 | + if (end !== null) { |
| 128 | + const variable = normalizeExpression(value.slice(start, end)) |
| 129 | + transformed += |
| 130 | + value.slice(lastIndex, match.index) + |
| 131 | + `var(--${genVarName(id, variable, isProd)})` |
| 132 | + lastIndex = end + 1 |
| 133 | + } |
| 134 | + } |
| 135 | + decl.value = transformed + value.slice(lastIndex) |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | +cssVarsPlugin.postcss = true |
| 141 | + |
| 142 | +export function genCssVarsCode( |
| 143 | + vars: string[], |
| 144 | + bindings: BindingMetadata, |
| 145 | + id: string, |
| 146 | + isProd: boolean |
| 147 | +) { |
| 148 | + const varsExp = genCssVarsFromList(vars, id, isProd) |
| 149 | + return `_${CSS_VARS_HELPER}((_vm, _setup) => ${prefixIdentifiers( |
| 150 | + `(${varsExp})`, |
| 151 | + false, |
| 152 | + false, |
| 153 | + undefined, |
| 154 | + bindings |
| 155 | + )})` |
| 156 | +} |
| 157 | + |
| 158 | +// <script setup> already gets the calls injected as part of the transform |
| 159 | +// this is only for single normal <script> |
| 160 | +export function genNormalScriptCssVarsCode( |
| 161 | + cssVars: string[], |
| 162 | + bindings: BindingMetadata, |
| 163 | + id: string, |
| 164 | + isProd: boolean |
| 165 | +): string { |
| 166 | + return ( |
| 167 | + `\nimport { ${CSS_VARS_HELPER} as _${CSS_VARS_HELPER} } from 'vue'\n` + |
| 168 | + `const __injectCSSVars__ = () => {\n${genCssVarsCode( |
| 169 | + cssVars, |
| 170 | + bindings, |
| 171 | + id, |
| 172 | + isProd |
| 173 | + )}}\n` + |
| 174 | + `const __setup__ = __default__.setup\n` + |
| 175 | + `__default__.setup = __setup__\n` + |
| 176 | + ` ? (props, ctx) => { __injectCSSVars__();return __setup__(props, ctx) }\n` + |
| 177 | + ` : __injectCSSVars__\n` |
| 178 | + ) |
| 179 | +} |
0 commit comments