@seyuna/postcss 1.0.0-canary.2 → 1.0.0-canary.20

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.
Files changed (58) hide show
  1. package/.github/workflows/release.yml +1 -1
  2. package/CHANGELOG.md +139 -0
  3. package/README.md +275 -0
  4. package/dist/at-rules/color-scheme.d.ts +4 -0
  5. package/dist/at-rules/color-scheme.js +43 -0
  6. package/dist/at-rules/color.d.ts +10 -0
  7. package/dist/at-rules/color.js +30 -0
  8. package/dist/at-rules/container.d.ts +19 -0
  9. package/dist/at-rules/container.js +48 -0
  10. package/dist/at-rules/index.d.ts +7 -3
  11. package/dist/at-rules/index.js +16 -13
  12. package/dist/config.d.ts +6 -0
  13. package/dist/config.js +47 -0
  14. package/dist/errors.d.ts +7 -0
  15. package/dist/errors.js +14 -0
  16. package/dist/functions/color.d.ts +7 -1
  17. package/dist/functions/color.js +59 -14
  18. package/dist/functions/index.d.ts +2 -1
  19. package/dist/functions/index.js +10 -11
  20. package/dist/functions/theme.d.ts +6 -0
  21. package/dist/functions/theme.js +17 -0
  22. package/dist/helpers.d.ts +11 -0
  23. package/dist/helpers.js +54 -0
  24. package/dist/index.d.ts +3 -3
  25. package/dist/index.js +6 -5
  26. package/dist/parser.d.ts +4 -0
  27. package/dist/parser.js +59 -0
  28. package/dist/plugin.d.ts +1 -3
  29. package/dist/plugin.js +22 -22
  30. package/dist/types.d.ts +59 -0
  31. package/dist/types.js +3 -0
  32. package/package.json +15 -5
  33. package/release.config.mjs +6 -1
  34. package/src/at-rules/color-scheme.ts +52 -0
  35. package/src/at-rules/color.ts +33 -0
  36. package/src/at-rules/container.ts +57 -0
  37. package/src/at-rules/index.ts +23 -9
  38. package/src/config.ts +59 -0
  39. package/src/errors.ts +23 -0
  40. package/src/functions/color.ts +80 -14
  41. package/src/functions/index.ts +11 -5
  42. package/src/functions/theme.ts +20 -0
  43. package/src/helpers.ts +74 -0
  44. package/src/index.ts +5 -3
  45. package/src/parser.ts +81 -0
  46. package/src/plugin.ts +21 -23
  47. package/src/types.ts +72 -0
  48. package/tests/plugin.test.ts +143 -0
  49. package/tsconfig.json +2 -2
  50. package/dist/at-rules/dark.d.ts +0 -2
  51. package/dist/at-rules/dark.js +0 -28
  52. package/dist/at-rules/light.d.ts +0 -2
  53. package/dist/at-rules/light.js +0 -28
  54. package/dist/functions/spacing.d.ts +0 -1
  55. package/dist/functions/spacing.js +0 -6
  56. package/src/at-rules/dark.ts +0 -33
  57. package/src/at-rules/light.ts +0 -33
  58. package/src/functions/spacing.ts +0 -3
@@ -1,11 +1,25 @@
1
- import dark from "./dark";
2
- import light from "./light";
3
- import type { AtRule } from "postcss";
1
+ import { AtRule } from "postcss";
2
+ import { eachStandardColor, eachFixedColor } from "./color.js";
3
+ import container from "./container.js";
4
+ import { light, dark } from "./color-scheme.js";
5
+ import { PluginContext } from "../types.js";
4
6
 
5
- export type AtRuleHandler = (atRule: AtRule) => void;
7
+ // Each handler has a name (matches the at-rule) and the function
8
+ export interface AtRuleHandler {
9
+ name: string;
10
+ handler: (atRule: AtRule, context: PluginContext) => void;
11
+ }
6
12
 
