@seyuna/postcss 1.0.0-canary.14 → 1.0.0-canary.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/release.yml +1 -1
- package/CHANGELOG.md +15 -0
- package/README.md +275 -0
- package/dist/at-rules/color-scheme.d.ts +4 -0
- package/dist/at-rules/color-scheme.js +43 -0
- package/dist/at-rules/color.d.ts +3 -2
- package/dist/at-rules/color.js +10 -21
- package/dist/at-rules/container.d.ts +2 -1
- package/dist/at-rules/container.js +12 -8
- package/dist/at-rules/index.d.ts +3 -2
- package/dist/at-rules/index.js +14 -23
- package/dist/config.d.ts +18 -0
- package/dist/config.js +47 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +14 -0
- package/dist/functions/color.d.ts +7 -2
- package/dist/functions/color.js +58 -29
- package/dist/functions/index.d.ts +2 -1
- package/dist/functions/index.js +10 -12
- package/dist/functions/theme.d.ts +6 -0
- package/dist/functions/theme.js +17 -0
- package/dist/helpers.d.ts +3 -2
- package/dist/helpers.js +13 -28
- package/dist/index.d.ts +2 -2
- package/dist/index.js +6 -5
- package/dist/parser.d.ts +4 -0
- package/dist/parser.js +59 -0
- package/dist/plugin.d.ts +2 -4
- package/dist/plugin.js +16 -22
- package/dist/types.d.ts +1 -19
- package/dist/types.js +1 -2
- package/package.json +13 -5
- package/release.config.mjs +6 -1
- package/src/at-rules/color-scheme.ts +52 -0
- package/src/at-rules/color.ts +11 -15
- package/src/at-rules/container.ts +13 -3
- package/src/at-rules/index.ts +6 -8
- package/src/config.ts +71 -0
- package/src/errors.ts +23 -0
- package/src/functions/color.ts +70 -29
- package/src/functions/index.ts +9 -4
- package/src/functions/theme.ts +20 -0
- package/src/helpers.ts +23 -22
- package/src/index.ts +3 -1
- package/src/parser.ts +80 -0
- package/src/plugin.ts +12 -21
- package/src/types.ts +1 -19
- package/tests/plugin.test.ts +143 -0
- package/tsconfig.json +2 -2
- package/dist/at-rules/dark.d.ts +0 -23
- package/dist/at-rules/dark.js +0 -65
- package/dist/at-rules/light.d.ts +0 -23
- package/dist/at-rules/light.js +0 -65
- package/dist/functions/spacing.d.ts +0 -1
- package/dist/functions/spacing.js +0 -6
- package/src/at-rules/dark.ts +0 -69
- package/src/at-rules/light.ts +0 -69
- package/src/functions/spacing.ts +0 -3
package/src/config.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { SeyunaConfig } from './types';
|
|
4
|
+
import { FunctionMap } from './parser';
|
|
5
|
+
|
|
6
|
+
export interface PluginOptions {
|
|
7
|
+
configPath?: string;
|
|
8
|
+
modeAttribute?: string;
|
|
9
|
+
strict?: boolean;
|
|
10
|
+
config?: SeyunaConfig;
|
|
11
|
+
functions?: FunctionMap;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface PluginContext {
|
|
15
|
+
config: SeyunaConfig;
|
|
16
|
+
options: Required<PluginOptions>;
|
|
17
|
+
functions: FunctionMap;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const DEFAULT_OPTIONS: Required<PluginOptions> = {
|
|
21
|
+
configPath: 'seyuna.json',
|
|
22
|
+
modeAttribute: 'data-mode',
|
|
23
|
+
strict: false,
|
|
24
|
+
config: undefined as any,
|
|
25
|
+
functions: undefined as any,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
let cachedConfig: SeyunaConfig | null = null;
|
|
29
|
+
let cachedConfigPath: string | null = null;
|
|
30
|
+
|
|
31
|
+
export function loadConfig(options: PluginOptions = {}): { config: SeyunaConfig, options: Required<PluginOptions> } {
|
|
32
|
+
const mergedOptions = { ...DEFAULT_OPTIONS, ...options };
|
|
33
|
+
|
|
34
|
+
if (mergedOptions.config) {
|
|
35
|
+
return { config: mergedOptions.config, options: mergedOptions };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const configPath = path.resolve(process.cwd(), mergedOptions.configPath);
|
|
39
|
+
|
|
40
|
+
// Cache config if it's the same path
|
|
41
|
+
if (cachedConfig && cachedConfigPath === configPath) {
|
|
42
|
+
return { config: cachedConfig, options: mergedOptions };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
if (!fs.existsSync(configPath)) {
|
|
47
|
+
if (mergedOptions.strict) {
|
|
48
|
+
throw new Error(`Seyuna config not found at ${configPath}`);
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
config: { ui: { theme: { hues: {}, light: { colors: {} }, dark: { colors: {} } } } } as any,
|
|
52
|
+
options: mergedOptions,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const data = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
|
57
|
+
cachedConfig = data;
|
|
58
|
+
cachedConfigPath = configPath;
|
|
59
|
+
|
|
60
|
+
return { config: data, options: mergedOptions };
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if (mergedOptions.strict) {
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
console.warn(`[Seyuna PostCSS] Warning: Failed to load config: ${error instanceof Error ? error.message : String(error)}`);
|
|
66
|
+
return {
|
|
67
|
+
config: { ui: { theme: { hues: {}, light: { colors: {} }, dark: { colors: {} } } } } as any,
|
|
68
|
+
options: mergedOptions,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Node } from 'postcss';
|
|
2
|
+
import { PluginContext } from './config';
|
|
3
|
+
|
|
4
|
+
export function reportError(
|
|
5
|
+
message: string,
|
|
6
|
+
node: Node,
|
|
7
|
+
context: PluginContext,
|
|
8
|
+
options: { word?: string; index?: number } = {}
|
|
9
|
+
) {
|
|
10
|
+
const { options: pluginOptions } = context;
|
|
11
|
+
const formattedMessage = `[Seyuna PostCSS] ${message}`;
|
|
12
|
+
|
|
13
|
+
if (pluginOptions.strict) {
|
|
14
|
+
throw node.error(formattedMessage, options);
|
|
15
|
+
} else {
|
|
16
|
+
reportWarning(formattedMessage, node);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function reportWarning(message: string, node: Node) {
|
|
21
|
+
const result = node.root().toResult();
|
|
22
|
+
node.warn(result, `[Seyuna PostCSS] ${message}`);
|
|
23
|
+
}
|
package/src/functions/color.ts
CHANGED
|
@@ -1,49 +1,90 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { PluginContext } from "../config";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Resolves a color name to its CSS variables based on its type (standard or fixed)
|
|
5
|
+
*/
|
|
6
|
+
function getColorVariables(context: PluginContext, color: string, type?: 'sc' | 'fc') {
|
|
7
|
+
const { config } = context;
|
|
8
|
+
const hues = config?.ui?.theme?.hues || {};
|
|
9
|
+
const colors = config?.ui?.theme?.colors || {};
|
|
10
|
+
const lightColors = config?.ui?.theme?.light?.colors || {};
|
|
11
|
+
const darkColors = config?.ui?.theme?.dark?.colors || {};
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
const isStandard = color in hues;
|
|
14
|
+
const isFixed = color in colors || color in lightColors || color in darkColors;
|
|
15
|
+
|
|
16
|
+
if (type === 'sc' && !isStandard) {
|
|
17
|
+
throw new Error(`Standard color '${color}' not found in seyuna.json hues`);
|
|
18
|
+
}
|
|
19
|
+
if (type === 'fc' && !isFixed) {
|
|
20
|
+
throw new Error(`Fixed color '${color}' not found in seyuna.json colors`);
|
|
13
21
|
}
|
|
14
22
|
|
|
15
|
-
if (
|
|
16
|
-
|
|
23
|
+
if (isStandard) {
|
|
24
|
+
return {
|
|
25
|
+
l: "var(--lightness)",
|
|
26
|
+
c: "var(--chroma)",
|
|
27
|
+
h: `var(--${color}-hue)`,
|
|
28
|
+
};
|
|
17
29
|
}
|
|
18
30
|
|
|
19
|
-
if (
|
|
20
|
-
|
|
31
|
+
if (isFixed) {
|
|
32
|
+
return {
|
|
33
|
+
l: `var(--${color}-lightness)`,
|
|
34
|
+
c: `var(--${color}-chroma)`,
|
|
35
|
+
h: `var(--${color}-hue)`,
|
|
36
|
+
};
|
|
21
37
|
}
|
|
22
38
|
|
|
23
|
-
|
|
39
|
+
throw new Error(`Color '${color}' not found in seyuna.json`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function sc(
|
|
43
|
+
context: PluginContext,
|
|
44
|
+
name: string,
|
|
45
|
+
alpha?: string,
|
|
46
|
+
lightness?: string,
|
|
47
|
+
chroma?: string
|
|
48
|
+
) {
|
|
49
|
+
const vars = getColorVariables(context, name, 'sc');
|
|
50
|
+
const a = alpha && alpha !== "null" ? alpha : "1";
|
|
51
|
+
const l = lightness && lightness !== "null" ? lightness : vars.l;
|
|
52
|
+
const c = chroma && chroma !== "null" ? chroma : vars.c;
|
|
53
|
+
|
|
54
|
+
return `oklch(${l} ${c} ${vars.h} / ${a})`;
|
|
24
55
|
}
|
|
25
56
|
|
|
26
57
|
export function fc(
|
|
58
|
+
context: PluginContext,
|
|
27
59
|
name: string,
|
|
28
60
|
alpha?: string,
|
|
29
61
|
lightness?: string,
|
|
30
62
|
chroma?: string
|
|
31
63
|
) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
64
|
+
const vars = getColorVariables(context, name, 'fc');
|
|
65
|
+
const a = alpha && alpha !== "null" ? alpha : "1";
|
|
66
|
+
const l = lightness && lightness !== "null" ? lightness : vars.l;
|
|
67
|
+
const c = chroma && chroma !== "null" ? chroma : vars.c;
|
|
35
68
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
69
|
+
return `oklch(${l} ${c} ${vars.h} / ${a})`;
|
|
70
|
+
}
|
|
39
71
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
72
|
+
export function alpha(context: PluginContext, color: string, value: string) {
|
|
73
|
+
const { l, c, h } = getColorVariables(context, color);
|
|
74
|
+
return `oklch(${l} ${c} ${h} / ${value})`;
|
|
75
|
+
}
|
|
43
76
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
77
|
+
export function lighten(context: PluginContext, color: string, amount: string) {
|
|
78
|
+
const { l, c, h } = getColorVariables(context, color);
|
|
79
|
+
return `oklch(calc(${l} + ${amount}) ${c} ${h} / 1)`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function darken(context: PluginContext, color: string, amount: string) {
|
|
83
|
+
const { l, c, h } = getColorVariables(context, color);
|
|
84
|
+
return `oklch(calc(${l} - ${amount}) ${c} ${h} / 1)`;
|
|
85
|
+
}
|
|
47
86
|
|
|
48
|
-
|
|
87
|
+
export function contrast(context: PluginContext, color: string) {
|
|
88
|
+
const { l } = getColorVariables(context, color);
|
|
89
|
+
return `oklch(calc((${l} - 0.6) * -1000) 0 0)`;
|
|
49
90
|
}
|
package/src/functions/index.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import { sc, fc } from "./color";
|
|
2
|
-
import
|
|
1
|
+
import { sc, fc, alpha, lighten, darken, contrast } from "./color";
|
|
2
|
+
import { theme } from "./theme";
|
|
3
|
+
import { PluginContext } from "../config";
|
|
3
4
|
|
|
4
|
-
export type FnHandler = (...args: string[]) => string;
|
|
5
|
+
export type FnHandler = (context: PluginContext, ...args: string[]) => string;
|
|
5
6
|
|
|
6
7
|
export const functions: Record<string, FnHandler> = {
|
|
7
8
|
sc,
|
|
8
9
|
fc,
|
|
9
|
-
|
|
10
|
+
alpha,
|
|
11
|
+
lighten,
|
|
12
|
+
darken,
|
|
13
|
+
contrast,
|
|
14
|
+
theme,
|
|
10
15
|
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { PluginContext } from "../config";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Accesses values from the Seyuna configuration using dot notation
|
|
5
|
+
* Example: theme(ui.theme.breakpoints.tablet)
|
|
6
|
+
*/
|
|
7
|
+
export function theme(context: PluginContext, path: string): string {
|
|
8
|
+
const parts = path.split('.');
|
|
9
|
+
let current: any = context.config;
|
|
10
|
+
|
|
11
|
+
for (const part of parts) {
|
|
12
|
+
if (current && typeof current === 'object' && part in current) {
|
|
13
|
+
current = current[part];
|
|
14
|
+
} else {
|
|
15
|
+
return path; // Return original path if not found
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return String(current);
|
|
20
|
+
}
|
package/src/helpers.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { Rule, ChildNode, Declaration, AtRule } from "postcss";
|
|
2
|
-
import {
|
|
2
|
+
import { processFunctions } from "./parser";
|
|
3
|
+
import { PluginContext } from "./config";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Helper: clone nodes and replace {name} placeholders safely
|
|
6
7
|
* Returns only valid Rules or Declarations (never raw AtRules)
|
|
7
8
|
*/
|
|
8
|
-
export function cloneNodes(
|
|
9
|
+
export function cloneNodes(
|
|
10
|
+
nodes: ChildNode[],
|
|
11
|
+
name: string,
|
|
12
|
+
context: PluginContext
|
|
13
|
+
): ChildNode[] {
|
|
14
|
+
const { functions: fnMap } = context;
|
|
15
|
+
|
|
9
16
|
return nodes.flatMap((node) => {
|
|
10
17
|
const cloned = node.clone();
|
|
11
18
|
|
|
@@ -13,22 +20,9 @@ export function cloneNodes(nodes: ChildNode[], name: string): ChildNode[] {
|
|
|
13
20
|
const decl = cloned as Declaration;
|
|
14
21
|
let value = decl.value.replace(/\{name\}/g, name);
|
|
15
22
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
.split(",")
|
|
20
|
-
.map((s) => s.trim());
|
|
21
|
-
if (args) value = sc(...(args as [string, string?, string?, string?]));
|
|
22
|
-
}
|
|
23
|
-
if (/fc\(/.test(value)) {
|
|
24
|
-
const args = value
|
|
25
|
-
.match(/fc\(([^)]*)\)/)?.[1]
|
|
26
|
-
.split(",")
|
|
27
|
-
.map((s) => s.trim());
|
|
28
|
-
if (args) value = fc(...(args as [string, string?, string?, string?]));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
decl.value = value;
|
|
23
|
+
// Process functions using the robust parser
|
|
24
|
+
decl.value = processFunctions(value, fnMap, decl, context);
|
|
25
|
+
|
|
32
26
|
return decl;
|
|
33
27
|
}
|
|
34
28
|
|
|
@@ -39,7 +33,7 @@ export function cloneNodes(nodes: ChildNode[], name: string): ChildNode[] {
|
|
|
39
33
|
rule.selector = rule.selector.replace(/\{name\}/g, name);
|
|
40
34
|
|
|
41
35
|
// Recursively clone child nodes and only keep valid rules/decls
|
|
42
|
-
rule.nodes = cloneNodes(rule.nodes || [], name).filter(
|
|
36
|
+
rule.nodes = cloneNodes(rule.nodes || [], name, context).filter(
|
|
43
37
|
(n) => n.type === "rule" || n.type === "decl"
|
|
44
38
|
);
|
|
45
39
|
|
|
@@ -55,13 +49,20 @@ export function cloneNodes(nodes: ChildNode[], name: string): ChildNode[] {
|
|
|
55
49
|
/**
|
|
56
50
|
* Generate CSS rules from a list of names
|
|
57
51
|
*/
|
|
58
|
-
export function generateRules(
|
|
52
|
+
export function generateRules(
|
|
53
|
+
names: string[],
|
|
54
|
+
atRule: AtRule,
|
|
55
|
+
context: PluginContext
|
|
56
|
+
): Rule[] {
|
|
59
57
|
const nodes = atRule.nodes ?? [];
|
|
60
58
|
const generatedRules: Rule[] = [];
|
|
61
59
|
|
|
62
60
|
for (const name of names) {
|
|
63
|
-
const rule = new Rule({
|
|
64
|
-
|
|
61
|
+
const rule = new Rule({
|
|
62
|
+
selector: `&.${name}`,
|
|
63
|
+
source: atRule.source,
|
|
64
|
+
});
|
|
65
|
+
cloneNodes(nodes, name, context).forEach((n) => {
|
|
65
66
|
if (n.type === "rule" && n.selector && n.nodes?.length) rule.append(n);
|
|
66
67
|
if (n.type === "decl") rule.append(n);
|
|
67
68
|
});
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,9 @@ import { dynamicFunctionsPlugin } from './plugin';
|
|
|
2
2
|
import { functions } from './functions';
|
|
3
3
|
|
|
4
4
|
// CommonJS-friendly export
|
|
5
|
-
|
|
5
|
+
const plugin = Object.assign(dynamicFunctionsPlugin, {
|
|
6
6
|
postcss: true,
|
|
7
7
|
functions
|
|
8
8
|
});
|
|
9
|
+
|
|
10
|
+
export default plugin;
|
package/src/parser.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import valueParser from 'postcss-value-parser';
|
|
2
|
+
import { Node } from 'postcss';
|
|
3
|
+
import { PluginContext } from './config';
|
|
4
|
+
import { reportError } from './errors';
|
|
5
|
+
|
|
6
|
+
export type FunctionMap = Record<string, (context: PluginContext, ...args: string[]) => string>;
|
|
7
|
+
|
|
8
|
+
export function processFunctions(
|
|
9
|
+
value: string,
|
|
10
|
+
fnMap: FunctionMap,
|
|
11
|
+
node: Node,
|
|
12
|
+
context: PluginContext
|
|
13
|
+
): string {
|
|
14
|
+
const parsed = valueParser(value);
|
|
15
|
+
|
|
16
|
+
// Helper to process nodes recursively (inner-first)
|
|
17
|
+
function processNode(parsedValue: valueParser.ParsedValue) {
|
|
18
|
+
parsedValue.walk((nodeIter) => {
|
|
19
|
+
// If nested nodes exist (e.g., in a function), process them first
|
|
20
|
+
if (nodeIter.type === 'function' && nodeIter.nodes.length > 0) {
|
|
21
|
+
// Here we recursively process child nodes before handling the current function
|
|
22
|
+
// However, value-parser's walk is already a visitor. To do inner-first,
|
|
23
|
+
// we can either walk in reverse or specifically handle children.
|
|
24
|
+
// A simpler way: since walk visits all, we can check if it's a function
|
|
25
|
+
// and if it's one we handle, we process its arguments.
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Actually, a manual walk is safer for inner-first replacement
|
|
30
|
+
function traverse(nodes: valueParser.Node[]) {
|
|
31
|
+
for (const nodeIter of nodes) {
|
|
32
|
+
if (nodeIter.type === 'function') {
|
|
33
|
+
// Process inner functions first
|
|
34
|
+
traverse(nodeIter.nodes);
|
|
35
|
+
|
|
36
|
+
// Now handle this function if it's in our map
|
|
37
|
+
if (fnMap[nodeIter.value]) {
|
|
38
|
+
const handler = fnMap[nodeIter.value];
|
|
39
|
+
const args: string[] = [];
|
|
40
|
+
let currentArg = '';
|
|
41
|
+
|
|
42
|
+
nodeIter.nodes.forEach((argNode) => {
|
|
43
|
+
if (argNode.type === 'div' && argNode.value === ',') {
|
|
44
|
+
args.push(currentArg.trim());
|
|
45
|
+
currentArg = '';
|
|
46
|
+
} else {
|
|
47
|
+
currentArg += valueParser.stringify(argNode);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (currentArg) {
|
|
52
|
+
args.push(currentArg.trim());
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const cleanedArgs = args.map(arg => arg.replace(/^['"]|['"]$/g, ''));
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const result = handler(context, ...cleanedArgs);
|
|
59
|
+
// Replace function with its result
|
|
60
|
+
(nodeIter as any).type = 'word';
|
|
61
|
+
(nodeIter as any).value = result;
|
|
62
|
+
(nodeIter as any).nodes = []; // Clear children
|
|
63
|
+
} catch (error) {
|
|
64
|
+
reportError(
|
|
65
|
+
`Failed to process function "${nodeIter.value}": ${error instanceof Error ? error.message : String(error)}`,
|
|
66
|
+
node,
|
|
67
|
+
context
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
traverse(parsedValue.nodes);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
processNode(parsed);
|
|
79
|
+
return parsed.toString();
|
|
80
|
+
}
|
package/src/plugin.ts
CHANGED
|
@@ -1,34 +1,25 @@
|
|
|
1
1
|
import { PluginCreator } from "postcss";
|
|
2
2
|
import { functions } from "./functions";
|
|
3
3
|
import { atRuleHandlers } from "./at-rules";
|
|
4
|
+
import { loadConfig, PluginOptions as ConfigOptions, PluginContext } from "./config";
|
|
5
|
+
import { processFunctions, FunctionMap } from "./parser";
|
|
4
6
|
|
|
5
|
-
export
|
|
6
|
-
functions?: Record<string, (...args: string[]) => string>;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const dynamicFunctionsPlugin: PluginCreator<PluginOptions> = (
|
|
7
|
+
export const dynamicFunctionsPlugin: PluginCreator<ConfigOptions> = (
|
|
10
8
|
opts = {}
|
|
11
9
|
) => {
|
|
12
|
-
const
|
|
10
|
+
const { config, options } = loadConfig(opts);
|
|
11
|
+
const fnMap: FunctionMap = { ...functions, ...opts.functions };
|
|
12
|
+
const context: PluginContext = {
|
|
13
|
+
config,
|
|
14
|
+
options,
|
|
15
|
+
functions: fnMap,
|
|
16
|
+
};
|
|
13
17
|
|
|
14
18
|
return {
|
|
15
19
|
postcssPlugin: "postcss-dynamic-functions",
|
|
16
20
|
|
|
17
21
|
Declaration(decl) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
for (const fnName in fnMap) {
|
|
21
|
-
const regex = new RegExp(`${fnName}\\(([^)]*)\\)`, "g");
|
|
22
|
-
|
|
23
|
-
value = value.replace(regex, (_match, argsStr) => {
|
|
24
|
-
const args = argsStr
|
|
25
|
-
.split(",")
|
|
26
|
-
.map((a: string) => a.trim().replace(/^['"]|['"]$/g, ""));
|
|
27
|
-
return fnMap[fnName](...args);
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
decl.value = value;
|
|
22
|
+
decl.value = processFunctions(decl.value, fnMap, decl, context);
|
|
32
23
|
},
|
|
33
24
|
|
|
34
25
|
// Override AtRule handler to ensure ordered execution
|
|
@@ -36,7 +27,7 @@ export const dynamicFunctionsPlugin: PluginCreator<PluginOptions> = (
|
|
|
36
27
|
// Iterate over handlers in order (array) instead of object spread
|
|
37
28
|
for (const { name, handler } of atRuleHandlers) {
|
|
38
29
|
if (atRule.name === name) {
|
|
39
|
-
handler(atRule);
|
|
30
|
+
handler(atRule, context);
|
|
40
31
|
}
|
|
41
32
|
}
|
|
42
33
|
},
|
package/src/types.ts
CHANGED
|
@@ -1,19 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
ui: {
|
|
3
|
-
theme: {
|
|
4
|
-
hues: Record<string, number>;
|
|
5
|
-
light: {
|
|
6
|
-
colors: Record<string, Color>;
|
|
7
|
-
};
|
|
8
|
-
dark: {
|
|
9
|
-
colors: Record<string, Color>;
|
|
10
|
-
};
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
type Color = {
|
|
16
|
-
lightness: number;
|
|
17
|
-
chroma: number;
|
|
18
|
-
hue: number;
|
|
19
|
-
};
|
|
1
|
+
export type { Config as SeyunaConfig, Color } from "@seyuna/cli/types";
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import postcss from 'postcss';
|
|
3
|
+
import plugin from '../src/index';
|
|
4
|
+
|
|
5
|
+
const mockConfig = {
|
|
6
|
+
ui: {
|
|
7
|
+
theme: {
|
|
8
|
+
hues: {
|
|
9
|
+
primary: "200",
|
|
10
|
+
secondary: "100"
|
|
11
|
+
},
|
|
12
|
+
colors: {
|
|
13
|
+
white: { lightness: 1, chroma: 0, hue: 0 },
|
|
14
|
+
black: { lightness: 0, chroma: 0, hue: 0 }
|
|
15
|
+
},
|
|
16
|
+
light: {
|
|
17
|
+
lightness: 0.66,
|
|
18
|
+
colors: {
|
|
19
|
+
surface: { lightness: 1, chroma: 0, hue: 0 }
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
dark: {
|
|
23
|
+
lightness: 0.66,
|
|
24
|
+
colors: {
|
|
25
|
+
surface: { lightness: 0, chroma: 0, hue: 0 }
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
breakpoints: {
|
|
29
|
+
tablet: "48rem"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
async function run(input: string, opts: any = {}) {
|
|
36
|
+
const mergedOpts = { config: mockConfig, ...opts };
|
|
37
|
+
return postcss([plugin(mergedOpts)]).process(input, { from: undefined });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('Seyuna PostCSS Plugin', () => {
|
|
41
|
+
it('processes sc() function', async () => {
|
|
42
|
+
const input = '.test { color: sc(primary); }';
|
|
43
|
+
const output = '.test { color: oklch(var(--lightness) var(--chroma) var(--primary-hue) / 1)';
|
|
44
|
+
const result = await run(input);
|
|
45
|
+
expect(result.css).toContain(output);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('processes sc() with alpha', async () => {
|
|
49
|
+
const input = '.test { color: sc(primary, 0.5); }';
|
|
50
|
+
const output = '.test { color: oklch(var(--lightness) var(--chroma) var(--primary-hue) / 0.5)';
|
|
51
|
+
const result = await run(input);
|
|
52
|
+
expect(result.css).toContain(output);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('processes alpha() function with color name', async () => {
|
|
56
|
+
const input = '.test { color: alpha(primary, 0.5); }';
|
|
57
|
+
const output = 'color: oklch(var(--lightness) var(--chroma) var(--primary-hue) / 0.5)';
|
|
58
|
+
const result = await run(input);
|
|
59
|
+
expect(result.css).toContain(output);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('processes lighten() function with color name', async () => {
|
|
63
|
+
const input = '.test { color: lighten(primary, 0.1); }';
|
|
64
|
+
const output = 'color: oklch(calc(var(--lightness) + 0.1) var(--chroma) var(--primary-hue) / 1)';
|
|
65
|
+
const result = await run(input);
|
|
66
|
+
expect(result.css).toContain(output);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('processes darken() function with color name', async () => {
|
|
70
|
+
const input = '.test { color: darken(primary, 0.1); }';
|
|
71
|
+
const output = 'color: oklch(calc(var(--lightness) - 0.1) var(--chroma) var(--primary-hue) / 1)';
|
|
72
|
+
const result = await run(input);
|
|
73
|
+
expect(result.css).toContain(output);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('processes theme() function', async () => {
|
|
77
|
+
// We pass the config via opts for testing
|
|
78
|
+
const input = '.test { border-radius: theme(ui.theme.breakpoints.tablet); }';
|
|
79
|
+
const result = await run(input, { config: mockConfig });
|
|
80
|
+
expect(result.css).toContain('.test { border-radius: 48rem');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('processes @xs container at-rule', async () => {
|
|
84
|
+
const input = '@xs { .box { color: red; } }';
|
|
85
|
+
const result = await run(input);
|
|
86
|
+
expect(result.css).toContain('@container (min-width: 20rem)');
|
|
87
|
+
expect(result.css).toContain('.box { color: red');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('processes @light at-rule', async () => {
|
|
91
|
+
const input = '@light { .test { color: black; } }';
|
|
92
|
+
const result = await run(input);
|
|
93
|
+
expect(result.css).toContain('@media (prefers-color-scheme: light)');
|
|
94
|
+
expect(result.css).toContain('[data-mode="system"] &');
|
|
95
|
+
expect(result.css).toContain('[data-mode="light"] &');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('processes alpha() with standard color', async () => {
|
|
99
|
+
const input = '.test { color: alpha(primary, 0.5); }';
|
|
100
|
+
const output = 'color: oklch(var(--lightness) var(--chroma) var(--primary-hue) / 0.5)';
|
|
101
|
+
const result = await run(input, { config: mockConfig });
|
|
102
|
+
expect(result.css).toContain(output);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('processes alpha() with fixed color', async () => {
|
|
106
|
+
const input = '.test { color: alpha(surface, 0.5); }';
|
|
107
|
+
const output = 'color: oklch(var(--surface-lightness) var(--surface-chroma) var(--surface-hue) / 0.5)';
|
|
108
|
+
const result = await run(input, { config: mockConfig });
|
|
109
|
+
expect(result.css).toContain(output);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('processes contrast() with fixed color', async () => {
|
|
113
|
+
const input = '.test { color: contrast(surface); }';
|
|
114
|
+
const output = 'color: oklch(calc((var(--surface-lightness) - 0.6) * -1000) 0 0)';
|
|
115
|
+
const result = await run(input, { config: mockConfig });
|
|
116
|
+
expect(result.css).toContain(output);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('processes contrast() with standard color', async () => {
|
|
120
|
+
const input = '.test { color: contrast(primary); }';
|
|
121
|
+
const output = 'color: oklch(calc((var(--lightness) - 0.6) * -1000) 0 0)';
|
|
122
|
+
const result = await run(input, { config: mockConfig });
|
|
123
|
+
expect(result.css).toContain(output);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('throws error for unknown standard color in strict mode', async () => {
|
|
127
|
+
const input = '.test { color: sc(unknown); }';
|
|
128
|
+
await expect(run(input, { config: mockConfig, strict: true }))
|
|
129
|
+
.rejects.toThrow(/Standard color 'unknown' not found/);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('throws error for unknown fixed color in strict mode', async () => {
|
|
133
|
+
const input = '.test { color: fc(unknown); }';
|
|
134
|
+
await expect(run(input, { config: mockConfig, strict: true }))
|
|
135
|
+
.rejects.toThrow(/Fixed color 'unknown' not found/);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('throws error for unknown color in alpha() in strict mode', async () => {
|
|
139
|
+
const input = '.test { color: alpha(unknown, 0.5); }';
|
|
140
|
+
await expect(run(input, { config: mockConfig, strict: true }))
|
|
141
|
+
.rejects.toThrow(/Color 'unknown' not found in seyuna.json/);
|
|
142
|
+
});
|
|
143
|
+
});
|
package/tsconfig.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"target": "ES2020",
|
|
4
|
-
"module": "
|
|
4
|
+
"module": "ES2020",
|
|
5
5
|
"lib": ["ES2020"],
|
|
6
6
|
"declaration": true,
|
|
7
7
|
"outDir": "dist",
|
|
8
8
|
"strict": true,
|
|
9
|
-
"moduleResolution": "
|
|
9
|
+
"moduleResolution": "bundler",
|
|
10
10
|
"esModuleInterop": true,
|
|
11
11
|
"skipLibCheck": true
|
|
12
12
|
},
|