@mochi-css/vanilla 3.0.1 → 4.0.2

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.
@@ -0,0 +1,284 @@
1
+ import { a as GlobalCssObject, c as KeyframesObject, d as isMochiCSS, f as mergeMochiCss, l as MochiCSS, p as CSSObject } from "../src-CJmAtVxm.mjs";
2
+ import { createExtractorsPlugin } from "@mochi-css/plugins";
3
+ import { getErrorMessage } from "@mochi-css/builder";
4
+ import { styledIdPlugin } from "@mochi-css/config";
5
+
6
+ //#region src/config/VanillaCssGenerator.ts
7
+ const emptySpan = {
8
+ start: 0,
9
+ end: 0,
10
+ ctxt: 0
11
+ };
12
+ function strLit(value) {
13
+ return {
14
+ type: "StringLiteral",
15
+ span: emptySpan,
16
+ value,
17
+ raw: void 0
18
+ };
19
+ }
20
+ function arrExpr(elements) {
21
+ return {
22
+ type: "ArrayExpression",
23
+ span: emptySpan,
24
+ elements: elements.map((e) => ({ expression: e }))
25
+ };
26
+ }
27
+ function objExpr(properties) {
28
+ return {
29
+ type: "ObjectExpression",
30
+ span: emptySpan,
31
+ properties: properties.map(([key, value]) => ({
32
+ type: "KeyValueProperty",
33
+ key: strLit(key),
34
+ value
35
+ }))
36
+ };
37
+ }
38
+ function mochiCssNode(instance) {
39
+ const classNamesNode = arrExpr(instance.classNames.map(strLit));
40
+ const variantClassNamesNode = objExpr(Object.entries(instance.variantClassNames).map(([varKey, opts]) => [varKey, objExpr(Object.entries(opts).map(([optKey, cls]) => [optKey, strLit(cls)]))]));
41
+ const defaultVariantsNode = objExpr(Object.entries(instance.defaultVariants).filter(([, v]) => v != null).map(([k, v]) => [k, strLit(String(v))]));
42
+ return {
43
+ type: "NewExpression",
44
+ span: emptySpan,
45
+ ctxt: 0,
46
+ callee: {
47
+ type: "Identifier",
48
+ span: emptySpan,
49
+ ctxt: 0,
50
+ value: "MochiCSS",
51
+ optional: false
52
+ },
53
+ arguments: [
54
+ { expression: classNamesNode },
55
+ { expression: variantClassNamesNode },
56
+ { expression: defaultVariantsNode }
57
+ ]
58
+ };
59
+ }
60
+ var VanillaCssGenerator = class {
61
+ collectedStyles = [];
62
+ generatedMochiCss = [];
63
+ constructor(onDiagnostic) {
64
+ this.onDiagnostic = onDiagnostic;
65
+ }
66
+ collectArgs(source, args) {
67
+ const validArgs = [];
68
+ let stableId;
69
+ for (const arg of args) {
70
+ if (typeof arg === "string") {
71
+ stableId = arg;
72
+ continue;
73
+ }
74
+ if (arg == null || typeof arg !== "object") {
75
+ this.onDiagnostic?.({
76
+ code: "MOCHI_INVALID_STYLE_ARG",
77
+ message: `Expected style object, got ${arg === null ? "null" : typeof arg}`,
78
+ severity: "warning",
79
+ file: source
80
+ });
81
+ continue;
82
+ }
83
+ if (isMochiCSS(arg)) continue;
84
+ validArgs.push(arg);
85
+ }
86
+ if (validArgs.length > 0) this.collectedStyles.push({
87
+ source,
88
+ args: validArgs,
89
+ stableId
90
+ });
91
+ }
92
+ async generateStyles() {
93
+ this.generatedMochiCss = [];
94
+ const filesCss = /* @__PURE__ */ new Map();
95
+ for (const { source, args, stableId } of this.collectedStyles) {
96
+ let css = filesCss.get(source);
97
+ if (!css) {
98
+ css = /* @__PURE__ */ new Set();
99
+ filesCss.set(source, css);
100
+ }
101
+ const mochiInstances = [];
102
+ for (const style of args) try {
103
+ const cssObj = new CSSObject(style, stableId);
104
+ css.add(cssObj.asCssString());
105
+ mochiInstances.push(MochiCSS.from(cssObj));
106
+ } catch (err) {
107
+ const message = getErrorMessage(err);
108
+ this.onDiagnostic?.({
109
+ code: "MOCHI_STYLE_GENERATION",
110
+ message: `Failed to generate CSS: ${message}`,
111
+ severity: "warning",
112
+ file: source
113
+ });
114
+ }
115
+ if (mochiInstances.length > 0) this.generatedMochiCss.push({
116
+ source,
117
+ instance: mergeMochiCss(mochiInstances)
118
+ });
119
+ }
120
+ const files = {};
121
+ for (const [source, css] of filesCss) files[source] = [...css.values()].sort().join("\n\n");
122
+ return { files };
123
+ }
124
+ getArgReplacements() {
125
+ return this.generatedMochiCss.map(({ source, instance }) => ({
126
+ source,
127
+ expression: mochiCssNode(instance)
128
+ }));
129
+ }
130
+ };
131
+
132
+ //#endregion
133
+ //#region src/config/VanillaCssExtractor.ts
134
+ var VanillaCssExtractor = class {
135
+ importPath;
136
+ symbolName;
137
+ constructor(importPath, symbolName, extractor) {
138
+ this.extractor = extractor;
139
+ this.importPath = importPath;
140
+ this.symbolName = symbolName;
141
+ }
142
+ extractStaticArgs(call) {
143
+ return this.extractor(call);
144
+ }
145
+ startGeneration(onDiagnostic) {
146
+ return new VanillaCssGenerator(onDiagnostic);
147
+ }
148
+ };
149
+ const mochiCssFunctionExtractor = new VanillaCssExtractor("@mochi-css/vanilla", "css", (call) => call.arguments.map((a) => a.expression));
150
+
151
+ //#endregion
152
+ //#region src/config/VanillaKeyframesGenerator.ts
153
+ var VanillaKeyframesGenerator = class {
154
+ collectedStyles = [];
155
+ constructor(onDiagnostic) {
156
+ this.onDiagnostic = onDiagnostic;
157
+ }
158
+ collectArgs(source, args) {
159
+ if (args.length === 0 || args[0] == null || typeof args[0] !== "object") {
160
+ this.onDiagnostic?.({
161
+ code: "MOCHI_INVALID_STYLE_ARG",
162
+ message: `Expected keyframe stops object, got ${args[0] === null ? "null" : typeof args[0]}`,
163
+ severity: "warning",
164
+ file: source
165
+ });
166
+ return;
167
+ }
168
+ this.collectedStyles.push({
169
+ source,
170
+ stops: args[0]
171
+ });
172
+ }
173
+ async generateStyles() {
174
+ const filesCss = /* @__PURE__ */ new Map();
175
+ for (const { source, stops } of this.collectedStyles) {
176
+ let css = filesCss.get(source);
177
+ if (!css) {
178
+ css = /* @__PURE__ */ new Set();
179
+ filesCss.set(source, css);
180
+ }
181
+ try {
182
+ const obj = new KeyframesObject(stops);
183
+ css.add(obj.asCssString());
184
+ } catch (err) {
185
+ const message = getErrorMessage(err);
186
+ this.onDiagnostic?.({
187
+ code: "MOCHI_STYLE_GENERATION",
188
+ message: `Failed to generate keyframes CSS: ${message}`,
189
+ severity: "warning",
190
+ file: source
191
+ });
192
+ }
193
+ }
194
+ const files = {};
195
+ for (const [source, css] of filesCss) files[source] = [...css.values()].sort().join("\n\n");
196
+ return { files };
197
+ }
198
+ };
199
+
200
+ //#endregion
201
+ //#region src/config/VanillaKeyframesExtractor.ts
202
+ var VanillaKeyframesExtractor = class {
203
+ importPath = "@mochi-css/vanilla";
204
+ symbolName = "keyframes";
205
+ extractStaticArgs(call) {
206
+ return call.arguments.slice(0, 1).map((a) => a.expression);
207
+ }
208
+ startGeneration(onDiagnostic) {
209
+ return new VanillaKeyframesGenerator(onDiagnostic);
210
+ }
211
+ };
212
+ const mochiKeyframesFunctionExtractor = new VanillaKeyframesExtractor();
213
+
214
+ //#endregion
215
+ //#region src/config/VanillaGlobalCssGenerator.ts
216
+ var VanillaGlobalCssGenerator = class {
217
+ collectedStyles = [];
218
+ constructor(onDiagnostic) {
219
+ this.onDiagnostic = onDiagnostic;
220
+ }
221
+ collectArgs(source, args) {
222
+ if (args.length === 0 || args[0] == null || typeof args[0] !== "object") {
223
+ this.onDiagnostic?.({
224
+ code: "MOCHI_INVALID_STYLE_ARG",
225
+ message: `Expected global CSS styles object, got ${args[0] === null ? "null" : typeof args[0]}`,
226
+ severity: "warning",
227
+ file: source
228
+ });
229
+ return;
230
+ }
231
+ this.collectedStyles.push({
232
+ source,
233
+ styles: args[0]
234
+ });
235
+ }
236
+ async generateStyles() {
237
+ const allCss = /* @__PURE__ */ new Set();
238
+ for (const { source, styles } of this.collectedStyles) try {
239
+ const obj = new GlobalCssObject(styles);
240
+ allCss.add(obj.asCssString());
241
+ } catch (err) {
242
+ const message = getErrorMessage(err);
243
+ this.onDiagnostic?.({
244
+ code: "MOCHI_STYLE_GENERATION",
245
+ message: `Failed to generate global CSS: ${message}`,
246
+ severity: "warning",
247
+ file: source
248
+ });
249
+ }
250
+ return { global: [...allCss].sort().join("\n\n") };
251
+ }
252
+ };
253
+
254
+ //#endregion
255
+ //#region src/config/VanillaGlobalCssExtractor.ts
256
+ var VanillaGlobalCssExtractor = class {
257
+ importPath = "@mochi-css/vanilla";
258
+ symbolName = "globalCss";
259
+ extractStaticArgs(call) {
260
+ return call.arguments.slice(0, 1).map((a) => a.expression);
261
+ }
262
+ startGeneration(onDiagnostic) {
263
+ return new VanillaGlobalCssGenerator(onDiagnostic);
264
+ }
265
+ };
266
+ const mochiGlobalCssFunctionExtractor = new VanillaGlobalCssExtractor();
267
+
268
+ //#endregion
269
+ //#region src/config/index.ts
270
+ const defaultVanillaExtractors = [
271
+ mochiCssFunctionExtractor,
272
+ mochiKeyframesFunctionExtractor,
273
+ mochiGlobalCssFunctionExtractor
274
+ ];
275
+ function defineConfig(config) {
276
+ const { extractors = [],...rest } = config;
277
+ return {
278
+ ...rest,
279
+ plugins: [createExtractorsPlugin([...defaultVanillaExtractors, ...extractors]), ...rest.plugins ?? []]
280
+ };
281
+ }
282
+
283
+ //#endregion
284
+ export { VanillaCssExtractor, VanillaCssGenerator, VanillaGlobalCssExtractor, VanillaGlobalCssGenerator, VanillaKeyframesExtractor, VanillaKeyframesGenerator, defineConfig, mochiCssFunctionExtractor, mochiGlobalCssFunctionExtractor, mochiKeyframesFunctionExtractor, styledIdPlugin };
package/dist/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ObsoleteProperties, Properties, PropertiesHyphen } from "csstype";
2
+ import { numberToBase62, shortHash } from "@mochi-css/core";
2
3
 
