svelte-origin 1.0.0-next.20 → 1.0.0-next.22
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.
Potentially problematic release.
This version of svelte-origin might be problematic. Click here for more details.
- package/dist/aliases.d.ts +1 -0
- package/dist/cli.js +194 -2
- package/dist/index.js +194 -2
- package/dist/plugin.js +379 -2
- package/dist/post-process.js +194 -2
- package/dist/preprocess.js +384 -2
- package/dist/transform/schema.d.ts +17 -0
- package/dist/vite-dts.js +174 -1
- package/package.json +1 -1
package/dist/plugin.js
CHANGED
|
@@ -1969,6 +1969,7 @@ function transformOriginDefinition(content, svelteImports) {
|
|
|
1969
1969
|
let attrsContent = "";
|
|
1970
1970
|
let contentWithoutAttrs = bodyContent;
|
|
1971
1971
|
let attrPropertyName = "props";
|
|
1972
|
+
let propsRef = null;
|
|
1972
1973
|
if (attrsMatch) {
|
|
1973
1974
|
attrPropertyName = attrsMatch[1];
|
|
1974
1975
|
const attrsStart = attrsMatch.index;
|
|
@@ -1999,13 +2000,31 @@ function transformOriginDefinition(content, svelteImports) {
|
|
|
1999
2000
|
}
|
|
2000
2001
|
contentWithoutAttrs = bodyContent.slice(0, attrsStart) + bodyContent.slice(cutEnd);
|
|
2001
2002
|
}
|
|
2003
|
+
} else {
|
|
2004
|
+
const externalRefMatch = bodyContent.match(/(\w+)\s*:\s*(\w+)\s*,/);
|
|
2005
|
+
if (externalRefMatch) {
|
|
2006
|
+
const propName = externalRefMatch[1];
|
|
2007
|
+
const refName = externalRefMatch[2];
|
|
2008
|
+
if ((propName === "props" || propName === "attrs") && /^[A-Z]/.test(refName) && !["$state", "$derived", "$effect", "$bindable"].includes(refName)) {
|
|
2009
|
+
attrPropertyName = propName;
|
|
2010
|
+
propsRef = refName;
|
|
2011
|
+
const refStart = externalRefMatch.index;
|
|
2012
|
+
const refEnd = refStart + externalRefMatch[0].length;
|
|
2013
|
+
contentWithoutAttrs = bodyContent.slice(0, refStart) + bodyContent.slice(refEnd);
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2002
2016
|
}
|
|
2003
2017
|
const attrDetails = parseAttrsSource(attrsContent);
|
|
2004
2018
|
const { body: createFnBody, attachments } = transformOriginBody(contentWithoutAttrs.trim(), parents, attrDetails, attrPropertyName, svelteImports);
|
|
2005
2019
|
const parentsCode = parents.length > 0 ? `[${parents.join(", ")}]` : "undefined";
|
|
2006
2020
|
let configCode = `{
|
|
2007
2021
|
__attrSchema: ${attrSchemaCode},
|
|
2008
|
-
__parents: ${parentsCode}
|
|
2022
|
+
__parents: ${parentsCode},`;
|
|
2023
|
+
if (propsRef) {
|
|
2024
|
+
configCode += `
|
|
2025
|
+
__propsRef: ${propsRef},`;
|
|
2026
|
+
}
|
|
2027
|
+
configCode += `
|
|
2009
2028
|
__create: (__inputAttrs${parents.length > 0 ? ", __super" : ""}) => {
|
|
2010
2029
|
${createFnBody}
|
|
2011
2030
|
}`;
|
|
@@ -2232,7 +2251,83 @@ function transformStandaloneAttrsDefinition(content) {
|
|
|
2232
2251
|
|
|
2233
2252
|
// src/transform/schema.ts
|
|
2234
2253
|
function parseOriginSchemaFromSource(source, exportName) {
|
|
2235
|
-
const
|
|
2254
|
+
const sourceResult = parseSourceOrigin(source, exportName);
|
|
2255
|
+
if (sourceResult)
|
|
2256
|
+
return sourceResult;
|
|
2257
|
+
const compiledResult = parseCompiledOrigin(source, exportName);
|
|
2258
|
+
if (compiledResult)
|
|
2259
|
+
return compiledResult;
|
|
2260
|
+
return null;
|
|
2261
|
+
}
|
|
2262
|
+
function parseAttrsSchemaFromSource(source, exportName) {
|
|
2263
|
+
const sourceResult = parseSourceAttrs(source, exportName);
|
|
2264
|
+
if (sourceResult)
|
|
2265
|
+
return sourceResult;
|
|
2266
|
+
const compiledResult = parseCompiledAttrs(source, exportName);
|
|
2267
|
+
if (compiledResult)
|
|
2268
|
+
return compiledResult;
|
|
2269
|
+
return null;
|
|
2270
|
+
}
|
|
2271
|
+
function parseSourceAttrs(source, exportName) {
|
|
2272
|
+
const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*\\$attrs\\s*\\(`, "s");
|
|
2273
|
+
const match = exportPattern.exec(source);
|
|
2274
|
+
if (!match)
|
|
2275
|
+
return null;
|
|
2276
|
+
const attrsStart = match.index + match[0].length - 1;
|
|
2277
|
+
const attrsEnd = findMatchingBracket(source, attrsStart);
|
|
2278
|
+
if (attrsEnd === -1)
|
|
2279
|
+
return null;
|
|
2280
|
+
let attrsContent = source.slice(attrsStart + 1, attrsEnd).trim();
|
|
2281
|
+
let parentNames = [];
|
|
2282
|
+
if (attrsContent.startsWith("[")) {
|
|
2283
|
+
const closeBracket = findMatchingBracket(attrsContent, 0, "[", "]");
|
|
2284
|
+
if (closeBracket !== -1) {
|
|
2285
|
+
const parentsContent = attrsContent.slice(1, closeBracket);
|
|
2286
|
+
parentNames = parentsContent.split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
|
|
2287
|
+
let i = closeBracket + 1;
|
|
2288
|
+
while (i < attrsContent.length && /[\s,]/.test(attrsContent[i]))
|
|
2289
|
+
i++;
|
|
2290
|
+
attrsContent = attrsContent.slice(i);
|
|
2291
|
+
}
|
|
2292
|
+
}
|
|
2293
|
+
if (!attrsContent.startsWith("{")) {
|
|
2294
|
+
return { attrs: [], parentNames };
|
|
2295
|
+
}
|
|
2296
|
+
const objEnd = findMatchingBracket(attrsContent, 0, "{", "}");
|
|
2297
|
+
if (objEnd === -1)
|
|
2298
|
+
return { attrs: [], parentNames };
|
|
2299
|
+
const objContent = attrsContent.slice(1, objEnd);
|
|
2300
|
+
const attrs = parseAttrsContent(objContent);
|
|
2301
|
+
return { attrs, parentNames };
|
|
2302
|
+
}
|
|
2303
|
+
function parseCompiledAttrs(source, exportName) {
|
|
2304
|
+
const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*__createAttrs\\s*\\(\\s*\\{`, "s");
|
|
2305
|
+
const match = exportPattern.exec(source);
|
|
2306
|
+
if (!match)
|
|
2307
|
+
return null;
|
|
2308
|
+
const braceStart = match.index + match[0].length - 1;
|
|
2309
|
+
const braceEnd = findMatchingBracket(source, braceStart, "{", "}");
|
|
2310
|
+
if (braceEnd === -1)
|
|
2311
|
+
return null;
|
|
2312
|
+
const configContent = source.slice(braceStart + 1, braceEnd);
|
|
2313
|
+
const schemaMatch = configContent.match(/__attrSchema\s*:\s*\{/);
|
|
2314
|
+
if (!schemaMatch)
|
|
2315
|
+
return { attrs: [], parentNames: [] };
|
|
2316
|
+
const schemaStart = schemaMatch.index + schemaMatch[0].length - 1;
|
|
2317
|
+
const schemaEnd = findMatchingBracket(configContent, schemaStart, "{", "}");
|
|
2318
|
+
if (schemaEnd === -1)
|
|
2319
|
+
return { attrs: [], parentNames: [] };
|
|
2320
|
+
const schemaContent = configContent.slice(schemaStart + 1, schemaEnd);
|
|
2321
|
+
const attrs = parseCompiledAttrSchema(schemaContent);
|
|
2322
|
+
const parentsMatch = configContent.match(/__parents\s*:\s*\[([^\]]*)\]/);
|
|
2323
|
+
let parentNames = [];
|
|
2324
|
+
if (parentsMatch && parentsMatch[1].trim()) {
|
|
2325
|
+
parentNames = parentsMatch[1].split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
|
|
2326
|
+
}
|
|
2327
|
+
return { attrs, parentNames };
|
|
2328
|
+
}
|
|
2329
|
+
function parseSourceOrigin(source, exportName) {
|
|
2330
|
+
const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*\\$origin\\s*\\(`, "s");
|
|
2236
2331
|
const match = exportPattern.exec(source);
|
|
2237
2332
|
if (!match)
|
|
2238
2333
|
return null;
|
|
@@ -2256,6 +2351,13 @@ function parseOriginSchemaFromSource(source, exportName) {
|
|
|
2256
2351
|
}
|
|
2257
2352
|
const attrsMatch = bodyContent.match(/(\w+)\s*:\s*\$attrs\s*\(\s*\{/);
|
|
2258
2353
|
if (!attrsMatch) {
|
|
2354
|
+
const externalRefMatch = bodyContent.match(/(?:props|attrs)\s*:\s*([A-Z]\w*)\b/);
|
|
2355
|
+
if (externalRefMatch) {
|
|
2356
|
+
const propsRef = externalRefMatch[1];
|
|
2357
|
+
if (!["Object", "Array", "String", "Number", "Boolean"].includes(propsRef)) {
|
|
2358
|
+
return { attrs: [], parentNames, propsRef };
|
|
2359
|
+
}
|
|
2360
|
+
}
|
|
2259
2361
|
return { attrs: [], parentNames };
|
|
2260
2362
|
}
|
|
2261
2363
|
const attrsStart = attrsMatch.index;
|
|
@@ -2268,6 +2370,96 @@ function parseOriginSchemaFromSource(source, exportName) {
|
|
|
2268
2370
|
const attrs = parseAttrsContent(attrsContent);
|
|
2269
2371
|
return { attrs, parentNames };
|
|
2270
2372
|
}
|
|
2373
|
+
function parseCompiledOrigin(source, exportName) {
|
|
2374
|
+
const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*__createOrigin\\s*\\(\\s*\\{`, "s");
|
|
2375
|
+
const match = exportPattern.exec(source);
|
|
2376
|
+
if (!match)
|
|
2377
|
+
return null;
|
|
2378
|
+
const braceStart = match.index + match[0].length - 1;
|
|
2379
|
+
const braceEnd = findMatchingBracket(source, braceStart, "{", "}");
|
|
2380
|
+
if (braceEnd === -1)
|
|
2381
|
+
return null;
|
|
2382
|
+
const configContent = source.slice(braceStart + 1, braceEnd);
|
|
2383
|
+
const schemaMatch = configContent.match(/__attrSchema\s*:\s*\{/);
|
|
2384
|
+
if (!schemaMatch)
|
|
2385
|
+
return { attrs: [], parentNames: [] };
|
|
2386
|
+
const schemaStart = schemaMatch.index + schemaMatch[0].length - 1;
|
|
2387
|
+
const schemaEnd = findMatchingBracket(configContent, schemaStart, "{", "}");
|
|
2388
|
+
if (schemaEnd === -1)
|
|
2389
|
+
return { attrs: [], parentNames: [] };
|
|
2390
|
+
const schemaContent = configContent.slice(schemaStart + 1, schemaEnd);
|
|
2391
|
+
const attrs = parseCompiledAttrSchema(schemaContent);
|
|
2392
|
+
const parentsMatch = configContent.match(/__parents\s*:\s*\[([^\]]*)\]/);
|
|
2393
|
+
let parentNames = [];
|
|
2394
|
+
if (parentsMatch && parentsMatch[1].trim()) {
|
|
2395
|
+
parentNames = parentsMatch[1].split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
|
|
2396
|
+
}
|
|
2397
|
+
const propsRefMatch = configContent.match(/__propsRef\s*:\s*(\w+)/);
|
|
2398
|
+
const propsRef = propsRefMatch ? propsRefMatch[1] : undefined;
|
|
2399
|
+
return { attrs, parentNames, propsRef };
|
|
2400
|
+
}
|
|
2401
|
+
function parseCompiledAttrSchema(content) {
|
|
2402
|
+
const result = [];
|
|
2403
|
+
let pos = 0;
|
|
2404
|
+
while (pos < content.length) {
|
|
2405
|
+
while (pos < content.length && /[\s,]/.test(content[pos]))
|
|
2406
|
+
pos++;
|
|
2407
|
+
if (pos >= content.length)
|
|
2408
|
+
break;
|
|
2409
|
+
if (content.slice(pos, pos + 2) === "/*") {
|
|
2410
|
+
const endComment = content.indexOf("*/", pos + 2);
|
|
2411
|
+
if (endComment !== -1) {
|
|
2412
|
+
pos = endComment + 2;
|
|
2413
|
+
continue;
|
|
2414
|
+
}
|
|
2415
|
+
}
|
|
2416
|
+
const keyMatch = content.slice(pos).match(/^(\w+)\s*:/);
|
|
2417
|
+
if (!keyMatch)
|
|
2418
|
+
break;
|
|
2419
|
+
const key = keyMatch[1];
|
|
2420
|
+
pos += keyMatch[0].length;
|
|
2421
|
+
while (pos < content.length && /\s/.test(content[pos]))
|
|
2422
|
+
pos++;
|
|
2423
|
+
if (content[pos] !== "{")
|
|
2424
|
+
break;
|
|
2425
|
+
const valueStart = pos;
|
|
2426
|
+
const valueEnd = findMatchingBracket(content, valueStart, "{", "}");
|
|
2427
|
+
if (valueEnd === -1)
|
|
2428
|
+
break;
|
|
2429
|
+
const valueContent = content.slice(valueStart + 1, valueEnd);
|
|
2430
|
+
let defaultValue = "undefined";
|
|
2431
|
+
let bindable = false;
|
|
2432
|
+
let hasDefault = false;
|
|
2433
|
+
const defaultMatch = valueContent.match(/\bdefault\s*:\s*/);
|
|
2434
|
+
if (defaultMatch) {
|
|
2435
|
+
const defaultStart = defaultMatch.index + defaultMatch[0].length;
|
|
2436
|
+
let depth = 0;
|
|
2437
|
+
let i = defaultStart;
|
|
2438
|
+
while (i < valueContent.length) {
|
|
2439
|
+
const char = valueContent[i];
|
|
2440
|
+
if (char === "{" || char === "(" || char === "[")
|
|
2441
|
+
depth++;
|
|
2442
|
+
else if (char === "}" || char === ")" || char === "]")
|
|
2443
|
+
depth--;
|
|
2444
|
+
else if (char === "," && depth === 0)
|
|
2445
|
+
break;
|
|
2446
|
+
i++;
|
|
2447
|
+
}
|
|
2448
|
+
defaultValue = valueContent.slice(defaultStart, i).trim();
|
|
2449
|
+
}
|
|
2450
|
+
const bindableMatch = valueContent.match(/\bbindable\s*:\s*(true|false)/);
|
|
2451
|
+
if (bindableMatch) {
|
|
2452
|
+
bindable = bindableMatch[1] === "true";
|
|
2453
|
+
}
|
|
2454
|
+
const hasDefaultMatch = valueContent.match(/\bhasDefault\s*:\s*(true|false)/);
|
|
2455
|
+
if (hasDefaultMatch) {
|
|
2456
|
+
hasDefault = hasDefaultMatch[1] === "true";
|
|
2457
|
+
}
|
|
2458
|
+
result.push({ key, defaultValue, bindable, hasDefault });
|
|
2459
|
+
pos = valueEnd + 1;
|
|
2460
|
+
}
|
|
2461
|
+
return result;
|
|
2462
|
+
}
|
|
2271
2463
|
function parseAttrsContent(content) {
|
|
2272
2464
|
const result = [];
|
|
2273
2465
|
const parts = splitByTopLevelCommas(content);
|
|
@@ -3162,6 +3354,108 @@ function resolveImportPath(importPath, aliases, importerDir) {
|
|
|
3162
3354
|
return join(aliasPath, remainder);
|
|
3163
3355
|
}
|
|
3164
3356
|
}
|
|
3357
|
+
const npmResolved = resolveNpmPackageImport(importPath, importerDir);
|
|
3358
|
+
if (npmResolved) {
|
|
3359
|
+
return npmResolved;
|
|
3360
|
+
}
|
|
3361
|
+
return null;
|
|
3362
|
+
}
|
|
3363
|
+
function resolveNpmPackageImport(importPath, importerDir) {
|
|
3364
|
+
if (importPath.startsWith(".") || importPath.startsWith("/") || /^[a-zA-Z]:/.test(importPath)) {
|
|
3365
|
+
return null;
|
|
3366
|
+
}
|
|
3367
|
+
let packageName;
|
|
3368
|
+
let subpath;
|
|
3369
|
+
if (importPath.startsWith("@")) {
|
|
3370
|
+
const parts = importPath.split("/");
|
|
3371
|
+
if (parts.length < 2)
|
|
3372
|
+
return null;
|
|
3373
|
+
packageName = `${parts[0]}/${parts[1]}`;
|
|
3374
|
+
subpath = parts.slice(2).join("/");
|
|
3375
|
+
} else {
|
|
3376
|
+
const slashIndex = importPath.indexOf("/");
|
|
3377
|
+
if (slashIndex === -1) {
|
|
3378
|
+
packageName = importPath;
|
|
3379
|
+
subpath = "";
|
|
3380
|
+
} else {
|
|
3381
|
+
packageName = importPath.slice(0, slashIndex);
|
|
3382
|
+
subpath = importPath.slice(slashIndex + 1);
|
|
3383
|
+
}
|
|
3384
|
+
}
|
|
3385
|
+
let currentDir = importerDir;
|
|
3386
|
+
const root = isAbsolute(importerDir) ? (importerDir.match(/^[a-zA-Z]:[/\\]/) || ["/"])[0] : "/";
|
|
3387
|
+
while (currentDir !== root && currentDir !== dirname(currentDir)) {
|
|
3388
|
+
const nodeModulesDir = join(currentDir, "node_modules");
|
|
3389
|
+
const packageDir = join(nodeModulesDir, packageName);
|
|
3390
|
+
if (existsSync(packageDir)) {
|
|
3391
|
+
const packageJsonPath = join(packageDir, "package.json");
|
|
3392
|
+
const exportKey = subpath ? `./${subpath}` : ".";
|
|
3393
|
+
if (existsSync(packageJsonPath)) {
|
|
3394
|
+
try {
|
|
3395
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
|
|
3396
|
+
if (packageJson.exports) {
|
|
3397
|
+
const resolvedExport = resolvePackageExports(packageJson.exports, exportKey);
|
|
3398
|
+
if (resolvedExport) {
|
|
3399
|
+
return join(packageDir, resolvedExport);
|
|
3400
|
+
}
|
|
3401
|
+
}
|
|
3402
|
+
if (!subpath) {
|
|
3403
|
+
const mainEntry = packageJson.module || packageJson.main || "index.js";
|
|
3404
|
+
return join(packageDir, mainEntry);
|
|
3405
|
+
}
|
|
3406
|
+
} catch {}
|
|
3407
|
+
}
|
|
3408
|
+
if (subpath) {
|
|
3409
|
+
const resolvedSubpath = join(packageDir, subpath);
|
|
3410
|
+
return resolvedSubpath;
|
|
3411
|
+
}
|
|
3412
|
+
return join(packageDir, "index.js");
|
|
3413
|
+
}
|
|
3414
|
+
currentDir = dirname(currentDir);
|
|
3415
|
+
}
|
|
3416
|
+
return null;
|
|
3417
|
+
}
|
|
3418
|
+
function resolvePackageExports(exports, subpath) {
|
|
3419
|
+
if (typeof exports === "string") {
|
|
3420
|
+
return subpath === "." ? exports : null;
|
|
3421
|
+
}
|
|
3422
|
+
if (typeof exports !== "object" || exports === null) {
|
|
3423
|
+
return null;
|
|
3424
|
+
}
|
|
3425
|
+
const exportEntry = exports[subpath];
|
|
3426
|
+
if (exportEntry) {
|
|
3427
|
+
return resolveExportCondition(exportEntry);
|
|
3428
|
+
}
|
|
3429
|
+
if (subpath === "." && exports["."]) {
|
|
3430
|
+
return resolveExportCondition(exports["."]);
|
|
3431
|
+
}
|
|
3432
|
+
if (subpath === ".") {
|
|
3433
|
+
const resolved = resolveExportCondition(exports);
|
|
3434
|
+
if (resolved)
|
|
3435
|
+
return resolved;
|
|
3436
|
+
}
|
|
3437
|
+
return null;
|
|
3438
|
+
}
|
|
3439
|
+
function resolveExportCondition(condition) {
|
|
3440
|
+
if (typeof condition === "string") {
|
|
3441
|
+
return condition;
|
|
3442
|
+
}
|
|
3443
|
+
if (typeof condition !== "object" || condition === null) {
|
|
3444
|
+
return null;
|
|
3445
|
+
}
|
|
3446
|
+
const condObj = condition;
|
|
3447
|
+
const preferredConditions = ["svelte", "import", "module", "default", "require"];
|
|
3448
|
+
for (const cond of preferredConditions) {
|
|
3449
|
+
if (cond in condObj) {
|
|
3450
|
+
const value = condObj[cond];
|
|
3451
|
+
if (typeof value === "string") {
|
|
3452
|
+
return value;
|
|
3453
|
+
}
|
|
3454
|
+
const resolved = resolveExportCondition(value);
|
|
3455
|
+
if (resolved)
|
|
3456
|
+
return resolved;
|
|
3457
|
+
}
|
|
3458
|
+
}
|
|
3165
3459
|
return null;
|
|
3166
3460
|
}
|
|
3167
3461
|
function resolveFilePath(basePath) {
|
|
@@ -3470,6 +3764,20 @@ async function resolveSchema(importPath, exportName, importerId, cache, debug, a
|
|
|
3470
3764
|
mergedAttrs.push(attr);
|
|
3471
3765
|
}
|
|
3472
3766
|
}
|
|
3767
|
+
if (schema.propsRef) {
|
|
3768
|
+
const propsRefImportPath = findImportPath(source, schema.propsRef);
|
|
3769
|
+
if (propsRefImportPath) {
|
|
3770
|
+
const propsRefSchema = await resolveAttrsSchema(propsRefImportPath, schema.propsRef, filePath, cache, debug, aliases);
|
|
3771
|
+
if (propsRefSchema) {
|
|
3772
|
+
for (const attr of propsRefSchema.attrs) {
|
|
3773
|
+
const existingIndex = mergedAttrs.findIndex((a) => a.key === attr.key);
|
|
3774
|
+
if (existingIndex === -1) {
|
|
3775
|
+
mergedAttrs.push(attr);
|
|
3776
|
+
}
|
|
3777
|
+
}
|
|
3778
|
+
}
|
|
3779
|
+
}
|
|
3780
|
+
}
|
|
3473
3781
|
const mergedSchema = {
|
|
3474
3782
|
attrs: mergedAttrs,
|
|
3475
3783
|
parentNames: schema.parentNames
|
|
@@ -3487,6 +3795,75 @@ async function resolveSchema(importPath, exportName, importerId, cache, debug, a
|
|
|
3487
3795
|
return null;
|
|
3488
3796
|
}
|
|
3489
3797
|
}
|
|
3798
|
+
async function resolveAttrsSchema(importPath, exportName, importerId, cache, debug, aliases) {
|
|
3799
|
+
const cacheKey = `attrs::${importPath}::${exportName}`;
|
|
3800
|
+
if (cache.has(cacheKey)) {
|
|
3801
|
+
if (debug) {
|
|
3802
|
+
console.log(`[svelte-origin] Cache hit for ${cacheKey}`);
|
|
3803
|
+
}
|
|
3804
|
+
return cache.get(cacheKey) || null;
|
|
3805
|
+
}
|
|
3806
|
+
try {
|
|
3807
|
+
const importerDir = dirname2(importerId);
|
|
3808
|
+
if (debug) {
|
|
3809
|
+
console.log(`[svelte-origin] Resolving attrs: ${exportName} from ${importPath}`);
|
|
3810
|
+
}
|
|
3811
|
+
const resolvedPath = resolveImportPath(importPath, aliases, importerDir);
|
|
3812
|
+
if (!resolvedPath) {
|
|
3813
|
+
cache.set(cacheKey, null);
|
|
3814
|
+
return null;
|
|
3815
|
+
}
|
|
3816
|
+
const filePath = resolveFilePath(resolvedPath);
|
|
3817
|
+
if (!filePath) {
|
|
3818
|
+
cache.set(cacheKey, null);
|
|
3819
|
+
return null;
|
|
3820
|
+
}
|
|
3821
|
+
const source = readFileSync2(filePath, "utf-8");
|
|
3822
|
+
const schema = parseAttrsSchemaFromSource(source, exportName);
|
|
3823
|
+
if (!schema) {
|
|
3824
|
+
cache.set(cacheKey, null);
|
|
3825
|
+
return null;
|
|
3826
|
+
}
|
|
3827
|
+
const mergedAttrs = [];
|
|
3828
|
+
for (const parentName of schema.parentNames) {
|
|
3829
|
+
const parentImportPath = findImportPath(source, parentName);
|
|
3830
|
+
if (parentImportPath) {
|
|
3831
|
+
const parentSchema = await resolveAttrsSchema(parentImportPath, parentName, filePath, cache, debug, aliases);
|
|
3832
|
+
if (parentSchema) {
|
|
3833
|
+
for (const attr of parentSchema.attrs) {
|
|
3834
|
+
const existingIndex = mergedAttrs.findIndex((a) => a.key === attr.key);
|
|
3835
|
+
if (existingIndex === -1) {
|
|
3836
|
+
mergedAttrs.push(attr);
|
|
3837
|
+
}
|
|
3838
|
+
}
|
|
3839
|
+
}
|
|
3840
|
+
}
|
|
3841
|
+
}
|
|
3842
|
+
for (const attr of schema.attrs) {
|
|
3843
|
+
const existingIndex = mergedAttrs.findIndex((a) => a.key === attr.key);
|
|
3844
|
+
if (existingIndex !== -1) {
|
|
3845
|
+
mergedAttrs[existingIndex] = attr;
|
|
3846
|
+
} else {
|
|
3847
|
+
mergedAttrs.push(attr);
|
|
3848
|
+
}
|
|
3849
|
+
}
|
|
3850
|
+
const mergedSchema = {
|
|
3851
|
+
attrs: mergedAttrs,
|
|
3852
|
+
parentNames: schema.parentNames
|
|
3853
|
+
};
|
|
3854
|
+
if (debug) {
|
|
3855
|
+
console.log(`[svelte-origin] Resolved attrs schema for ${exportName}:`, mergedSchema.attrs.map((a) => a.key));
|
|
3856
|
+
}
|
|
3857
|
+
cache.set(cacheKey, mergedSchema);
|
|
3858
|
+
return mergedSchema;
|
|
3859
|
+
} catch (error) {
|
|
3860
|
+
if (debug) {
|
|
3861
|
+
console.error(`[svelte-origin] Error resolving attrs schema for ${importPath}:`, error);
|
|
3862
|
+
}
|
|
3863
|
+
cache.set(cacheKey, null);
|
|
3864
|
+
return null;
|
|
3865
|
+
}
|
|
3866
|
+
}
|
|
3490
3867
|
var plugin_default = svelteOrigin;
|
|
3491
3868
|
export {
|
|
3492
3869
|
svelteOrigin,
|
package/dist/post-process.js
CHANGED
|
@@ -1973,6 +1973,7 @@ function transformOriginDefinition(content, svelteImports) {
|
|
|
1973
1973
|
let attrsContent = "";
|
|
1974
1974
|
let contentWithoutAttrs = bodyContent;
|
|
1975
1975
|
let attrPropertyName = "props";
|
|
1976
|
+
let propsRef = null;
|
|
1976
1977
|
if (attrsMatch) {
|
|
1977
1978
|
attrPropertyName = attrsMatch[1];
|
|
1978
1979
|
const attrsStart = attrsMatch.index;
|
|
@@ -2003,13 +2004,31 @@ function transformOriginDefinition(content, svelteImports) {
|
|
|
2003
2004
|
}
|
|
2004
2005
|
contentWithoutAttrs = bodyContent.slice(0, attrsStart) + bodyContent.slice(cutEnd);
|
|
2005
2006
|
}
|
|
2007
|
+
} else {
|
|
2008
|
+
const externalRefMatch = bodyContent.match(/(\w+)\s*:\s*(\w+)\s*,/);
|
|
2009
|
+
if (externalRefMatch) {
|
|
2010
|
+
const propName = externalRefMatch[1];
|
|
2011
|
+
const refName = externalRefMatch[2];
|
|
2012
|
+
if ((propName === "props" || propName === "attrs") && /^[A-Z]/.test(refName) && !["$state", "$derived", "$effect", "$bindable"].includes(refName)) {
|
|
2013
|
+
attrPropertyName = propName;
|
|
2014
|
+
propsRef = refName;
|
|
2015
|
+
const refStart = externalRefMatch.index;
|
|
2016
|
+
const refEnd = refStart + externalRefMatch[0].length;
|
|
2017
|
+
contentWithoutAttrs = bodyContent.slice(0, refStart) + bodyContent.slice(refEnd);
|
|
2018
|
+
}
|
|
2019
|
+
}
|
|
2006
2020
|
}
|
|
2007
2021
|
const attrDetails = parseAttrsSource(attrsContent);
|
|
2008
2022
|
const { body: createFnBody, attachments } = transformOriginBody(contentWithoutAttrs.trim(), parents, attrDetails, attrPropertyName, svelteImports);
|
|
2009
2023
|
const parentsCode = parents.length > 0 ? `[${parents.join(", ")}]` : "undefined";
|
|
2010
2024
|
let configCode = `{
|
|
2011
2025
|
__attrSchema: ${attrSchemaCode},
|
|
2012
|
-
__parents: ${parentsCode}
|
|
2026
|
+
__parents: ${parentsCode},`;
|
|
2027
|
+
if (propsRef) {
|
|
2028
|
+
configCode += `
|
|
2029
|
+
__propsRef: ${propsRef},`;
|
|
2030
|
+
}
|
|
2031
|
+
configCode += `
|
|
2013
2032
|
__create: (__inputAttrs${parents.length > 0 ? ", __super" : ""}) => {
|
|
2014
2033
|
${createFnBody}
|
|
2015
2034
|
}`;
|
|
@@ -2236,7 +2255,83 @@ function transformStandaloneAttrsDefinition(content) {
|
|
|
2236
2255
|
|
|
2237
2256
|
// src/transform/schema.ts
|
|
2238
2257
|
function parseOriginSchemaFromSource(source, exportName) {
|
|
2239
|
-
const
|
|
2258
|
+
const sourceResult = parseSourceOrigin(source, exportName);
|
|
2259
|
+
if (sourceResult)
|
|
2260
|
+
return sourceResult;
|
|
2261
|
+
const compiledResult = parseCompiledOrigin(source, exportName);
|
|
2262
|
+
if (compiledResult)
|
|
2263
|
+
return compiledResult;
|
|
2264
|
+
return null;
|
|
2265
|
+
}
|
|
2266
|
+
function parseAttrsSchemaFromSource(source, exportName) {
|
|
2267
|
+
const sourceResult = parseSourceAttrs(source, exportName);
|
|
2268
|
+
if (sourceResult)
|
|
2269
|
+
return sourceResult;
|
|
2270
|
+
const compiledResult = parseCompiledAttrs(source, exportName);
|
|
2271
|
+
if (compiledResult)
|
|
2272
|
+
return compiledResult;
|
|
2273
|
+
return null;
|
|
2274
|
+
}
|
|
2275
|
+
function parseSourceAttrs(source, exportName) {
|
|
2276
|
+
const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*\\$attrs\\s*\\(`, "s");
|
|
2277
|
+
const match = exportPattern.exec(source);
|
|
2278
|
+
if (!match)
|
|
2279
|
+
return null;
|
|
2280
|
+
const attrsStart = match.index + match[0].length - 1;
|
|
2281
|
+
const attrsEnd = findMatchingBracket(source, attrsStart);
|
|
2282
|
+
if (attrsEnd === -1)
|
|
2283
|
+
return null;
|
|
2284
|
+
let attrsContent = source.slice(attrsStart + 1, attrsEnd).trim();
|
|
2285
|
+
let parentNames = [];
|
|
2286
|
+
if (attrsContent.startsWith("[")) {
|
|
2287
|
+
const closeBracket = findMatchingBracket(attrsContent, 0, "[", "]");
|
|
2288
|
+
if (closeBracket !== -1) {
|
|
2289
|
+
const parentsContent = attrsContent.slice(1, closeBracket);
|
|
2290
|
+
parentNames = parentsContent.split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
|
|
2291
|
+
let i = closeBracket + 1;
|
|
2292
|
+
while (i < attrsContent.length && /[\s,]/.test(attrsContent[i]))
|
|
2293
|
+
i++;
|
|
2294
|
+
attrsContent = attrsContent.slice(i);
|
|
2295
|
+
}
|
|
2296
|
+
}
|
|
2297
|
+
if (!attrsContent.startsWith("{")) {
|
|
2298
|
+
return { attrs: [], parentNames };
|
|
2299
|
+
}
|
|
2300
|
+
const objEnd = findMatchingBracket(attrsContent, 0, "{", "}");
|
|
2301
|
+
if (objEnd === -1)
|
|
2302
|
+
return { attrs: [], parentNames };
|
|
2303
|
+
const objContent = attrsContent.slice(1, objEnd);
|
|
2304
|
+
const attrs = parseAttrsContent(objContent);
|
|
2305
|
+
return { attrs, parentNames };
|
|
2306
|
+
}
|
|
2307
|
+
function parseCompiledAttrs(source, exportName) {
|
|
2308
|
+
const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*__createAttrs\\s*\\(\\s*\\{`, "s");
|
|
2309
|
+
const match = exportPattern.exec(source);
|
|
2310
|
+
if (!match)
|
|
2311
|
+
return null;
|
|
2312
|
+
const braceStart = match.index + match[0].length - 1;
|
|
2313
|
+
const braceEnd = findMatchingBracket(source, braceStart, "{", "}");
|
|
2314
|
+
if (braceEnd === -1)
|
|
2315
|
+
return null;
|
|
2316
|
+
const configContent = source.slice(braceStart + 1, braceEnd);
|
|
2317
|
+
const schemaMatch = configContent.match(/__attrSchema\s*:\s*\{/);
|
|
2318
|
+
if (!schemaMatch)
|
|
2319
|
+
return { attrs: [], parentNames: [] };
|
|
2320
|
+
const schemaStart = schemaMatch.index + schemaMatch[0].length - 1;
|
|
2321
|
+
const schemaEnd = findMatchingBracket(configContent, schemaStart, "{", "}");
|
|
2322
|
+
if (schemaEnd === -1)
|
|
2323
|
+
return { attrs: [], parentNames: [] };
|
|
2324
|
+
const schemaContent = configContent.slice(schemaStart + 1, schemaEnd);
|
|
2325
|
+
const attrs = parseCompiledAttrSchema(schemaContent);
|
|
2326
|
+
const parentsMatch = configContent.match(/__parents\s*:\s*\[([^\]]*)\]/);
|
|
2327
|
+
let parentNames = [];
|
|
2328
|
+
if (parentsMatch && parentsMatch[1].trim()) {
|
|
2329
|
+
parentNames = parentsMatch[1].split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
|
|
2330
|
+
}
|
|
2331
|
+
return { attrs, parentNames };
|
|
2332
|
+
}
|
|
2333
|
+
function parseSourceOrigin(source, exportName) {
|
|
2334
|
+
const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*\\$origin\\s*\\(`, "s");
|
|
2240
2335
|
const match = exportPattern.exec(source);
|
|
2241
2336
|
if (!match)
|
|
2242
2337
|
return null;
|
|
@@ -2260,6 +2355,13 @@ function parseOriginSchemaFromSource(source, exportName) {
|
|
|
2260
2355
|
}
|
|
2261
2356
|
const attrsMatch = bodyContent.match(/(\w+)\s*:\s*\$attrs\s*\(\s*\{/);
|
|
2262
2357
|
if (!attrsMatch) {
|
|
2358
|
+
const externalRefMatch = bodyContent.match(/(?:props|attrs)\s*:\s*([A-Z]\w*)\b/);
|
|
2359
|
+
if (externalRefMatch) {
|
|
2360
|
+
const propsRef = externalRefMatch[1];
|
|
2361
|
+
if (!["Object", "Array", "String", "Number", "Boolean"].includes(propsRef)) {
|
|
2362
|
+
return { attrs: [], parentNames, propsRef };
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2263
2365
|
return { attrs: [], parentNames };
|
|
2264
2366
|
}
|
|
2265
2367
|
const attrsStart = attrsMatch.index;
|
|
@@ -2272,6 +2374,96 @@ function parseOriginSchemaFromSource(source, exportName) {
|
|
|
2272
2374
|
const attrs = parseAttrsContent(attrsContent);
|
|
2273
2375
|
return { attrs, parentNames };
|
|
2274
2376
|
}
|
|
2377
|
+
function parseCompiledOrigin(source, exportName) {
|
|
2378
|
+
const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*__createOrigin\\s*\\(\\s*\\{`, "s");
|
|
2379
|
+
const match = exportPattern.exec(source);
|
|
2380
|
+
if (!match)
|
|
2381
|
+
return null;
|
|
2382
|
+
const braceStart = match.index + match[0].length - 1;
|
|
2383
|
+
const braceEnd = findMatchingBracket(source, braceStart, "{", "}");
|
|
2384
|
+
if (braceEnd === -1)
|
|
2385
|
+
return null;
|
|
2386
|
+
const configContent = source.slice(braceStart + 1, braceEnd);
|
|
2387
|
+
const schemaMatch = configContent.match(/__attrSchema\s*:\s*\{/);
|
|
2388
|
+
if (!schemaMatch)
|
|
2389
|
+
return { attrs: [], parentNames: [] };
|
|
2390
|
+
const schemaStart = schemaMatch.index + schemaMatch[0].length - 1;
|
|
2391
|
+
const schemaEnd = findMatchingBracket(configContent, schemaStart, "{", "}");
|
|
2392
|
+
if (schemaEnd === -1)
|
|
2393
|
+
return { attrs: [], parentNames: [] };
|
|
2394
|
+
const schemaContent = configContent.slice(schemaStart + 1, schemaEnd);
|
|
2395
|
+
const attrs = parseCompiledAttrSchema(schemaContent);
|
|
2396
|
+
const parentsMatch = configContent.match(/__parents\s*:\s*\[([^\]]*)\]/);
|
|
2397
|
+
let parentNames = [];
|
|
2398
|
+
if (parentsMatch && parentsMatch[1].trim()) {
|
|
2399
|
+
parentNames = parentsMatch[1].split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
|
|
2400
|
+
}
|
|
2401
|
+
const propsRefMatch = configContent.match(/__propsRef\s*:\s*(\w+)/);
|
|
2402
|
+
const propsRef = propsRefMatch ? propsRefMatch[1] : undefined;
|
|
2403
|
+
return { attrs, parentNames, propsRef };
|
|
2404
|
+
}
|
|
2405
|
+
function parseCompiledAttrSchema(content) {
|
|
2406
|
+
const result = [];
|
|
2407
|
+
let pos = 0;
|
|
2408
|
+
while (pos < content.length) {
|
|
2409
|
+
while (pos < content.length && /[\s,]/.test(content[pos]))
|
|
2410
|
+
pos++;
|
|
2411
|
+
if (pos >= content.length)
|
|
2412
|
+
break;
|
|
2413
|
+
if (content.slice(pos, pos + 2) === "/*") {
|
|
2414
|
+
const endComment = content.indexOf("*/", pos + 2);
|
|
2415
|
+
if (endComment !== -1) {
|
|
2416
|
+
pos = endComment + 2;
|
|
2417
|
+
continue;
|
|
2418
|
+
}
|
|
2419
|
+
}
|
|
2420
|
+
const keyMatch = content.slice(pos).match(/^(\w+)\s*:/);
|
|
2421
|
+
if (!keyMatch)
|
|
2422
|
+
break;
|
|
2423
|
+
const key = keyMatch[1];
|
|
2424
|
+
pos += keyMatch[0].length;
|
|
2425
|
+
while (pos < content.length && /\s/.test(content[pos]))
|
|
2426
|
+
pos++;
|
|
2427
|
+
if (content[pos] !== "{")
|
|
2428
|
+
break;
|
|
2429
|
+
const valueStart = pos;
|
|
2430
|
+
const valueEnd = findMatchingBracket(content, valueStart, "{", "}");
|
|
2431
|
+
if (valueEnd === -1)
|
|
2432
|
+
break;
|
|
2433
|
+
const valueContent = content.slice(valueStart + 1, valueEnd);
|
|
2434
|
+
let defaultValue = "undefined";
|
|
2435
|
+
let bindable = false;
|
|
2436
|
+
let hasDefault = false;
|
|
2437
|
+
const defaultMatch = valueContent.match(/\bdefault\s*:\s*/);
|
|
2438
|
+
if (defaultMatch) {
|
|
2439
|
+
const defaultStart = defaultMatch.index + defaultMatch[0].length;
|
|
2440
|
+
let depth = 0;
|
|
2441
|
+
let i = defaultStart;
|
|
2442
|
+
while (i < valueContent.length) {
|
|
2443
|
+
const char = valueContent[i];
|
|
2444
|
+
if (char === "{" || char === "(" || char === "[")
|
|
2445
|
+
depth++;
|
|
2446
|
+
else if (char === "}" || char === ")" || char === "]")
|
|
2447
|
+
depth--;
|
|
2448
|
+
else if (char === "," && depth === 0)
|
|
2449
|
+
break;
|
|
2450
|
+
i++;
|
|
2451
|
+
}
|
|
2452
|
+
defaultValue = valueContent.slice(defaultStart, i).trim();
|
|
2453
|
+
}
|
|
2454
|
+
const bindableMatch = valueContent.match(/\bbindable\s*:\s*(true|false)/);
|
|
2455
|
+
if (bindableMatch) {
|
|
2456
|
+
bindable = bindableMatch[1] === "true";
|
|
2457
|
+
}
|
|
2458
|
+
const hasDefaultMatch = valueContent.match(/\bhasDefault\s*:\s*(true|false)/);
|
|
2459
|
+
if (hasDefaultMatch) {
|
|
2460
|
+
hasDefault = hasDefaultMatch[1] === "true";
|
|
2461
|
+
}
|
|
2462
|
+
result.push({ key, defaultValue, bindable, hasDefault });
|
|
2463
|
+
pos = valueEnd + 1;
|
|
2464
|
+
}
|
|
2465
|
+
return result;
|
|
2466
|
+
}
|
|
2275
2467
|
function parseAttrsContent(content) {
|
|
2276
2468
|
const result = [];
|
|
2277
2469
|
const parts = splitByTopLevelCommas(content);
|