Skip to content

Commit

Permalink
feat(core): layer for preset
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 2, 2022
1 parent 2e43710 commit 7800d39
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
30 changes: 19 additions & 11 deletions packages/core/src/config.ts
@@ -1,4 +1,4 @@
import type { Postprocessor, Preprocessor, Preset, ResolvedConfig, Shortcut, ThemeExtender, UserConfig, UserConfigDefaults, UserShortcuts } from './types'
import type { Postprocessor, Preprocessor, Preset, ResolvedConfig, Rule, Shortcut, ThemeExtender, UserConfig, UserConfigDefaults, UserShortcuts } from './types'
import { clone, isStaticRule, mergeDeep, normalizeVariant, toArray, uniq } from './utils'
import { extractorSplit } from './extractors'
import { DEAFULT_LAYERS } from './constants'
Expand All @@ -12,17 +12,25 @@ export function resolveShortcuts(shortcuts: UserShortcuts): Shortcut[] {
}

export function resolvePreset(preset: Preset): Preset {
if (preset.prefix) {
preset.rules?.forEach((i) => {
if (i[2]) {
if (i[2].prefix == null)
i[2].prefix = preset.prefix
}
else {
i[2] = { prefix: preset.prefix }
}
})
const shortcuts = preset.shortcuts
? resolveShortcuts(preset.shortcuts)
: undefined
preset.shortcuts = shortcuts as any

if (preset.prefix || preset.layer) {
const apply = (i: Rule | Shortcut) => {
if (!i[2])
i[2] = {}
const meta = i[2]
if (meta.prefix == null && preset.prefix)
meta.prefix = preset.prefix
if (meta.layer == null && preset.layer)
meta.prefix = preset.layer
}
shortcuts?.forEach(apply)
preset.rules?.forEach(apply)
}

return preset
}

Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/generator/index.ts
Expand Up @@ -509,16 +509,17 @@ export class UnoGenerator {
let meta: RuleMeta | undefined
let result: string | string[] | undefined
for (const s of this.config.shortcuts) {
const unprefixed = s[2]?.prefix ? input.slice(s[2].prefix.length) : input
if (isStaticShortcut(s)) {
if (s[0] === input) {
if (s[0] === unprefixed) {
meta = meta || s[2]
result = s[1]
recordShortcut(s)
break
}
}
else {
const match = input.match(s[0])
const match = unprefixed.match(s[0])
if (match)
result = s[1](match, context)
if (result) {
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/types.ts
Expand Up @@ -455,9 +455,13 @@ export interface Preset<Theme extends {} = {}> extends ConfigBase<Theme> {
*/
options?: PresetOptions
/**
* Apply prefix to all utilities
* Apply prefix to all utilities and shortcuts
*/
prefix?: string
/**
* Apply layer to all utilities and shortcuts
*/
layer?: string
}

export interface GeneratorOptions {
Expand Down
3 changes: 2 additions & 1 deletion test/__snapshots__/prefix.test.ts.snap
Expand Up @@ -4,5 +4,6 @@ exports[`prefix > preset prefix 2`] = `
"/* layer: default */
.hover\\\\:foo-p4:hover{padding:1rem;}
.foo-text-red{--un-text-opacity:1;color:rgba(248,113,113,var(--un-text-opacity));}
.bar-bar{color:bar;}"
.bar-bar,
.bar-shortcut{color:bar;}"
`;
11 changes: 9 additions & 2 deletions test/prefix.test.ts
Expand Up @@ -11,22 +11,29 @@ describe('prefix', () => {
rules: [
['bar', { color: 'bar' }, { prefix: 'bar-' }],
],
shortcuts: [
['shortcut', 'bar-bar', { prefix: 'bar-' }],
],
})

const { css, matched } = await uno.generate(new Set([
'text-red',
'foo-text-red',
'hover:p4',
'hover:foo-p4',
'bar',
'shortcut',
// expected
'foo-text-red',
'hover:foo-p4',
'bar-bar',
'bar-shortcut',
]), { preflights: false })

expect(matched).toMatchInlineSnapshot(`
Set {
"bar-bar",
"foo-text-red",
"hover:foo-p4",
"bar-shortcut",
}
`)

Expand Down

0 comments on commit 7800d39

Please sign in to comment.