@pandacss/parser 0.22.1 → 0.24.0
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/dist/index.d.mts +31 -40
- package/dist/index.d.ts +31 -40
- package/dist/index.js +286 -234
- package/dist/index.mjs +283 -227
- package/package.json +16 -12
package/dist/index.js
CHANGED
|
@@ -31,8 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
ParserResult: () => ParserResult,
|
|
34
|
-
|
|
35
|
-
createProject: () => createProject
|
|
34
|
+
Project: () => Project
|
|
36
35
|
});
|
|
37
36
|
module.exports = __toCommonJS(src_exports);
|
|
38
37
|
|
|
@@ -40,9 +39,10 @@ module.exports = __toCommonJS(src_exports);
|
|
|
40
39
|
var import_ts_morph2 = require("ts-morph");
|
|
41
40
|
|
|
42
41
|
// src/parser.ts
|
|
42
|
+
var import_ts_path = require("@pandacss/config/ts-path");
|
|
43
43
|
var import_extractor = require("@pandacss/extractor");
|
|
44
44
|
var import_logger = require("@pandacss/logger");
|
|
45
|
-
var
|
|
45
|
+
var import_shared3 = require("@pandacss/shared");
|
|
46
46
|
var import_ts_morph = require("ts-morph");
|
|
47
47
|
var import_ts_pattern = require("ts-pattern");
|
|
48
48
|
|
|
@@ -101,7 +101,14 @@ function getImportDeclarations(file, options) {
|
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
// src/parser-result.ts
|
|
104
|
-
var
|
|
104
|
+
var import_shared2 = require("@pandacss/shared");
|
|
105
|
+
var ParserResult = class {
|
|
106
|
+
constructor(context, encoder) {
|
|
107
|
+
this.context = context;
|
|
108
|
+
this.encoder = encoder ?? context.encoder;
|
|
109
|
+
}
|
|
110
|
+
/** Ordered list of all ResultItem */
|
|
111
|
+
all = [];
|
|
105
112
|
jsx = /* @__PURE__ */ new Set();
|
|
106
113
|
css = /* @__PURE__ */ new Set();
|
|
107
114
|
cva = /* @__PURE__ */ new Set();
|
|
@@ -109,42 +116,95 @@ var ParserResult = class _ParserResult {
|
|
|
109
116
|
recipe = /* @__PURE__ */ new Map();
|
|
110
117
|
pattern = /* @__PURE__ */ new Map();
|
|
111
118
|
filePath;
|
|
119
|
+
encoder;
|
|
120
|
+
append(result) {
|
|
121
|
+
this.all.push(result);
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
112
124
|
set(name, result) {
|
|
113
|
-
this[name].add({ type: "object",
|
|
125
|
+
this[name].add(this.append(Object.assign({ type: "object" }, result)));
|
|
126
|
+
const encoder = this.encoder;
|
|
127
|
+
if (name == "css") {
|
|
128
|
+
result.data.forEach((obj) => encoder.processStyleProps(obj));
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (name === "cva") {
|
|
132
|
+
result.data.forEach((data) => encoder.processAtomicRecipe(data));
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (name === "sva") {
|
|
136
|
+
result.data.forEach((data) => encoder.processAtomicSlotRecipe(data));
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
114
139
|
}
|
|
115
140
|
setCva(result) {
|
|
116
|
-
this.cva.add({ type: "cva",
|
|
141
|
+
this.cva.add(this.append(Object.assign({ type: "cva" }, result)));
|
|
142
|
+
const encoder = this.encoder;
|
|
143
|
+
result.data.forEach((data) => encoder.processAtomicRecipe(data));
|
|
117
144
|
}
|
|
118
145
|
setSva(result) {
|
|
119
|
-
this.sva.add({ type: "sva",
|
|
146
|
+
this.sva.add(this.append(Object.assign({ type: "sva" }, result)));
|
|
147
|
+
const encoder = this.encoder;
|
|
148
|
+
result.data.forEach((data) => encoder.processAtomicSlotRecipe(data));
|
|
120
149
|
}
|
|
121
150
|
setJsx(result) {
|
|
122
|
-
this.jsx.add({ type: "jsx",
|
|
151
|
+
this.jsx.add(this.append(Object.assign({ type: "jsx" }, result)));
|
|
152
|
+
const encoder = this.encoder;
|
|
153
|
+
result.data.forEach((obj) => encoder.processStyleProps(obj));
|
|
123
154
|
}
|
|
124
155
|
setPattern(name, result) {
|
|
125
|
-
|
|
126
|
-
this.
|
|
156
|
+
const set = (0, import_shared2.getOrCreateSet)(this.pattern, name);
|
|
157
|
+
set.add(this.append(Object.assign({ type: "pattern", name }, result)));
|
|
158
|
+
const encoder = this.encoder;
|
|
159
|
+
result.data.forEach(
|
|
160
|
+
(obj) => encoder.processPattern(name, obj, result.type ?? "pattern", result.name)
|
|
161
|
+
);
|
|
127
162
|
}
|
|
128
|
-
setRecipe(
|
|
129
|
-
|
|
130
|
-
this.
|
|
163
|
+
setRecipe(recipeName, result) {
|
|
164
|
+
const set = (0, import_shared2.getOrCreateSet)(this.recipe, recipeName);
|
|
165
|
+
set.add(this.append(Object.assign({ type: "recipe" }, result)));
|
|
166
|
+
const encoder = this.encoder;
|
|
167
|
+
const recipes = this.context.recipes;
|
|
168
|
+
const recipeConfig = recipes.getConfig(recipeName);
|
|
169
|
+
if (!recipeConfig)
|
|
170
|
+
return;
|
|
171
|
+
const recipe = result;
|
|
172
|
+
if (result.type) {
|
|
173
|
+
recipe.data.forEach((data) => {
|
|
174
|
+
const [recipeProps, styleProps] = recipes.splitProps(recipeName, data);
|
|
175
|
+
encoder.processStyleProps(styleProps);
|
|
176
|
+
encoder.processRecipe(recipeName, recipeProps);
|
|
177
|
+
});
|
|
178
|
+
} else {
|
|
179
|
+
recipe.data.forEach((data) => {
|
|
180
|
+
encoder.processRecipe(recipeName, data);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
131
183
|
}
|
|
132
184
|
isEmpty() {
|
|
133
|
-
return this.
|
|
185
|
+
return this.all.length === 0;
|
|
134
186
|
}
|
|
135
187
|
setFilePath(filePath) {
|
|
136
188
|
this.filePath = filePath;
|
|
137
189
|
return this;
|
|
138
190
|
}
|
|
191
|
+
merge(result) {
|
|
192
|
+
result.css.forEach((item) => this.css.add(this.append(item)));
|
|
193
|
+
result.cva.forEach((item) => this.cva.add(this.append(item)));
|
|
194
|
+
result.sva.forEach((item) => this.sva.add(this.append(item)));
|
|
195
|
+
result.jsx.forEach((item) => this.jsx.add(this.append(item)));
|
|
196
|
+
result.recipe.forEach((items, name) => {
|
|
197
|
+
const set = (0, import_shared2.getOrCreateSet)(this.recipe, name);
|
|
198
|
+
items.forEach((item) => set.add(this.append(item)));
|
|
199
|
+
});
|
|
200
|
+
result.pattern.forEach((items, name) => {
|
|
201
|
+
const set = (0, import_shared2.getOrCreateSet)(this.pattern, name);
|
|
202
|
+
items.forEach((item) => set.add(this.append(item)));
|
|
203
|
+
});
|
|
204
|
+
return this;
|
|
205
|
+
}
|
|
139
206
|
toArray() {
|
|
140
|
-
|
|
141
|
-
this.css.forEach((item) => result.push(item));
|
|
142
|
-
this.cva.forEach((item) => result.push(item));
|
|
143
|
-
this.sva.forEach((item) => result.push(item));
|
|
144
|
-
this.jsx.forEach((item) => result.push(item));
|
|
145
|
-
this.recipe.forEach((items) => items.forEach((item) => result.push(item)));
|
|
146
|
-
this.pattern.forEach((items) => items.forEach((item) => result.push(item)));
|
|
147
|
-
return result;
|
|
207
|
+
return this.all;
|
|
148
208
|
}
|
|
149
209
|
toJSON() {
|
|
150
210
|
return {
|
|
@@ -156,41 +216,14 @@ var ParserResult = class _ParserResult {
|
|
|
156
216
|
pattern: Object.fromEntries(Array.from(this.pattern.entries()).map(([key, value]) => [key, Array.from(value)]))
|
|
157
217
|
};
|
|
158
218
|
}
|
|
159
|
-
merge(result) {
|
|
160
|
-
result.css.forEach((item) => this.css.add(item));
|
|
161
|
-
result.cva.forEach((item) => this.cva.add(item));
|
|
162
|
-
result.sva.forEach((item) => this.sva.add(item));
|
|
163
|
-
result.jsx.forEach((item) => this.jsx.add(item));
|
|
164
|
-
result.recipe.forEach((items, name) => {
|
|
165
|
-
this.recipe.get(name) ?? this.recipe.set(name, /* @__PURE__ */ new Set());
|
|
166
|
-
items.forEach((item) => this.recipe.get(name)?.add(item));
|
|
167
|
-
});
|
|
168
|
-
result.pattern.forEach((items, name) => {
|
|
169
|
-
this.pattern.get(name) ?? this.pattern.set(name, /* @__PURE__ */ new Set());
|
|
170
|
-
items.forEach((item) => this.pattern.get(name)?.add(item));
|
|
171
|
-
});
|
|
172
|
-
return this;
|
|
173
|
-
}
|
|
174
|
-
static fromJSON(json) {
|
|
175
|
-
const data = JSON.parse(json);
|
|
176
|
-
const result = new _ParserResult();
|
|
177
|
-
result.css = new Set(data.css);
|
|
178
|
-
result.cva = new Set(data.cva);
|
|
179
|
-
result.sva = new Set(data.sva);
|
|
180
|
-
result.jsx = new Set(data.jsx);
|
|
181
|
-
result.recipe = new Map(Object.entries(data.recipe));
|
|
182
|
-
result.pattern = new Map(Object.entries(data.pattern));
|
|
183
|
-
return result;
|
|
184
|
-
}
|
|
185
219
|
};
|
|
186
|
-
var createParserResult = () => new ParserResult();
|
|
187
220
|
|
|
188
221
|
// src/parser.ts
|
|
189
|
-
var import_ts_path = require("@pandacss/config/ts-path");
|
|
190
222
|
var isNodeRecipe = (node) => node.type === "recipe";
|
|
191
223
|
var isNodePattern = (node) => node.type === "pattern";
|
|
192
224
|
var cvaProps = ["compoundVariants", "defaultVariants", "variants", "base"];
|
|
193
225
|
var isCva = (map) => cvaProps.some((prop) => map.has(prop));
|
|
226
|
+
var noop = (..._args) => void 0;
|
|
194
227
|
function createImportMatcher(mod, values) {
|
|
195
228
|
const regex = values ? new RegExp(`^(${values.join("|")})$`) : /.*/;
|
|
196
229
|
return {
|
|
@@ -212,18 +245,22 @@ var fallback = (box2) => ({
|
|
|
212
245
|
var defaultEnv = { preset: "ECMA" };
|
|
213
246
|
var identityFn = (styles) => styles;
|
|
214
247
|
var evaluateOptions = { environment: defaultEnv };
|
|
215
|
-
function createParser(
|
|
216
|
-
const { jsx,
|
|
217
|
-
const
|
|
248
|
+
function createParser(context) {
|
|
249
|
+
const { jsx, isValidProperty, tsOptions, join, syntax } = context;
|
|
250
|
+
const getRecipesByJsxName = context.recipes.filter;
|
|
251
|
+
const getPatternsByJsxName = context.patterns.filter;
|
|
252
|
+
const [recipeKeys, patternKeys] = [context.recipes.keys, context.patterns.keys];
|
|
253
|
+
const importMap = Object.fromEntries(Object.entries(context.importMap).map(([key, value]) => [key, join(...value)]));
|
|
254
|
+
const isJsxEnabled = jsx.framework;
|
|
218
255
|
const importRegex = [
|
|
219
256
|
createImportMatcher(importMap.css, ["css", "cva", "sva"]),
|
|
220
257
|
createImportMatcher(importMap.recipe),
|
|
221
258
|
createImportMatcher(importMap.pattern)
|
|
222
259
|
];
|
|
223
|
-
if (
|
|
260
|
+
if (isJsxEnabled) {
|
|
224
261
|
importRegex.push(createImportMatcher(importMap.jsx, [jsx.factory, ...jsx.nodes.map((node) => node.jsxName)]));
|
|
225
262
|
}
|
|
226
|
-
return function parse2(sourceFile) {
|
|
263
|
+
return function parse2(sourceFile, encoder) {
|
|
227
264
|
if (!sourceFile)
|
|
228
265
|
return;
|
|
229
266
|
const filePath = sourceFile.getFilePath();
|
|
@@ -248,36 +285,37 @@ function createParser(options) {
|
|
|
248
285
|
return found;
|
|
249
286
|
}
|
|
250
287
|
});
|
|
251
|
-
const
|
|
288
|
+
const parserResult = new ParserResult(context, encoder);
|
|
252
289
|
import_logger.logger.debug(
|
|
253
290
|
"ast:import",
|
|
254
291
|
imports.value.length ? `Found import { ${imports} } in ${filePath}` : `No import found in ${filePath}`
|
|
255
292
|
);
|
|
293
|
+
if (!imports.value.length && !isJsxEnabled) {
|
|
294
|
+
return parserResult;
|
|
295
|
+
}
|
|
256
296
|
const [css] = importRegex;
|
|
257
|
-
const jsxFactoryAlias =
|
|
258
|
-
const isValidPattern = imports.createMatch(importMap.pattern,
|
|
259
|
-
const isValidRecipe = imports.createMatch(importMap.recipe,
|
|
260
|
-
const isValidStyleFn = (name) => name === jsx
|
|
261
|
-
const isFactory = (name) => Boolean(
|
|
297
|
+
const jsxFactoryAlias = isJsxEnabled ? imports.getAlias(jsx.factory) : "styled";
|
|
298
|
+
const isValidPattern = imports.createMatch(importMap.pattern, patternKeys);
|
|
299
|
+
const isValidRecipe = imports.createMatch(importMap.recipe, recipeKeys);
|
|
300
|
+
const isValidStyleFn = (name) => name === jsx.factory;
|
|
301
|
+
const isFactory = (name) => Boolean(isJsxEnabled && name.startsWith(jsxFactoryAlias));
|
|
262
302
|
const isRawFn = (fullName) => {
|
|
263
303
|
const name = fullName.split(".raw")[0] ?? "";
|
|
264
304
|
return name === "css" || isValidPattern(name) || isValidRecipe(name);
|
|
265
305
|
};
|
|
266
|
-
const
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
{ string: /* @__PURE__ */ new Set(), regex: [] }
|
|
280
|
-
);
|
|
306
|
+
const patternPropertiesByJsxName = /* @__PURE__ */ new Map();
|
|
307
|
+
const initialPatterns = { string: /* @__PURE__ */ new Set(), regex: [] };
|
|
308
|
+
const patternJsxLists = isJsxEnabled ? (jsx.nodes ?? []).filter(isNodePattern).reduce((acc, pattern) => {
|
|
309
|
+
patternPropertiesByJsxName.set(pattern.jsxName, new Set(pattern.props ?? []));
|
|
310
|
+
pattern.jsx.forEach((jsx2) => {
|
|
311
|
+
if (typeof jsx2 === "string") {
|
|
312
|
+
acc.string.add(jsx2);
|
|
313
|
+
} else if (jsx2) {
|
|
314
|
+
acc.regex.push(jsx2);
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
return acc;
|
|
318
|
+
}, initialPatterns) : initialPatterns;
|
|
281
319
|
const recipes = /* @__PURE__ */ new Set();
|
|
282
320
|
const patterns = /* @__PURE__ */ new Set();
|
|
283
321
|
imports.value.forEach((importDeclaration) => {
|
|
@@ -293,25 +331,23 @@ function createParser(options) {
|
|
|
293
331
|
const components = /* @__PURE__ */ new Map();
|
|
294
332
|
const propertiesMap = /* @__PURE__ */ new Map();
|
|
295
333
|
const recipePropertiesByJsxName = /* @__PURE__ */ new Map();
|
|
296
|
-
const
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
{ string: /* @__PURE__ */ new Set(), regex: [] }
|
|
309
|
-
);
|
|
334
|
+
const initialRecipes = { string: /* @__PURE__ */ new Set(), regex: [] };
|
|
335
|
+
const recipeJsxLists = isJsxEnabled ? (jsx.nodes ?? []).filter(isNodeRecipe).reduce((acc, recipe) => {
|
|
336
|
+
recipePropertiesByJsxName.set(recipe.jsxName, new Set(recipe.props ?? []));
|
|
337
|
+
recipe.jsx.forEach((jsx2) => {
|
|
338
|
+
if (typeof jsx2 === "string") {
|
|
339
|
+
acc.string.add(jsx2);
|
|
340
|
+
} else {
|
|
341
|
+
acc.regex.push(jsx2);
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
return acc;
|
|
345
|
+
}, initialRecipes) : initialRecipes;
|
|
310
346
|
const cvaAlias = imports.getAlias("cva");
|
|
311
347
|
const cssAlias = imports.getAlias("css");
|
|
312
348
|
const svaAlias = imports.getAlias("sva");
|
|
313
|
-
if (
|
|
314
|
-
|
|
349
|
+
if (context.jsx) {
|
|
350
|
+
context.jsx.nodes.forEach((node) => {
|
|
315
351
|
const alias = imports.getAlias(node.jsxName);
|
|
316
352
|
node.props?.forEach((prop) => propertiesMap.set(prop, true));
|
|
317
353
|
functions.set(node.baseName, propertiesMap);
|
|
@@ -319,37 +355,40 @@ function createParser(options) {
|
|
|
319
355
|
components.set(alias, propertiesMap);
|
|
320
356
|
});
|
|
321
357
|
}
|
|
322
|
-
const isJsxTagRecipe = (0,
|
|
358
|
+
const isJsxTagRecipe = isJsxEnabled ? (0, import_shared3.memo)(
|
|
323
359
|
(tagName) => recipeJsxLists.string.has(tagName) || recipeJsxLists.regex.some((regex) => regex.test(tagName))
|
|
324
|
-
);
|
|
325
|
-
const isJsxTagPattern = (0,
|
|
360
|
+
) : noop;
|
|
361
|
+
const isJsxTagPattern = isJsxEnabled ? (0, import_shared3.memo)(
|
|
326
362
|
(tagName) => patternJsxLists.string.has(tagName) || patternJsxLists.regex.some((regex) => regex.test(tagName))
|
|
327
|
-
);
|
|
328
|
-
const matchTag = (0,
|
|
363
|
+
) : noop;
|
|
364
|
+
const matchTag = isJsxEnabled ? (0, import_shared3.memo)((tagName) => {
|
|
329
365
|
if (!tagName)
|
|
330
366
|
return false;
|
|
331
367
|
return components.has(tagName) || isUpperCase(tagName) || isFactory(tagName) || isJsxTagRecipe(tagName) || isJsxTagPattern(tagName);
|
|
332
|
-
});
|
|
333
|
-
const isRecipeOrPatternProp = (0,
|
|
334
|
-
if (isJsxTagRecipe(tagName)) {
|
|
368
|
+
}) : noop;
|
|
369
|
+
const isRecipeOrPatternProp = (0, import_shared3.memo)((tagName, propName) => {
|
|
370
|
+
if (isJsxEnabled && isJsxTagRecipe(tagName)) {
|
|
335
371
|
const recipeList = getRecipesByJsxName(tagName);
|
|
336
372
|
return recipeList.some((recipe) => recipePropertiesByJsxName.get(recipe.jsxName)?.has(propName));
|
|
337
373
|
}
|
|
338
|
-
if (isJsxTagPattern(tagName)) {
|
|
374
|
+
if (isJsxEnabled && isJsxTagPattern(tagName)) {
|
|
339
375
|
const patternList = getPatternsByJsxName(tagName);
|
|
340
|
-
return patternList.some((pattern) =>
|
|
376
|
+
return patternList.some((pattern) => patternPropertiesByJsxName.get(pattern.jsxName)?.has(propName));
|
|
341
377
|
}
|
|
342
378
|
return false;
|
|
343
379
|
});
|
|
344
|
-
const matchTagProp = (0, import_ts_pattern.match)(jsx
|
|
380
|
+
const matchTagProp = isJsxEnabled ? (0, import_ts_pattern.match)(jsx.styleProps).with(
|
|
345
381
|
"all",
|
|
346
|
-
() => (0,
|
|
347
|
-
return Boolean(components.get(tagName)?.has(propName)) ||
|
|
382
|
+
() => (0, import_shared3.memo)((tagName, propName) => {
|
|
383
|
+
return Boolean(components.get(tagName)?.has(propName)) || isValidProperty(propName) || propertiesMap.has(propName) || isRecipeOrPatternProp(tagName, propName);
|
|
348
384
|
})
|
|
349
|
-
).with(
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
385
|
+
).with(
|
|
386
|
+
"minimal",
|
|
387
|
+
() => (0, import_shared3.memo)((tagName, propName) => {
|
|
388
|
+
return propName === "css" || isRecipeOrPatternProp(tagName, propName);
|
|
389
|
+
})
|
|
390
|
+
).with("none", () => (0, import_shared3.memo)((tagName, propName) => isRecipeOrPatternProp(tagName, propName))).exhaustive() : noop;
|
|
391
|
+
const matchFn = (0, import_shared3.memo)((fnName) => {
|
|
353
392
|
if (recipes.has(fnName) || patterns.has(fnName))
|
|
354
393
|
return true;
|
|
355
394
|
if (fnName === cvaAlias || fnName === cssAlias || fnName === svaAlias || isRawFn(fnName) || isFactory(fnName))
|
|
@@ -359,10 +398,10 @@ function createParser(options) {
|
|
|
359
398
|
const measure = import_logger.logger.time.debug(`Tokens extracted from ${filePath}`);
|
|
360
399
|
const extractResultByName = (0, import_extractor.extract)({
|
|
361
400
|
ast: sourceFile,
|
|
362
|
-
components: {
|
|
363
|
-
matchTag: (prop) => matchTag(prop.tagName),
|
|
364
|
-
matchProp: (prop) => matchTagProp(prop.tagName, prop.propName)
|
|
365
|
-
},
|
|
401
|
+
components: isJsxEnabled ? {
|
|
402
|
+
matchTag: (prop) => !!matchTag(prop.tagName),
|
|
403
|
+
matchProp: (prop) => !!matchTagProp(prop.tagName, prop.propName)
|
|
404
|
+
} : void 0,
|
|
366
405
|
functions: {
|
|
367
406
|
matchFn: (prop) => matchFn(prop.fnName),
|
|
368
407
|
matchProp: () => true,
|
|
@@ -372,9 +411,7 @@ function createParser(options) {
|
|
|
372
411
|
return true;
|
|
373
412
|
}
|
|
374
413
|
},
|
|
375
|
-
taggedTemplates: {
|
|
376
|
-
matchTaggedTemplate: (tag) => matchFn(tag.fnName)
|
|
377
|
-
},
|
|
414
|
+
taggedTemplates: syntax === "template-literal" ? { matchTaggedTemplate: (tag) => matchFn(tag.fnName) } : void 0,
|
|
378
415
|
getEvaluateOptions: (node) => {
|
|
379
416
|
if (!import_ts_morph.Node.isCallExpression(node))
|
|
380
417
|
return evaluateOptions;
|
|
@@ -394,16 +431,17 @@ function createParser(options) {
|
|
|
394
431
|
});
|
|
395
432
|
measure();
|
|
396
433
|
extractResultByName.forEach((result, alias) => {
|
|
397
|
-
let name =
|
|
434
|
+
let name = alias;
|
|
398
435
|
if (isRawFn(name))
|
|
399
436
|
name = name.replace(".raw", "");
|
|
437
|
+
name = imports.getName(name);
|
|
400
438
|
import_logger.logger.debug(`ast:${name}`, name !== alias ? { kind: result.kind, alias } : { kind: result.kind });
|
|
401
439
|
if (result.kind === "function") {
|
|
402
440
|
(0, import_ts_pattern.match)(name).when(css.match, (name2) => {
|
|
403
441
|
result.queryList.forEach((query) => {
|
|
404
442
|
if (query.kind === "call-expression") {
|
|
405
443
|
if (query.box.value.length > 1) {
|
|
406
|
-
|
|
444
|
+
parserResult.set(name2, {
|
|
407
445
|
name: name2,
|
|
408
446
|
box: query.box,
|
|
409
447
|
data: query.box.value.reduce(
|
|
@@ -412,15 +450,15 @@ function createParser(options) {
|
|
|
412
450
|
)
|
|
413
451
|
});
|
|
414
452
|
} else {
|
|
415
|
-
|
|
453
|
+
parserResult.set(name2, {
|
|
416
454
|
name: name2,
|
|
417
455
|
box: query.box.value[0] ?? fallback(query.box),
|
|
418
456
|
data: combineResult((0, import_extractor.unbox)(query.box.value[0]))
|
|
419
457
|
});
|
|
420
458
|
}
|
|
421
459
|
} else if (query.kind === "tagged-template") {
|
|
422
|
-
const obj = (0,
|
|
423
|
-
|
|
460
|
+
const obj = (0, import_shared3.astish)(query.box.value);
|
|
461
|
+
parserResult.set(name2, {
|
|
424
462
|
name: name2,
|
|
425
463
|
box: query.box ?? fallback(query.box),
|
|
426
464
|
data: [obj]
|
|
@@ -430,7 +468,7 @@ function createParser(options) {
|
|
|
430
468
|
}).when(isValidPattern, (name2) => {
|
|
431
469
|
result.queryList.forEach((query) => {
|
|
432
470
|
if (query.kind === "call-expression") {
|
|
433
|
-
|
|
471
|
+
parserResult.setPattern(name2, {
|
|
434
472
|
name: name2,
|
|
435
473
|
box: query.box.value[0] ?? fallback(query.box),
|
|
436
474
|
data: combineResult((0, import_extractor.unbox)(query.box.value[0]))
|
|
@@ -440,7 +478,7 @@ function createParser(options) {
|
|
|
440
478
|
}).when(isValidRecipe, (name2) => {
|
|
441
479
|
result.queryList.forEach((query) => {
|
|
442
480
|
if (query.kind === "call-expression") {
|
|
443
|
-
|
|
481
|
+
parserResult.setRecipe(name2, {
|
|
444
482
|
name: name2,
|
|
445
483
|
box: query.box.value[0] ?? fallback(query.box),
|
|
446
484
|
data: combineResult((0, import_extractor.unbox)(query.box.value[0]))
|
|
@@ -454,27 +492,27 @@ function createParser(options) {
|
|
|
454
492
|
const boxNode = import_extractor.box.isMap(map) ? map : fallback(query.box);
|
|
455
493
|
const result2 = { name, box: boxNode, data: combineResult((0, import_extractor.unbox)(boxNode)) };
|
|
456
494
|
if (import_extractor.box.isMap(map) && isCva(map.value)) {
|
|
457
|
-
|
|
495
|
+
parserResult.setCva(result2);
|
|
458
496
|
} else {
|
|
459
|
-
|
|
497
|
+
parserResult.set("css", result2);
|
|
460
498
|
}
|
|
461
|
-
const
|
|
462
|
-
if (import_extractor.box.isUnresolvable(map) &&
|
|
499
|
+
const options = query.box.value[2];
|
|
500
|
+
if (import_extractor.box.isUnresolvable(map) && options && import_extractor.box.isMap(options) && options.value.has("defaultProps")) {
|
|
463
501
|
const maybeIdentifier = map.getNode();
|
|
464
502
|
if (import_ts_morph.Node.isIdentifier(maybeIdentifier)) {
|
|
465
503
|
const name2 = maybeIdentifier.getText();
|
|
466
504
|
const recipeName = imports.getName(name2);
|
|
467
|
-
|
|
505
|
+
parserResult.setRecipe(recipeName, {
|
|
468
506
|
type: "jsx-recipe",
|
|
469
507
|
name: recipeName,
|
|
470
|
-
box:
|
|
471
|
-
data: combineResult((0, import_extractor.unbox)(
|
|
508
|
+
box: options,
|
|
509
|
+
data: combineResult((0, import_extractor.unbox)(options.value.get("defaultProps")))
|
|
472
510
|
});
|
|
473
511
|
}
|
|
474
512
|
}
|
|
475
513
|
} else if (query.kind === "tagged-template") {
|
|
476
|
-
const obj = (0,
|
|
477
|
-
|
|
514
|
+
const obj = (0, import_shared3.astish)(query.box.value);
|
|
515
|
+
parserResult.set("css", {
|
|
478
516
|
name,
|
|
479
517
|
box: query.box ?? fallback(query.box),
|
|
480
518
|
data: [obj]
|
|
@@ -488,13 +526,13 @@ function createParser(options) {
|
|
|
488
526
|
const boxNode = import_extractor.box.isMap(map) ? map : fallback(query.box);
|
|
489
527
|
const result2 = { name: name2, box: boxNode, data: combineResult((0, import_extractor.unbox)(boxNode)) };
|
|
490
528
|
if (import_extractor.box.isMap(map) && isCva(map.value)) {
|
|
491
|
-
|
|
529
|
+
parserResult.setCva(result2);
|
|
492
530
|
} else {
|
|
493
|
-
|
|
531
|
+
parserResult.set("css", result2);
|
|
494
532
|
}
|
|
495
533
|
} else if (query.kind === "tagged-template") {
|
|
496
|
-
const obj = (0,
|
|
497
|
-
|
|
534
|
+
const obj = (0, import_shared3.astish)(query.box.value);
|
|
535
|
+
parserResult.set("css", {
|
|
498
536
|
name: name2,
|
|
499
537
|
box: query.box ?? fallback(query.box),
|
|
500
538
|
data: [obj]
|
|
@@ -503,32 +541,57 @@ function createParser(options) {
|
|
|
503
541
|
});
|
|
504
542
|
}).otherwise(() => {
|
|
505
543
|
});
|
|
506
|
-
} else if (result.kind === "component") {
|
|
544
|
+
} else if (isJsxEnabled && result.kind === "component") {
|
|
507
545
|
result.queryList.forEach((query) => {
|
|
508
546
|
const data = combineResult((0, import_extractor.unbox)(query.box));
|
|
509
547
|
(0, import_ts_pattern.match)(name).when(isFactory, (jsxName) => {
|
|
510
|
-
|
|
548
|
+
parserResult.setJsx({ name: jsxName, box: query.box, type: "jsx-factory", data });
|
|
511
549
|
}).when(isJsxTagPattern, (jsxName) => {
|
|
512
|
-
|
|
550
|
+
parserResult.setPattern(jsxName, { type: "jsx-pattern", name: jsxName, box: query.box, data });
|
|
513
551
|
}).when(isJsxTagRecipe, (jsxName) => {
|
|
514
552
|
const recipeList = getRecipesByJsxName(jsxName);
|
|
515
553
|
recipeList.map((recipe) => {
|
|
516
|
-
|
|
554
|
+
parserResult.setRecipe(recipe.baseName, { type: "jsx-recipe", name: jsxName, box: query.box, data });
|
|
517
555
|
});
|
|
518
556
|
}).otherwise(() => {
|
|
519
|
-
|
|
557
|
+
parserResult.setJsx({ name, box: query.box, type: "jsx", data });
|
|
520
558
|
});
|
|
521
559
|
});
|
|
522
560
|
}
|
|
523
561
|
});
|
|
524
|
-
return
|
|
562
|
+
return parserResult;
|
|
525
563
|
};
|
|
526
564
|
}
|
|
527
565
|
var isUpperCase = (value) => value[0] === value[0]?.toUpperCase();
|
|
528
566
|
|
|
567
|
+
// src/svelte-to-tsx.ts
|
|
568
|
+
var import_magic_string = __toESM(require("magic-string"));
|
|
569
|
+
var regex_style_tags = /<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi;
|
|
570
|
+
var regex_script_tags = /<!--[^]*?-->|<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi;
|
|
571
|
+
var svelteToTsx = (code) => {
|
|
572
|
+
try {
|
|
573
|
+
const scripts = [];
|
|
574
|
+
const original = new import_magic_string.default(code);
|
|
575
|
+
let match2;
|
|
576
|
+
while ((match2 = regex_script_tags.exec(code)) != null) {
|
|
577
|
+
const [fullMatch, _attributesStr, scriptContent] = match2;
|
|
578
|
+
if (scriptContent) {
|
|
579
|
+
scripts.push(scriptContent);
|
|
580
|
+
original.remove(match2.index, match2.index + fullMatch.length);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
const templateContent = original.toString().trimStart().replace(regex_style_tags, "").replace(regex_style_tags, "");
|
|
584
|
+
const transformed = `${scripts.join("")}
|
|
585
|
+
const render = <div>${templateContent}</div>`;
|
|
586
|
+
return transformed.toString().trim();
|
|
587
|
+
} catch (err) {
|
|
588
|
+
return "";
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
|
|
529
592
|
// src/vue-to-tsx.ts
|
|
530
593
|
var import_compiler_sfc = require("@vue/compiler-sfc");
|
|
531
|
-
var
|
|
594
|
+
var import_magic_string2 = __toESM(require("magic-string"));
|
|
532
595
|
var NodeTypes = {
|
|
533
596
|
ROOT: 0,
|
|
534
597
|
ELEMENT: 1,
|
|
@@ -561,7 +624,7 @@ var NodeTypes = {
|
|
|
561
624
|
var vueToTsx = (code) => {
|
|
562
625
|
try {
|
|
563
626
|
const parsed = (0, import_compiler_sfc.parse)(code);
|
|
564
|
-
const fileStr = new
|
|
627
|
+
const fileStr = new import_magic_string2.default(`<template>${parsed.descriptor.template?.content}</template>` ?? "");
|
|
565
628
|
const rewriteProp = (prop) => {
|
|
566
629
|
if (prop.type === NodeTypes.DIRECTIVE && prop.exp?.type === NodeTypes.SIMPLE_EXPRESSION && prop.arg?.type === NodeTypes.SIMPLE_EXPRESSION) {
|
|
567
630
|
fileStr.replace(prop.loc.source, `${prop.arg.content}={${prop.exp.content}}`);
|
|
@@ -578,7 +641,7 @@ var vueToTsx = (code) => {
|
|
|
578
641
|
}
|
|
579
642
|
}
|
|
580
643
|
const scriptContent = (parsed.descriptor.scriptSetup ?? parsed.descriptor.script)?.content + "\n";
|
|
581
|
-
const transformed = new
|
|
644
|
+
const transformed = new import_magic_string2.default(`${scriptContent}
|
|
582
645
|
const render = ${fileStr.toString()}`);
|
|
583
646
|
return transformed.toString();
|
|
584
647
|
} catch (err) {
|
|
@@ -586,31 +649,6 @@ const render = ${fileStr.toString()}`);
|
|
|
586
649
|
}
|
|
587
650
|
};
|
|
588
651
|
|
|
589
|
-
// src/svelte-to-tsx.ts
|
|
590
|
-
var import_magic_string2 = __toESM(require("magic-string"));
|
|
591
|
-
var regex_style_tags = /<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi;
|
|
592
|
-
var regex_script_tags = /<!--[^]*?-->|<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi;
|
|
593
|
-
var svelteToTsx = (code) => {
|
|
594
|
-
try {
|
|
595
|
-
const scripts = [];
|
|
596
|
-
const original = new import_magic_string2.default(code);
|
|
597
|
-
let match2;
|
|
598
|
-
while ((match2 = regex_script_tags.exec(code)) != null) {
|
|
599
|
-
const [fullMatch, _attributesStr, scriptContent] = match2;
|
|
600
|
-
if (scriptContent) {
|
|
601
|
-
scripts.push(scriptContent);
|
|
602
|
-
original.remove(match2.index, match2.index + fullMatch.length);
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
const templateContent = original.toString().trimStart().replace(regex_style_tags, "").replace(regex_style_tags, "");
|
|
606
|
-
const transformed = `${scripts.join("")}
|
|
607
|
-
const render = <div>${templateContent}</div>`;
|
|
608
|
-
return transformed.toString().trim();
|
|
609
|
-
} catch (err) {
|
|
610
|
-
return "";
|
|
611
|
-
}
|
|
612
|
-
};
|
|
613
|
-
|
|
614
652
|
// src/project.ts
|
|
615
653
|
var createTsProject = (options) => new import_ts_morph2.Project({
|
|
616
654
|
skipAddingFilesFromTsConfig: true,
|
|
@@ -624,87 +662,101 @@ var createTsProject = (options) => new import_ts_morph2.Project({
|
|
|
624
662
|
...options.compilerOptions
|
|
625
663
|
}
|
|
626
664
|
});
|
|
627
|
-
var
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
665
|
+
var Project = class {
|
|
666
|
+
constructor(options) {
|
|
667
|
+
this.options = options;
|
|
668
|
+
const { parserOptions } = options;
|
|
669
|
+
this.project = createTsProject(options);
|
|
670
|
+
this.parser = createParser(parserOptions);
|
|
671
|
+
this.createSourceFiles();
|
|
672
|
+
}
|
|
673
|
+
project;
|
|
674
|
+
parser;
|
|
675
|
+
get files() {
|
|
676
|
+
return this.options.getFiles();
|
|
677
|
+
}
|
|
678
|
+
getSourceFile = (filePath) => {
|
|
679
|
+
return this.project.getSourceFile(filePath);
|
|
641
680
|
};
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
681
|
+
createSourceFile = (filePath) => {
|
|
682
|
+
const { readFile } = this.options;
|
|
683
|
+
return this.project.createSourceFile(filePath, readFile(filePath), {
|
|
684
|
+
overwrite: true,
|
|
685
|
+
scriptKind: import_ts_morph2.ScriptKind.TSX
|
|
686
|
+
});
|
|
687
|
+
};
|
|
688
|
+
createSourceFiles = () => {
|
|
689
|
+
const files = this.getFiles();
|
|
690
|
+
for (const file of files) {
|
|
691
|
+
this.createSourceFile(file);
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
addSourceFile = (filePath, content) => {
|
|
695
|
+
return this.project.createSourceFile(filePath, content, {
|
|
696
|
+
overwrite: true,
|
|
697
|
+
scriptKind: import_ts_morph2.ScriptKind.TSX
|
|
698
|
+
});
|
|
699
|
+
};
|
|
700
|
+
removeSourceFile = (filePath) => {
|
|
701
|
+
const sourceFile = this.project.getSourceFile(filePath);
|
|
702
|
+
if (sourceFile) {
|
|
703
|
+
return this.project.removeSourceFile(sourceFile);
|
|
704
|
+
}
|
|
705
|
+
return false;
|
|
706
|
+
};
|
|
707
|
+
reloadSourceFile = (filePath) => {
|
|
708
|
+
return this.getSourceFile(filePath)?.refreshFromFileSystemSync();
|
|
709
|
+
};
|
|
710
|
+
reloadSourceFiles = () => {
|
|
711
|
+
const files = this.getFiles();
|
|
712
|
+
for (const file of files) {
|
|
713
|
+
const source = this.getSourceFile(file);
|
|
714
|
+
source?.refreshFromFileSystemSync() ?? this.project.addSourceFileAtPath(file);
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
get readFile() {
|
|
718
|
+
return this.options.readFile;
|
|
719
|
+
}
|
|
720
|
+
get getFiles() {
|
|
721
|
+
return this.options.getFiles;
|
|
722
|
+
}
|
|
723
|
+
parseJson = (filePath) => {
|
|
724
|
+
const { readFile, parserOptions } = this.options;
|
|
725
|
+
const content = readFile(filePath);
|
|
726
|
+
parserOptions.encoder.fromJSON(content);
|
|
727
|
+
const result = new ParserResult(parserOptions);
|
|
728
|
+
return result.setFilePath(filePath);
|
|
729
|
+
};
|
|
730
|
+
parseSourceFile = (filePath, encoder) => {
|
|
731
|
+
const { hooks } = this.options;
|
|
651
732
|
if (filePath.endsWith(".json")) {
|
|
652
|
-
|
|
653
|
-
hooks.callHook("parser:before", filePath, content2);
|
|
654
|
-
const result2 = ParserResult.fromJSON(content2).setFilePath(filePath);
|
|
655
|
-
hooks.callHook("parser:after", filePath, result2);
|
|
656
|
-
return result2;
|
|
733
|
+
return this.parseJson(filePath);
|
|
657
734
|
}
|
|
658
|
-
const sourceFile = project.getSourceFile(filePath);
|
|
735
|
+
const sourceFile = this.project.getSourceFile(filePath);
|
|
659
736
|
if (!sourceFile)
|
|
660
737
|
return;
|
|
661
738
|
const content = sourceFile.getText();
|
|
662
|
-
const transformed = transformFile(filePath, content);
|
|
739
|
+
const transformed = this.transformFile(filePath, content);
|
|
663
740
|
if (content !== transformed) {
|
|
664
741
|
sourceFile.replaceWithText(transformed);
|
|
665
742
|
}
|
|
666
743
|
hooks.callHook("parser:before", filePath, content);
|
|
667
|
-
const result = parser(sourceFile)?.setFilePath(filePath);
|
|
744
|
+
const result = this.parser(sourceFile, encoder)?.setFilePath(filePath);
|
|
668
745
|
hooks.callHook("parser:after", filePath, result);
|
|
669
746
|
return result;
|
|
670
747
|
};
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
}
|
|
675
|
-
const reloadSourceFile = (filePath) => getSourceFile(filePath)?.refreshFromFileSystemSync();
|
|
676
|
-
const reloadSourceFiles = () => {
|
|
677
|
-
const files2 = getFiles();
|
|
678
|
-
for (const file of files2) {
|
|
679
|
-
const source = getSourceFile(file);
|
|
680
|
-
source?.refreshFromFileSystemSync() ?? project.addSourceFileAtPath(file);
|
|
748
|
+
transformFile = (filePath, content) => {
|
|
749
|
+
if (filePath.endsWith(".vue")) {
|
|
750
|
+
return vueToTsx(content);
|
|
681
751
|
}
|
|
752
|
+
if (filePath.endsWith(".svelte")) {
|
|
753
|
+
return svelteToTsx(content);
|
|
754
|
+
}
|
|
755
|
+
return content;
|
|
682
756
|
};
|
|
683
|
-
return {
|
|
684
|
-
getSourceFile,
|
|
685
|
-
removeSourceFile,
|
|
686
|
-
createSourceFile,
|
|
687
|
-
addSourceFile,
|
|
688
|
-
parseSourceFile,
|
|
689
|
-
reloadSourceFile,
|
|
690
|
-
reloadSourceFiles,
|
|
691
|
-
files,
|
|
692
|
-
getFiles,
|
|
693
|
-
readFile
|
|
694
|
-
};
|
|
695
|
-
};
|
|
696
|
-
var transformFile = (filePath, content) => {
|
|
697
|
-
if (filePath.endsWith(".vue")) {
|
|
698
|
-
return vueToTsx(content);
|
|
699
|
-
}
|
|
700
|
-
if (filePath.endsWith(".svelte")) {
|
|
701
|
-
return svelteToTsx(content);
|
|
702
|
-
}
|
|
703
|
-
return content;
|
|
704
757
|
};
|
|
705
758
|
// Annotate the CommonJS export names for ESM import in node:
|
|
706
759
|
0 && (module.exports = {
|
|
707
760
|
ParserResult,
|
|
708
|
-
|
|
709
|
-
createProject
|
|
761
|
+
Project
|
|
710
762
|
});
|