Skip to content

Commit

Permalink
fix(core): fix for svelte extractor (#1830)
Browse files Browse the repository at this point in the history
  • Loading branch information
chu121su12 committed Nov 2, 2022
1 parent 73d00d6 commit 52a71f4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/extractors/svelte.ts
@@ -1,14 +1,16 @@
import type { Extractor } from '../types'
import { splitCode } from './split'

const rightTrimRe = /=$/

export const extractorSvelte: Extractor = {
name: 'svelte',
order: 0,
extract({ code, id }) {
let result = splitCode(code)
if (id && id.endsWith('.svelte')) {
result = result.map((r) => {
return r.startsWith('class:') ? r.slice(6) : r
return r.startsWith('class:') ? r.slice(6).replace(rightTrimRe, '') : r
})
}
return new Set(result)
Expand Down
46 changes: 35 additions & 11 deletions test/extractor.test.ts
@@ -1,21 +1,45 @@
import { extractorSplit } from '@unocss/core'
import { extractorSplit, extractorSvelte } from '@unocss/core'
import { expect, it } from 'vitest'

it('extractorSplit', async () => {
let code = ''
async function extract() {
async function extract(code: string) {
return [...await extractorSplit.extract({ code, original: code }) || []]
}

code = 'foo'
expect(await extract()).eql(['foo'])
expect(await extract('foo')).eql(['foo'])
expect(await extract('<div class="text-red border">foo</div>')).toContain('text-red')
expect(await extract('<div class="<sm:text-lg">foo</div>')).toContain('<sm:text-lg')
expect(await extract('"class=\"bg-white\""')).toContain('bg-white')
})

it('extractorSvelte uses regular split with non .svelte files', async () => {
async function extract(code: string) {
return [...await extractorSvelte.extract({ code, original: code }) || []]
}

expect(await extract('foo')).eql(['foo'])
expect(await extract('<div class="text-red border">foo</div>')).toContain('text-red')
expect(await extract('<div class="<sm:text-lg">foo</div>')).toContain('<sm:text-lg')
expect(await extract('"class=\"bg-white\""')).toContain('bg-white')

code = '<div class="text-red border">foo</div>'
expect(await extract()).toContain('text-red')
expect(await extract('<div class:text-orange-400={foo} />')).toContain('class:text-orange-400=')
expect(await extract('class:text-gray-800={$page.url.pathname.startsWith(\'/test\')}')).toContain('class:text-gray-800=')
expect(await extract('<div class="data-[a~=b]:text-red">foo</div>')).toContain('data-[a~=b]:text-red')
expect(await extract('<div class:text-[32px]="{true}" />')).toContain('class:text-[32px]=')
})

it('extractorSvelte uses svelte-specific split with .svelte files', async () => {
async function extract(code: string) {
return [...await extractorSvelte.extract({ code, original: code, id: 'file.svelte' }) || []]
}

code = '<div class="<sm:text-lg">foo</div>'
expect(await extract()).toContain('<sm:text-lg')
expect(await extract('foo')).eql(['foo'])
expect(await extract('<div class="text-red border">foo</div>')).toContain('text-red')
expect(await extract('<div class="<sm:text-lg">foo</div>')).toContain('<sm:text-lg')
expect(await extract('"class=\"bg-white\""')).toContain('bg-white')

code = '"class=\"bg-white\""'
expect(await extract()).toContain('bg-white')
expect(await extract('<div class:text-orange-400={foo} />')).toContain('text-orange-400')
expect(await extract('class:text-gray-800={$page.url.pathname.startsWith(\'/test\')}')).toContain('text-gray-800')
expect(await extract('<div class="data-[a~=b]:text-red">foo</div>')).toContain('data-[a~=b]:text-red')
expect(await extract('<div class:text-[32px]="{true}" />')).toContain('text-[32px]')
})

0 comments on commit 52a71f4

Please sign in to comment.