svelte-origin 1.0.0-next.21 → 1.0.0-next.23
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/cli.js +233 -6
- package/dist/index.js +240 -39
- package/dist/plugin.js +354 -6
- package/dist/post-process.js +233 -6
- package/dist/preprocess.js +356 -6
- package/dist/runtime/index.js +7 -33
- package/dist/transform/attrs-for-transform.d.ts +36 -5
- package/dist/transform/schema.d.ts +17 -0
- package/dist/vite-dts.js +77 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1975,6 +1975,7 @@ function transformOriginDefinition(content, svelteImports) {
|
|
|
1975
1975
|
let attrsContent = "";
|
|
1976
1976
|
let contentWithoutAttrs = bodyContent;
|
|
1977
1977
|
let attrPropertyName = "props";
|
|
1978
|
+
let propsRef = null;
|
|
1978
1979
|
if (attrsMatch) {
|
|
1979
1980
|
attrPropertyName = attrsMatch[1];
|
|
1980
1981
|
const attrsStart = attrsMatch.index;
|
|
@@ -2005,13 +2006,31 @@ function transformOriginDefinition(content, svelteImports) {
|
|
|
2005
2006
|
}
|
|
2006
2007
|
contentWithoutAttrs = bodyContent.slice(0, attrsStart) + bodyContent.slice(cutEnd);
|
|
2007
2008
|
}
|
|
2009
|
+
} else {
|
|
2010
|
+
const externalRefMatch = bodyContent.match(/(\w+)\s*:\s*(\w+)\s*,/);
|
|
2011
|
+
if (externalRefMatch) {
|
|
2012
|
+
const propName = externalRefMatch[1];
|
|
2013
|
+
const refName = externalRefMatch[2];
|
|
2014
|
+
if ((propName === "props" || propName === "attrs") && /^[A-Z]/.test(refName) && !["$state", "$derived", "$effect", "$bindable"].includes(refName)) {
|
|
2015
|
+
attrPropertyName = propName;
|
|
2016
|
+
propsRef = refName;
|
|
2017
|
+
const refStart = externalRefMatch.index;
|
|
2018
|
+
const refEnd = refStart + externalRefMatch[0].length;
|
|
2019
|
+
contentWithoutAttrs = bodyContent.slice(0, refStart) + bodyContent.slice(refEnd);
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2008
2022
|
}
|
|
2009
2023
|
const attrDetails = parseAttrsSource(attrsContent);
|
|
2010
2024
|
const { body: createFnBody, attachments } = transformOriginBody(contentWithoutAttrs.trim(), parents, attrDetails, attrPropertyName, svelteImports);
|
|
2011
2025
|
const parentsCode = parents.length > 0 ? `[${parents.join(", ")}]` : "undefined";
|
|
2012
2026
|
let configCode = `{
|
|
2013
2027
|
__attrSchema: ${attrSchemaCode},
|
|
2014
|
-
__parents: ${parentsCode}
|
|
2028
|
+
__parents: ${parentsCode},`;
|
|
2029
|
+
if (propsRef) {
|
|
2030
|
+
configCode += `
|
|
2031
|
+
__propsRef: ${propsRef},`;
|
|
2032
|
+
}
|
|
2033
|
+
configCode += `
|
|
2015
2034
|
__create: (__inputAttrs${parents.length > 0 ? ", __super" : ""}) => {
|
|
2016
2035
|
${createFnBody}
|
|
2017
2036
|
}`;
|
|
@@ -2246,6 +2265,73 @@ function parseOriginSchemaFromSource(source, exportName) {
|
|
|
2246
2265
|
return compiledResult;
|
|
2247
2266
|
return null;
|
|
2248
2267
|
}
|
|
2268
|
+
function parseAttrsSchemaFromSource(source, exportName) {
|
|
2269
|
+
const sourceResult = parseSourceAttrs(source, exportName);
|
|
2270
|
+
if (sourceResult)
|
|
2271
|
+
return sourceResult;
|
|
2272
|
+
const compiledResult = parseCompiledAttrs(source, exportName);
|
|
2273
|
+
if (compiledResult)
|
|
2274
|
+
return compiledResult;
|
|
2275
|
+
return null;
|
|
2276
|
+
}
|
|
2277
|
+
function parseSourceAttrs(source, exportName) {
|
|
2278
|
+
const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*\\$attrs\\s*\\(`, "s");
|
|
2279
|
+
const match = exportPattern.exec(source);
|
|
2280
|
+
if (!match)
|
|
2281
|
+
return null;
|
|
2282
|
+
const attrsStart = match.index + match[0].length - 1;
|
|
2283
|
+
const attrsEnd = findMatchingBracket(source, attrsStart);
|
|
2284
|
+
if (attrsEnd === -1)
|
|
2285
|
+
return null;
|
|
2286
|
+
let attrsContent = source.slice(attrsStart + 1, attrsEnd).trim();
|
|
2287
|
+
let parentNames = [];
|
|
2288
|
+
if (attrsContent.startsWith("[")) {
|
|
2289
|
+
const closeBracket = findMatchingBracket(attrsContent, 0, "[", "]");
|
|
2290
|
+
if (closeBracket !== -1) {
|
|
2291
|
+
const parentsContent = attrsContent.slice(1, closeBracket);
|
|
2292
|
+
parentNames = parentsContent.split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
|
|
2293
|
+
let i = closeBracket + 1;
|
|
2294
|
+
while (i < attrsContent.length && /[\s,]/.test(attrsContent[i]))
|
|
2295
|
+
i++;
|
|
2296
|
+
attrsContent = attrsContent.slice(i);
|
|
2297
|
+
}
|
|
2298
|
+
}
|
|
2299
|
+
if (!attrsContent.startsWith("{")) {
|
|
2300
|
+
return { attrs: [], parentNames };
|
|
2301
|
+
}
|
|
2302
|
+
const objEnd = findMatchingBracket(attrsContent, 0, "{", "}");
|
|
2303
|
+
if (objEnd === -1)
|
|
2304
|
+
return { attrs: [], parentNames };
|
|
2305
|
+
const objContent = attrsContent.slice(1, objEnd);
|
|
2306
|
+
const attrs = parseAttrsContent(objContent);
|
|
2307
|
+
return { attrs, parentNames };
|
|
2308
|
+
}
|
|
2309
|
+
function parseCompiledAttrs(source, exportName) {
|
|
2310
|
+
const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*__createAttrs\\s*\\(\\s*\\{`, "s");
|
|
2311
|
+
const match = exportPattern.exec(source);
|
|
2312
|
+
if (!match)
|
|
2313
|
+
return null;
|
|
2314
|
+
const braceStart = match.index + match[0].length - 1;
|
|
2315
|
+
const braceEnd = findMatchingBracket(source, braceStart, "{", "}");
|
|
2316
|
+
if (braceEnd === -1)
|
|
2317
|
+
return null;
|
|
2318
|
+
const configContent = source.slice(braceStart + 1, braceEnd);
|
|
2319
|
+
const schemaMatch = configContent.match(/__attrSchema\s*:\s*\{/);
|
|
2320
|
+
if (!schemaMatch)
|
|
2321
|
+
return { attrs: [], parentNames: [] };
|
|
2322
|
+
const schemaStart = schemaMatch.index + schemaMatch[0].length - 1;
|
|
2323
|
+
const schemaEnd = findMatchingBracket(configContent, schemaStart, "{", "}");
|
|
2324
|
+
if (schemaEnd === -1)
|
|
2325
|
+
return { attrs: [], parentNames: [] };
|
|
2326
|
+
const schemaContent = configContent.slice(schemaStart + 1, schemaEnd);
|
|
2327
|
+
const attrs = parseCompiledAttrSchema(schemaContent);
|
|
2328
|
+
const parentsMatch = configContent.match(/__parents\s*:\s*\[([^\]]*)\]/);
|
|
2329
|
+
let parentNames = [];
|
|
2330
|
+
if (parentsMatch && parentsMatch[1].trim()) {
|
|
2331
|
+
parentNames = parentsMatch[1].split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
|
|
2332
|
+
}
|
|
2333
|
+
return { attrs, parentNames };
|
|
2334
|
+
}
|
|
2249
2335
|
function parseSourceOrigin(source, exportName) {
|
|
2250
2336
|
const exportPattern = new RegExp(`export\\s+(?:const|var|let)\\s+${exportName}\\s*=\\s*\\$origin\\s*\\(`, "s");
|
|
2251
2337
|
const match = exportPattern.exec(source);
|
|
@@ -2271,6 +2357,13 @@ function parseSourceOrigin(source, exportName) {
|
|
|
2271
2357
|
}
|
|
2272
2358
|
const attrsMatch = bodyContent.match(/(\w+)\s*:\s*\$attrs\s*\(\s*\{/);
|
|
2273
2359
|
if (!attrsMatch) {
|
|
2360
|
+
const externalRefMatch = bodyContent.match(/(?:props|attrs)\s*:\s*([A-Z]\w*)\b/);
|
|
2361
|
+
if (externalRefMatch) {
|
|
2362
|
+
const propsRef = externalRefMatch[1];
|
|
2363
|
+
if (!["Object", "Array", "String", "Number", "Boolean"].includes(propsRef)) {
|
|
2364
|
+
return { attrs: [], parentNames, propsRef };
|
|
2365
|
+
}
|
|
2366
|
+
}
|
|
2274
2367
|
return { attrs: [], parentNames };
|
|
2275
2368
|
}
|
|
2276
2369
|
const attrsStart = attrsMatch.index;
|
|
@@ -2307,7 +2400,9 @@ function parseCompiledOrigin(source, exportName) {
|
|
|
2307
2400
|
if (parentsMatch && parentsMatch[1].trim()) {
|
|
2308
2401
|
parentNames = parentsMatch[1].split(",").map((p) => p.trim()).filter((p) => p && /^\w+$/.test(p));
|
|
2309
2402
|
}
|
|
2310
|
-
|
|
2403
|
+
const propsRefMatch = configContent.match(/__propsRef\s*:\s*(\w+)/);
|
|
2404
|
+
const propsRef = propsRefMatch ? propsRefMatch[1] : undefined;
|
|
2405
|
+
return { attrs, parentNames, propsRef };
|
|
2311
2406
|
}
|
|
2312
2407
|
function parseCompiledAttrSchema(content) {
|
|
2313
2408
|
const result = [];
|
|
@@ -2747,7 +2842,34 @@ function getElementTypeImport(element) {
|
|
|
2747
2842
|
}
|
|
2748
2843
|
|
|
2749
2844
|
// src/transform/attrs-for-transform.ts
|
|
2750
|
-
function
|
|
2845
|
+
function generateReactiveAttrsWrapper(attrs) {
|
|
2846
|
+
const parts = [];
|
|
2847
|
+
for (const attr of attrs) {
|
|
2848
|
+
parts.push(`get ${attr.key}() { return ${attr.key} }`);
|
|
2849
|
+
if (attr.bindable) {
|
|
2850
|
+
parts.push(`set ${attr.key}(v) { ${attr.key} = v }`);
|
|
2851
|
+
}
|
|
2852
|
+
}
|
|
2853
|
+
return `{ ${parts.join(", ")} }`;
|
|
2854
|
+
}
|
|
2855
|
+
function generateAttrsForMerge(attrs) {
|
|
2856
|
+
const parts = [];
|
|
2857
|
+
for (const attr of attrs) {
|
|
2858
|
+
if (attr.bindable) {
|
|
2859
|
+
if (attr.hasDefault) {
|
|
2860
|
+
parts.push(`${attr.key} = $bindable(${attr.defaultValue})`);
|
|
2861
|
+
} else {
|
|
2862
|
+
parts.push(`${attr.key} = $bindable()`);
|
|
2863
|
+
}
|
|
2864
|
+
} else if (attr.hasDefault) {
|
|
2865
|
+
parts.push(`${attr.key} = ${attr.defaultValue}`);
|
|
2866
|
+
} else {
|
|
2867
|
+
parts.push(attr.key);
|
|
2868
|
+
}
|
|
2869
|
+
}
|
|
2870
|
+
return parts.join(", ");
|
|
2871
|
+
}
|
|
2872
|
+
function transformAttrsForCallsSync(s, source, neededImports) {
|
|
2751
2873
|
let changed = false;
|
|
2752
2874
|
const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
2753
2875
|
let restVarName = null;
|
|
@@ -2813,6 +2935,111 @@ function transformAttrsForCalls(s, source, neededImports) {
|
|
|
2813
2935
|
}
|
|
2814
2936
|
return changed;
|
|
2815
2937
|
}
|
|
2938
|
+
async function transformAttrsForCalls(s, source, neededImports, schemaResolver) {
|
|
2939
|
+
let changed = false;
|
|
2940
|
+
const existingPropsMatch = source.match(/let\s*\{([^}]*)\}\s*(?::\s*[^=]+)?\s*=\s*\$props\s*\(\s*\)/);
|
|
2941
|
+
let restVarName = null;
|
|
2942
|
+
let existingPropsStart = -1;
|
|
2943
|
+
let existingDestructureContent = "";
|
|
2944
|
+
if (existingPropsMatch) {
|
|
2945
|
+
existingPropsStart = existingPropsMatch.index;
|
|
2946
|
+
existingDestructureContent = existingPropsMatch[1];
|
|
2947
|
+
const restMatch = existingDestructureContent.match(/\.\.\.\s*(\w+)\s*$/);
|
|
2948
|
+
if (restMatch) {
|
|
2949
|
+
restVarName = restMatch[1];
|
|
2950
|
+
}
|
|
2951
|
+
}
|
|
2952
|
+
const declarations = findVariableDeclarationsWithMacro(source, "$attrs.for");
|
|
2953
|
+
for (const decl of declarations) {
|
|
2954
|
+
const { varName, startIndex, macroOpenParen } = decl;
|
|
2955
|
+
const closeParenIndex = findMatchingBracket(source, macroOpenParen);
|
|
2956
|
+
if (closeParenIndex === -1)
|
|
2957
|
+
continue;
|
|
2958
|
+
const arg = source.slice(macroOpenParen + 1, closeParenIndex).trim();
|
|
2959
|
+
const endIndex = closeParenIndex + 1;
|
|
2960
|
+
const stringMatch = arg.match(/^['"](\w+)['"]$/);
|
|
2961
|
+
if (stringMatch) {
|
|
2962
|
+
const element = stringMatch[1];
|
|
2963
|
+
const typeImport = getElementTypeImport(element);
|
|
2964
|
+
if (existingPropsMatch && existingPropsStart !== -1) {
|
|
2965
|
+
if (!restVarName) {
|
|
2966
|
+
restVarName = "___restAttrs";
|
|
2967
|
+
const newDestructure = existingDestructureContent.trimEnd() ? `${existingDestructureContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
2968
|
+
const braceEnd = source.indexOf("}", existingPropsStart + 5);
|
|
2969
|
+
if (braceEnd !== -1) {
|
|
2970
|
+
s.overwrite(existingPropsStart + 5, braceEnd, ` ${newDestructure} `);
|
|
2971
|
+
}
|
|
2972
|
+
}
|
|
2973
|
+
s.overwrite(startIndex, endIndex, `let ${varName} = ${restVarName} as ${typeImport}`);
|
|
2974
|
+
} else {
|
|
2975
|
+
const expansion = `let { ...${varName} } = $props<${typeImport}>()`;
|
|
2976
|
+
s.overwrite(startIndex, endIndex, expansion);
|
|
2977
|
+
}
|
|
2978
|
+
} else {
|
|
2979
|
+
const factoryName = arg;
|
|
2980
|
+
let transformed = false;
|
|
2981
|
+
if (schemaResolver) {
|
|
2982
|
+
const importPath = findImportPath(source, factoryName);
|
|
2983
|
+
if (importPath) {
|
|
2984
|
+
const schema = await schemaResolver.resolve(importPath, factoryName);
|
|
2985
|
+
if (schema && schema.attrs.length > 0) {
|
|
2986
|
+
if (existingPropsMatch && existingPropsStart !== -1) {
|
|
2987
|
+
const additionalAttrs = generateAttrsForMerge(schema.attrs);
|
|
2988
|
+
const restMatch = existingDestructureContent.match(/,?\s*\.\.\.\s*(\w+)\s*$/);
|
|
2989
|
+
let baseContent = existingDestructureContent;
|
|
2990
|
+
let restPart = "";
|
|
2991
|
+
if (restMatch) {
|
|
2992
|
+
restPart = restMatch[0];
|
|
2993
|
+
baseContent = existingDestructureContent.slice(0, restMatch.index);
|
|
2994
|
+
}
|
|
2995
|
+
const mergedDestructure = baseContent.trimEnd() ? `${baseContent.trimEnd()}, ${additionalAttrs}${restPart}` : `${additionalAttrs}${restPart}`;
|
|
2996
|
+
const braceEnd = source.indexOf("}", existingPropsStart + 5);
|
|
2997
|
+
if (braceEnd !== -1) {
|
|
2998
|
+
s.overwrite(existingPropsStart + 5, braceEnd, ` ${mergedDestructure} `);
|
|
2999
|
+
}
|
|
3000
|
+
const wrapper = generateReactiveAttrsWrapper(schema.attrs);
|
|
3001
|
+
s.overwrite(startIndex, endIndex, `let ${varName} = __attrsFor(${factoryName}, ${wrapper})`);
|
|
3002
|
+
transformed = true;
|
|
3003
|
+
} else {
|
|
3004
|
+
const propsDestructure = generatePropsDestructuring(schema.attrs, factoryName);
|
|
3005
|
+
const wrapper = generateReactiveAttrsWrapper(schema.attrs);
|
|
3006
|
+
const expansion = [
|
|
3007
|
+
propsDestructure,
|
|
3008
|
+
`let ${varName} = __attrsFor(${factoryName}, ${wrapper})`
|
|
3009
|
+
].join(`
|
|
3010
|
+
`);
|
|
3011
|
+
s.overwrite(startIndex, endIndex, expansion);
|
|
3012
|
+
transformed = true;
|
|
3013
|
+
}
|
|
3014
|
+
}
|
|
3015
|
+
}
|
|
3016
|
+
}
|
|
3017
|
+
if (!transformed) {
|
|
3018
|
+
if (existingPropsMatch && existingPropsStart !== -1) {
|
|
3019
|
+
if (!restVarName) {
|
|
3020
|
+
restVarName = "___restAttrs";
|
|
3021
|
+
const newDestructure = existingDestructureContent.trimEnd() ? `${existingDestructureContent.trimEnd()}, ...${restVarName}` : `...${restVarName}`;
|
|
3022
|
+
const braceEnd = source.indexOf("}", existingPropsStart + 5);
|
|
3023
|
+
if (braceEnd !== -1) {
|
|
3024
|
+
s.overwrite(existingPropsStart + 5, braceEnd, ` ${newDestructure} `);
|
|
3025
|
+
}
|
|
3026
|
+
}
|
|
3027
|
+
s.overwrite(startIndex, endIndex, `let ${varName} = __attrsFor(${factoryName}, ${restVarName})`);
|
|
3028
|
+
} else {
|
|
3029
|
+
const expansion = [
|
|
3030
|
+
`let ___attrs: $attrs.Of<typeof ${factoryName}> = $props()`,
|
|
3031
|
+
`let ${varName} = __attrsFor(${factoryName}, ___attrs)`
|
|
3032
|
+
].join(`
|
|
3033
|
+
`);
|
|
3034
|
+
s.overwrite(startIndex, endIndex, expansion);
|
|
3035
|
+
}
|
|
3036
|
+
}
|
|
3037
|
+
neededImports.add("__attrsFor");
|
|
3038
|
+
}
|
|
3039
|
+
changed = true;
|
|
3040
|
+
}
|
|
3041
|
+
return changed;
|
|
3042
|
+
}
|
|
2816
3043
|
|
|
2817
3044
|
// src/transform/core.ts
|
|
2818
3045
|
function transformScript(source, options = {}) {
|
|
@@ -2838,7 +3065,7 @@ function transformScript(source, options = {}) {
|
|
|
2838
3065
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
2839
3066
|
}
|
|
2840
3067
|
if (isComponent) {
|
|
2841
|
-
changed =
|
|
3068
|
+
changed = transformAttrsForCallsSync(s, source, neededImports) || changed;
|
|
2842
3069
|
}
|
|
2843
3070
|
const needsPropsInjection = propsTypeDeclaration && propsTypeDeclaration.length > 0;
|
|
2844
3071
|
if (isComponent && (neededImports.size > 0 || needsPropsInjection)) {
|
|
@@ -2909,7 +3136,7 @@ async function transformScriptAsync(source, options) {
|
|
|
2909
3136
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
2910
3137
|
}
|
|
2911
3138
|
if (isComponent) {
|
|
2912
|
-
changed = transformAttrsForCalls(s, source, neededImports) || changed;
|
|
3139
|
+
changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
|
|
2913
3140
|
}
|
|
2914
3141
|
const needsPropsInjectionAsync = propsTypeDeclaration && propsTypeDeclaration.length > 0;
|
|
2915
3142
|
if (isComponent && (neededImports.size > 0 || needsPropsInjectionAsync)) {
|
|
@@ -2979,7 +3206,7 @@ async function transformScriptContent(source, options) {
|
|
|
2979
3206
|
propsTypeDeclaration = result.propsTypeDeclaration;
|
|
2980
3207
|
}
|
|
2981
3208
|
if (isComponent) {
|
|
2982
|
-
changed = transformAttrsForCalls(s, source, neededImports) || changed;
|
|
3209
|
+
changed = await transformAttrsForCalls(s, source, neededImports, schemaResolver) || changed;
|
|
2983
3210
|
}
|
|
2984
3211
|
if (neededImports.size > 0) {
|
|
2985
3212
|
const importStatement = generateRuntimeImport([...neededImports]);
|