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

feat(core): support safelist access to RuleContext #3693

Merged
merged 7 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 11 additions & 3 deletions packages/core/src/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ export class UnoGenerator<Theme extends object = object> {
if (safelist) {
this.config.safelist.forEach((s) => {
// We don't want to increment count if token is already in the set
if (typeof s === 'function') {
const result = toArray(s(this.config))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Providing the entire config is a bit heavy, I suggest creating a lightweight context.

result.forEach((s) => {
if (!tokens.has(s))
tokens.add(s)
})
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't return at here to end the uno parsing.

}
if (!tokens.has(s))
tokens.add(s)
})
Expand Down Expand Up @@ -284,7 +292,7 @@ export class UnoGenerator<Theme extends object = object> {
|| a[2]?.localeCompare(b[2] || '') // body
|| 0
})
.map(([, selector, body,, meta,, variantNoMerge]) => {
.map(([, selector, body, , meta, , variantNoMerge]) => {
const scopedSelector = selector ? applyScope(selector, scope) : selector
return [
[[scopedSelector ?? '', meta?.sort ?? 0]],
Expand Down Expand Up @@ -725,8 +733,8 @@ export class UnoGenerator<Theme extends object = object> {
}

const merges = [
[e.filter(([, noMerge]) => noMerge).map(([entries,, sort]) => [entries, sort]), true],
[e.filter(([, noMerge]) => !noMerge).map(([entries,, sort]) => [entries, sort]), false],
[e.filter(([, noMerge]) => noMerge).map(([entries, , sort]) => [entries, sort]), true],
[e.filter(([, noMerge]) => !noMerge).map(([entries, , sort]) => [entries, sort]), false],
] as [[CSSEntries, number][], boolean][]

return merges.map(([e, noMerge]) => [
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ export interface ConfigBase<Theme extends object = object> {
/**
* Utilities that always been included
*/
safelist?: string[]
safelist?: (string | ((config: ResolvedConfig<Theme>) => string[] | string))[]

/**
* Extractors to handle the source file and outputs possible classes/selectors
Expand Down Expand Up @@ -686,7 +686,7 @@ export interface ContentOptions {
/**
* Inline text to be extracted
*/
inline?: (string | { code: string, id?: string } | (() => Awaitable<string | { code: string, id?: string }>)) []
inline?: (string | { code: string, id?: string } | (() => Awaitable<string | { code: string, id?: string }>))[]

/**
* Filters to determine whether to extract certain modules from the build tools' transformation pipeline.
Expand Down Expand Up @@ -722,7 +722,7 @@ export interface ContentOptions {
/**
* @deprecated Renamed to `inline`
*/
plain?: (string | { code: string, id?: string }) []
plain?: (string | { code: string, id?: string })[]
}

/**
Expand Down Expand Up @@ -778,12 +778,12 @@ export interface PluginOptions {
exclude?: FilterPattern
}

export interface UserConfig<Theme extends object = object> extends ConfigBase<Theme>, UserOnlyOptions<Theme>, GeneratorOptions, PluginOptions, CliOptions {}
export interface UserConfigDefaults<Theme extends object = object> extends ConfigBase<Theme>, UserOnlyOptions<Theme> {}
export interface UserConfig<Theme extends object = object> extends ConfigBase<Theme>, UserOnlyOptions<Theme>, GeneratorOptions, PluginOptions, CliOptions { }
export interface UserConfigDefaults<Theme extends object = object> extends ConfigBase<Theme>, UserOnlyOptions<Theme> { }

export interface ResolvedConfig<Theme extends object = object> extends Omit<
RequiredByKey<UserConfig<Theme>, 'mergeSelectors' | 'theme' | 'rules' | 'variants' | 'layers' | 'extractors' | 'blocklist' | 'safelist' | 'preflights' | 'sortLayers'>,
'rules' | 'shortcuts' | 'autocomplete'
RequiredByKey<UserConfig<Theme>, 'mergeSelectors' | 'theme' | 'rules' | 'variants' | 'layers' | 'extractors' | 'blocklist' | 'safelist' | 'preflights' | 'sortLayers'>,
'rules' | 'shortcuts' | 'autocomplete'
> {
presets: Preset<Theme>[]
shortcuts: Shortcut<Theme>[]
Expand Down
3 changes: 3 additions & 0 deletions test/safelist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ describe('safelist', () => {
],
safelist: [
'm1',
() => ['m3', 'm4'],
],
})
const { css } = await uno.generate('m2')
expect(css).toContain('.m1')
expect(css).toContain('.m2')
expect(css).toContain('.m3')
expect(css).toContain('.m4')
})
})