-
-
Notifications
You must be signed in to change notification settings - Fork 778
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 factory with
definePreset
(#3098)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
Showing
14 changed files
with
125 additions
and
45 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 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 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 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 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { createGenerator, definePreset } from '@unocss/core' | ||
import { describe, expect, it } from 'vitest' | ||
|
||
describe('definePreset', () => { | ||
it('functional', async () => { | ||
const presetFn1 = definePreset(() => { | ||
return { | ||
name: 'custom-preset-functional-1', | ||
rules: [ | ||
['text-fn1', { | ||
color: 'red', | ||
}], | ||
], | ||
} | ||
}) | ||
|
||
const presetFn2 = definePreset(() => { | ||
return { | ||
name: 'custom-preset-functional-2', | ||
rules: [ | ||
['text-fn2', { | ||
color: 'green', | ||
}], | ||
], | ||
} | ||
}) | ||
|
||
const presetStatic = definePreset({ | ||
name: 'custom-preset-static', | ||
rules: [ | ||
['text-static', { | ||
color: 'blue', | ||
}], | ||
], | ||
}) | ||
|
||
const presetOptional = definePreset((option: { color?: string } = {}) => { | ||
const color = option.color ?? 'red' | ||
return { | ||
name: 'custom-preset', | ||
rules: [ | ||
['text-option', { | ||
color, | ||
}], | ||
], | ||
} | ||
}) | ||
|
||
const uno = createGenerator({ | ||
presets: [ | ||
presetFn1(), | ||
presetFn2, // presetFn2 is a factory that will be evaluated by uno | ||
presetStatic, | ||
presetOptional({ | ||
color: 'dark', | ||
}), | ||
], | ||
}) | ||
|
||
const { css } = await uno.generate('text-fn1 text-fn2 text-static text-option') | ||
expect(css) | ||
.toMatchInlineSnapshot(` | ||
"/* layer: default */ | ||
.text-fn1{color:red;} | ||
.text-fn2{color:green;} | ||
.text-static{color:blue;} | ||
.text-option{color:dark;}" | ||
`) | ||
}) | ||
}) |