@seyuna/postcss 1.0.0-canary.13 → 1.0.0-canary.15
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 +16 -0
- package/README.md +179 -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 +5 -4
- package/dist/at-rules/color.js +22 -87
- package/dist/at-rules/conditional.d.ts +6 -0
- package/dist/at-rules/conditional.js +29 -0
- package/dist/at-rules/container.d.ts +2 -1
- package/dist/at-rules/container.js +12 -8
- package/dist/at-rules/custom-media.d.ts +15 -0
- package/dist/at-rules/custom-media.js +40 -0
- package/dist/at-rules/index.d.ts +3 -2
- package/dist/at-rules/index.js +22 -22
- package/dist/at-rules/mixin.d.ts +10 -0
- package/dist/at-rules/mixin.js +37 -0
- package/dist/config.d.ts +20 -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 +103 -27
- 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 +11 -0
- package/dist/helpers.js +54 -0
- 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 +17 -22
- package/dist/types.d.ts +1 -19
- package/dist/types.js +1 -2
- package/package.json +14 -5
- package/src/at-rules/color-scheme.ts +52 -0
- package/src/at-rules/color.ts +20 -87
- package/src/at-rules/conditional.ts +34 -0
- package/src/at-rules/container.ts +13 -3
- package/src/at-rules/custom-media.ts +50 -0
- package/src/at-rules/index.ts +13 -6
- package/src/at-rules/mixin.ts +46 -0
- package/src/config.ts +74 -0
- package/src/errors.ts +23 -0
- package/src/functions/color.ts +120 -26
- package/src/functions/index.ts +9 -4
- package/src/functions/theme.ts +20 -0
- package/src/helpers.ts +74 -0
- package/src/index.ts +3 -1
- package/src/parser.ts +80 -0
- package/src/plugin.ts +13 -21
- package/src/types.ts +1 -19
- package/tests/plugin.test.ts +251 -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/dist/config.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { SeyunaConfig } from './types';
|
|
2
|
+
import { FunctionMap } from './parser';
|
|
3
|
+
export interface PluginOptions {
|
|
4
|
+
configPath?: string;
|
|
5
|
+
modeAttribute?: string;
|
|
6
|
+
strict?: boolean;
|
|
7
|
+
config?: SeyunaConfig;
|
|
8
|
+
functions?: FunctionMap;
|
|
9
|
+
}
|
|
10
|
+
import { ChildNode } from 'postcss';
|
|
11
|
+
export interface PluginContext {
|
|
12
|
+
config: SeyunaConfig;
|
|
13
|
+
options: Required<PluginOptions>;
|
|
14
|
+
functions: FunctionMap;
|
|
15
|
+
mixins: Record<string, ChildNode[]>;
|
|
16
|
+
}
|
|
17
|
+
export declare function loadConfig(options?: PluginOptions): {
|
|
18
|
+
config: SeyunaConfig;
|
|
19
|
+
options: Required<PluginOptions>;
|
|
20
|
+
};
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
const DEFAULT_OPTIONS = {
|
|
4
|
+
configPath: 'seyuna.json',
|
|
5
|
+
modeAttribute: 'data-mode',
|
|
6
|
+
strict: false,
|
|
7
|
+
config: undefined,
|
|
8
|
+
functions: undefined,
|
|
9
|
+
};
|
|
10
|
+
let cachedConfig = null;
|
|
11
|
+
let cachedConfigPath = null;
|
|
12
|
+
export function loadConfig(options = {}) {
|
|
13
|
+
const mergedOptions = { ...DEFAULT_OPTIONS, ...options };
|
|
14
|
+
if (mergedOptions.config) {
|
|
15
|
+
return { config: mergedOptions.config, options: mergedOptions };
|
|
16
|
+
}
|
|
17
|
+
const configPath = path.resolve(process.cwd(), mergedOptions.configPath);
|
|
18
|
+
// Cache config if it's the same path
|
|
19
|
+
if (cachedConfig && cachedConfigPath === configPath) {
|
|
20
|
+
return { config: cachedConfig, options: mergedOptions };
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
if (!fs.existsSync(configPath)) {
|
|
24
|
+
if (mergedOptions.strict) {
|
|
25
|
+
throw new Error(`Seyuna config not found at ${configPath}`);
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
config: { ui: { theme: { hues: {}, light: { colors: {} }, dark: { colors: {} } } } },
|
|
29
|
+
options: mergedOptions,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
const data = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
|
33
|
+
cachedConfig = data;
|
|
34
|
+
cachedConfigPath = configPath;
|
|
35
|
+
return { config: data, options: mergedOptions };
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
if (mergedOptions.strict) {
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
console.warn(`[Seyuna PostCSS] Warning: Failed to load config: ${error instanceof Error ? error.message : String(error)}`);
|
|
42
|
+
return {
|
|
43
|
+
config: { ui: { theme: { hues: {}, light: { colors: {} }, dark: { colors: {} } } } },
|
|
44
|
+
options: mergedOptions,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Node } from 'postcss';
|
|
2
|
+
import { PluginContext } from './config';
|
|
3
|
+
export declare function reportError(message: string, node: Node, context: PluginContext, options?: {
|
|
4
|
+
word?: string;
|
|
5
|
+
index?: number;
|
|
6
|
+
}): void;
|
|
7
|
+
export declare function reportWarning(message: string, node: Node): void;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function reportError(message, node, context, options = {}) {
|
|
2
|
+
const { options: pluginOptions } = context;
|
|
3
|
+
const formattedMessage = `[Seyuna PostCSS] ${message}`;
|
|
4
|
+
if (pluginOptions.strict) {
|
|
5
|
+
throw node.error(formattedMessage, options);
|
|
6
|
+
}
|
|
7
|
+
else {
|
|
8
|
+
reportWarning(formattedMessage, node);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function reportWarning(message, node) {
|
|
12
|
+
const result = node.root().toResult();
|
|
13
|
+
node.warn(result, `[Seyuna PostCSS] ${message}`);
|
|
14
|
+
}
|
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function
|
|
1
|
+
import { PluginContext } from "../config";
|
|
2
|
+
export declare function sc(context: PluginContext, name: string, alpha?: string, lightness?: string, chroma?: string): string;
|
|
3
|
+
export declare function fc(context: PluginContext, name: string, alpha?: string, lightness?: string, chroma?: string): string;
|
|
4
|
+
export declare function alpha(context: PluginContext, color: string, value: string): string;
|
|
5
|
+
export declare function lighten(context: PluginContext, color: string, amount: string): string;
|
|
6
|
+
export declare function darken(context: PluginContext, color: string, amount: string): string;
|
|
7
|
+
export declare function contrast(context: PluginContext, color: string): string;
|
package/dist/functions/color.js
CHANGED
|
@@ -1,34 +1,110 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Utility to parse an oklch string and return its parts
|
|
3
|
+
*/
|
|
4
|
+
function parseOklch(color) {
|
|
5
|
+
const match = color.match(/oklch\(([^ ]+) ([^ ]+) ([^ /]+)(?: \/ ([^)]+))?\)/);
|
|
6
|
+
if (!match)
|
|
7
|
+
return null;
|
|
8
|
+
return {
|
|
9
|
+
l: match[1],
|
|
10
|
+
c: match[2],
|
|
11
|
+
h: match[3],
|
|
12
|
+
a: match[4] || "1",
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function sc(context, name, alpha, lightness, chroma) {
|
|
16
|
+
let a = alpha && alpha !== "null" ? alpha : "1";
|
|
17
|
+
let l = lightness && lightness !== "null" ? lightness : "var(--lightness)";
|
|
18
|
+
let c = chroma && chroma !== "null" ? chroma : "var(--chroma)";
|
|
19
|
+
let h = `var(--${name}-hue)`;
|
|
20
|
+
return `oklch(${l} ${c} ${h} / ${a})`;
|
|
21
|
+
}
|
|
22
|
+
export function fc(context, name, alpha, lightness, chroma) {
|
|
23
|
+
let a = alpha && alpha !== "null" ? alpha : "1";
|
|
24
|
+
let l = lightness && lightness !== "null" ? lightness : `var(--${name}-lightness)`;
|
|
25
|
+
let c = chroma && chroma !== "null" ? chroma : `var(--${name}-chroma)`;
|
|
26
|
+
let h = `var(--${name}-hue)`;
|
|
27
|
+
return `oklch(${l} ${c} ${h} / ${a})`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Resolves a color name to its CSS variables based on its type (standard or fixed)
|
|
31
|
+
*/
|
|
32
|
+
function getColorVariables(context, color) {
|
|
33
|
+
const { config } = context;
|
|
34
|
+
const hues = config?.ui?.theme?.hues || {};
|
|
35
|
+
const colors = config?.ui?.theme?.colors || {};
|
|
36
|
+
const lightColors = config?.ui?.theme?.light?.colors || {};
|
|
37
|
+
const darkColors = config?.ui?.theme?.dark?.colors || {};
|
|
38
|
+
// Check if it's a standard color (sc)
|
|
39
|
+
if (color in hues) {
|
|
40
|
+
return {
|
|
41
|
+
l: "var(--lightness)",
|
|
42
|
+
c: "var(--chroma)",
|
|
43
|
+
h: `var(--${color}-hue)`,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
// Check if it's a fixed color (fc)
|
|
47
|
+
if (color in colors || color in lightColors || color in darkColors) {
|
|
48
|
+
return {
|
|
49
|
+
l: `var(--${color}-lightness)`,
|
|
50
|
+
c: `var(--${color}-chroma)`,
|
|
51
|
+
h: `var(--${color}-hue)`,
|
|
52
|
+
};
|
|
11
53
|
}
|
|
12
|
-
if
|
|
13
|
-
|
|
54
|
+
// Fallback to assume it's a fixed color if unknown but used as a name
|
|
55
|
+
return {
|
|
56
|
+
l: `var(--${color}-lightness)`,
|
|
57
|
+
c: `var(--${color}-chroma)`,
|
|
58
|
+
h: `var(--${color}-hue)`,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export function alpha(context, color, value) {
|
|
62
|
+
const parsed = parseOklch(color);
|
|
63
|
+
if (parsed) {
|
|
64
|
+
return `oklch(${parsed.l} ${parsed.c} ${parsed.h} / ${value})`;
|
|
14
65
|
}
|
|
15
|
-
|
|
16
|
-
|
|
66
|
+
const { l, c, h } = getColorVariables(context, color);
|
|
67
|
+
return `oklch(${l} ${c} ${h} / ${value})`;
|
|
68
|
+
}
|
|
69
|
+
export function lighten(context, color, amount) {
|
|
70
|
+
const parsed = parseOklch(color);
|
|
71
|
+
if (parsed) {
|
|
72
|
+
if (parsed.l.startsWith('var(')) {
|
|
73
|
+
return `oklch(calc(${parsed.l} + ${amount}) ${parsed.c} ${parsed.h} / ${parsed.a})`;
|
|
74
|
+
}
|
|
75
|
+
const lValue = parseFloat(parsed.l);
|
|
76
|
+
const amtValue = parseFloat(amount);
|
|
77
|
+
return `oklch(${Math.min(1, lValue + amtValue)} ${parsed.c} ${parsed.h} / ${parsed.a})`;
|
|
17
78
|
}
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
79
|
+
const { l, c, h } = getColorVariables(context, color);
|
|
80
|
+
return `oklch(calc(${l} + ${amount}) ${c} ${h} / 1)`;
|
|
81
|
+
}
|
|
82
|
+
export function darken(context, color, amount) {
|
|
83
|
+
const parsed = parseOklch(color);
|
|
84
|
+
if (parsed) {
|
|
85
|
+
if (parsed.l.startsWith('var(')) {
|
|
86
|
+
return `oklch(calc(${parsed.l} - ${amount}) ${parsed.c} ${parsed.h} / ${parsed.a})`;
|
|
87
|
+
}
|
|
88
|
+
const lValue = parseFloat(parsed.l);
|
|
89
|
+
const amtValue = parseFloat(amount);
|
|
90
|
+
return `oklch(${Math.max(0, lValue - amtValue)} ${parsed.c} ${parsed.h} / ${parsed.a})`;
|
|
26
91
|
}
|
|
27
|
-
|
|
28
|
-
|
|
92
|
+
const { l, c, h } = getColorVariables(context, color);
|
|
93
|
+
return `oklch(calc(${l} - ${amount}) ${c} ${h} / 1)`;
|
|
94
|
+
}
|
|
95
|
+
export function contrast(context, color) {
|
|
96
|
+
const parsed = parseOklch(color);
|
|
97
|
+
let l;
|
|
98
|
+
if (parsed) {
|
|
99
|
+
l = parsed.l;
|
|
29
100
|
}
|
|
30
|
-
|
|
31
|
-
|
|
101
|
+
else {
|
|
102
|
+
const vars = getColorVariables(context, color);
|
|
103
|
+
l = vars.l;
|
|
32
104
|
}
|
|
33
|
-
|
|
105
|
+
// Dynamic CSS contrast logic:
|
|
106
|
+
// (L - 0.6) * -1000 will be:
|
|
107
|
+
// - very negative if L > 0.6 (clamped to 0 / black)
|
|
108
|
+
// - very positive if L < 0.6 (clamped to 1 / white)
|
|
109
|
+
return `oklch(calc((${l} - 0.6) * -1000) 0 0)`;
|
|
34
110
|
}
|
package/dist/functions/index.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
fc: color_1.fc,
|
|
12
|
-
spacing: spacing_1.default,
|
|
1
|
+
import { sc, fc, alpha, lighten, darken, contrast } from "./color";
|
|
2
|
+
import { theme } from "./theme";
|
|
3
|
+
export const functions = {
|
|
4
|
+
sc,
|
|
5
|
+
fc,
|
|
6
|
+
alpha,
|
|
7
|
+
lighten,
|
|
8
|
+
darken,
|
|
9
|
+
contrast,
|
|
10
|
+
theme,
|
|
13
11
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accesses values from the Seyuna configuration using dot notation
|
|
3
|
+
* Example: theme(ui.theme.breakpoints.tablet)
|
|
4
|
+
*/
|
|
5
|
+
export function theme(context, path) {
|
|
6
|
+
const parts = path.split('.');
|
|
7
|
+
let current = context.config;
|
|
8
|
+
for (const part of parts) {
|
|
9
|
+
if (current && typeof current === 'object' && part in current) {
|
|
10
|
+
current = current[part];
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
return path; // Return original path if not found
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return String(current);
|
|
17
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Rule, ChildNode, AtRule } from "postcss";
|
|
2
|
+
import { PluginContext } from "./config";
|
|
3
|
+
/**
|
|
4
|
+
* Helper: clone nodes and replace {name} placeholders safely
|
|
5
|
+
* Returns only valid Rules or Declarations (never raw AtRules)
|
|
6
|
+
*/
|
|
7
|
+
export declare function cloneNodes(nodes: ChildNode[], name: string, context: PluginContext): ChildNode[];
|
|
8
|
+
/**
|
|
9
|
+
* Generate CSS rules from a list of names
|
|
10
|
+
*/
|
|
11
|
+
export declare function generateRules(names: string[], atRule: AtRule, context: PluginContext): Rule[];
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Rule } from "postcss";
|
|
2
|
+
import { processFunctions } from "./parser";
|
|
3
|
+
/**
|
|
4
|
+
* Helper: clone nodes and replace {name} placeholders safely
|
|
5
|
+
* Returns only valid Rules or Declarations (never raw AtRules)
|
|
6
|
+
*/
|
|
7
|
+
export function cloneNodes(nodes, name, context) {
|
|
8
|
+
const { functions: fnMap } = context;
|
|
9
|
+
return nodes.flatMap((node) => {
|
|
10
|
+
const cloned = node.clone();
|
|
11
|
+
if (cloned.type === "decl") {
|
|
12
|
+
const decl = cloned;
|
|
13
|
+
let value = decl.value.replace(/\{name\}/g, name);
|
|
14
|
+
// Process functions using the robust parser
|
|
15
|
+
decl.value = processFunctions(value, fnMap, decl, context);
|
|
16
|
+
return decl;
|
|
17
|
+
}
|
|
18
|
+
if (cloned.type === "rule") {
|
|
19
|
+
const rule = cloned;
|
|
20
|
+
if (!rule.selector)
|
|
21
|
+
return [];
|
|
22
|
+
rule.selector = rule.selector.replace(/\{name\}/g, name);
|
|
23
|
+
// Recursively clone child nodes and only keep valid rules/decls
|
|
24
|
+
rule.nodes = cloneNodes(rule.nodes || [], name, context).filter((n) => n.type === "rule" || n.type === "decl");
|
|
25
|
+
if (!rule.nodes.length)
|
|
26
|
+
return [];
|
|
27
|
+
return rule;
|
|
28
|
+
}
|
|
29
|
+
// Ignore AtRules inside rules — they must be processed first
|
|
30
|
+
return [];
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Generate CSS rules from a list of names
|
|
35
|
+
*/
|
|
36
|
+
export function generateRules(names, atRule, context) {
|
|
37
|
+
const nodes = atRule.nodes ?? [];
|
|
38
|
+
const generatedRules = [];
|
|
39
|
+
for (const name of names) {
|
|
40
|
+
const rule = new Rule({
|
|
41
|
+
selector: `&.${name}`,
|
|
42
|
+
source: atRule.source,
|
|
43
|
+
});
|
|
44
|
+
cloneNodes(nodes, name, context).forEach((n) => {
|
|
45
|
+
if (n.type === "rule" && n.selector && n.nodes?.length)
|
|
46
|
+
rule.append(n);
|
|
47
|
+
if (n.type === "decl")
|
|
48
|
+
rule.append(n);
|
|
49
|
+
});
|
|
50
|
+
if (rule.nodes.length)
|
|
51
|
+
generatedRules.push(rule);
|
|
52
|
+
}
|
|
53
|
+
return generatedRules;
|
|
54
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
declare const
|
|
1
|
+
declare const plugin: import("postcss").PluginCreator<import("./config").PluginOptions> & {
|
|
2
2
|
postcss: boolean;
|
|
3
3
|
functions: Record<string, import("./functions").FnHandler>;
|
|
4
4
|
};
|
|
5
|
-
export
|
|
5
|
+
export default plugin;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { dynamicFunctionsPlugin } from './plugin';
|
|
2
|
+
import { functions } from './functions';
|
|
3
|
+
// CommonJS-friendly export
|
|
4
|
+
const plugin = Object.assign(dynamicFunctionsPlugin, {
|
|
5
5
|
postcss: true,
|
|
6
|
-
functions
|
|
6
|
+
functions
|
|
7
7
|
});
|
|
8
|
+
export default plugin;
|
package/dist/parser.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Node } from 'postcss';
|
|
2
|
+
import { PluginContext } from './config';
|
|
3
|
+
export type FunctionMap = Record<string, (context: PluginContext, ...args: string[]) => string>;
|
|
4
|
+
export declare function processFunctions(value: string, fnMap: FunctionMap, node: Node, context: PluginContext): string;
|
package/dist/parser.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import valueParser from 'postcss-value-parser';
|
|
2
|
+
import { reportError } from './errors';
|
|
3
|
+
export function processFunctions(value, fnMap, node, context) {
|
|
4
|
+
const parsed = valueParser(value);
|
|
5
|
+
// Helper to process nodes recursively (inner-first)
|
|
6
|
+
function processNode(parsedValue) {
|
|
7
|
+
parsedValue.walk((nodeIter) => {
|
|
8
|
+
// If nested nodes exist (e.g., in a function), process them first
|
|
9
|
+
if (nodeIter.type === 'function' && nodeIter.nodes.length > 0) {
|
|
10
|
+
// Here we recursively process child nodes before handling the current function
|
|
11
|
+
// However, value-parser's walk is already a visitor. To do inner-first,
|
|
12
|
+
// we can either walk in reverse or specifically handle children.
|
|
13
|
+
// A simpler way: since walk visits all, we can check if it's a function
|
|
14
|
+
// and if it's one we handle, we process its arguments.
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
// Actually, a manual walk is safer for inner-first replacement
|
|
18
|
+
function traverse(nodes) {
|
|
19
|
+
for (const nodeIter of nodes) {
|
|
20
|
+
if (nodeIter.type === 'function') {
|
|
21
|
+
// Process inner functions first
|
|
22
|
+
traverse(nodeIter.nodes);
|
|
23
|
+
// Now handle this function if it's in our map
|
|
24
|
+
if (fnMap[nodeIter.value]) {
|
|
25
|
+
const handler = fnMap[nodeIter.value];
|
|
26
|
+
const args = [];
|
|
27
|
+
let currentArg = '';
|
|
28
|
+
nodeIter.nodes.forEach((argNode) => {
|
|
29
|
+
if (argNode.type === 'div' && argNode.value === ',') {
|
|
30
|
+
args.push(currentArg.trim());
|
|
31
|
+
currentArg = '';
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
currentArg += valueParser.stringify(argNode);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
if (currentArg) {
|
|
38
|
+
args.push(currentArg.trim());
|
|
39
|
+
}
|
|
40
|
+
const cleanedArgs = args.map(arg => arg.replace(/^['"]|['"]$/g, ''));
|
|
41
|
+
try {
|
|
42
|
+
const result = handler(context, ...cleanedArgs);
|
|
43
|
+
// Replace function with its result
|
|
44
|
+
nodeIter.type = 'word';
|
|
45
|
+
nodeIter.value = result;
|
|
46
|
+
nodeIter.nodes = []; // Clear children
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
reportError(`Failed to process function "${nodeIter.value}": ${error instanceof Error ? error.message : String(error)}`, node, context);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
traverse(parsedValue.nodes);
|
|
56
|
+
}
|
|
57
|
+
processNode(parsed);
|
|
58
|
+
return parsed.toString();
|
|
59
|
+
}
|
package/dist/plugin.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
1
|
import { PluginCreator } from "postcss";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
export declare const dynamicFunctionsPlugin: PluginCreator<PluginOptions>;
|
|
2
|
+
import { PluginOptions as ConfigOptions } from "./config";
|
|
3
|
+
export declare const dynamicFunctionsPlugin: PluginCreator<ConfigOptions>;
|
package/dist/plugin.js
CHANGED
|
@@ -1,35 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const fnMap = { ...
|
|
1
|
+
import { functions } from "./functions";
|
|
2
|
+
import { atRuleHandlers } from "./at-rules";
|
|
3
|
+
import { loadConfig } from "./config";
|
|
4
|
+
import { processFunctions } from "./parser";
|
|
5
|
+
export const dynamicFunctionsPlugin = (opts = {}) => {
|
|
6
|
+
const { config, options } = loadConfig(opts);
|
|
7
|
+
const fnMap = { ...functions, ...opts.functions };
|
|
8
|
+
const context = {
|
|
9
|
+
config,
|
|
10
|
+
options,
|
|
11
|
+
functions: fnMap,
|
|
12
|
+
mixins: {},
|
|
13
|
+
};
|
|
8
14
|
return {
|
|
9
15
|
postcssPlugin: "postcss-dynamic-functions",
|
|
10
16
|
Declaration(decl) {
|
|
11
|
-
|
|
12
|
-
for (const fnName in fnMap) {
|
|
13
|
-
const regex = new RegExp(`${fnName}\\(([^)]*)\\)`, "g");
|
|
14
|
-
value = value.replace(regex, (_match, argsStr) => {
|
|
15
|
-
const args = argsStr
|
|
16
|
-
.split(",")
|
|
17
|
-
.map((a) => a.trim().replace(/^['"]|['"]$/g, ""));
|
|
18
|
-
return fnMap[fnName](...args);
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
decl.value = value;
|
|
17
|
+
decl.value = processFunctions(decl.value, fnMap, decl, context);
|
|
22
18
|
},
|
|
23
19
|
// Override AtRule handler to ensure ordered execution
|
|
24
20
|
AtRule(atRule) {
|
|
25
21
|
// Iterate over handlers in order (array) instead of object spread
|
|
26
|
-
for (const { name, handler } of
|
|
22
|
+
for (const { name, handler } of atRuleHandlers) {
|
|
27
23
|
if (atRule.name === name) {
|
|
28
|
-
handler(atRule);
|
|
24
|
+
handler(atRule, context);
|
|
29
25
|
}
|
|
30
26
|
}
|
|
31
27
|
},
|
|
32
28
|
};
|
|
33
29
|
};
|
|
34
|
-
|
|
35
|
-
exports.dynamicFunctionsPlugin.postcss = true;
|
|
30
|
+
dynamicFunctionsPlugin.postcss = true;
|
package/dist/types.d.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
|
-
type Color = {
|
|
15
|
-
lightness: number;
|
|
16
|
-
chroma: number;
|
|
17
|
-
hue: number;
|
|
18
|
-
};
|
|
19
|
-
export {};
|
|
1
|
+
export type { Config as SeyunaConfig, Color } from "@seyuna/cli/types";
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seyuna/postcss",
|
|
3
|
-
"version": "1.0.0-canary.
|
|
3
|
+
"version": "1.0.0-canary.15",
|
|
4
4
|
"description": "Seyuna UI's postcss plugin",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"build": "tsc"
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"dev": "tsc -w",
|
|
10
|
+
"test": "vitest",
|
|
11
|
+
"test:run": "vitest run"
|
|
9
12
|
},
|
|
10
13
|
"keywords": [
|
|
11
14
|
"postcss",
|
|
@@ -17,6 +20,10 @@
|
|
|
17
20
|
"type": "git",
|
|
18
21
|
"url": "git+https://github.com/seyuna-corp/seyuna-postcss.git"
|
|
19
22
|
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@seyuna/cli": "^1.0.0-canary.28",
|
|
25
|
+
"postcss-value-parser": "^4.2.0"
|
|
26
|
+
},
|
|
20
27
|
"peerDependencies": {
|
|
21
28
|
"postcss": "^8.5.6"
|
|
22
29
|
},
|
|
@@ -26,11 +33,13 @@
|
|
|
26
33
|
"@semantic-release/exec": "^7.1.0",
|
|
27
34
|
"@semantic-release/git": "^10.0.1",
|
|
28
35
|
"@semantic-release/github": "^11.0.3",
|
|
29
|
-
"@semantic-release/npm": "^
|
|
36
|
+
"@semantic-release/npm": "^13.1.3",
|
|
30
37
|
"@semantic-release/release-notes-generator": "^14.0.3",
|
|
31
38
|
"@types/node": "^20.0.0",
|
|
32
39
|
"postcss": "^8.5.6",
|
|
33
|
-
"
|
|
34
|
-
"
|
|
40
|
+
"postcss-selector-parser": "^7.1.0",
|
|
41
|
+
"semantic-release": "^25.0.2",
|
|
42
|
+
"typescript": "^5.0.0",
|
|
43
|
+
"vitest": "^4.0.16"
|
|
35
44
|
}
|
|
36
45
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Rule, AtRule, ChildNode } from "postcss";
|
|
2
|
+
import { PluginContext } from "../config";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Custom PostCSS plugin handler factory for `@light` and `@dark` at-rules.
|
|
6
|
+
*/
|
|
7
|
+
function createColorSchemeHandler(scheme: 'light' | 'dark') {
|
|
8
|
+
return (atRule: AtRule, context: PluginContext) => {
|
|
9
|
+
const { options } = context;
|
|
10
|
+
const modeAttribute = options.modeAttribute;
|
|
11
|
+
const clonedNodes: ChildNode[] = [];
|
|
12
|
+
|
|
13
|
+
// Clone all child nodes inside the block
|
|
14
|
+
atRule.each((node: ChildNode) => {
|
|
15
|
+
clonedNodes.push(node.clone());
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Rule 1: [data-mode="scheme"] & { ... } (Explicit mode)
|
|
20
|
+
*/
|
|
21
|
+
const explicitRule = new Rule({
|
|
22
|
+
selector: `[${modeAttribute}="${scheme}"] &`,
|
|
23
|
+
source: atRule.source,
|
|
24
|
+
});
|
|
25
|
+
clonedNodes.forEach((node) => explicitRule.append(node.clone()));
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Rule 2: @media (prefers-color-scheme: scheme) { [data-mode="system"] & { ... } } (System preference)
|
|
29
|
+
*/
|
|
30
|
+
const mediaAtRule = new AtRule({
|
|
31
|
+
name: "media",
|
|
32
|
+
params: `(prefers-color-scheme: ${scheme})`,
|
|
33
|
+
source: atRule.source,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const systemRule = new Rule({
|
|
37
|
+
selector: `[${modeAttribute}="system"] &`,
|
|
38
|
+
source: atRule.source,
|
|
39
|
+
});
|
|
40
|
+
clonedNodes.forEach((node) => systemRule.append(node.clone()));
|
|
41
|
+
|
|
42
|
+
mediaAtRule.append(systemRule);
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Replace the original at-rule with the generated rules.
|
|
46
|
+
*/
|
|
47
|
+
atRule.replaceWith(mediaAtRule, explicitRule);
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const light = createColorSchemeHandler('light');
|
|
52
|
+
export const dark = createColorSchemeHandler('dark');
|