@unocss/core
Advanced tools
Comparing version 0.1.5 to 0.2.0
@@ -20,20 +20,27 @@ declare type Awaitable<T> = T | Promise<T>; | ||
declare type ExcludeRule = string | RegExp; | ||
declare type Variant<Theme extends {} = {}> = { | ||
interface VariantHandler { | ||
/** | ||
* The entry function to match and rewrite the selector for futher processing. | ||
* The result rewritten selector for the next round of matching | ||
*/ | ||
match: (input: string, theme: Theme) => string | undefined; | ||
matcher: string; | ||
/** | ||
* Rewrite the output selector. Often be used to append pesudo classes or parents. | ||
*/ | ||
selector?: (input: string, theme: Theme) => string | undefined; | ||
selector?: (input: string) => string | undefined; | ||
/** | ||
* Rewrite the output css body. The input come in [key,value][] pairs. | ||
*/ | ||
rewrite?: (input: CSSEntries, theme: Theme) => CSSEntries | undefined; | ||
body?: (body: CSSEntries) => CSSEntries | undefined; | ||
/** | ||
* Provide media query to the output css. | ||
*/ | ||
mediaQuery?: (selector: string, theme: Theme) => string | undefined; | ||
mediaQuery?: string | undefined; | ||
} | ||
declare type VariantFunction<Theme extends {} = {}> = (matcher: string, raw: string, theme: Theme) => string | VariantHandler | undefined; | ||
declare type VariantObject<Theme extends {} = {}> = { | ||
/** | ||
* The entry function to match and rewrite the selector for futher processing. | ||
*/ | ||
match: VariantFunction<Theme>; | ||
/** | ||
* Allows this variant to be used more than once in matching a single rule | ||
@@ -45,2 +52,3 @@ * | ||
}; | ||
declare type Variant<Theme extends {} = {}> = VariantFunction<Theme> | VariantObject<Theme>; | ||
interface ConfigBase<Theme extends {} = {}> { | ||
@@ -103,2 +111,3 @@ /** | ||
shortcuts: Shortcut[]; | ||
variants: VariantObject[]; | ||
rulesSize: number; | ||
@@ -115,3 +124,3 @@ rulesDynamic: (DynamicRule | undefined)[]; | ||
string, | ||
Variant[] | ||
VariantHandler[] | ||
]; | ||
@@ -122,3 +131,3 @@ declare type ParsedUtil = readonly [ | ||
CSSEntries, | ||
Variant[] | ||
VariantHandler[] | ||
]; | ||
@@ -150,4 +159,2 @@ declare type StringifiedUtil = readonly [ | ||
declare const variantMatcher: (name: string) => (input: string) => string | undefined; | ||
declare function hex2rgba(hex: string): [number, number, number, number] | [number, number, number] | undefined; | ||
@@ -159,2 +166,3 @@ | ||
declare function isValidSelector(selector?: string): selector is string; | ||
declare function normalizeVariant(variant: Variant): VariantObject; | ||
@@ -188,3 +196,3 @@ declare class TwoKeyMap<K1, K2, V> { | ||
matchVariants(raw: string): VariantMatchedResult; | ||
applyVariants(parsed: ParsedUtil, variants?: Variant<{}>[], raw?: string): readonly [string, CSSEntries, string | undefined]; | ||
applyVariants(parsed: ParsedUtil, variantHandlers?: VariantHandler[], raw?: string): readonly [string, CSSEntries, string | undefined]; | ||
parseUtil(input: string | VariantMatchedResult): Promise<ParsedUtil | undefined>; | ||
@@ -199,2 +207,2 @@ stringifyUtil(parsed?: ParsedUtil): StringifiedUtil | undefined; | ||
export { ArgumentType, Awaitable, BetterMap, CSSEntries, CSSObject, ConfigBase, DeepPartial, DynamicRule, DynamicShortcut, ExcludeRule, Extractor, GenerateResult, GeneratorOptions, ParsedUtil, Preset, ResolvedConfig, RestArgs, Rule, Shift, Shortcut, StaticRule, StaticShortcut, StaticShortcutMap, StringifiedUtil, TwoKeyMap, UnoGenerator, UserConfig, UserConfigDefaults, UserShortcuts, Variant, VariantMatchedResult, attributifyRE, createGenerator, defineConfig, e, entriesToCss, escapeRegExp, escapeSelector, extractorSplit, hasScopePlaceholder, hex2rgba, isAttributifySelector, isObject, isStaticRule, isStaticShortcut, isValidSelector, mergeDeep, mergeSet, toArray, uniq, validateFilterRE, variantMatcher }; | ||
export { ArgumentType, Awaitable, BetterMap, CSSEntries, CSSObject, ConfigBase, DeepPartial, DynamicRule, DynamicShortcut, ExcludeRule, Extractor, GenerateResult, GeneratorOptions, ParsedUtil, Preset, ResolvedConfig, RestArgs, Rule, Shift, Shortcut, StaticRule, StaticShortcut, StaticShortcutMap, StringifiedUtil, TwoKeyMap, UnoGenerator, UserConfig, UserConfigDefaults, UserShortcuts, Variant, VariantFunction, VariantHandler, VariantMatchedResult, VariantObject, attributifyRE, createGenerator, defineConfig, e, entriesToCss, escapeRegExp, escapeSelector, extractorSplit, hasScopePlaceholder, hex2rgba, isAttributifySelector, isObject, isStaticRule, isStaticShortcut, isValidSelector, mergeDeep, mergeSet, normalizeVariant, toArray, uniq, validateFilterRE }; |
@@ -31,6 +31,6 @@ var __defProp = Object.defineProperty; | ||
mergeSet: () => mergeSet, | ||
normalizeVariant: () => normalizeVariant, | ||
toArray: () => toArray, | ||
uniq: () => uniq, | ||
validateFilterRE: () => validateFilterRE, | ||
variantMatcher: () => variantMatcher | ||
validateFilterRE: () => validateFilterRE | ||
}); | ||
@@ -131,9 +131,2 @@ | ||
// src/utils/variant.ts | ||
var variantMatcher = (name) => { | ||
const length = name.length + 1; | ||
const re = new RegExp(`^${name}[:-]`); | ||
return (input) => input.match(re) ? input.slice(length) : void 0; | ||
}; | ||
// src/utils/colors.ts | ||
@@ -162,3 +155,3 @@ var hexRE = /^#?([\da-f]+)$/i; | ||
// src/utils/regex.ts | ||
// src/utils/helpers.ts | ||
var attributifyRE = /^\[(.+?)~?="(.*)"\]$/; | ||
@@ -172,2 +165,5 @@ var validateFilterRE = /[a-z]/; | ||
} | ||
function normalizeVariant(variant) { | ||
return typeof variant === "function" ? { match: variant } : variant; | ||
} | ||
@@ -280,3 +276,3 @@ // src/utils/map.ts | ||
rulesStaticMap, | ||
variants: mergePresets("variants"), | ||
variants: mergePresets("variants").map(normalizeVariant), | ||
shortcuts: resolveShortcuts(mergePresets("shortcuts")), | ||
@@ -388,3 +384,4 @@ extractors | ||
matchVariants(raw) { | ||
const variants = []; | ||
const usedVariants = new Set(); | ||
const handlers = []; | ||
let processed = raw; | ||
@@ -395,8 +392,13 @@ let applied = false; | ||
for (const v of this.config.variants) { | ||
if (!v.multiPass && variants.includes(v)) | ||
if (!v.multiPass && usedVariants.has(v)) | ||
continue; | ||
const result = v.match(processed, this.config.theme); | ||
if (result && result !== processed) { | ||
processed = result; | ||
variants.push(v); | ||
let handler = v.match(processed, raw, this.config.theme); | ||
if (!handler) | ||
continue; | ||
if (typeof handler === "string") | ||
handler = { matcher: handler }; | ||
if (handler && handler.matcher !== processed) { | ||
processed = handler.matcher; | ||
handlers.push(handler); | ||
usedVariants.add(v); | ||
applied = true; | ||
@@ -409,17 +411,13 @@ break; | ||
} | ||
return [raw, processed, variants]; | ||
return [raw, processed, handlers]; | ||
} | ||
applyVariants(parsed, variants = parsed[3], raw = parsed[1]) { | ||
const theme = this.config.theme; | ||
const selector = variants.reduce((p, v) => { | ||
applyVariants(parsed, variantHandlers = parsed[3], raw = parsed[1]) { | ||
const selector = variantHandlers.reduce((p, v) => { | ||
var _a; | ||
return ((_a = v.selector) == null ? void 0 : _a.call(v, p, theme)) || p; | ||
return ((_a = v.selector) == null ? void 0 : _a.call(v, p)) || p; | ||
}, toEscapedSelector(raw)); | ||
const mediaQuery = variants.reduce((p, v) => { | ||
const mediaQuery = variantHandlers.reduce((p, v) => v.mediaQuery || p, void 0); | ||
const entries = variantHandlers.reduce((p, v) => { | ||
var _a; | ||
return ((_a = v.mediaQuery) == null ? void 0 : _a.call(v, parsed[1], theme)) || p; | ||
}, void 0); | ||
const entries = variants.reduce((p, v) => { | ||
var _a; | ||
return ((_a = v.rewrite) == null ? void 0 : _a.call(v, p, theme)) || p; | ||
return ((_a = v.body) == null ? void 0 : _a.call(v, p)) || p; | ||
}, parsed[2]); | ||
@@ -544,6 +542,6 @@ return [ | ||
mergeSet, | ||
normalizeVariant, | ||
toArray, | ||
uniq, | ||
validateFilterRE, | ||
variantMatcher | ||
validateFilterRE | ||
}); |
{ | ||
"name": "@unocss/core", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40473
1211