Skip to content

Commit

Permalink
fix(vite): svelte-scoped: search attributify candidates only on templ…
Browse files Browse the repository at this point in the history
…ate (#2287)
  • Loading branch information
sibbng committed Mar 1, 2023
1 parent d92ce42 commit 9af4936
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
9 changes: 6 additions & 3 deletions packages/vite/src/modes/svelte-scoped/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,18 @@ export async function transformSvelteSFC(code: string, id: string, uno: UnoGener
s.overwrite(start, start + match[1].length, `${className}={${token}}`)
}

const { matched } = await uno.generate(code, { preflights: false, safelist: false, minify: true })
// search attributify candidates only on template
const templateCode = code
.replace(/<(script|style)[^>]*>[\s\S]*?<\/\1\s*>/g, match => Array(match.length).fill(' ').join(''))
const { matched } = await uno.generate(templateCode, { preflights: false, safelist: false, minify: true })

for (const token of matched) {
const match = token.match(attributifyRE)
if (match) {
const [,name, value] = match
if (!value) {
let start = 0
code.split(/([\s"'`;*]|:\(|\)"|\)\s)/g).forEach((i) => {
templateCode.split(/([\s"'`;*]|:\(|\)"|\)\s)/g).forEach((i) => {
const end = start + i.length
if (i === name && !processedMap.has(start)) {
const className = queueCompiledClass([name])
Expand All @@ -165,7 +168,7 @@ export async function transformSvelteSFC(code: string, id: string, uno: UnoGener
}
else {
const regex = new RegExp(`(${escapeRegExp(name)}=)(['"])[^\\2]*?${escapeRegExp(value)}[^\\2]*?\\2`, 'g')
for (const match of code.matchAll(regex)) {
for (const match of templateCode.matchAll(regex)) {
const escaped = match[1]
const body = match[0].slice(escaped.length)
let bodyIndex = body.match(`[\\b\\s'"]${escapeRegExp(value)}[\\b\\s'"]`)?.index ?? -1
Expand Down
4 changes: 4 additions & 0 deletions test/__snapshots__/svelte-scoped.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ exports[`svelte-scoped > everything 2`] = `
"
`;

exports[`svelte-scoped > search attributify candidates only on template 1`] = `undefined`;

exports[`svelte-scoped > search attributify candidates only on template 2`] = `undefined`;

exports[`svelte-scoped > shortcut with icon 1`] = `
"<span class=\\"logo\\" />
Expand Down
19 changes: 16 additions & 3 deletions test/svelte-scoped.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,13 @@ describe('svelte-scoped', () => {
})

test('handles backticks and single quotes', async () => {
const backticks = await transform('<span class=`font-bold` />', { format: false })
const backticks = await transform(`<script></script>
<span class=\`font-bold\` />
<style></style>`, { format: false })
expect(backticks).toMatchInlineSnapshot(`
"<span class=\`uno-k2ufqh\` />
<style>:global(.uno-k2ufqh){font-weight:700;}</style>"
"<script></script>
<span class=\`uno-k2ufqh\` />
<style>:global(.uno-k2ufqh){font-weight:700;}</style>"
`)
const singleQuotes = await transform(`
<span class='font-bold' />`.trim())
Expand Down Expand Up @@ -345,6 +348,16 @@ describe('svelte-scoped', () => {
expect(await transform(code, { combine: false })).toMatchSnapshot()
})

test('search attributify candidates only on template', async () => {
const code = `<script lang="ts">
$: visible
</script>
<Render {visible}></Render>`.trim()
expect(await transform(code)).toMatchSnapshot()
expect(await transform(code, { combine: false })).toMatchSnapshot()
})

test('everything', async () => {
const code = `
<div class="bg-red-500 sm:text-xl dark:hover:bg-green-500 transform scale-5" />
Expand Down

0 comments on commit 9af4936

Please sign in to comment.