3
4
  //#region src/values/index.d.ts
4
5
  type CssVar = `--${string}`;
@@ -649,7 +650,7 @@ declare class CSSObject<V extends AllVariants = DefaultVariants> {
649
650
  }: MochiCSSProps<V>, className?: string);
650
651
  /**
651
652
  * Serializes the entire CSS object to a CSS string.
652
- * Outputs main block first, then all variant blocks in sorted order.
653
+ * Outputs the main block first, then all variant blocks in sorted order.
653
654
  * @returns Complete CSS string ready for injection into a stylesheet
654
655
  */
655
656
  asCssString(): string;
@@ -814,28 +815,4 @@ interface SupportsHelper {
814
815
  /** Helper for constructing `@supports` at-rule keys. */
815
816
  declare const supports: SupportsHelper;
816
817
  //#endregion
817
- //#region src/hash.d.ts
818
- /**
819
- * Hashing utilities for generating short, deterministic class names.
820
- * Uses djb2 algorithm for fast string hashing.
821
- * @module hash
822
- */
823
- /**
824
- * Converts a number to a base-62 string representation.
825
- * @param num - The number to convert
826
- * @param maxLength - Optional maximum length of the output string
827
- * @returns Base-62 encoded string representation of the number
828
- */
829
- declare function numberToBase62(num: number, maxLength?: number): string;
830
- /**
831
- * Generates a short hash string from input using the djb2 algorithm.
832
- * Used to create unique, deterministic CSS class names from style content.
833
- * @param input - The string to hash
834
- * @param length - Maximum length of the hash output (default: 8)
835
- * @returns A short, css-safe hash string
836
- * @example
837
- * shortHash("color: red;") // Returns something like "A1b2C3d4"
838
- */
839
- declare function shortHash(input: string, length?: number): string;
840
- //#endregion
841
818
  export { AllVariants, type AtRuleKey, CSSObject, CompoundVariant, CssLike, CssLikeObject, CssObjectBlock, CssObjectSubBlock, CssVar, CssVarVal, DefaultVariants, GlobalCssObject, GlobalCssProperties, type GlobalCssStyles, type KeyframeStops, KeyframesObject, MergeCSSVariants, MochiCSS, MochiCSSProps, MochiKeyframes, MochiSelector, RefineVariantType, RefineVariants, type StyleProps, Token, VariantProps, camelToKebab, container, createToken, css, cssFromProps, globalCss, isAtRuleKey, isMochiCSS, kebabToCamel, keyframes, media, mergeMochiCss, numberToBase62, shortHash, supports };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ObsoleteProperties, Properties, PropertiesHyphen } from "csstype";
