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(core): fix for svelte extractor #1830

Merged
merged 8 commits into from Nov 2, 2022
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
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]')
})