7
- export const atRuleHandlers: Record<string, AtRuleHandler> = {
8
- light,
9
- dark,
10
- // add more handlers here as needed
11
- };
13
+ // Ordered array ensures execution order
14
+ export const atRuleHandlers: AtRuleHandler[] = [
15
+ { name: "each-standard-color", handler: eachStandardColor },
16
+ { name: "each-fixed-color", handler: eachFixedColor },
17
+ { name: "light", handler: light },
18
+ { name: "dark", handler: dark },
19
+ { name: "xs", handler: container },
20
+ { name: "sm", handler: container },
21
+ { name: "md", handler: container },
22
+ { name: "lg", handler: container },
23
+ { name: "xl", handler: container },
24
+ { name: "2xl", handler: container },
25
+ ];
package/src/config.ts ADDED
@@ -0,0 +1,59 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { SeyunaConfig, PluginOptions, PluginContext, FunctionMap } from './types.js';
4
+
5
+ // Re-export types for backwards compatibility
6
+ export type { PluginOptions, PluginContext, FunctionMap } from './types.js';
7
+
8
+ const DEFAULT_OPTIONS: Required<PluginOptions> = {
9
+ configPath: 'seyuna.json',
10
+ modeAttribute: 'data-mode',
11
+ strict: false,
12
+ config: undefined as any,
13
+ functions: undefined as any,
14
+ };
15
+
16
+ let cachedConfig: SeyunaConfig | null = null;
17
+ let cachedConfigPath: string | null = null;
18
+
19
+ export function loadConfig(options: PluginOptions = {}): { config: SeyunaConfig, options: Required<PluginOptions> } {
20
+ const mergedOptions = { ...DEFAULT_OPTIONS, ...options };
21
+
22
+ if (mergedOptions.config) {
23
+ return { config: mergedOptions.config, options: mergedOptions };
24
+ }
25
+
26
+ const configPath = path.resolve(process.cwd(), mergedOptions.configPath);
27
+
28
+ // Cache config if it's the same path
29
+ if (cachedConfig && cachedConfigPath === configPath) {
30
+ return { config: cachedConfig, options: mergedOptions };
31
+ }
32
+
33
+ try {
34
+ if (!fs.existsSync(configPath)) {
35
+ if (mergedOptions.strict) {
36
+ throw new Error(`Seyuna config not found at ${configPath}`);
37
+ }
38
+ return {
39
+ config: { ui: { theme: { hues: {}, light: { colors: {} }, dark: { colors: {} } } } } as any,
40
+ options: mergedOptions,
41
+ };
42
+ }
43
+
44
+ const data = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
45
+ cachedConfig = data;
46
+ cachedConfigPath = configPath;
47
+
48
+ return { config: data, options: mergedOptions };
49
+ } catch (error) {
50
+ if (mergedOptions.strict) {
51
+ throw error;
52
+ }
53
+ console.warn(`[Seyuna PostCSS] Warning: Failed to load config: ${error instanceof Error ? error.message : String(error)}`);
54
+ return {
55
+ config: { ui: { theme: { hues: {}, light: { colors: {} }, dark: { colors: {} } } } } as any,
56
+ options: mergedOptions,
57
+ };
58
+ }
59
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,23 @@
1
+ import { Node } from 'postcss';
2
+ import { PluginContext } from './types.js';
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
+ }
@@ -1,24 +1,90 @@
1
- export default function color(
1
+ import { PluginContext } from "../types.js";
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 || {};
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`);
21
+ }
22
+
23
+ if (isStandard) {
24
+ return {
25
+ l: "var(--lightness)",
26
+ c: "var(--chroma)",
27
+ h: `var(--${color}-hue)`,
28
+ };
29
+ }
30
+
31
+ if (isFixed) {
32
+ return {
33
+ l: `var(--${color}-lightness)`,
34
+ c: `var(--${color}-chroma)`,
35
+ h: `var(--${color}-hue)`,
36
+ };
37
+ }
38
+
39
+ throw new Error(`Color '${color}' not found in seyuna.json`);
40
+ }
41
+
42
+ export function sc(
43
+ context: PluginContext,
2
44
  name: string,
3
45
  alpha?: string,
4
46
  lightness?: string,
5
47
  chroma?: string
6
48
  ) {
7
- let a: string = "1";
8
- let l: string = "var(--lightness)";
9
- let c: string = "var(--chroma)";
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;
10
53
 
11
- if (alpha && alpha !== "null") {
12
- a = alpha;
13
- }
54
+ return `oklch(${l} ${c} ${vars.h} / ${a})`;
55
+ }
14
56
 
15
- if (lightness && lightness !== "null") {
16
- l = lightness;
17
- }
57
+ export function fc(
58
+ context: PluginContext,
59
+ name: string,
60
+ alpha?: string,
61
+ lightness?: string,
62
+ chroma?: string
63
+ ) {
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;
18
68
 
19
- if (chroma && chroma !== "null") {
20
- c = chroma;
21
- }
69
+ return `oklch(${l} ${c} ${vars.h} / ${a})`;
70
+ }
71
+
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
+ }
76
+
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
+ }
22
86
 
23
- return `oklch(${l} ${c} var(--${name}) / ${a})`;
87
+ export function contrast(context: PluginContext, color: string) {
88
+ const { l } = getColorVariables(context, color);
89
+ return `oklch(calc((${l} - 0.6) * -1000) 0 0)`;
24
90
  }
@@ -1,9 +1,15 @@
1
- import color from "./color";
2
- import spacing from "./spacing";
1
+ import { sc, fc, alpha, lighten, darken, contrast } from "./color.js";
2
+ import { theme } from "./theme.js";
3
+ import { PluginContext } from "../types.js";
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
- color,
8
- spacing,
8
+ sc,
9
+ fc,
10
+ alpha,
11
+ lighten,
12
+ darken,
13
+ contrast,
14
+ theme,
9
15
  };
@@ -0,0 +1,20 @@
1
+ import { PluginContext } from "../types.js";
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 ADDED
@@ -0,0 +1,74 @@
1
+ import { Rule, ChildNode, Declaration, AtRule } from "postcss";
2
+ import { processFunctions } from "./parser.js";
3
+ import { PluginContext } from './types.js';
4
+
5
+ /**
6
+ * Helper: clone nodes and replace {name} placeholders safely
7
+ * Returns only valid Rules or Declarations (never raw AtRules)
8
+ */
9
+ export function cloneNodes(
10
+ nodes: ChildNode[],
11
+ name: string,
12
+ context: PluginContext
13
+ ): ChildNode[] {
14
+ const { functions: fnMap } = context;
15
+
16
+ return nodes.flatMap((node) => {
17
+ const cloned = node.clone();
18
+
19
+ if (cloned.type === "decl") {
20
+ const decl = cloned as Declaration;
21
+ let value = decl.value.replace(/\{name\}/g, name);
22
+
23
+ // Process functions using the robust parser
24
+ decl.value = processFunctions(value, fnMap, decl, context);
25
+
26
+ return decl;
27
+ }
28
+
29
+ if (cloned.type === "rule") {
30
+ const rule = cloned as Rule;
31
+ if (!rule.selector) return [];
32
+
33
+ rule.selector = rule.selector.replace(/\{name\}/g, name);
34
+
35
+ // Recursively clone child nodes and only keep valid rules/decls
36
+ rule.nodes = cloneNodes(rule.nodes || [], name, context).filter(
37
+ (n) => n.type === "rule" || n.type === "decl"
38
+ );
39
+
40
+ if (!rule.nodes.length) return [];
41
+ return rule;
42
+ }
43
+
44
+ // Ignore AtRules inside rules — they must be processed first
45
+ return [];
46
+ });
47
+ }
48
+
49
+ /**
50
+ * Generate CSS rules from a list of names
51
+ */
52
+ export function generateRules(
53
+ names: string[],
54
+ atRule: AtRule,
55
+ context: PluginContext
56
+ ): Rule[] {
57
+ const nodes = atRule.nodes ?? [];
58
+ const generatedRules: Rule[] = [];
59
+
60
+ for (const name of names) {
61
+ const rule = new Rule({
62
+ selector: `&.${name}`,
63
+ source: atRule.source,
64
+ });
65
+ cloneNodes(nodes, name, context).forEach((n) => {
66
+ if (n.type === "rule" && n.selector && n.nodes?.length) rule.append(n);
67
+ if (n.type === "decl") rule.append(n);
68
+ });
69
+
70
+ if (rule.nodes.length) generatedRules.push(rule);
71
+ }
72
+
73
+ return generatedRules;
74
+ }
package/src/index.ts CHANGED
@@ -1,8 +1,10 @@
1
- import { dynamicFunctionsPlugin } from './plugin';
2
- import { functions } from './functions';
1
+ import { dynamicFunctionsPlugin } from './plugin.js';
2
+ import { functions } from './functions/index.js';
3
3
 
4
4
  // CommonJS-friendly export
5
- export = Object.assign(dynamicFunctionsPlugin, {
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,81 @@
1
+ import valueParser from 'postcss-value-parser';
2
+ import { Node } from 'postcss';
3
+ import { PluginContext, FunctionMap } from './types.js';
4
+ import { reportError } from './errors.js';
5
+
6
+ // Re-export FunctionMap for backwards compatibility
7
+ export type { FunctionMap } from './types.js';
8
+
9
+ export function processFunctions(
10
+ value: string,
11
+ fnMap: FunctionMap,
12
+ node: Node,
13
+ context: PluginContext
14
+ ): string {
15
+ const parsed = valueParser(value);
16
+
17
+ // Helper to process nodes recursively (inner-first)
18
+ function processNode(parsedValue: valueParser.ParsedValue) {
19
+ parsedValue.walk((nodeIter) => {
20
+ // If nested nodes exist (e.g., in a function), process them first
21
+ if (nodeIter.type === 'function' && nodeIter.nodes.length > 0) {
22
+ // Here we recursively process child nodes before handling the current function
23
+ // However, value-parser's walk is already a visitor. To do inner-first,
24
+ // we can either walk in reverse or specifically handle children.
25
+ // A simpler way: since walk visits all, we can check if it's a function
26
+ // and if it's one we handle, we process its arguments.
27
+ }
28
+ });
29
+
30
+ // Actually, a manual walk is safer for inner-first replacement
31
+ function traverse(nodes: valueParser.Node[]) {
32
+ for (const nodeIter of nodes) {
33
+ if (nodeIter.type === 'function') {
34
+ // Process inner functions first
35
+ traverse(nodeIter.nodes);
36
+
37
+ // Now handle this function if it's in our map
38
+ if (fnMap[nodeIter.value]) {
39
+ const handler = fnMap[nodeIter.value];
40
+ const args: string[] = [];
41
+ let currentArg = '';
42
+
43
+ nodeIter.nodes.forEach((argNode) => {
44
+ if (argNode.type === 'div' && argNode.value === ',') {
45
+ args.push(currentArg.trim());
46
+ currentArg = '';
47
+ } else {
48
+ currentArg += valueParser.stringify(argNode);
49
+ }
50
+ });
51
+
52
+ if (currentArg) {
53
+ args.push(currentArg.trim());
54
+ }
55
+
56
+ const cleanedArgs = args.map(arg => arg.replace(/^['"]|['"]$/g, ''));
57
+
58
+ try {
59
+ const result = handler(context, ...cleanedArgs);
60
+ // Replace function with its result
61
+ (nodeIter as any).type = 'word';
62
+ (nodeIter as any).value = result;
63
+ (nodeIter as any).nodes = []; // Clear children
64
+ } catch (error) {
65
+ reportError(
66
+ `Failed to process function "${nodeIter.value}": ${error instanceof Error ? error.message : String(error)}`,
67
+ node,
68
+ context
69
+ );
70
+ }
71
+ }
72
+ }
73
+ }
74
+ }
75
+
76
+ traverse(parsedValue.nodes);
77
+ }
78
+
79
+ processNode(parsed);
80
+ return parsed.toString();
81
+ }
package/src/plugin.ts CHANGED
@@ -1,38 +1,36 @@
1
1
  import { PluginCreator } from "postcss";
2
- import { functions } from "./functions";
3
- import { atRuleHandlers } from "./at-rules";
4
-
5
- export interface PluginOptions {
6
- functions?: Record<string, (...args: string[]) => string>;
7
- }
2
+ import { functions } from "./functions/index.js";
3
+ import { atRuleHandlers } from "./at-rules/index.js";
4
+ import { loadConfig } from "./config.js";
5
+ import { PluginOptions, PluginContext, FunctionMap } from "./types.js";
6
+ import { processFunctions } from "./parser.js";
8
7
 
9
8
  export const dynamicFunctionsPlugin: PluginCreator<PluginOptions> = (
10
9
  opts = {}
11
10
  ) => {
12
- const fnMap = { ...functions, ...opts.functions };
11
+ const { config, options } = loadConfig(opts);
12
+ const fnMap: FunctionMap = { ...functions, ...opts.functions };
13
+ const context: PluginContext = {
14
+ config,
15
+ options,
16
+ functions: fnMap,
17
+ };
13
18
 
14
19
  return {
15
20
  postcssPlugin: "postcss-dynamic-functions",
16
21
 
17
22
  Declaration(decl) {
18
- let value = decl.value;
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;
23
+ decl.value = processFunctions(decl.value, fnMap, decl, context);
32
24
  },
33
25
 
34
- AtRule: {
35
- ...atRuleHandlers,
26
+ // Override AtRule handler to ensure ordered execution
27
+ AtRule(atRule) {
28
+ // Iterate over handlers in order (array) instead of object spread
29
+ for (const { name, handler } of atRuleHandlers) {
30
+ if (atRule.name === name) {
31
+ handler(atRule, context);
32
+ }
33
+ }
36
34
  },
37
35
  };
38
36
  };
package/src/types.ts ADDED
@@ -0,0 +1,72 @@
1
+ // Local type definitions to avoid @seyuna/cli runtime import
2
+ // The @seyuna/cli package imports Deno polyfills that conflict with Vite/Astro
3
+
4
+ /**
5
+ * Represents a color in the Oklch color space.
6
+ */
7
+ export interface Color {
8
+ lightness: number;
9
+ chroma: number;
10
+ hue: number;
11
+ }
12
+
13
+ /**
14
+ * A set of colors for a specific theme mode.
15
+ */
16
+ export interface Palette {
17
+ chroma: number;
18
+ lightness: number;
19
+ background: Color;
20
+ text: Color;
21
+ colors: Record<string, Color>;
22
+ }
23
+
24
+ /**
25
+ * The complete theme specification.
26
+ */
27
+ export interface Theme {
28
+ hues: Record<string, number>;
29
+ colors: Record<string, Color>;
30
+ light: Palette;
31
+ dark: Palette;
32
+ }
33
+
34
+ /**
35
+ * Supported color appearance modes.
36
+ */
37
+ export type Mode = "system" | "light" | "dark";
38
+
39
+ /**
40
+ * UI Engine Configuration.
41
+ */
42
+ export interface UI {
43
+ theme: Theme;
44
+ mode: Mode;
45
+ output_dir?: string;
46
+ }
47
+
48
+ /**
49
+ * Root configuration structure for a Seyuna project.
50
+ */
51
+ export interface SeyunaConfig {
52
+ license?: string;
53
+ ui?: UI;
54
+ }
55
+
56
+ // Shared types to avoid circular dependencies between config.ts and parser.ts
57
+
58
+ export type FunctionMap = Record<string, (context: PluginContext, ...args: string[]) => string>;
59
+
60
+ export interface PluginOptions {
61
+ configPath?: string;
62
+ modeAttribute?: string;
63
+ strict?: boolean;
64
+ config?: SeyunaConfig;
65
+ functions?: FunctionMap;
66
+ }
67
+
68
+ export interface PluginContext {
69
+ config: SeyunaConfig;
70
+ options: Required<PluginOptions>;
71
+ functions: FunctionMap;
72
+ }