forked from unocss/unocss
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): support generator in rule matcher (unocss#3884)
- Loading branch information
1 parent
b9a660c
commit 82bdf08
Showing
5 changed files
with
151 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { createGenerator } from '@unocss/core' | ||
import { expect, it } from 'vitest' | ||
|
||
it('rule-first', async () => { | ||
const order: number[] = [] | ||
const uno = createGenerator({ | ||
rules: [ | ||
[/^rule$/, () => new Promise(resolve => setTimeout(() => { | ||
order.push(1) | ||
resolve('/* rule */') | ||
}, 10))], | ||
], | ||
preflights: [ | ||
{ | ||
getCSS: () => new Promise(resolve => setTimeout(() => { | ||
order.push(2) | ||
resolve('/* preflight */') | ||
}, 20)), | ||
}, | ||
], | ||
}) | ||
await uno.generate('rule') | ||
expect(order).eql([1, 2]) | ||
}) | ||
|
||
it('preflight at the end', async () => { | ||
const order: number[] = [] | ||
const uno = createGenerator({ | ||
rules: [ | ||
[/^rule$/, () => new Promise(resolve => setTimeout(() => { | ||
order.push(1) | ||
resolve('/* rule */') | ||
}, 20))], | ||
], | ||
preflights: [ | ||
{ | ||
getCSS: () => new Promise((resolve) => { | ||
order.push(2) | ||
resolve('/* preflight */') | ||
}), | ||
}, | ||
], | ||
}) | ||
await uno.generate('rule') | ||
expect(order).eql([1, 2]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { createGenerator } from '@unocss/core' | ||
import { expect, it } from 'vitest' | ||
|
||
it('rule-generator', async () => { | ||
const uno = createGenerator({ | ||
rules: [ | ||
[/^rule$/, function* () { | ||
yield { | ||
color: 'red', | ||
} | ||
yield { | ||
'font-size': '12px', | ||
} | ||
}], | ||
], | ||
}) | ||
expect((await uno.generate('rule')).css) | ||
.toMatchInlineSnapshot(` | ||
"/* layer: default */ | ||
.rule{color:red;} | ||
.rule{font-size:12px;}" | ||
`) | ||
}) | ||
|
||
it('rule-generator async', async () => { | ||
const uno = createGenerator({ | ||
rules: [ | ||
[/^rule$/, async function* () { | ||
yield { | ||
color: 'red', | ||
} | ||
await new Promise(resolve => setTimeout(resolve, 2)) | ||
yield { | ||
'font-size': '12px', | ||
} | ||
yield { | ||
'font-weight': 'bold', | ||
} | ||
}], | ||
], | ||
}) | ||
expect((await uno.generate('rule')).css) | ||
.toMatchInlineSnapshot(` | ||
"/* layer: default */ | ||
.rule{color:red;} | ||
.rule{font-size:12px;} | ||
.rule{font-weight:bold;}" | ||
`) | ||
}) | ||
|
||
it('rule-generator bail out', async () => { | ||
const uno = createGenerator({ | ||
rules: [ | ||
[/^rule-(.*)$/, function () { | ||
return { | ||
content: '"fallback"', | ||
} | ||
}], | ||
[/^rule-(.*)$/, function* ([_, str]) { | ||
if (str === 'bail') { | ||
return // early return should still work | ||
} | ||
yield { | ||
color: str, | ||
} | ||
yield { | ||
'font-size': '12px', | ||
} | ||
}], | ||
], | ||
}) | ||
expect((await uno.generate('rule-bail rule-red')).css) | ||
.toMatchInlineSnapshot(` | ||
"/* layer: default */ | ||
.rule-bail{content:"fallback";} | ||
.rule-red{color:red;} | ||
.rule-red{font-size:12px;}" | ||
`) | ||
}) |