2
+ import { numberToBase62, shortHash } from "@mochi-css/core";
2
3
 
3
4
  //#region src/values/index.d.ts
4
5
  type CssVar = `--${string}`;
@@ -649,7 +650,7 @@ declare class CSSObject<V extends AllVariants = DefaultVariants> {
649
650
  }: MochiCSSProps<V>, className?: string);
650
651
  /**
651
652
  * Serializes the entire CSS object to a CSS string.
652
- * Outputs main block first, then all variant blocks in sorted order.
653
+ * Outputs the main block first, then all variant blocks in sorted order.
653
654
  * @returns Complete CSS string ready for injection into a stylesheet
654
655
  */
655
656
  asCssString(): string;
@@ -814,29 +815,4 @@ interface SupportsHelper {
814
815
  /** Helper for constructing `@supports` at-rule keys. */
815
816
  declare const supports: SupportsHelper;
816
817
  //#endregion
817
- //#region src/hash.d.ts
818
- /**
819
- * Hashing utilities for generating short, deterministic class names.
820
- * Uses djb2 algorithm for fast string hashing.
821
- * @module hash
822
- */
823
- /**
824
- * Converts a number to a base-62 string representation.
825
- * @param num - The number to convert
826
- * @param maxLength - Optional maximum length of the output string
827
- * @returns Base-62 encoded string representation of the number
828
- */
829
- declare function numberToBase62(num: number, maxLength?: number): string;
830
- /**
831
- * Generates a short hash string from input using the djb2 algorithm.
832
- * Used to create unique, deterministic CSS class names from style content.
833
- * @param input - The string to hash
834
- * @param length - Maximum length of the hash output (default: 8)
835
- * @returns A short, css-safe hash string
836
- * @example
837
- * shortHash("color: red;") // Returns something like "A1b2C3d4"
838
- */
839
- declare function shortHash(input: string, length?: number): string;
840
- //#endregion
841
- export { AllVariants, type AtRuleKey, CSSObject, CompoundVariant, CssLike, CssLikeObject, CssObjectBlock, CssObjectSubBlock, CssVar, CssVarVal, DefaultVariants, GlobalCssObject, GlobalCssProperties, type GlobalCssStyles, type KeyframeStops, KeyframesObject, MergeCSSVariants, MochiCSS, MochiCSSProps, MochiKeyframes, MochiSelector, RefineVariantType, RefineVariants, type StyleProps, Token, VariantProps, camelToKebab, container, createToken, css, cssFromProps, globalCss, isAtRuleKey, isMochiCSS, kebabToCamel, keyframes, media, mergeMochiCss, numberToBase62, shortHash, supports };
842
- //# sourceMappingURL=index.d.ts.map
818
+ export { AllVariants, type AtRuleKey, CSSObject, CompoundVariant, CssLike, CssLikeObject, CssObjectBlock, CssObjectSubBlock, CssVar, CssVarVal, DefaultVariants, GlobalCssObject, GlobalCssProperties, type GlobalCssStyles, type KeyframeStops, KeyframesObject, MergeCSSVariants, MochiCSS, MochiCSSProps, MochiKeyframes, MochiSelector, RefineVariantType, RefineVariants, type StyleProps, Token, VariantProps, camelToKebab, container, createToken, css, cssFromProps, globalCss, isAtRuleKey, isMochiCSS, kebabToCamel, keyframes, media, mergeMochiCss, numberToBase62, shortHash, supports };