Skip to content

Commit 4214f62

Browse files
authored
fix(transformer-compile-class): support trigger with new line (#5089)
1 parent 2f7f267 commit 4214f62

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

packages-presets/transformer-compile-class/src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export interface CompileClassOptions {
6666

6767
export default function transformerCompileClass(options: CompileClassOptions = {}): SourceCodeTransformer {
6868
const {
69-
trigger = /(["'`]):uno-?(?<name>\S+)?:\s(.*?)\1/g,
69+
trigger = /(["'`])\s*:uno-?(?<name>\S+)?:\s([\s\S]*?)\1/g,
7070
classPrefix = 'uno-',
7171
hashFn = hash,
7272
keepUnknown = true,
@@ -78,22 +78,22 @@ export default function transformerCompileClass(options: CompileClassOptions = {
7878
// Provides backwards compatibility. We either accept a trigger string which
7979
// gets turned into a regexp (like previously) or a regex literal directly.
8080
const regexp = typeof trigger === 'string'
81-
? new RegExp(`(["'\`])${escapeRegExp(trigger)}\\s([^\\1]*?)\\1`, 'g')
81+
? new RegExp(`(["'\`])\\s*${escapeRegExp(trigger)}\\s([\\s\\S]*?)\\1`, 'g')
8282
: trigger
8383

8484
return {
8585
name: '@unocss/transformer-compile-class',
8686
enforce: 'pre',
8787
async transform(s, _, { uno, tokens, invalidate }) {
88-
const matches = [...s.original.matchAll(regexp)]
88+
const matches = Array.from(s.original.matchAll(regexp))
8989
if (!matches.length)
9090
return
9191

9292
const size = compiledClass.size
9393
for (const match of matches) {
94-
let body = (match.length === 4 && match.groups)
94+
let body = ((match.length === 4 && match.groups)
9595
? expandVariantGroup(match[3].trim())
96-
: expandVariantGroup(match[2].trim())
96+
: expandVariantGroup(match[2].trim())).replace(/\s+/g, ' ')
9797

9898
const start = match.index!
9999
const replacements = []

test/transformer-compile-class.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,22 @@ describe('transformer-compile-class', () => {
152152

153153
expect(invalidateFn).toHaveBeenCalledTimes(2)
154154
})
155+
156+
it('trigger with new line', async () => {
157+
const result = await transform(`
158+
<div class="
159+
:uno:
160+
w-1 h-1
161+
bg-red text-blue
162+
">
163+
test
164+
</div>
165+
`)
166+
167+
expect(result.code.trim()).toMatchInlineSnapshot(`
168+
"<div class="uno-4o94tx">
169+
test
170+
</div>"
171+
`)
172+
})
155173
})

0 commit comments

Comments
 (0)