@csszyx/vue-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 +21 -0
- package/dist/index.cjs +262 -0
- package/dist/index.d.cts +129 -0
- package/dist/index.d.mts +129 -0
- package/dist/index.d.ts +129 -0
- package/dist/index.js +262 -0
- package/dist/index.mjs +243 -0
- package/package.json +62 -0
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,262 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
exports.extractTemplate = extractTemplate;
|
|
8
|
+
exports.mergeClassAttributes = mergeClassAttributes;
|
|
9
|
+
exports.parseObjectLiteral = parseObjectLiteral;
|
|
10
|
+
exports.preprocess = preprocess;
|
|
11
|
+
exports.transformTemplate = transformTemplate;
|
|
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 extractTemplate(source) {
|
|
86
|
+
const lowered = source.toLowerCase();
|
|
87
|
+
let openStart = -1;
|
|
88
|
+
let openEnd = -1;
|
|
89
|
+
let i = 0;
|
|
90
|
+
while (i < lowered.length) {
|
|
91
|
+
const candidate = lowered.indexOf("<template", i);
|
|
92
|
+
if (candidate === -1) return null;
|
|
93
|
+
const after = lowered.charAt(candidate + "<template".length);
|
|
94
|
+
if (after === ">" || after === " " || after === " " || after === "\n" || after === "\r") {
|
|
95
|
+
const tagClose = lowered.indexOf(">", candidate);
|
|
96
|
+
if (tagClose === -1) return null;
|
|
97
|
+
openStart = candidate;
|
|
98
|
+
openEnd = tagClose + 1;
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
i = candidate + 1;
|
|
102
|
+
}
|
|
103
|
+
if (openStart === -1) return null;
|
|
104
|
+
const closeStart = lowered.indexOf("</template>", openEnd);
|
|
105
|
+
if (closeStart === -1) return null;
|
|
106
|
+
return {
|
|
107
|
+
content: source.slice(openEnd, closeStart),
|
|
108
|
+
start: openEnd,
|
|
109
|
+
end: closeStart
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function transformTemplate(template, options = {}) {
|
|
113
|
+
let result = "";
|
|
114
|
+
let count = 0;
|
|
115
|
+
let cursor = 0;
|
|
116
|
+
while (cursor < template.length) {
|
|
117
|
+
const attribute = findVueSzAttribute(template, cursor);
|
|
118
|
+
if (!attribute) {
|
|
119
|
+
result += template.slice(cursor);
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
result += template.slice(cursor, attribute.start);
|
|
123
|
+
const match = readQuotedSzAttribute(template, attribute.start, attribute.valueStart);
|
|
124
|
+
if (!match) {
|
|
125
|
+
result += template.slice(attribute.start, attribute.valueStart);
|
|
126
|
+
cursor = attribute.valueStart;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const szObj = parseObjectLiteral(match.objectSource);
|
|
130
|
+
if (!szObj) {
|
|
131
|
+
if (options.debug) {
|
|
132
|
+
console.warn(`[csszyx/vue] Failed to parse sz object: ${match.objectSource}`);
|
|
133
|
+
}
|
|
134
|
+
result += template.slice(attribute.start, match.end);
|
|
135
|
+
} else {
|
|
136
|
+
const className = (0, _compiler.transform)(szObj).className;
|
|
137
|
+
count += 1;
|
|
138
|
+
if (options.debug) {
|
|
139
|
+
console.log(`[csszyx/vue] Transformed: ${match.objectSource} -> "${className}"`);
|
|
140
|
+
}
|
|
141
|
+
result += `class="${className}"`;
|
|
142
|
+
}
|
|
143
|
+
cursor = match.end;
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
code: result,
|
|
147
|
+
transformed: count > 0,
|
|
148
|
+
count
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function findVueSzAttribute(template, from) {
|
|
152
|
+
let szStart = template.indexOf("sz=", from);
|
|
153
|
+
while (szStart !== -1) {
|
|
154
|
+
for (const prefix of ["v-bind:", ":", ""]) {
|
|
155
|
+
const start = szStart - prefix.length;
|
|
156
|
+
if (start < 0 || template.slice(start, szStart) !== prefix) {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const before = start === 0 ? "<" : template.charAt(start - 1);
|
|
160
|
+
if (isAttributeBoundary(before)) {
|
|
161
|
+
return {
|
|
162
|
+
start,
|
|
163
|
+
valueStart: szStart + 3
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
szStart = template.indexOf("sz=", szStart + 3);
|
|
168
|
+
}
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
function readQuotedSzAttribute(template, start, valueStart) {
|
|
172
|
+
const quote = template.charAt(valueStart);
|
|
173
|
+
if (quote !== '"' && quote !== "'") {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
const endQuote = template.indexOf(quote, valueStart + 1);
|
|
177
|
+
if (endQuote === -1) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
const objectSource = template.slice(valueStart + 1, endQuote);
|
|
181
|
+
if (!objectSource.startsWith("{") || !objectSource.endsWith("}")) {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
end: endQuote + 1,
|
|
186
|
+
objectSource
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
function isAttributeBoundary(char) {
|
|
190
|
+
return char === "<" || char === " " || char === " " || char === "\n" || char === "\r";
|
|
191
|
+
}
|
|
192
|
+
function mergeClassAttributes(template) {
|
|
193
|
+
let result = template;
|
|
194
|
+
let i = 0;
|
|
195
|
+
while (i < result.length) {
|
|
196
|
+
const start = result.indexOf("<", i);
|
|
197
|
+
if (start === -1) break;
|
|
198
|
+
const end = result.indexOf(">", start);
|
|
199
|
+
if (end === -1) break;
|
|
200
|
+
const tag = result.slice(start, end + 1);
|
|
201
|
+
i = end + 1;
|
|
202
|
+
if (!/^<[a-z]/i.test(tag)) continue;
|
|
203
|
+
const staticMatch = /\bclass="([^"]*)"/.exec(tag);
|
|
204
|
+
const dynamicMatch = /\b(?::class|v-bind:class)="([^"]*)"/.exec(tag);
|
|
205
|
+
if (!staticMatch || !dynamicMatch) continue;
|
|
206
|
+
const cleaned = tag.replace(/\bclass="[^"]*"/, "").replace(/\b(?::class|v-bind:class)="[^"]*"/, "");
|
|
207
|
+
const insertIdx = cleaned.indexOf(" ") + 1;
|
|
208
|
+
const newTag = `${cleaned.slice(0, insertIdx)}:class="['${staticMatch[1]}', ${dynamicMatch[1]}]" ${cleaned.slice(insertIdx)}`;
|
|
209
|
+
result = result.slice(0, start) + newTag + result.slice(end + 1);
|
|
210
|
+
i = start + newTag.length;
|
|
211
|
+
}
|
|
212
|
+
return result;
|
|
213
|
+
}
|
|
214
|
+
function preprocess(source, options = {}) {
|
|
215
|
+
const templateInfo = extractTemplate(source);
|
|
216
|
+
if (!templateInfo) {
|
|
217
|
+
return {
|
|
218
|
+
code: source,
|
|
219
|
+
transformed: false,
|
|
220
|
+
count: 0
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
const transformResult = transformTemplate(templateInfo.content, options);
|
|
224
|
+
if (!transformResult.transformed) {
|
|
225
|
+
return {
|
|
226
|
+
code: source,
|
|
227
|
+
transformed: false,
|
|
228
|
+
count: 0
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
const mergedContent = mergeClassAttributes(transformResult.code);
|
|
232
|
+
const code = source.slice(0, templateInfo.start) + mergedContent + source.slice(templateInfo.end);
|
|
233
|
+
return {
|
|
234
|
+
code,
|
|
235
|
+
transformed: true,
|
|
236
|
+
count: transformResult.count
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function vitePlugin(options = {}) {
|
|
240
|
+
return {
|
|
241
|
+
name: "csszyx-vue",
|
|
242
|
+
enforce: "pre",
|
|
243
|
+
transform(code, id) {
|
|
244
|
+
if (!id.endsWith(".vue")) {
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
if (!code.includes("sz=")) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
const result = preprocess(code, options);
|
|
251
|
+
if (!result.transformed) {
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
code: result.code,
|
|
256
|
+
map: null
|
|
257
|
+
// TODO: Generate source map
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
module.exports = vitePlugin;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @csszyx/vue-adapter - Vue SFC preprocessor for csszyx.
|
|
3
|
+
*
|
|
4
|
+
* Transforms `sz` props in Vue SFC templates into Tailwind CSS class strings.
|
|
5
|
+
*
|
|
6
|
+
* @module @csszyx/vue-adapter
|
|
7
|
+
*/
|
|
8
|
+
import { type SzObject } from '@csszyx/compiler';
|
|
9
|
+
/**
|
|
10
|
+
* Preprocessor options.
|
|
11
|
+
*/
|
|
12
|
+
export interface VueAdapterOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Enable verbose logging for debugging.
|
|
15
|
+
*/
|
|
16
|
+
debug?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Result of preprocessing a Vue SFC.
|
|
20
|
+
*/
|
|
21
|
+
export interface PreprocessResult {
|
|
22
|
+
/**
|
|
23
|
+
* The transformed source code.
|
|
24
|
+
*/
|
|
25
|
+
code: string;
|
|
26
|
+
/**
|
|
27
|
+
* Whether any transformations were made.
|
|
28
|
+
*/
|
|
29
|
+
transformed: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Number of sz props transformed.
|
|
32
|
+
*/
|
|
33
|
+
count: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Parse a JavaScript object literal string into an object.
|
|
37
|
+
* Handles nested objects for variants like hover, focus, etc.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} objStr - Object literal string (e.g., "{ p: 4, bg: 'red-500' }")
|
|
40
|
+
* @returns {SzObject | null} Parsed object or null if invalid
|
|
41
|
+
*/
|
|
42
|
+
export declare function parseObjectLiteral(objStr: string): SzObject | null;
|
|
43
|
+
/**
|
|
44
|
+
* Extract the template section from a Vue SFC.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} source - Vue SFC source code
|
|
47
|
+
* @returns {{ content: string; start: number; end: number } | null} Template info or null
|
|
48
|
+
*/
|
|
49
|
+
export declare function extractTemplate(source: string): {
|
|
50
|
+
content: string;
|
|
51
|
+
start: number;
|
|
52
|
+
end: number;
|
|
53
|
+
} | null;
|
|
54
|
+
/**
|
|
55
|
+
* Transform sz props in a template string.
|
|
56
|
+
*
|
|
57
|
+
* Supports:
|
|
58
|
+
* - Static: sz="{ p: 4 }" or sz='{ p: 4 }'
|
|
59
|
+
* - Bound (Vue): :sz="{ p: 4 }" or v-bind:sz="{ p: 4 }"
|
|
60
|
+
*
|
|
61
|
+
* @param {string} template - Template string
|
|
62
|
+
* @param {VueAdapterOptions} options - Options
|
|
63
|
+
* @returns {PreprocessResult} Transformation result
|
|
64
|
+
*/
|
|
65
|
+
export declare function transformTemplate(template: string, options?: VueAdapterOptions): PreprocessResult;
|
|
66
|
+
/**
|
|
67
|
+
* Merge transformed classes with existing class attribute.
|
|
68
|
+
*
|
|
69
|
+
* @param {string} template - Template with sz props transformed to class
|
|
70
|
+
* @returns {string} Template with merged class attributes
|
|
71
|
+
*/
|
|
72
|
+
export declare function mergeClassAttributes(template: string): string;
|
|
73
|
+
/**
|
|
74
|
+
* Preprocess a Vue SFC file, transforming sz props to class attributes.
|
|
75
|
+
*
|
|
76
|
+
* @param {string} source - Vue SFC source code
|
|
77
|
+
* @param {VueAdapterOptions} options - Preprocessor options
|
|
78
|
+
* @returns {PreprocessResult} Preprocessing result
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* import { preprocess } from '@csszyx/vue-adapter';
|
|
83
|
+
*
|
|
84
|
+
* const result = preprocess(`
|
|
85
|
+
* <template>
|
|
86
|
+
* <div sz="{ p: 4, bg: 'red-500' }">Hello</div>
|
|
87
|
+
* </template>
|
|
88
|
+
* `);
|
|
89
|
+
*
|
|
90
|
+
* // result.code contains:
|
|
91
|
+
* // <template>
|
|
92
|
+
* // <div class="p-4 bg-red-500">Hello</div>
|
|
93
|
+
* // </template>
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export declare function preprocess(source: string, options?: VueAdapterOptions): PreprocessResult;
|
|
97
|
+
/**
|
|
98
|
+
* Create a Vite plugin for Vue SFC preprocessing.
|
|
99
|
+
*
|
|
100
|
+
* @param {VueAdapterOptions} options - Plugin options
|
|
101
|
+
* @returns {object} Vite plugin
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* // vite.config.ts
|
|
106
|
+
* import { defineConfig } from 'vite';
|
|
107
|
+
* import vue from '@vitejs/plugin-vue';
|
|
108
|
+
* import { vitePlugin as csszyx } from '@csszyx/vue-adapter';
|
|
109
|
+
*
|
|
110
|
+
* export default defineConfig({
|
|
111
|
+
* plugins: [
|
|
112
|
+
* csszyx(),
|
|
113
|
+
* vue(),
|
|
114
|
+
* ],
|
|
115
|
+
* });
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
import type { Plugin } from 'vite';
|
|
119
|
+
/**
|
|
120
|
+
* Create a Vite plugin for Vue SFC preprocessing.
|
|
121
|
+
*
|
|
122
|
+
* @param {VueAdapterOptions} options - Plugin options
|
|
123
|
+
* @returns {Plugin} Vite plugin
|
|
124
|
+
*/
|
|
125
|
+
export declare function vitePlugin(options?: VueAdapterOptions): Plugin;
|
|
126
|
+
/**
|
|
127
|
+
* Default export for convenient importing.
|
|
128
|
+
*/
|
|
129
|
+
export default vitePlugin;
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @csszyx/vue-adapter - Vue SFC preprocessor for csszyx.
|
|
3
|
+
*
|
|
4
|
+
* Transforms `sz` props in Vue SFC templates into Tailwind CSS class strings.
|
|
5
|
+
*
|
|
6
|
+
* @module @csszyx/vue-adapter
|
|
7
|
+
*/
|
|
8
|
+
import { type SzObject } from '@csszyx/compiler';
|
|
9
|
+
/**
|
|
10
|
+
* Preprocessor options.
|
|
11
|
+
*/
|
|
12
|
+
export interface VueAdapterOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Enable verbose logging for debugging.
|
|
15
|
+
*/
|
|
16
|
+
debug?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Result of preprocessing a Vue SFC.
|
|
20
|
+
*/
|
|
21
|
+
export interface PreprocessResult {
|
|
22
|
+
/**
|
|
23
|
+
* The transformed source code.
|
|
24
|
+
*/
|
|
25
|
+
code: string;
|
|
26
|
+
/**
|
|
27
|
+
* Whether any transformations were made.
|
|
28
|
+
*/
|
|
29
|
+
transformed: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Number of sz props transformed.
|
|
32
|
+
*/
|
|
33
|
+
count: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Parse a JavaScript object literal string into an object.
|
|
37
|
+
* Handles nested objects for variants like hover, focus, etc.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} objStr - Object literal string (e.g., "{ p: 4, bg: 'red-500' }")
|
|
40
|
+
* @returns {SzObject | null} Parsed object or null if invalid
|
|
41
|
+
*/
|
|
42
|
+
export declare function parseObjectLiteral(objStr: string): SzObject | null;
|
|
43
|
+
/**
|
|
44
|
+
* Extract the template section from a Vue SFC.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} source - Vue SFC source code
|
|
47
|
+
* @returns {{ content: string; start: number; end: number } | null} Template info or null
|
|
48
|
+
*/
|
|
49
|
+
export declare function extractTemplate(source: string): {
|
|
50
|
+
content: string;
|
|
51
|
+
start: number;
|
|
52
|
+
end: number;
|
|
53
|
+
} | null;
|
|
54
|
+
/**
|
|
55
|
+
* Transform sz props in a template string.
|
|
56
|
+
*
|
|
57
|
+
* Supports:
|
|
58
|
+
* - Static: sz="{ p: 4 }" or sz='{ p: 4 }'
|
|
59
|
+
* - Bound (Vue): :sz="{ p: 4 }" or v-bind:sz="{ p: 4 }"
|
|
60
|
+
*
|
|
61
|
+
* @param {string} template - Template string
|
|
62
|
+
* @param {VueAdapterOptions} options - Options
|
|
63
|
+
* @returns {PreprocessResult} Transformation result
|
|
64
|
+
*/
|
|
65
|
+
export declare function transformTemplate(template: string, options?: VueAdapterOptions): PreprocessResult;
|
|
66
|
+
/**
|
|
67
|
+
* Merge transformed classes with existing class attribute.
|
|
68
|
+
*
|
|
69
|
+
* @param {string} template - Template with sz props transformed to class
|
|
70
|
+
* @returns {string} Template with merged class attributes
|
|
71
|
+
*/
|
|
72
|
+
export declare function mergeClassAttributes(template: string): string;
|
|
73
|
+
/**
|
|
74
|
+
* Preprocess a Vue SFC file, transforming sz props to class attributes.
|
|
75
|
+
*
|
|
76
|
+
* @param {string} source - Vue SFC source code
|
|
77
|
+
* @param {VueAdapterOptions} options - Preprocessor options
|
|
78
|
+
* @returns {PreprocessResult} Preprocessing result
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* import { preprocess } from '@csszyx/vue-adapter';
|
|
83
|
+
*
|
|
84
|
+
* const result = preprocess(`
|
|
85
|
+
* <template>
|
|
86
|
+
* <div sz="{ p: 4, bg: 'red-500' }">Hello</div>
|
|
87
|
+
* </template>
|
|
88
|
+
* `);
|
|
89
|
+
*
|
|
90
|
+
* // result.code contains:
|
|
91
|
+
* // <template>
|
|
92
|
+
* // <div class="p-4 bg-red-500">Hello</div>
|
|
93
|
+
* // </template>
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export declare function preprocess(source: string, options?: VueAdapterOptions): PreprocessResult;
|
|
97
|
+
/**
|
|
98
|
+
* Create a Vite plugin for Vue SFC preprocessing.
|
|
99
|
+
*
|
|
100
|
+
* @param {VueAdapterOptions} options - Plugin options
|
|
101
|
+
* @returns {object} Vite plugin
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* // vite.config.ts
|
|
106
|
+
* import { defineConfig } from 'vite';
|
|
107
|
+
* import vue from '@vitejs/plugin-vue';
|
|
108
|
+
* import { vitePlugin as csszyx } from '@csszyx/vue-adapter';
|
|
109
|
+
*
|
|
110
|
+
* export default defineConfig({
|
|
111
|
+
* plugins: [
|
|
112
|
+
* csszyx(),
|
|
113
|
+
* vue(),
|
|
114
|
+
* ],
|
|
115
|
+
* });
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
import type { Plugin } from 'vite';
|
|
119
|
+
/**
|
|
120
|
+
* Create a Vite plugin for Vue SFC preprocessing.
|
|
121
|
+
*
|
|
122
|
+
* @param {VueAdapterOptions} options - Plugin options
|
|
123
|
+
* @returns {Plugin} Vite plugin
|
|
124
|
+
*/
|
|
125
|
+
export declare function vitePlugin(options?: VueAdapterOptions): Plugin;
|
|
126
|
+
/**
|
|
127
|
+
* Default export for convenient importing.
|
|
128
|
+
*/
|
|
129
|
+
export default vitePlugin;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @csszyx/vue-adapter - Vue SFC preprocessor for csszyx.
|
|
3
|
+
*
|
|
4
|
+
* Transforms `sz` props in Vue SFC templates into Tailwind CSS class strings.
|
|
5
|
+
*
|
|
6
|
+
* @module @csszyx/vue-adapter
|
|
7
|
+
*/
|
|
8
|
+
import { type SzObject } from '@csszyx/compiler';
|
|
9
|
+
/**
|
|
10
|
+
* Preprocessor options.
|
|
11
|
+
*/
|
|
12
|
+
export interface VueAdapterOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Enable verbose logging for debugging.
|
|
15
|
+
*/
|
|
16
|
+
debug?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Result of preprocessing a Vue SFC.
|
|
20
|
+
*/
|
|
21
|
+
export interface PreprocessResult {
|
|
22
|
+
/**
|
|
23
|
+
* The transformed source code.
|
|
24
|
+
*/
|
|
25
|
+
code: string;
|
|
26
|
+
/**
|
|
27
|
+
* Whether any transformations were made.
|
|
28
|
+
*/
|
|
29
|
+
transformed: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Number of sz props transformed.
|
|
32
|
+
*/
|
|
33
|
+
count: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Parse a JavaScript object literal string into an object.
|
|
37
|
+
* Handles nested objects for variants like hover, focus, etc.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} objStr - Object literal string (e.g., "{ p: 4, bg: 'red-500' }")
|
|
40
|
+
* @returns {SzObject | null} Parsed object or null if invalid
|
|
41
|
+
*/
|
|
42
|
+
export declare function parseObjectLiteral(objStr: string): SzObject | null;
|
|
43
|
+
/**
|
|
44
|
+
* Extract the template section from a Vue SFC.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} source - Vue SFC source code
|
|
47
|
+
* @returns {{ content: string; start: number; end: number } | null} Template info or null
|
|
48
|
+
*/
|
|
49
|
+
export declare function extractTemplate(source: string): {
|
|
50
|
+
content: string;
|
|
51
|
+
start: number;
|
|
52
|
+
end: number;
|
|
53
|
+
} | null;
|
|
54
|
+
/**
|
|
55
|
+
* Transform sz props in a template string.
|
|
56
|
+
*
|
|
57
|
+
* Supports:
|
|
58
|
+
* - Static: sz="{ p: 4 }" or sz='{ p: 4 }'
|
|
59
|
+
* - Bound (Vue): :sz="{ p: 4 }" or v-bind:sz="{ p: 4 }"
|
|
60
|
+
*
|
|
61
|
+
* @param {string} template - Template string
|
|
62
|
+
* @param {VueAdapterOptions} options - Options
|
|
63
|
+
* @returns {PreprocessResult} Transformation result
|
|
64
|
+
*/
|
|
65
|
+
export declare function transformTemplate(template: string, options?: VueAdapterOptions): PreprocessResult;
|
|
66
|
+
/**
|
|
67
|
+
* Merge transformed classes with existing class attribute.
|
|
68
|
+
*
|
|
69
|
+
* @param {string} template - Template with sz props transformed to class
|
|
70
|
+
* @returns {string} Template with merged class attributes
|
|
71
|
+
*/
|
|
72
|
+
export declare function mergeClassAttributes(template: string): string;
|
|
73
|
+
/**
|
|
74
|
+
* Preprocess a Vue SFC file, transforming sz props to class attributes.
|
|
75
|
+
*
|
|
76
|
+
* @param {string} source - Vue SFC source code
|
|
77
|
+
* @param {VueAdapterOptions} options - Preprocessor options
|
|
78
|
+
* @returns {PreprocessResult} Preprocessing result
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* import { preprocess } from '@csszyx/vue-adapter';
|
|
83
|
+
*
|
|
84
|
+
* const result = preprocess(`
|
|
85
|
+
* <template>
|
|
86
|
+
* <div sz="{ p: 4, bg: 'red-500' }">Hello</div>
|
|
87
|
+
* </template>
|
|
88
|
+
* `);
|
|
89
|
+
*
|
|
90
|
+
* // result.code contains:
|
|
91
|
+
* // <template>
|
|
92
|
+
* // <div class="p-4 bg-red-500">Hello</div>
|
|
93
|
+
* // </template>
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export declare function preprocess(source: string, options?: VueAdapterOptions): PreprocessResult;
|
|
97
|
+
/**
|
|
98
|
+
* Create a Vite plugin for Vue SFC preprocessing.
|
|
99
|
+
*
|
|
100
|
+
* @param {VueAdapterOptions} options - Plugin options
|
|
101
|
+
* @returns {object} Vite plugin
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* // vite.config.ts
|
|
106
|
+
* import { defineConfig } from 'vite';
|
|
107
|
+
* import vue from '@vitejs/plugin-vue';
|
|
108
|
+
* import { vitePlugin as csszyx } from '@csszyx/vue-adapter';
|
|
109
|
+
*
|
|
110
|
+
* export default defineConfig({
|
|
111
|
+
* plugins: [
|
|
112
|
+
* csszyx(),
|
|
113
|
+
* vue(),
|
|
114
|
+
* ],
|
|
115
|
+
* });
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
import type { Plugin } from 'vite';
|
|
119
|
+
/**
|
|
120
|
+
* Create a Vite plugin for Vue SFC preprocessing.
|
|
121
|
+
*
|
|
122
|
+
* @param {VueAdapterOptions} options - Plugin options
|
|
123
|
+
* @returns {Plugin} Vite plugin
|
|
124
|
+
*/
|
|
125
|
+
export declare function vitePlugin(options?: VueAdapterOptions): Plugin;
|
|
126
|
+
/**
|
|
127
|
+
* Default export for convenient importing.
|
|
128
|
+
*/
|
|
129
|
+
export default vitePlugin;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
exports.extractTemplate = extractTemplate;
|
|
8
|
+
exports.mergeClassAttributes = mergeClassAttributes;
|
|
9
|
+
exports.parseObjectLiteral = parseObjectLiteral;
|
|
10
|
+
exports.preprocess = preprocess;
|
|
11
|
+
exports.transformTemplate = transformTemplate;
|
|
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 extractTemplate(source) {
|
|
86
|
+
const lowered = source.toLowerCase();
|
|
87
|
+
let openStart = -1;
|
|
88
|
+
let openEnd = -1;
|
|
89
|
+
let i = 0;
|
|
90
|
+
while (i < lowered.length) {
|
|
91
|
+
const candidate = lowered.indexOf("<template", i);
|
|
92
|
+
if (candidate === -1) return null;
|
|
93
|
+
const after = lowered.charAt(candidate + "<template".length);
|
|
94
|
+
if (after === ">" || after === " " || after === " " || after === "\n" || after === "\r") {
|
|
95
|
+
const tagClose = lowered.indexOf(">", candidate);
|
|
96
|
+
if (tagClose === -1) return null;
|
|
97
|
+
openStart = candidate;
|
|
98
|
+
openEnd = tagClose + 1;
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
i = candidate + 1;
|
|
102
|
+
}
|
|
103
|
+
if (openStart === -1) return null;
|
|
104
|
+
const closeStart = lowered.indexOf("</template>", openEnd);
|
|
105
|
+
if (closeStart === -1) return null;
|
|
106
|
+
return {
|
|
107
|
+
content: source.slice(openEnd, closeStart),
|
|
108
|
+
start: openEnd,
|
|
109
|
+
end: closeStart
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function transformTemplate(template, options = {}) {
|
|
113
|
+
let result = "";
|
|
114
|
+
let count = 0;
|
|
115
|
+
let cursor = 0;
|
|
116
|
+
while (cursor < template.length) {
|
|
117
|
+
const attribute = findVueSzAttribute(template, cursor);
|
|
118
|
+
if (!attribute) {
|
|
119
|
+
result += template.slice(cursor);
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
result += template.slice(cursor, attribute.start);
|
|
123
|
+
const match = readQuotedSzAttribute(template, attribute.start, attribute.valueStart);
|
|
124
|
+
if (!match) {
|
|
125
|
+
result += template.slice(attribute.start, attribute.valueStart);
|
|
126
|
+
cursor = attribute.valueStart;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const szObj = parseObjectLiteral(match.objectSource);
|
|
130
|
+
if (!szObj) {
|
|
131
|
+
if (options.debug) {
|
|
132
|
+
console.warn(`[csszyx/vue] Failed to parse sz object: ${match.objectSource}`);
|
|
133
|
+
}
|
|
134
|
+
result += template.slice(attribute.start, match.end);
|
|
135
|
+
} else {
|
|
136
|
+
const className = (0, _compiler.transform)(szObj).className;
|
|
137
|
+
count += 1;
|
|
138
|
+
if (options.debug) {
|
|
139
|
+
console.log(`[csszyx/vue] Transformed: ${match.objectSource} -> "${className}"`);
|
|
140
|
+
}
|
|
141
|
+
result += `class="${className}"`;
|
|
142
|
+
}
|
|
143
|
+
cursor = match.end;
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
code: result,
|
|
147
|
+
transformed: count > 0,
|
|
148
|
+
count
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function findVueSzAttribute(template, from) {
|
|
152
|
+
let szStart = template.indexOf("sz=", from);
|
|
153
|
+
while (szStart !== -1) {
|
|
154
|
+
for (const prefix of ["v-bind:", ":", ""]) {
|
|
155
|
+
const start = szStart - prefix.length;
|
|
156
|
+
if (start < 0 || template.slice(start, szStart) !== prefix) {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const before = start === 0 ? "<" : template.charAt(start - 1);
|
|
160
|
+
if (isAttributeBoundary(before)) {
|
|
161
|
+
return {
|
|
162
|
+
start,
|
|
163
|
+
valueStart: szStart + 3
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
szStart = template.indexOf("sz=", szStart + 3);
|
|
168
|
+
}
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
function readQuotedSzAttribute(template, start, valueStart) {
|
|
172
|
+
const quote = template.charAt(valueStart);
|
|
173
|
+
if (quote !== '"' && quote !== "'") {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
const endQuote = template.indexOf(quote, valueStart + 1);
|
|
177
|
+
if (endQuote === -1) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
const objectSource = template.slice(valueStart + 1, endQuote);
|
|
181
|
+
if (!objectSource.startsWith("{") || !objectSource.endsWith("}")) {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
end: endQuote + 1,
|
|
186
|
+
objectSource
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
function isAttributeBoundary(char) {
|
|
190
|
+
return char === "<" || char === " " || char === " " || char === "\n" || char === "\r";
|
|
191
|
+
}
|
|
192
|
+
function mergeClassAttributes(template) {
|
|
193
|
+
let result = template;
|
|
194
|
+
let i = 0;
|
|
195
|
+
while (i < result.length) {
|
|
196
|
+
const start = result.indexOf("<", i);
|
|
197
|
+
if (start === -1) break;
|
|
198
|
+
const end = result.indexOf(">", start);
|
|
199
|
+
if (end === -1) break;
|
|
200
|
+
const tag = result.slice(start, end + 1);
|
|
201
|
+
i = end + 1;
|
|
202
|
+
if (!/^<[a-z]/i.test(tag)) continue;
|
|
203
|
+
const staticMatch = /\bclass="([^"]*)"/.exec(tag);
|
|
204
|
+
const dynamicMatch = /\b(?::class|v-bind:class)="([^"]*)"/.exec(tag);
|
|
205
|
+
if (!staticMatch || !dynamicMatch) continue;
|
|
206
|
+
const cleaned = tag.replace(/\bclass="[^"]*"/, "").replace(/\b(?::class|v-bind:class)="[^"]*"/, "");
|
|
207
|
+
const insertIdx = cleaned.indexOf(" ") + 1;
|
|
208
|
+
const newTag = `${cleaned.slice(0, insertIdx)}:class="['${staticMatch[1]}', ${dynamicMatch[1]}]" ${cleaned.slice(insertIdx)}`;
|
|
209
|
+
result = result.slice(0, start) + newTag + result.slice(end + 1);
|
|
210
|
+
i = start + newTag.length;
|
|
211
|
+
}
|
|
212
|
+
return result;
|
|
213
|
+
}
|
|
214
|
+
function preprocess(source, options = {}) {
|
|
215
|
+
const templateInfo = extractTemplate(source);
|
|
216
|
+
if (!templateInfo) {
|
|
217
|
+
return {
|
|
218
|
+
code: source,
|
|
219
|
+
transformed: false,
|
|
220
|
+
count: 0
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
const transformResult = transformTemplate(templateInfo.content, options);
|
|
224
|
+
if (!transformResult.transformed) {
|
|
225
|
+
return {
|
|
226
|
+
code: source,
|
|
227
|
+
transformed: false,
|
|
228
|
+
count: 0
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
const mergedContent = mergeClassAttributes(transformResult.code);
|
|
232
|
+
const code = source.slice(0, templateInfo.start) + mergedContent + source.slice(templateInfo.end);
|
|
233
|
+
return {
|
|
234
|
+
code,
|
|
235
|
+
transformed: true,
|
|
236
|
+
count: transformResult.count
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function vitePlugin(options = {}) {
|
|
240
|
+
return {
|
|
241
|
+
name: "csszyx-vue",
|
|
242
|
+
enforce: "pre",
|
|
243
|
+
transform(code, id) {
|
|
244
|
+
if (!id.endsWith(".vue")) {
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
if (!code.includes("sz=")) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
const result = preprocess(code, options);
|
|
251
|
+
if (!result.transformed) {
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
code: result.code,
|
|
256
|
+
map: null
|
|
257
|
+
// TODO: Generate source map
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
module.exports = vitePlugin;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
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 extractTemplate(source) {
|
|
73
|
+
const lowered = source.toLowerCase();
|
|
74
|
+
let openStart = -1;
|
|
75
|
+
let openEnd = -1;
|
|
76
|
+
let i = 0;
|
|
77
|
+
while (i < lowered.length) {
|
|
78
|
+
const candidate = lowered.indexOf("<template", i);
|
|
79
|
+
if (candidate === -1) return null;
|
|
80
|
+
const after = lowered.charAt(candidate + "<template".length);
|
|
81
|
+
if (after === ">" || after === " " || after === " " || after === "\n" || after === "\r") {
|
|
82
|
+
const tagClose = lowered.indexOf(">", candidate);
|
|
83
|
+
if (tagClose === -1) return null;
|
|
84
|
+
openStart = candidate;
|
|
85
|
+
openEnd = tagClose + 1;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
i = candidate + 1;
|
|
89
|
+
}
|
|
90
|
+
if (openStart === -1) return null;
|
|
91
|
+
const closeStart = lowered.indexOf("</template>", openEnd);
|
|
92
|
+
if (closeStart === -1) return null;
|
|
93
|
+
return {
|
|
94
|
+
content: source.slice(openEnd, closeStart),
|
|
95
|
+
start: openEnd,
|
|
96
|
+
end: closeStart
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
export function transformTemplate(template, options = {}) {
|
|
100
|
+
let result = "";
|
|
101
|
+
let count = 0;
|
|
102
|
+
let cursor = 0;
|
|
103
|
+
while (cursor < template.length) {
|
|
104
|
+
const attribute = findVueSzAttribute(template, cursor);
|
|
105
|
+
if (!attribute) {
|
|
106
|
+
result += template.slice(cursor);
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
result += template.slice(cursor, attribute.start);
|
|
110
|
+
const match = readQuotedSzAttribute(template, attribute.start, attribute.valueStart);
|
|
111
|
+
if (!match) {
|
|
112
|
+
result += template.slice(attribute.start, attribute.valueStart);
|
|
113
|
+
cursor = attribute.valueStart;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const szObj = parseObjectLiteral(match.objectSource);
|
|
117
|
+
if (!szObj) {
|
|
118
|
+
if (options.debug) {
|
|
119
|
+
console.warn(`[csszyx/vue] Failed to parse sz object: ${match.objectSource}`);
|
|
120
|
+
}
|
|
121
|
+
result += template.slice(attribute.start, match.end);
|
|
122
|
+
} else {
|
|
123
|
+
const className = transform(szObj).className;
|
|
124
|
+
count += 1;
|
|
125
|
+
if (options.debug) {
|
|
126
|
+
console.log(`[csszyx/vue] Transformed: ${match.objectSource} -> "${className}"`);
|
|
127
|
+
}
|
|
128
|
+
result += `class="${className}"`;
|
|
129
|
+
}
|
|
130
|
+
cursor = match.end;
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
code: result,
|
|
134
|
+
transformed: count > 0,
|
|
135
|
+
count
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function findVueSzAttribute(template, from) {
|
|
139
|
+
let szStart = template.indexOf("sz=", from);
|
|
140
|
+
while (szStart !== -1) {
|
|
141
|
+
for (const prefix of ["v-bind:", ":", ""]) {
|
|
142
|
+
const start = szStart - prefix.length;
|
|
143
|
+
if (start < 0 || template.slice(start, szStart) !== prefix) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const before = start === 0 ? "<" : template.charAt(start - 1);
|
|
147
|
+
if (isAttributeBoundary(before)) {
|
|
148
|
+
return { start, valueStart: szStart + 3 };
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
szStart = template.indexOf("sz=", szStart + 3);
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
function readQuotedSzAttribute(template, start, valueStart) {
|
|
156
|
+
const quote = template.charAt(valueStart);
|
|
157
|
+
if (quote !== '"' && quote !== "'") {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
const endQuote = template.indexOf(quote, valueStart + 1);
|
|
161
|
+
if (endQuote === -1) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
const objectSource = template.slice(valueStart + 1, endQuote);
|
|
165
|
+
if (!objectSource.startsWith("{") || !objectSource.endsWith("}")) {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
return { end: endQuote + 1, objectSource };
|
|
169
|
+
}
|
|
170
|
+
function isAttributeBoundary(char) {
|
|
171
|
+
return char === "<" || char === " " || char === " " || char === "\n" || char === "\r";
|
|
172
|
+
}
|
|
173
|
+
export function mergeClassAttributes(template) {
|
|
174
|
+
let result = template;
|
|
175
|
+
let i = 0;
|
|
176
|
+
while (i < result.length) {
|
|
177
|
+
const start = result.indexOf("<", i);
|
|
178
|
+
if (start === -1) break;
|
|
179
|
+
const end = result.indexOf(">", start);
|
|
180
|
+
if (end === -1) break;
|
|
181
|
+
const tag = result.slice(start, end + 1);
|
|
182
|
+
i = end + 1;
|
|
183
|
+
if (!/^<[a-z]/i.test(tag)) continue;
|
|
184
|
+
const staticMatch = /\bclass="([^"]*)"/.exec(tag);
|
|
185
|
+
const dynamicMatch = /\b(?::class|v-bind:class)="([^"]*)"/.exec(tag);
|
|
186
|
+
if (!staticMatch || !dynamicMatch) continue;
|
|
187
|
+
const cleaned = tag.replace(/\bclass="[^"]*"/, "").replace(/\b(?::class|v-bind:class)="[^"]*"/, "");
|
|
188
|
+
const insertIdx = cleaned.indexOf(" ") + 1;
|
|
189
|
+
const newTag = `${cleaned.slice(0, insertIdx)}:class="['${staticMatch[1]}', ${dynamicMatch[1]}]" ${cleaned.slice(insertIdx)}`;
|
|
190
|
+
result = result.slice(0, start) + newTag + result.slice(end + 1);
|
|
191
|
+
i = start + newTag.length;
|
|
192
|
+
}
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
export function preprocess(source, options = {}) {
|
|
196
|
+
const templateInfo = extractTemplate(source);
|
|
197
|
+
if (!templateInfo) {
|
|
198
|
+
return {
|
|
199
|
+
code: source,
|
|
200
|
+
transformed: false,
|
|
201
|
+
count: 0
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
const transformResult = transformTemplate(templateInfo.content, options);
|
|
205
|
+
if (!transformResult.transformed) {
|
|
206
|
+
return {
|
|
207
|
+
code: source,
|
|
208
|
+
transformed: false,
|
|
209
|
+
count: 0
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
const mergedContent = mergeClassAttributes(transformResult.code);
|
|
213
|
+
const code = source.slice(0, templateInfo.start) + mergedContent + source.slice(templateInfo.end);
|
|
214
|
+
return {
|
|
215
|
+
code,
|
|
216
|
+
transformed: true,
|
|
217
|
+
count: transformResult.count
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
export function vitePlugin(options = {}) {
|
|
221
|
+
return {
|
|
222
|
+
name: "csszyx-vue",
|
|
223
|
+
enforce: "pre",
|
|
224
|
+
transform(code, id) {
|
|
225
|
+
if (!id.endsWith(".vue")) {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
if (!code.includes("sz=")) {
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
const result = preprocess(code, options);
|
|
232
|
+
if (!result.transformed) {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
code: result.code,
|
|
237
|
+
map: null
|
|
238
|
+
// TODO: Generate source map
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
export default vitePlugin;
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@csszyx/vue-adapter",
|
|
3
|
+
"version": "0.9.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Vue SFC preprocessor for csszyx - transform sz props to className",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/nguyennhutien/csszyx/tree/main/packages/vue-adapter#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/nguyennhutien/csszyx.git",
|
|
11
|
+
"directory": "packages/vue-adapter"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/nguyennhutien/csszyx/issues"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "./dist/index.cjs",
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.mts",
|
|
22
|
+
"import": "./dist/index.mjs",
|
|
23
|
+
"require": "./dist/index.cjs"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"oxc-parser": "0.131.0",
|
|
28
|
+
"@csszyx/compiler": "0.9.1"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"vue": "^3.0.0"
|
|
32
|
+
},
|
|
33
|
+
"peerDependenciesMeta": {
|
|
34
|
+
"vue": {
|
|
35
|
+
"optional": true
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "^20.11.0",
|
|
40
|
+
"typescript": "^6.0.3",
|
|
41
|
+
"vite": "^8.0.13",
|
|
42
|
+
"vitest": "^4.1.6",
|
|
43
|
+
"vue": "^3.4.0",
|
|
44
|
+
"unbuild": "^3.6.1"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"dist"
|
|
48
|
+
],
|
|
49
|
+
"keywords": [
|
|
50
|
+
"csszyx",
|
|
51
|
+
"vue",
|
|
52
|
+
"sfc",
|
|
53
|
+
"preprocessor",
|
|
54
|
+
"tailwind"
|
|
55
|
+
],
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "unbuild",
|
|
58
|
+
"dev": "unbuild --stub",
|
|
59
|
+
"test": "vitest run",
|
|
60
|
+
"type-check": "tsc --noEmit"
|
|
61
|
+
}
|
|
62
|
+
}
|