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(svelte-scoped): handle expressions in class attribute correctly #2505

Merged
merged 1 commit into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 27 additions & 15 deletions packages/vite/src/modes/svelte-scoped/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { type UnoGenerator, attributifyRE, escapeRegExp, expandVariantGroup } fr
import { wrapSelectorsWithGlobal } from './wrap-global'
import { hash } from './hash'

const classesRE = /class=(["'\`])([\S\s]+?)\1/g // class="mb-1"
const classesRE = /class=(["'\`])([^\{][\S\s]*?)\1/g // class="mb-1"
const classesExpressionsRE = /class=(["'\`])?(\{[\S\s]+?\})\1/g // class={clsx('mb-1')} or class="{clsx('mb-1')}"
const classesDirectivesRE = /class:([\S]+?)={/g // class:mb-1={foo}
const classDirectivesShorthandRE = /class:([^=>\s/]+)[{>\s/]/g // class:mb-1 (compiled to class:uno-1hashz={mb-1})
const classesFromInlineConditionalsRE = /'([\S\s]+?)'/g // { foo ? 'mt-1' : 'mt-2'}
const classesDirectivesShorthandRE = /class:([^=>\s/]+)[{>\s/]/g // class:mb-1 (compiled to class:uno-1hashz={mb-1})
const classesInsideExpressionsRE = /(["'\`])([\S\s]+?)\1/g // { foo ? 'mt-1' : "mt-2"}

export interface TransformSFCOptions {
/**
Expand Down Expand Up @@ -44,9 +45,9 @@ export async function transformSvelteSFC(code: string, id: string, uno: UnoGener
styles = css
}

const classes = [...code.matchAll(classesRE)]
const classes = [...code.matchAll(classesRE), ...code.matchAll(classesExpressionsRE)]
const classDirectives = [...code.matchAll(classesDirectivesRE)]
const classDirectivesShorthand = [...code.matchAll(classDirectivesShorthandRE)]
const classDirectivesShorthand = [...code.matchAll(classesDirectivesShorthandRE)]

const originalShortcuts = uno.config.shortcuts
const shortcuts: Record<string, string[]> = {}
Expand Down Expand Up @@ -103,24 +104,35 @@ export async function transformSvelteSFC(code: string, id: string, uno: UnoGener
const className = queueCompiledClass(known)
return [className, ...replacements].join(' ')
}

const processedMap = new Set()

for (const match of classes) {
let body = expandVariantGroup(match[2].trim())

const inlineConditionals = [...body.matchAll(classesFromInlineConditionalsRE)]
for (const conditional of inlineConditionals) {
const replacement = await sortKnownAndUnknownClasses(conditional[1].trim())
if (replacement)
body = body.replace(conditional[0], `'${replacement}'`)
let replaced = false

const expressions = [...body.matchAll(classesInsideExpressionsRE)]
for (const expression of expressions) {
const replacement = await sortKnownAndUnknownClasses(expression[2].trim())
if (replacement) {
body = body.replace(expression[2], replacement)
replaced = true
}
}

const replacement = await sortKnownAndUnknownClasses(body)
if (replacement) {
const start = match.index! + 7
const end = match.index! + match[0].length - 1
processedMap.add(start)
s.overwrite(start, end, replacement)
body = body.replace(body, replacement)
replaced = true
}

if (!replaced)
continue

const start = match.index! + (match[1] ? 7 : 6)
const end = match.index! + match[0].length - (match[1] ? 1 : 0)
processedMap.add(start)
s.overwrite(start, end, body)
}

for (const match of classDirectives) {
Expand Down
70 changes: 70 additions & 0 deletions test/svelte-scoped.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,76 @@ describe('svelte-scoped', () => {
`)
})

test('handles classes in inline expressions', async () => {
const result = await transform(`
<span class={classnames('text-red-500', foo ? "font-bold" : 'font-medium', 'foo', bar && 'hover:(bg-blue-500 text-white) baz')}>Hello</span>`.trim())
expect(result).toMatchInlineSnapshot(`
"<span
class={classnames(
\\"uno-ik91av\\",
foo ? \\"uno-k2ufqh\\" : \\"uno-wgrcwx\\",
\\"foo\\",
bar && \\"uno-484hzz baz\\"
)}>Hello</span
>

<style>
:global(.uno-484hzz:hover) {
--un-bg-opacity: 1;
background-color: rgba(59, 130, 246, var(--un-bg-opacity));
--un-text-opacity: 1;
color: rgba(255, 255, 255, var(--un-text-opacity));
}
:global(.uno-k2ufqh) {
font-weight: 700;
}
:global(.uno-wgrcwx) {
font-weight: 500;
}
:global(.uno-ik91av) {
--un-text-opacity: 1;
color: rgba(239, 68, 68, var(--un-text-opacity));
}
</style>
"
`)
})

test('handles classes in quoted inline expressions', async () => {
const result = await transform(`
<span class="{classnames('text-red-500', foo ? 'font-bold' : 'font-medium', 'foo', bar && 'hover:(bg-blue-500 text-white) baz')}">Hello</span>`.trim())
expect(result).toMatchInlineSnapshot(`
"<span
class={classnames(
\\"uno-ik91av\\",
foo ? \\"uno-k2ufqh\\" : \\"uno-wgrcwx\\",
\\"foo\\",
bar && \\"uno-484hzz baz\\"
)}>Hello</span
>

<style>
:global(.uno-484hzz:hover) {
--un-bg-opacity: 1;
background-color: rgba(59, 130, 246, var(--un-bg-opacity));
--un-text-opacity: 1;
color: rgba(255, 255, 255, var(--un-text-opacity));
}
:global(.uno-k2ufqh) {
font-weight: 700;
}
:global(.uno-wgrcwx) {
font-weight: 500;
}
:global(.uno-ik91av) {
--un-text-opacity: 1;
color: rgba(239, 68, 68, var(--un-text-opacity));
}
</style>
"
`)
})

test('no tokens found returns undefined', async () => {
const result = await transform(`
<div class="foo" />
Expand Down