@csszyx/svelte-adapter 0.9.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 csszyx contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,284 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ exports.mergeClassAttributes = mergeClassAttributes;
8
+ exports.parseObjectLiteral = parseObjectLiteral;
9
+ exports.preprocess = preprocess;
10
+ exports.preprocessor = preprocessor;
11
+ exports.transformMarkup = transformMarkup;
12
+ exports.vitePlugin = vitePlugin;
13
+ var _compiler = require("@csszyx/compiler");
14
+ var _oxcParser = require("oxc-parser");
15
+ function extractValue(node) {
16
+ switch (node.type) {
17
+ case "Literal":
18
+ return node.value;
19
+ case "UnaryExpression":
20
+ if (node.operator === "-" && node.argument.type === "Literal" && typeof node.argument.value === "number") {
21
+ return -node.argument.value;
22
+ }
23
+ return void 0;
24
+ case "ArrayExpression":
25
+ {
26
+ const arr = [];
27
+ for (const el of node.elements ?? []) {
28
+ if (!el) {
29
+ arr.push(null);
30
+ continue;
31
+ }
32
+ const v = extractValue(el);
33
+ if (v === void 0) return void 0;
34
+ arr.push(v);
35
+ }
36
+ return arr;
37
+ }
38
+ case "ObjectExpression":
39
+ return extractObjectNode(node);
40
+ case "TemplateLiteral":
41
+ if ((node.expressions ?? []).length === 0) {
42
+ const quasis = node.quasis;
43
+ return quasis[0].value.cooked ?? void 0;
44
+ }
45
+ return void 0;
46
+ default:
47
+ return void 0;
48
+ }
49
+ }
50
+ function extractObjectNode(node) {
51
+ const obj = {};
52
+ for (const prop of node.properties ?? []) {
53
+ if (prop.type !== "Property") return void 0;
54
+ if (prop.computed) return void 0;
55
+ const key_node = prop.key;
56
+ let key;
57
+ if (key_node.type === "Identifier") {
58
+ key = key_node.name;
59
+ } else if (key_node.type === "Literal" && typeof key_node.value === "string") {
60
+ key = key_node.value;
61
+ } else if (key_node.type === "Literal" && typeof key_node.value === "number") {
62
+ key = String(key_node.value);
63
+ } else {
64
+ return void 0;
65
+ }
66
+ const value = extractValue(prop.value);
67
+ if (value === void 0) return void 0;
68
+ obj[key] = value;
69
+ }
70
+ return obj;
71
+ }
72
+ function parseObjectLiteral(objStr) {
73
+ try {
74
+ const src = `const _=${objStr.trim()}`;
75
+ const parsed = (0, _oxcParser.parseSync)("sz.js", src);
76
+ if (parsed.errors.length > 0) return null;
77
+ const decl = parsed.program.body;
78
+ const init = decl[0].declarations[0].init ?? null;
79
+ if (!init || init.type !== "ObjectExpression") return null;
80
+ return extractObjectNode(init) ?? null;
81
+ } catch {
82
+ return null;
83
+ }
84
+ }
85
+ function transformMarkup(content, options = {}) {
86
+ let result = "";
87
+ let count = 0;
88
+ let cursor = 0;
89
+ while (cursor < content.length) {
90
+ const attributeStart = findSzAttribute(content, cursor);
91
+ if (attributeStart === -1) {
92
+ result += content.slice(cursor);
93
+ break;
94
+ }
95
+ result += content.slice(cursor, attributeStart);
96
+ const match = readSzAttribute(content, attributeStart);
97
+ if (!match) {
98
+ result += content.slice(attributeStart, attributeStart + 3);
99
+ cursor = attributeStart + 3;
100
+ continue;
101
+ }
102
+ const szObj = parseObjectLiteral(match.objectSource);
103
+ if (!szObj) {
104
+ if (options.debug) {
105
+ console.warn(`[csszyx/svelte] Failed to parse sz object: ${match.objectSource}`);
106
+ }
107
+ result += content.slice(attributeStart, match.end);
108
+ } else {
109
+ const className = (0, _compiler.transform)(szObj).className;
110
+ count += 1;
111
+ if (options.debug) {
112
+ console.log(`[csszyx/svelte] Transformed: ${match.objectSource} -> "${className}"`);
113
+ }
114
+ result += `class="${className}"`;
115
+ }
116
+ cursor = match.end;
117
+ }
118
+ return {
119
+ code: result,
120
+ count
121
+ };
122
+ }
123
+ function findSzAttribute(content, from) {
124
+ let index = content.indexOf("sz=", from);
125
+ while (index !== -1) {
126
+ const before = index === 0 ? "<" : content.charAt(index - 1);
127
+ if (before === "<" || before === " " || before === " " || before === "\n" || before === "\r") {
128
+ return index;
129
+ }
130
+ index = content.indexOf("sz=", index + 3);
131
+ }
132
+ return -1;
133
+ }
134
+ function readSzAttribute(content, start) {
135
+ const valueStart = start + 3;
136
+ const opener = content.charAt(valueStart);
137
+ if (opener === '"' || opener === "'") {
138
+ const endQuote = content.indexOf(opener, valueStart + 1);
139
+ if (endQuote === -1) {
140
+ return null;
141
+ }
142
+ let objectSource = content.slice(valueStart + 1, endQuote);
143
+ if (objectSource.startsWith("{{") && objectSource.endsWith("}}")) {
144
+ objectSource = objectSource.slice(1, -1);
145
+ }
146
+ if (!objectSource.startsWith("{") || !objectSource.endsWith("}")) {
147
+ return null;
148
+ }
149
+ return {
150
+ end: endQuote + 1,
151
+ objectSource
152
+ };
153
+ }
154
+ if (opener !== "{" || content.charAt(valueStart + 1) !== "{") {
155
+ return null;
156
+ }
157
+ const end = findBalancedBraceEnd(content, valueStart);
158
+ if (end === -1) {
159
+ return null;
160
+ }
161
+ return {
162
+ end: end + 1,
163
+ objectSource: content.slice(valueStart + 1, end)
164
+ };
165
+ }
166
+ function findBalancedBraceEnd(content, start) {
167
+ let depth = 0;
168
+ let quote = "";
169
+ let escaped = false;
170
+ for (let index = start; index < content.length; index += 1) {
171
+ const char = content.charAt(index);
172
+ if (quote) {
173
+ if (escaped) {
174
+ escaped = false;
175
+ } else if (char === "\\") {
176
+ escaped = true;
177
+ } else if (char === quote) {
178
+ quote = "";
179
+ }
180
+ continue;
181
+ }
182
+ if (char === '"' || char === "'" || char === "`") {
183
+ quote = char;
184
+ } else if (char === "{") {
185
+ depth += 1;
186
+ } else if (char === "}") {
187
+ depth -= 1;
188
+ if (depth === 0) {
189
+ return index;
190
+ }
191
+ }
192
+ }
193
+ return -1;
194
+ }
195
+ function mergeClassAttributes(content) {
196
+ let result = content;
197
+ let i = 0;
198
+ while (i < result.length) {
199
+ const start = result.indexOf("<", i);
200
+ if (start === -1) break;
201
+ const end = result.indexOf(">", start);
202
+ if (end === -1) break;
203
+ const tag = result.slice(start, end + 1);
204
+ i = end + 1;
205
+ if (!/^<[a-z]/i.test(tag)) continue;
206
+ const matches = [...tag.matchAll(/\bclass="([^"]*)"/g)].map(m => m[1]);
207
+ if (matches.length < 2) continue;
208
+ const merged = matches.join(" ");
209
+ const firstIdx = tag.indexOf('class="');
210
+ const cleaned = tag.replace(/\bclass="[^"]*"/g, "");
211
+ const newTag = `${cleaned.slice(0, firstIdx)}class="${merged}"${cleaned.slice(firstIdx)}`;
212
+ result = result.slice(0, start) + newTag + result.slice(end + 1);
213
+ i = start + newTag.length;
214
+ }
215
+ return result;
216
+ }
217
+ function preprocessor(options = {}) {
218
+ return {
219
+ name: "csszyx-svelte",
220
+ markup({
221
+ content,
222
+ filename
223
+ }) {
224
+ if (!content.includes("sz=")) {
225
+ return;
226
+ }
227
+ if (options.debug && filename) {
228
+ console.log(`[csszyx/svelte] Processing: ${filename}`);
229
+ }
230
+ const transformResult = transformMarkup(content, options);
231
+ if (transformResult.count === 0) {
232
+ return;
233
+ }
234
+ const mergedContent = mergeClassAttributes(transformResult.code);
235
+ if (options.debug) {
236
+ console.log(`[csszyx/svelte] Transformed ${transformResult.count} sz props`);
237
+ }
238
+ return {
239
+ code: mergedContent,
240
+ map: void 0
241
+ // TODO: Generate source map
242
+ };
243
+ }
244
+ };
245
+ }
246
+ function vitePlugin(options = {}) {
247
+ return {
248
+ name: "csszyx-svelte-vite",
249
+ enforce: "pre",
250
+ transform(code, id) {
251
+ if (!id.endsWith(".svelte")) {
252
+ return null;
253
+ }
254
+ if (!code.includes("sz=")) {
255
+ return null;
256
+ }
257
+ const transformResult = transformMarkup(code, options);
258
+ if (transformResult.count === 0) {
259
+ return null;
260
+ }
261
+ const mergedContent = mergeClassAttributes(transformResult.code);
262
+ return {
263
+ code: mergedContent,
264
+ map: void 0
265
+ // TODO: Generate source map
266
+ };
267
+ }
268
+ };
269
+ }
270
+ function preprocess(source, options = {}) {
271
+ const transformResult = transformMarkup(source, options);
272
+ if (transformResult.count === 0) {
273
+ return {
274
+ code: source,
275
+ map: void 0
276
+ };
277
+ }
278
+ const mergedContent = mergeClassAttributes(transformResult.code);
279
+ return {
280
+ code: mergedContent,
281
+ map: void 0
282
+ };
283
+ }
284
+ module.exports = preprocessor;
@@ -0,0 +1,124 @@
1
+ /**
2
+ * @csszyx/svelte-adapter - Svelte preprocessor for csszyx.
3
+ *
4
+ * Transforms `sz` props in Svelte templates into Tailwind CSS class strings.
5
+ *
6
+ * @module @csszyx/svelte-adapter
7
+ */
8
+ import { type SzObject } from '@csszyx/compiler';
9
+ /**
10
+ * Preprocessor options.
11
+ */
12
+ export interface SvelteAdapterOptions {
13
+ /**
14
+ * Enable verbose logging for debugging.
15
+ */
16
+ debug?: boolean;
17
+ }
18
+ /**
19
+ * Svelte preprocessor markup result.
20
+ */
21
+ export interface PreprocessorResult {
22
+ /**
23
+ * The transformed source code.
24
+ */
25
+ code: string;
26
+ /**
27
+ * Optional source map.
28
+ */
29
+ map?: object | null;
30
+ }
31
+ /**
32
+ * Parse a JavaScript object literal string into an object.
33
+ * Handles nested objects for variants like hover, focus, etc.
34
+ *
35
+ * @param {string} objStr - Object literal string (e.g., "{ p: 4, bg: 'red-500' }")
36
+ * @returns {SzObject | null} Parsed object or null if invalid
37
+ */
38
+ export declare function parseObjectLiteral(objStr: string): SzObject | null;
39
+ /**
40
+ * Transform sz props in a Svelte template string.
41
+ *
42
+ * Supports:
43
+ * - Static: sz="{{ p: 4 }}" or sz="{ p: 4 }"
44
+ * - Shorthand bind: sz={expression} (only static objects supported)
45
+ *
46
+ * @param {string} content - Template content
47
+ * @param {SvelteAdapterOptions} options - Options
48
+ * @returns {{ code: string; count: number }} Transformation result
49
+ */
50
+ export declare function transformMarkup(content: string, options?: SvelteAdapterOptions): {
51
+ code: string;
52
+ count: number;
53
+ };
54
+ /**
55
+ * Merge transformed classes with existing class attribute.
56
+ *
57
+ * @param {string} content - Content with sz props transformed to class
58
+ * @returns {string} Content with merged class attributes
59
+ */
60
+ export declare function mergeClassAttributes(content: string): string;
61
+ /**
62
+ * Create a Svelte preprocessor for csszyx.
63
+ *
64
+ * @param {SvelteAdapterOptions} options - Preprocessor options
65
+ * @returns {object} Svelte preprocessor
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * // svelte.config.js
70
+ * import { preprocessor } from '@csszyx/svelte-adapter';
71
+ *
72
+ * export default {
73
+ * preprocess: [
74
+ * preprocessor(),
75
+ * ],
76
+ * };
77
+ * ```
78
+ */
79
+ import type { PreprocessorGroup } from 'svelte/compiler';
80
+ import type { Plugin } from 'vite';
81
+ /**
82
+ * Create a Svelte preprocessor for csszyx.
83
+ *
84
+ * @param {SvelteAdapterOptions} options - Preprocessor options
85
+ * @returns {PreprocessorGroup} Svelte preprocessor group
86
+ */
87
+ export declare function preprocessor(options?: SvelteAdapterOptions): PreprocessorGroup;
88
+ /**
89
+ * Create a Vite plugin for Svelte preprocessing.
90
+ *
91
+ * Note: In most cases, you should use the preprocessor directly in svelte.config.js.
92
+ * This Vite plugin is provided for cases where you need to integrate at the Vite level.
93
+ *
94
+ * @param {SvelteAdapterOptions} options - Plugin options
95
+ * @returns {Plugin} Vite plugin
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * // vite.config.ts
100
+ * import { defineConfig } from 'vite';
101
+ * import { svelte } from '@sveltejs/vite-plugin-svelte';
102
+ * import { vitePlugin as csszyx } from '@csszyx/svelte-adapter';
103
+ *
104
+ * export default defineConfig({
105
+ * plugins: [
106
+ * csszyx(),
107
+ * svelte(),
108
+ * ],
109
+ * });
110
+ * ```
111
+ */
112
+ export declare function vitePlugin(options?: SvelteAdapterOptions): Plugin;
113
+ /**
114
+ * Preprocess a Svelte file, transforming sz props to class attributes.
115
+ *
116
+ * @param {string} source - Svelte source code
117
+ * @param {SvelteAdapterOptions} options - Preprocessor options
118
+ * @returns {PreprocessorResult} Preprocessing result
119
+ */
120
+ export declare function preprocess(source: string, options?: SvelteAdapterOptions): PreprocessorResult;
121
+ /**
122
+ * Default export - the preprocessor function.
123
+ */
124
+ export default preprocessor;
@@ -0,0 +1,124 @@
1
+ /**
2
+ * @csszyx/svelte-adapter - Svelte preprocessor for csszyx.
3
+ *
4
+ * Transforms `sz` props in Svelte templates into Tailwind CSS class strings.
5
+ *
6
+ * @module @csszyx/svelte-adapter
7
+ */
8
+ import { type SzObject } from '@csszyx/compiler';
9
+ /**
10
+ * Preprocessor options.
11
+ */
12
+ export interface SvelteAdapterOptions {
13
+ /**
14
+ * Enable verbose logging for debugging.
15
+ */
16
+ debug?: boolean;
17
+ }
18
+ /**
19
+ * Svelte preprocessor markup result.
20
+ */
21
+ export interface PreprocessorResult {
22
+ /**
23
+ * The transformed source code.
24
+ */
25
+ code: string;
26
+ /**
27
+ * Optional source map.
28
+ */
29
+ map?: object | null;
30
+ }
31
+ /**
32
+ * Parse a JavaScript object literal string into an object.
33
+ * Handles nested objects for variants like hover, focus, etc.
34
+ *
35
+ * @param {string} objStr - Object literal string (e.g., "{ p: 4, bg: 'red-500' }")
36
+ * @returns {SzObject | null} Parsed object or null if invalid
37
+ */
38
+ export declare function parseObjectLiteral(objStr: string): SzObject | null;
39
+ /**
40
+ * Transform sz props in a Svelte template string.
41
+ *
42
+ * Supports:
43
+ * - Static: sz="{{ p: 4 }}" or sz="{ p: 4 }"
44
+ * - Shorthand bind: sz={expression} (only static objects supported)
45
+ *
46
+ * @param {string} content - Template content
47
+ * @param {SvelteAdapterOptions} options - Options
48
+ * @returns {{ code: string; count: number }} Transformation result
49
+ */
50
+ export declare function transformMarkup(content: string, options?: SvelteAdapterOptions): {
51
+ code: string;
52
+ count: number;
53
+ };
54
+ /**
55
+ * Merge transformed classes with existing class attribute.
56
+ *
57
+ * @param {string} content - Content with sz props transformed to class
58
+ * @returns {string} Content with merged class attributes
59
+ */
60
+ export declare function mergeClassAttributes(content: string): string;
61
+ /**
62
+ * Create a Svelte preprocessor for csszyx.
63
+ *
64
+ * @param {SvelteAdapterOptions} options - Preprocessor options
65
+ * @returns {object} Svelte preprocessor
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * // svelte.config.js
70
+ * import { preprocessor } from '@csszyx/svelte-adapter';
71
+ *
72
+ * export default {
73
+ * preprocess: [
74
+ * preprocessor(),
75
+ * ],
76
+ * };
77
+ * ```
78
+ */
79
+ import type { PreprocessorGroup } from 'svelte/compiler';
80
+ import type { Plugin } from 'vite';
81
+ /**
82
+ * Create a Svelte preprocessor for csszyx.
83
+ *
84
+ * @param {SvelteAdapterOptions} options - Preprocessor options
85
+ * @returns {PreprocessorGroup} Svelte preprocessor group
86
+ */
87
+ export declare function preprocessor(options?: SvelteAdapterOptions): PreprocessorGroup;
88
+ /**
89
+ * Create a Vite plugin for Svelte preprocessing.
90
+ *
91
+ * Note: In most cases, you should use the preprocessor directly in svelte.config.js.
92
+ * This Vite plugin is provided for cases where you need to integrate at the Vite level.
93
+ *
94
+ * @param {SvelteAdapterOptions} options - Plugin options
95
+ * @returns {Plugin} Vite plugin
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * // vite.config.ts
100
+ * import { defineConfig } from 'vite';
101
+ * import { svelte } from '@sveltejs/vite-plugin-svelte';
102
+ * import { vitePlugin as csszyx } from '@csszyx/svelte-adapter';
103
+ *
104
+ * export default defineConfig({
105
+ * plugins: [
106
+ * csszyx(),
107
+ * svelte(),
108
+ * ],
109
+ * });
110
+ * ```
111
+ */
112
+ export declare function vitePlugin(options?: SvelteAdapterOptions): Plugin;
113
+ /**
114
+ * Preprocess a Svelte file, transforming sz props to class attributes.
115
+ *
116
+ * @param {string} source - Svelte source code
117
+ * @param {SvelteAdapterOptions} options - Preprocessor options
118
+ * @returns {PreprocessorResult} Preprocessing result
119
+ */
120
+ export declare function preprocess(source: string, options?: SvelteAdapterOptions): PreprocessorResult;
121
+ /**
122
+ * Default export - the preprocessor function.
123
+ */
124
+ export default preprocessor;
@@ -0,0 +1,124 @@
1
+ /**
2
+ * @csszyx/svelte-adapter - Svelte preprocessor for csszyx.
3
+ *
4
+ * Transforms `sz` props in Svelte templates into Tailwind CSS class strings.
5
+ *
6
+ * @module @csszyx/svelte-adapter
7
+ */
8
+ import { type SzObject } from '@csszyx/compiler';
9
+ /**
10
+ * Preprocessor options.
11
+ */
12
+ export interface SvelteAdapterOptions {
13
+ /**
14
+ * Enable verbose logging for debugging.
15
+ */
16
+ debug?: boolean;
17
+ }
18
+ /**
19
+ * Svelte preprocessor markup result.
20
+ */
21
+ export interface PreprocessorResult {
22
+ /**
23
+ * The transformed source code.
24
+ */
25
+ code: string;
26
+ /**
27
+ * Optional source map.
28
+ */
29
+ map?: object | null;
30
+ }
31
+ /**
32
+ * Parse a JavaScript object literal string into an object.
33
+ * Handles nested objects for variants like hover, focus, etc.
34
+ *
35
+ * @param {string} objStr - Object literal string (e.g., "{ p: 4, bg: 'red-500' }")
36
+ * @returns {SzObject | null} Parsed object or null if invalid
37
+ */
38
+ export declare function parseObjectLiteral(objStr: string): SzObject | null;
39
+ /**
40
+ * Transform sz props in a Svelte template string.
41
+ *
42
+ * Supports:
43
+ * - Static: sz="{{ p: 4 }}" or sz="{ p: 4 }"
44
+ * - Shorthand bind: sz={expression} (only static objects supported)
45
+ *
46
+ * @param {string} content - Template content
47
+ * @param {SvelteAdapterOptions} options - Options
48
+ * @returns {{ code: string; count: number }} Transformation result
49
+ */
50
+ export declare function transformMarkup(content: string, options?: SvelteAdapterOptions): {
51
+ code: string;
52
+ count: number;
53
+ };
54
+ /**
55
+ * Merge transformed classes with existing class attribute.
56
+ *
57
+ * @param {string} content - Content with sz props transformed to class
58
+ * @returns {string} Content with merged class attributes
59
+ */
60
+ export declare function mergeClassAttributes(content: string): string;
61
+ /**
62
+ * Create a Svelte preprocessor for csszyx.
63
+ *
64
+ * @param {SvelteAdapterOptions} options - Preprocessor options
65
+ * @returns {object} Svelte preprocessor
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * // svelte.config.js
70
+ * import { preprocessor } from '@csszyx/svelte-adapter';
71
+ *
72
+ * export default {
73
+ * preprocess: [
74
+ * preprocessor(),
75
+ * ],
76
+ * };
77
+ * ```
78
+ */
79
+ import type { PreprocessorGroup } from 'svelte/compiler';
80
+ import type { Plugin } from 'vite';
81
+ /**
82
+ * Create a Svelte preprocessor for csszyx.
83
+ *
84
+ * @param {SvelteAdapterOptions} options - Preprocessor options
85
+ * @returns {PreprocessorGroup} Svelte preprocessor group
86
+ */
87
+ export declare function preprocessor(options?: SvelteAdapterOptions): PreprocessorGroup;
88
+ /**
89
+ * Create a Vite plugin for Svelte preprocessing.
90
+ *
91
+ * Note: In most cases, you should use the preprocessor directly in svelte.config.js.
92
+ * This Vite plugin is provided for cases where you need to integrate at the Vite level.
93
+ *
94
+ * @param {SvelteAdapterOptions} options - Plugin options
95
+ * @returns {Plugin} Vite plugin
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * // vite.config.ts
100
+ * import { defineConfig } from 'vite';
101
+ * import { svelte } from '@sveltejs/vite-plugin-svelte';
102
+ * import { vitePlugin as csszyx } from '@csszyx/svelte-adapter';
103
+ *
104
+ * export default defineConfig({
105
+ * plugins: [
106
+ * csszyx(),
107
+ * svelte(),
108
+ * ],
109
+ * });
110
+ * ```
111
+ */
112
+ export declare function vitePlugin(options?: SvelteAdapterOptions): Plugin;
113
+ /**
114
+ * Preprocess a Svelte file, transforming sz props to class attributes.
115
+ *
116
+ * @param {string} source - Svelte source code
117
+ * @param {SvelteAdapterOptions} options - Preprocessor options
118
+ * @returns {PreprocessorResult} Preprocessing result
119
+ */
120
+ export declare function preprocess(source: string, options?: SvelteAdapterOptions): PreprocessorResult;
121
+ /**
122
+ * Default export - the preprocessor function.
123
+ */
124
+ export default preprocessor;
package/dist/index.js ADDED
@@ -0,0 +1,284 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ exports.mergeClassAttributes = mergeClassAttributes;
8
+ exports.parseObjectLiteral = parseObjectLiteral;
9
+ exports.preprocess = preprocess;
10
+ exports.preprocessor = preprocessor;
11
+ exports.transformMarkup = transformMarkup;
12
+ exports.vitePlugin = vitePlugin;
13
+ var _compiler = require("@csszyx/compiler");
14
+ var _oxcParser = require("oxc-parser");
15
+ function extractValue(node) {
16
+ switch (node.type) {
17
+ case "Literal":
18
+ return node.value;
19
+ case "UnaryExpression":
20
+ if (node.operator === "-" && node.argument.type === "Literal" && typeof node.argument.value === "number") {
21
+ return -node.argument.value;
22
+ }
23
+ return void 0;
24
+ case "ArrayExpression":
25
+ {
26
+ const arr = [];
27
+ for (const el of node.elements ?? []) {
28
+ if (!el) {
29
+ arr.push(null);
30
+ continue;
31
+ }
32
+ const v = extractValue(el);
33
+ if (v === void 0) return void 0;
34
+ arr.push(v);
35
+ }
36
+ return arr;
37
+ }
38
+ case "ObjectExpression":
39
+ return extractObjectNode(node);
40
+ case "TemplateLiteral":
41
+ if ((node.expressions ?? []).length === 0) {
42
+ const quasis = node.quasis;
43
+ return quasis[0].value.cooked ?? void 0;
44
+ }
45
+ return void 0;
46
+ default:
47
+ return void 0;
48
+ }
49
+ }
50
+ function extractObjectNode(node) {
51
+ const obj = {};
52
+ for (const prop of node.properties ?? []) {
53
+ if (prop.type !== "Property") return void 0;
54
+ if (prop.computed) return void 0;
55
+ const key_node = prop.key;
56
+ let key;
57
+ if (key_node.type === "Identifier") {
58
+ key = key_node.name;
59
+ } else if (key_node.type === "Literal" && typeof key_node.value === "string") {
60
+ key = key_node.value;
61
+ } else if (key_node.type === "Literal" && typeof key_node.value === "number") {
62
+ key = String(key_node.value);
63
+ } else {
64
+ return void 0;
65
+ }
66
+ const value = extractValue(prop.value);
67
+ if (value === void 0) return void 0;
68
+ obj[key] = value;
69
+ }
70
+ return obj;
71
+ }
72
+ function parseObjectLiteral(objStr) {
73
+ try {
74
+ const src = `const _=${objStr.trim()}`;
75
+ const parsed = (0, _oxcParser.parseSync)("sz.js", src);
76
+ if (parsed.errors.length > 0) return null;
77
+ const decl = parsed.program.body;
78
+ const init = decl[0].declarations[0].init ?? null;
79
+ if (!init || init.type !== "ObjectExpression") return null;
80
+ return extractObjectNode(init) ?? null;
81
+ } catch {
82
+ return null;
83
+ }
84
+ }
85
+ function transformMarkup(content, options = {}) {
86
+ let result = "";
87
+ let count = 0;
88
+ let cursor = 0;
89
+ while (cursor < content.length) {
90
+ const attributeStart = findSzAttribute(content, cursor);
91
+ if (attributeStart === -1) {
92
+ result += content.slice(cursor);
93
+ break;
94
+ }
95
+ result += content.slice(cursor, attributeStart);
96
+ const match = readSzAttribute(content, attributeStart);
97
+ if (!match) {
98
+ result += content.slice(attributeStart, attributeStart + 3);
99
+ cursor = attributeStart + 3;
100
+ continue;
101
+ }
102
+ const szObj = parseObjectLiteral(match.objectSource);
103
+ if (!szObj) {
104
+ if (options.debug) {
105
+ console.warn(`[csszyx/svelte] Failed to parse sz object: ${match.objectSource}`);
106
+ }
107
+ result += content.slice(attributeStart, match.end);
108
+ } else {
109
+ const className = (0, _compiler.transform)(szObj).className;
110
+ count += 1;
111
+ if (options.debug) {
112
+ console.log(`[csszyx/svelte] Transformed: ${match.objectSource} -> "${className}"`);
113
+ }
114
+ result += `class="${className}"`;
115
+ }
116
+ cursor = match.end;
117
+ }
118
+ return {
119
+ code: result,
120
+ count
121
+ };
122
+ }
123
+ function findSzAttribute(content, from) {
124
+ let index = content.indexOf("sz=", from);
125
+ while (index !== -1) {
126
+ const before = index === 0 ? "<" : content.charAt(index - 1);
127
+ if (before === "<" || before === " " || before === " " || before === "\n" || before === "\r") {
128
+ return index;
129
+ }
130
+ index = content.indexOf("sz=", index + 3);
131
+ }
132
+ return -1;
133
+ }
134
+ function readSzAttribute(content, start) {
135
+ const valueStart = start + 3;
136
+ const opener = content.charAt(valueStart);
137
+ if (opener === '"' || opener === "'") {
138
+ const endQuote = content.indexOf(opener, valueStart + 1);
139
+ if (endQuote === -1) {
140
+ return null;
141
+ }
142
+ let objectSource = content.slice(valueStart + 1, endQuote);
143
+ if (objectSource.startsWith("{{") && objectSource.endsWith("}}")) {
144
+ objectSource = objectSource.slice(1, -1);
145
+ }
146
+ if (!objectSource.startsWith("{") || !objectSource.endsWith("}")) {
147
+ return null;
148
+ }
149
+ return {
150
+ end: endQuote + 1,
151
+ objectSource
152
+ };
153
+ }
154
+ if (opener !== "{" || content.charAt(valueStart + 1) !== "{") {
155
+ return null;
156
+ }
157
+ const end = findBalancedBraceEnd(content, valueStart);
158
+ if (end === -1) {
159
+ return null;
160
+ }
161
+ return {
162
+ end: end + 1,
163
+ objectSource: content.slice(valueStart + 1, end)
164
+ };
165
+ }
166
+ function findBalancedBraceEnd(content, start) {
167
+ let depth = 0;
168
+ let quote = "";
169
+ let escaped = false;
170
+ for (let index = start; index < content.length; index += 1) {
171
+ const char = content.charAt(index);
172
+ if (quote) {
173
+ if (escaped) {
174
+ escaped = false;
175
+ } else if (char === "\\") {
176
+ escaped = true;
177
+ } else if (char === quote) {
178
+ quote = "";
179
+ }
180
+ continue;
181
+ }
182
+ if (char === '"' || char === "'" || char === "`") {
183
+ quote = char;
184
+ } else if (char === "{") {
185
+ depth += 1;
186
+ } else if (char === "}") {
187
+ depth -= 1;
188
+ if (depth === 0) {
189
+ return index;
190
+ }
191
+ }
192
+ }
193
+ return -1;
194
+ }
195
+ function mergeClassAttributes(content) {
196
+ let result = content;
197
+ let i = 0;
198
+ while (i < result.length) {
199
+ const start = result.indexOf("<", i);
200
+ if (start === -1) break;
201
+ const end = result.indexOf(">", start);
202
+ if (end === -1) break;
203
+ const tag = result.slice(start, end + 1);
204
+ i = end + 1;
205
+ if (!/^<[a-z]/i.test(tag)) continue;
206
+ const matches = [...tag.matchAll(/\bclass="([^"]*)"/g)].map(m => m[1]);
207
+ if (matches.length < 2) continue;
208
+ const merged = matches.join(" ");
209
+ const firstIdx = tag.indexOf('class="');
210
+ const cleaned = tag.replace(/\bclass="[^"]*"/g, "");
211
+ const newTag = `${cleaned.slice(0, firstIdx)}class="${merged}"${cleaned.slice(firstIdx)}`;
212
+ result = result.slice(0, start) + newTag + result.slice(end + 1);
213
+ i = start + newTag.length;
214
+ }
215
+ return result;
216
+ }
217
+ function preprocessor(options = {}) {
218
+ return {
219
+ name: "csszyx-svelte",
220
+ markup({
221
+ content,
222
+ filename
223
+ }) {
224
+ if (!content.includes("sz=")) {
225
+ return;
226
+ }
227
+ if (options.debug && filename) {
228
+ console.log(`[csszyx/svelte] Processing: ${filename}`);
229
+ }
230
+ const transformResult = transformMarkup(content, options);
231
+ if (transformResult.count === 0) {
232
+ return;
233
+ }
234
+ const mergedContent = mergeClassAttributes(transformResult.code);
235
+ if (options.debug) {
236
+ console.log(`[csszyx/svelte] Transformed ${transformResult.count} sz props`);
237
+ }
238
+ return {
239
+ code: mergedContent,
240
+ map: void 0
241
+ // TODO: Generate source map
242
+ };
243
+ }
244
+ };
245
+ }
246
+ function vitePlugin(options = {}) {
247
+ return {
248
+ name: "csszyx-svelte-vite",
249
+ enforce: "pre",
250
+ transform(code, id) {
251
+ if (!id.endsWith(".svelte")) {
252
+ return null;
253
+ }
254
+ if (!code.includes("sz=")) {
255
+ return null;
256
+ }
257
+ const transformResult = transformMarkup(code, options);
258
+ if (transformResult.count === 0) {
259
+ return null;
260
+ }
261
+ const mergedContent = mergeClassAttributes(transformResult.code);
262
+ return {
263
+ code: mergedContent,
264
+ map: void 0
265
+ // TODO: Generate source map
266
+ };
267
+ }
268
+ };
269
+ }
270
+ function preprocess(source, options = {}) {
271
+ const transformResult = transformMarkup(source, options);
272
+ if (transformResult.count === 0) {
273
+ return {
274
+ code: source,
275
+ map: void 0
276
+ };
277
+ }
278
+ const mergedContent = mergeClassAttributes(transformResult.code);
279
+ return {
280
+ code: mergedContent,
281
+ map: void 0
282
+ };
283
+ }
284
+ module.exports = preprocessor;
package/dist/index.mjs ADDED
@@ -0,0 +1,262 @@
1
+ import { transform } from "@csszyx/compiler";
2
+ import { parseSync } from "oxc-parser";
3
+ function extractValue(node) {
4
+ switch (node.type) {
5
+ case "Literal":
6
+ return node.value;
7
+ case "UnaryExpression":
8
+ if (node.operator === "-" && node.argument.type === "Literal" && typeof node.argument.value === "number") {
9
+ return -node.argument.value;
10
+ }
11
+ return void 0;
12
+ case "ArrayExpression": {
13
+ const arr = [];
14
+ for (const el of node.elements ?? []) {
15
+ if (!el) {
16
+ arr.push(null);
17
+ continue;
18
+ }
19
+ const v = extractValue(el);
20
+ if (v === void 0) return void 0;
21
+ arr.push(v);
22
+ }
23
+ return arr;
24
+ }
25
+ case "ObjectExpression":
26
+ return extractObjectNode(node);
27
+ case "TemplateLiteral":
28
+ if ((node.expressions ?? []).length === 0) {
29
+ const quasis = node.quasis;
30
+ return quasis[0].value.cooked ?? void 0;
31
+ }
32
+ return void 0;
33
+ default:
34
+ return void 0;
35
+ }
36
+ }
37
+ function extractObjectNode(node) {
38
+ const obj = {};
39
+ for (const prop of node.properties ?? []) {
40
+ if (prop.type !== "Property") return void 0;
41
+ if (prop.computed) return void 0;
42
+ const key_node = prop.key;
43
+ let key;
44
+ if (key_node.type === "Identifier") {
45
+ key = key_node.name;
46
+ } else if (key_node.type === "Literal" && typeof key_node.value === "string") {
47
+ key = key_node.value;
48
+ } else if (key_node.type === "Literal" && typeof key_node.value === "number") {
49
+ key = String(key_node.value);
50
+ } else {
51
+ return void 0;
52
+ }
53
+ const value = extractValue(prop.value);
54
+ if (value === void 0) return void 0;
55
+ obj[key] = value;
56
+ }
57
+ return obj;
58
+ }
59
+ export function parseObjectLiteral(objStr) {
60
+ try {
61
+ const src = `const _=${objStr.trim()}`;
62
+ const parsed = parseSync("sz.js", src);
63
+ if (parsed.errors.length > 0) return null;
64
+ const decl = parsed.program.body;
65
+ const init = decl[0].declarations[0].init ?? null;
66
+ if (!init || init.type !== "ObjectExpression") return null;
67
+ return extractObjectNode(init) ?? null;
68
+ } catch {
69
+ return null;
70
+ }
71
+ }
72
+ export function transformMarkup(content, options = {}) {
73
+ let result = "";
74
+ let count = 0;
75
+ let cursor = 0;
76
+ while (cursor < content.length) {
77
+ const attributeStart = findSzAttribute(content, cursor);
78
+ if (attributeStart === -1) {
79
+ result += content.slice(cursor);
80
+ break;
81
+ }
82
+ result += content.slice(cursor, attributeStart);
83
+ const match = readSzAttribute(content, attributeStart);
84
+ if (!match) {
85
+ result += content.slice(attributeStart, attributeStart + 3);
86
+ cursor = attributeStart + 3;
87
+ continue;
88
+ }
89
+ const szObj = parseObjectLiteral(match.objectSource);
90
+ if (!szObj) {
91
+ if (options.debug) {
92
+ console.warn(`[csszyx/svelte] Failed to parse sz object: ${match.objectSource}`);
93
+ }
94
+ result += content.slice(attributeStart, match.end);
95
+ } else {
96
+ const className = transform(szObj).className;
97
+ count += 1;
98
+ if (options.debug) {
99
+ console.log(`[csszyx/svelte] Transformed: ${match.objectSource} -> "${className}"`);
100
+ }
101
+ result += `class="${className}"`;
102
+ }
103
+ cursor = match.end;
104
+ }
105
+ return { code: result, count };
106
+ }
107
+ function findSzAttribute(content, from) {
108
+ let index = content.indexOf("sz=", from);
109
+ while (index !== -1) {
110
+ const before = index === 0 ? "<" : content.charAt(index - 1);
111
+ if (before === "<" || before === " " || before === " " || before === "\n" || before === "\r") {
112
+ return index;
113
+ }
114
+ index = content.indexOf("sz=", index + 3);
115
+ }
116
+ return -1;
117
+ }
118
+ function readSzAttribute(content, start) {
119
+ const valueStart = start + 3;
120
+ const opener = content.charAt(valueStart);
121
+ if (opener === '"' || opener === "'") {
122
+ const endQuote = content.indexOf(opener, valueStart + 1);
123
+ if (endQuote === -1) {
124
+ return null;
125
+ }
126
+ let objectSource = content.slice(valueStart + 1, endQuote);
127
+ if (objectSource.startsWith("{{") && objectSource.endsWith("}}")) {
128
+ objectSource = objectSource.slice(1, -1);
129
+ }
130
+ if (!objectSource.startsWith("{") || !objectSource.endsWith("}")) {
131
+ return null;
132
+ }
133
+ return { end: endQuote + 1, objectSource };
134
+ }
135
+ if (opener !== "{" || content.charAt(valueStart + 1) !== "{") {
136
+ return null;
137
+ }
138
+ const end = findBalancedBraceEnd(content, valueStart);
139
+ if (end === -1) {
140
+ return null;
141
+ }
142
+ return {
143
+ end: end + 1,
144
+ objectSource: content.slice(valueStart + 1, end)
145
+ };
146
+ }
147
+ function findBalancedBraceEnd(content, start) {
148
+ let depth = 0;
149
+ let quote = "";
150
+ let escaped = false;
151
+ for (let index = start; index < content.length; index += 1) {
152
+ const char = content.charAt(index);
153
+ if (quote) {
154
+ if (escaped) {
155
+ escaped = false;
156
+ } else if (char === "\\") {
157
+ escaped = true;
158
+ } else if (char === quote) {
159
+ quote = "";
160
+ }
161
+ continue;
162
+ }
163
+ if (char === '"' || char === "'" || char === "`") {
164
+ quote = char;
165
+ } else if (char === "{") {
166
+ depth += 1;
167
+ } else if (char === "}") {
168
+ depth -= 1;
169
+ if (depth === 0) {
170
+ return index;
171
+ }
172
+ }
173
+ }
174
+ return -1;
175
+ }
176
+ export function mergeClassAttributes(content) {
177
+ let result = content;
178
+ let i = 0;
179
+ while (i < result.length) {
180
+ const start = result.indexOf("<", i);
181
+ if (start === -1) break;
182
+ const end = result.indexOf(">", start);
183
+ if (end === -1) break;
184
+ const tag = result.slice(start, end + 1);
185
+ i = end + 1;
186
+ if (!/^<[a-z]/i.test(tag)) continue;
187
+ const matches = [...tag.matchAll(/\bclass="([^"]*)"/g)].map((m) => m[1]);
188
+ if (matches.length < 2) continue;
189
+ const merged = matches.join(" ");
190
+ const firstIdx = tag.indexOf('class="');
191
+ const cleaned = tag.replace(/\bclass="[^"]*"/g, "");
192
+ const newTag = `${cleaned.slice(0, firstIdx)}class="${merged}"${cleaned.slice(firstIdx)}`;
193
+ result = result.slice(0, start) + newTag + result.slice(end + 1);
194
+ i = start + newTag.length;
195
+ }
196
+ return result;
197
+ }
198
+ export function preprocessor(options = {}) {
199
+ return {
200
+ name: "csszyx-svelte",
201
+ markup({ content, filename }) {
202
+ if (!content.includes("sz=")) {
203
+ return;
204
+ }
205
+ if (options.debug && filename) {
206
+ console.log(`[csszyx/svelte] Processing: ${filename}`);
207
+ }
208
+ const transformResult = transformMarkup(content, options);
209
+ if (transformResult.count === 0) {
210
+ return;
211
+ }
212
+ const mergedContent = mergeClassAttributes(transformResult.code);
213
+ if (options.debug) {
214
+ console.log(`[csszyx/svelte] Transformed ${transformResult.count} sz props`);
215
+ }
216
+ return {
217
+ code: mergedContent,
218
+ map: void 0
219
+ // TODO: Generate source map
220
+ };
221
+ }
222
+ };
223
+ }
224
+ export function vitePlugin(options = {}) {
225
+ return {
226
+ name: "csszyx-svelte-vite",
227
+ enforce: "pre",
228
+ transform(code, id) {
229
+ if (!id.endsWith(".svelte")) {
230
+ return null;
231
+ }
232
+ if (!code.includes("sz=")) {
233
+ return null;
234
+ }
235
+ const transformResult = transformMarkup(code, options);
236
+ if (transformResult.count === 0) {
237
+ return null;
238
+ }
239
+ const mergedContent = mergeClassAttributes(transformResult.code);
240
+ return {
241
+ code: mergedContent,
242
+ map: void 0
243
+ // TODO: Generate source map
244
+ };
245
+ }
246
+ };
247
+ }
248
+ export function preprocess(source, options = {}) {
249
+ const transformResult = transformMarkup(source, options);
250
+ if (transformResult.count === 0) {
251
+ return {
252
+ code: source,
253
+ map: void 0
254
+ };
255
+ }
256
+ const mergedContent = mergeClassAttributes(transformResult.code);
257
+ return {
258
+ code: mergedContent,
259
+ map: void 0
260
+ };
261
+ }
262
+ export default preprocessor;
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@csszyx/svelte-adapter",
3
+ "version": "0.9.1",
4
+ "private": false,
5
+ "description": "Svelte preprocessor for csszyx - transforms sz props into Tailwind CSS classes",
6
+ "homepage": "https://github.com/nguyennhutien/csszyx/tree/main/packages/svelte-adapter#readme",
7
+ "bugs": {
8
+ "url": "https://github.com/nguyennhutien/csszyx/issues"
9
+ },
10
+ "type": "module",
11
+ "main": "./dist/index.cjs",
12
+ "module": "./dist/index.mjs",
13
+ "types": "./dist/index.d.mts",
14
+ "exports": {
15
+ ".": {
16
+ "import": "./dist/index.mjs",
17
+ "types": "./dist/index.d.mts",
18
+ "require": "./dist/index.cjs"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "dependencies": {
25
+ "oxc-parser": "0.131.0",
26
+ "@csszyx/compiler": "0.9.1"
27
+ },
28
+ "peerDependencies": {
29
+ "svelte": "^4.0.0 || ^5.0.0"
30
+ },
31
+ "peerDependenciesMeta": {
32
+ "svelte": {
33
+ "optional": true
34
+ }
35
+ },
36
+ "devDependencies": {
37
+ "@types/node": "^20.11.0",
38
+ "svelte": "^5.0.0",
39
+ "typescript": "^6.0.3",
40
+ "unbuild": "^3.6.1",
41
+ "vitest": "^4.1.6"
42
+ },
43
+ "keywords": [
44
+ "csszyx",
45
+ "svelte",
46
+ "preprocessor",
47
+ "tailwindcss",
48
+ "css-in-js"
49
+ ],
50
+ "license": "MIT",
51
+ "repository": {
52
+ "type": "git",
53
+ "url": "https://github.com/nguyennhutien/csszyx.git",
54
+ "directory": "packages/svelte-adapter"
55
+ },
56
+ "scripts": {
57
+ "build": "unbuild",
58
+ "dev": "unbuild --stub",
59
+ "test": "vitest run",
60
+ "type-check": "tsc --noEmit"
61
+ }
62
+ }