Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): should distinguish between normal script and script setup when processing css vars #3690

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/compiler-core/src/transforms/transformExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ export function processExpression(

const { inline, bindingMetadata } = context
const rewriteIdentifier = (raw: string, parent?: Node, id?: Identifier) => {
const type = hasOwn(bindingMetadata, raw) && bindingMetadata[raw]
const type =
bindingMetadata.__isScriptSetup !== false &&
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we narrow this behavior to only css vars processing?

hasOwn(bindingMetadata, raw) &&
bindingMetadata[raw]
if (inline) {
// x = y
const isAssignmentLVal =
Expand Down
23 changes: 23 additions & 0 deletions packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,26 @@ return { color, size, ref }

}"
`;

exports[`CSS vars injection w/ normal <script> binding analysis 1`] = `
"
const __default__ = {
setup() {
return {
size: ref('100px')
}
}
}

import { useCssVars as _useCssVars } from 'vue'
const __injectCSSVars__ = () => {
_useCssVars(_ctx => ({
\\"xxxxxxxx-size\\": (_ctx.size)
}))}
const __setup__ = __default__.setup
__default__.setup = __setup__
? (props, ctx) => { __injectCSSVars__();return __setup__(props, ctx) }
: __injectCSSVars__

export default __default__"
`;
24 changes: 24 additions & 0 deletions packages/compiler-sfc/__tests__/cssVars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ describe('CSS vars injection', () => {
assertCode(content)
})

test('w/ normal <script> binding analysis', () => {
const { content } = compileSFCScript(
`<script>
export default {
setup() {
return {
size: ref('100px')
}
}
}
</script>\n` +
`<style>
div {
font-size: v-bind(size);
}
</style>`
)
expect(content).toMatch(`_useCssVars(_ctx => ({
"${mockId}-size": (_ctx.size)
})`)
expect(content).toMatch(`import { useCssVars as _useCssVars } from 'vue'`)
assertCode(content)
})

test('w/ <script setup> binding analysis', () => {
const { content } = compileSFCScript(
`<script setup>
Expand Down