svelte-origin 1.0.0-next.16 → 1.0.0-next.18

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 CHANGED
@@ -2027,38 +2027,48 @@ ${createFnBody}
2027
2027
  }`;
2028
2028
  return configCode;
2029
2029
  }
2030
- function transformOriginBody(content, parents = [], attrDetails = [], propName = "props", svelteImports) {
2030
+ function transformOriginBody(content, parents = [], attrDetails = [], propName = "props", _svelteImports) {
2031
2031
  let body = content.trim();
2032
2032
  const hasParents = parents.length > 0;
2033
- const attrStateDeclarations = [];
2034
- const attrSyncEffects = [];
2035
- const attrGettersSetters = [];
2036
- if (attrDetails.length > 0 && svelteImports) {
2037
- svelteImports.add("untrack");
2038
- }
2039
- for (const attr of attrDetails) {
2040
- const defaultExpr = attr.hasDefault ? ` ?? ${attr.defaultValue}` : "";
2041
- attrStateDeclarations.push(`let __attr_${attr.key} = $state(__inputAttrs.${attr.key}${defaultExpr})`);
2042
- attrSyncEffects.push(`$effect(() => {
2043
- const __parentVal = __inputAttrs.${attr.key}
2044
- const __currentVal = untrack(() => __attr_${attr.key})
2045
- if (__parentVal !== __currentVal) {
2046
- __attr_${attr.key} = __parentVal${defaultExpr}
2047
- }
2048
- })`);
2049
- attrGettersSetters.push(`get ${attr.key}() { return __attr_${attr.key} }`);
2050
- attrGettersSetters.push(`set ${attr.key}(v) { __attr_${attr.key} = v; __inputAttrs.${attr.key} = v }`);
2051
- }
2052
- const attrStateSection = attrStateDeclarations.length > 0 ? attrStateDeclarations.join(`
2053
- `) + `
2054
- ` : "";
2055
- const attrEffectSection = attrSyncEffects.length > 0 ? attrSyncEffects.join(`
2056
- `) + `
2057
- ` : "";
2058
- const propsObjectCode = attrGettersSetters.length > 0 ? `${attrStateSection}${attrEffectSection}const __props = {
2059
- ${attrGettersSetters.join(`,
2060
- `)}
2061
- }` : ` const __props = __inputAttrs`;
2033
+ const propsLines = [];
2034
+ if (attrDetails.length > 0) {
2035
+ const initialProps = [];
2036
+ for (const attr of attrDetails) {
2037
+ const defaultExpr = attr.hasDefault ? ` ?? ${attr.defaultValue}` : "";
2038
+ initialProps.push(`${attr.key}: __inputAttrs.${attr.key}${defaultExpr}`);
2039
+ }
2040
+ propsLines.push(`let __props = $state({ ${initialProps.join(", ")} })`);
2041
+ for (const attr of attrDetails) {
2042
+ const defaultExpr = attr.hasDefault ? ` ?? ${attr.defaultValue}` : "";
2043
+ const parentLast = `__last_parent_${attr.key}`;
2044
+ const localLast = `__last_local_${attr.key}`;
2045
+ propsLines.push(`let ${parentLast} = __inputAttrs.${attr.key}`, `let ${localLast} = __props.${attr.key}`, `$effect(() => {
2046
+ ` + ` const __parent = __inputAttrs.${attr.key}
2047
+ ` + ` const __local = __props.${attr.key}
2048
+
2049
+ ` + ` // Parent -> local
2050
+ ` + ` if (__parent !== ${parentLast}) {
2051
+ ` + ` ${parentLast} = __parent
2052
+ ` + ` if (__local !== __parent) {
2053
+ ` + ` __props.${attr.key} = __parent${defaultExpr}
2054
+ ` + ` }
2055
+ ` + ` ${localLast} = __props.${attr.key}
2056
+ ` + ` return
2057
+ ` + ` }
2058
+
2059
+ ` + ` // Local -> parent
2060
+ ` + ` if (__local !== ${localLast}) {
2061
+ ` + ` ${localLast} = __local
2062
+ ` + (attr.bindable ? ` try { __inputAttrs.${attr.key} = __local } catch {}
2063
+ ${parentLast} = __inputAttrs.${attr.key}
2064
+ ` : "") + ` }
2065
+ ` + `})`);
2066
+ }
2067
+ } else {
2068
+ propsLines.push("const __props = __inputAttrs");
2069
+ }
2070
+ const propsObjectCode = "\t\t" + propsLines.join(`
2071
+ `);
2062
2072
  if (!body) {
2063
2073
  const baseMembers = [`get ${propName}() { return __props }`];
2064
2074
  if (hasParents)
@@ -2935,6 +2945,9 @@ async function transformScriptContent(source, options) {
2935
2945
  var EXTENSIONS = [".svelte", ".svelte.ts", ".svelte.js", ".js", ".ts"];
2936
2946
  function cleanupForLibrary(code, schemaCache) {
2937
2947
  let result = code;
2948
+ result = result.replace(/(?:var|const|let)\s+\$\w+\s*=\s*globalThis\.\$\w+(?:\s*,\s*\$\w+\s*=\s*globalThis\.\$\w+)*\s*;?\n?/g, "");
2949
+ result = result.replace(/(?:var|const|let)\s*\{\s*\$origin\s*(?:,\s*\$\w+\s*)*\}\s*=\s*globalThis\s*;?\n?/g, "");
2950
+ result = result.replace(/\/\/\s*@ts-ignore\s*-?\s*macros are transformed at build time\n?/g, "");
2938
2951
  result = result.replace(/\ntype \$\$Props = \$attrs\.Of<[^>]+>\n/g, `
2939
2952
  `);
2940
2953
  const attrsOfPattern = /(\}\s*:\s*)\$attrs\.Of<typeof (\w+)>(\s*=\s*\$props\(\))/g;
@@ -3065,10 +3078,12 @@ async function postProcess(options) {
3065
3078
  isComponent,
3066
3079
  schemaResolver
3067
3080
  });
3068
- if (transformed.changed) {
3081
+ let finalCode = cleanupForLibrary(transformed.changed ? transformed.code : source, schemaCache);
3082
+ const sourceToCompare = transformed.changed ? transformed.code : source;
3083
+ const cleanupChanged = finalCode !== sourceToCompare;
3084
+ if (transformed.changed || cleanupChanged) {
3069
3085
  result.filesProcessed++;
3070
3086
  result.files.push(filePath);
3071
- let finalCode = cleanupForLibrary(transformed.code, schemaCache);
3072
3087
  if (!dryRun) {
3073
3088
  fs.writeFileSync(filePath, finalCode, "utf-8");
3074
3089
  }
package/dist/index.js CHANGED
@@ -3475,24 +3475,39 @@ function __attrsFor(factory, rawAttrs) {
3475
3475
  const wrapper = {};
3476
3476
  const localValues = {};
3477
3477
  const hasLocalValue = {};
3478
- for (const key2 of Object.keys(schema)) {
3479
- Object.defineProperty(wrapper, key2, {
3478
+ const defineExposedProp = (key2, info) => {
3479
+ if (Object.prototype.hasOwnProperty.call(wrapper, key2))
3480
+ return;
3481
+ const base = {
3480
3482
  get() {
3481
- if (hasLocalValue[key2]) {
3483
+ if (hasLocalValue[key2])
3482
3484
  return localValues[key2];
3483
- }
3484
3485
  return rawAttrs[key2];
3485
3486
  },
3486
- set(value) {
3487
- localValues[key2] = value;
3488
- hasLocalValue[key2] = true;
3489
- try {
3490
- rawAttrs[key2] = value;
3491
- } catch {}
3492
- },
3493
3487
  enumerable: true,
3494
3488
  configurable: true
3495
- });
3489
+ };
3490
+ if (info.bindable) {
3491
+ Object.defineProperty(wrapper, key2, {
3492
+ ...base,
3493
+ set(value) {
3494
+ localValues[key2] = value;
3495
+ hasLocalValue[key2] = true;
3496
+ try {
3497
+ rawAttrs[key2] = value;
3498
+ } catch {}
3499
+ }
3500
+ });
3501
+ } else {
3502
+ Object.defineProperty(wrapper, key2, base);
3503
+ }
3504
+ };
3505
+ for (const [key2, info] of Object.entries(schema)) {
3506
+ const parentValue = rawAttrs[key2];
3507
+ const shouldExpose = !info.bindable || !info.hasDefault || parentValue !== undefined;
3508
+ if (shouldExpose) {
3509
+ defineExposedProp(key2, info);
3510
+ }
3496
3511
  }
3497
3512
  for (const sym of Object.getOwnPropertySymbols(rawAttrs)) {
3498
3513
  Object.defineProperty(wrapper, sym, {
@@ -3505,34 +3520,57 @@ function __attrsFor(factory, rawAttrs) {
3505
3520
  }
3506
3521
  return new Proxy(wrapper, {
3507
3522
  get(target, prop2) {
3508
- if (prop2 in target) {
3509
- return target[prop2];
3523
+ if (typeof prop2 === "string" && prop2 in schema) {
3524
+ if (hasLocalValue[prop2])
3525
+ return localValues[prop2];
3526
+ return rawAttrs[prop2];
3510
3527
  }
3528
+ if (prop2 in target)
3529
+ return target[prop2];
3511
3530
  return rawAttrs[prop2];
3512
3531
  },
3513
3532
  set(target, prop2, value) {
3533
+ if (typeof prop2 === "string" && prop2 in schema) {
3534
+ localValues[prop2] = value;
3535
+ hasLocalValue[prop2] = true;
3536
+ if (!Object.prototype.hasOwnProperty.call(wrapper, prop2)) {
3537
+ defineExposedProp(prop2, schema[prop2]);
3538
+ }
3539
+ if (schema[prop2].bindable) {
3540
+ try {
3541
+ rawAttrs[prop2] = value;
3542
+ } catch {}
3543
+ }
3544
+ return true;
3545
+ }
3514
3546
  if (prop2 in target) {
3515
- target[prop2] = value;
3547
+ try {
3548
+ target[prop2] = value;
3549
+ } catch {}
3516
3550
  return true;
3517
3551
  }
3518
3552
  try {
3519
3553
  rawAttrs[prop2] = value;
3520
3554
  } catch {
3521
- localValues[prop2] = value;
3522
- hasLocalValue[prop2] = true;
3555
+ if (typeof prop2 === "string") {
3556
+ localValues[prop2] = value;
3557
+ hasLocalValue[prop2] = true;
3558
+ }
3523
3559
  }
3524
3560
  return true;
3525
3561
  },
3526
3562
  has(target, prop2) {
3563
+ if (typeof prop2 === "string" && prop2 in schema) {
3564
+ return prop2 in target || hasLocalValue[prop2];
3565
+ }
3527
3566
  return prop2 in target || prop2 in rawAttrs;
3528
3567
  },
3529
3568
  ownKeys() {
3530
3569
  return [...Object.keys(wrapper), ...Object.getOwnPropertySymbols(rawAttrs)];
3531
3570
  },
3532
3571
  getOwnPropertyDescriptor(target, prop2) {
3533
- if (prop2 in target) {
3572
+ if (prop2 in target)
3534
3573
  return Object.getOwnPropertyDescriptor(target, prop2);
3535
- }
3536
3574
  return Object.getOwnPropertyDescriptor(rawAttrs, prop2);
3537
3575
  }
3538
3576
  });
@@ -6077,38 +6115,48 @@ ${createFnBody}
6077
6115
  }`;
6078
6116
  return configCode;
6079
6117
  }
6080
- function transformOriginBody(content, parents = [], attrDetails = [], propName = "props", svelteImports) {
6118
+ function transformOriginBody(content, parents = [], attrDetails = [], propName = "props", _svelteImports) {
6081
6119
  let body = content.trim();
6082
6120
  const hasParents = parents.length > 0;
6083
- const attrStateDeclarations = [];
6084
- const attrSyncEffects = [];
6085
- const attrGettersSetters = [];
6086
- if (attrDetails.length > 0 && svelteImports) {
6087
- svelteImports.add("untrack");
6088
- }
6089
- for (const attr2 of attrDetails) {
6090
- const defaultExpr = attr2.hasDefault ? ` ?? ${attr2.defaultValue}` : "";
6091
- attrStateDeclarations.push(`let __attr_${attr2.key} = $state(__inputAttrs.${attr2.key}${defaultExpr})`);
6092
- attrSyncEffects.push(`$effect(() => {
6093
- const __parentVal = __inputAttrs.${attr2.key}
6094
- const __currentVal = untrack(() => __attr_${attr2.key})
6095
- if (__parentVal !== __currentVal) {
6096
- __attr_${attr2.key} = __parentVal${defaultExpr}
6097
- }
6098
- })`);
6099
- attrGettersSetters.push(`get ${attr2.key}() { return __attr_${attr2.key} }`);
6100
- attrGettersSetters.push(`set ${attr2.key}(v) { __attr_${attr2.key} = v; __inputAttrs.${attr2.key} = v }`);
6101
- }
6102
- const attrStateSection = attrStateDeclarations.length > 0 ? attrStateDeclarations.join(`
6103
- `) + `
6104
- ` : "";
6105
- const attrEffectSection = attrSyncEffects.length > 0 ? attrSyncEffects.join(`
6106
- `) + `
6107
- ` : "";
6108
- const propsObjectCode = attrGettersSetters.length > 0 ? `${attrStateSection}${attrEffectSection}const __props = {
6109
- ${attrGettersSetters.join(`,
6110
- `)}
6111
- }` : ` const __props = __inputAttrs`;
6121
+ const propsLines = [];
6122
+ if (attrDetails.length > 0) {
6123
+ const initialProps = [];
6124
+ for (const attr2 of attrDetails) {
6125
+ const defaultExpr = attr2.hasDefault ? ` ?? ${attr2.defaultValue}` : "";
6126
+ initialProps.push(`${attr2.key}: __inputAttrs.${attr2.key}${defaultExpr}`);
6127
+ }
6128
+ propsLines.push(`let __props = $state({ ${initialProps.join(", ")} })`);
6129
+ for (const attr2 of attrDetails) {
6130
+ const defaultExpr = attr2.hasDefault ? ` ?? ${attr2.defaultValue}` : "";
6131
+ const parentLast = `__last_parent_${attr2.key}`;
6132
+ const localLast = `__last_local_${attr2.key}`;
6133
+ propsLines.push(`let ${parentLast} = __inputAttrs.${attr2.key}`, `let ${localLast} = __props.${attr2.key}`, `$effect(() => {
6134
+ ` + ` const __parent = __inputAttrs.${attr2.key}
6135
+ ` + ` const __local = __props.${attr2.key}
6136
+
6137
+ ` + ` // Parent -> local
6138
+ ` + ` if (__parent !== ${parentLast}) {
6139
+ ` + ` ${parentLast} = __parent
6140
+ ` + ` if (__local !== __parent) {
6141
+ ` + ` __props.${attr2.key} = __parent${defaultExpr}
6142
+ ` + ` }
6143
+ ` + ` ${localLast} = __props.${attr2.key}
6144
+ ` + ` return
6145
+ ` + ` }
6146
+
6147
+ ` + ` // Local -> parent
6148
+ ` + ` if (__local !== ${localLast}) {
6149
+ ` + ` ${localLast} = __local
6150
+ ` + (attr2.bindable ? ` try { __inputAttrs.${attr2.key} = __local } catch {}
6151
+ ${parentLast} = __inputAttrs.${attr2.key}
6152
+ ` : "") + ` }
6153
+ ` + `})`);
6154
+ }
6155
+ } else {
6156
+ propsLines.push("const __props = __inputAttrs");
6157
+ }
6158
+ const propsObjectCode = "\t\t" + propsLines.join(`
6159
+ `);
6112
6160
  if (!body) {
6113
6161
  const baseMembers = [`get ${propName}() { return __props }`];
6114
6162
  if (hasParents)
package/dist/plugin.js CHANGED
@@ -2021,38 +2021,48 @@ ${createFnBody}
2021
2021
  }`;
2022
2022
  return configCode;
2023
2023
  }
2024
- function transformOriginBody(content, parents = [], attrDetails = [], propName = "props", svelteImports) {
2024
+ function transformOriginBody(content, parents = [], attrDetails = [], propName = "props", _svelteImports) {
2025
2025
  let body = content.trim();
2026
2026
  const hasParents = parents.length > 0;
2027
- const attrStateDeclarations = [];
2028
- const attrSyncEffects = [];
2029
- const attrGettersSetters = [];
2030
- if (attrDetails.length > 0 && svelteImports) {
2031
- svelteImports.add("untrack");
2032
- }
2033
- for (const attr of attrDetails) {
2034
- const defaultExpr = attr.hasDefault ? ` ?? ${attr.defaultValue}` : "";
2035
- attrStateDeclarations.push(`let __attr_${attr.key} = $state(__inputAttrs.${attr.key}${defaultExpr})`);
2036
- attrSyncEffects.push(`$effect(() => {
2037
- const __parentVal = __inputAttrs.${attr.key}
2038
- const __currentVal = untrack(() => __attr_${attr.key})
2039
- if (__parentVal !== __currentVal) {
2040
- __attr_${attr.key} = __parentVal${defaultExpr}
2041
- }
2042
- })`);
2043
- attrGettersSetters.push(`get ${attr.key}() { return __attr_${attr.key} }`);
2044
- attrGettersSetters.push(`set ${attr.key}(v) { __attr_${attr.key} = v; __inputAttrs.${attr.key} = v }`);
2045
- }
2046
- const attrStateSection = attrStateDeclarations.length > 0 ? attrStateDeclarations.join(`
2047
- `) + `
2048
- ` : "";
2049
- const attrEffectSection = attrSyncEffects.length > 0 ? attrSyncEffects.join(`
2050
- `) + `
2051
- ` : "";
2052
- const propsObjectCode = attrGettersSetters.length > 0 ? `${attrStateSection}${attrEffectSection}const __props = {
2053
- ${attrGettersSetters.join(`,
2054
- `)}
2055
- }` : ` const __props = __inputAttrs`;
2027
+ const propsLines = [];
2028
+ if (attrDetails.length > 0) {
2029
+ const initialProps = [];
2030
+ for (const attr of attrDetails) {
2031
+ const defaultExpr = attr.hasDefault ? ` ?? ${attr.defaultValue}` : "";
2032
+ initialProps.push(`${attr.key}: __inputAttrs.${attr.key}${defaultExpr}`);
2033
+ }
2034
+ propsLines.push(`let __props = $state({ ${initialProps.join(", ")} })`);
2035
+ for (const attr of attrDetails) {
2036
+ const defaultExpr = attr.hasDefault ? ` ?? ${attr.defaultValue}` : "";
2037
+ const parentLast = `__last_parent_${attr.key}`;
2038
+ const localLast = `__last_local_${attr.key}`;
2039
+ propsLines.push(`let ${parentLast} = __inputAttrs.${attr.key}`, `let ${localLast} = __props.${attr.key}`, `$effect(() => {
2040
+ ` + ` const __parent = __inputAttrs.${attr.key}
2041
+ ` + ` const __local = __props.${attr.key}
2042
+
2043
+ ` + ` // Parent -> local
2044
+ ` + ` if (__parent !== ${parentLast}) {
2045
+ ` + ` ${parentLast} = __parent
2046
+ ` + ` if (__local !== __parent) {
2047
+ ` + ` __props.${attr.key} = __parent${defaultExpr}
2048
+ ` + ` }
2049
+ ` + ` ${localLast} = __props.${attr.key}
2050
+ ` + ` return
2051
+ ` + ` }
2052
+
2053
+ ` + ` // Local -> parent
2054
+ ` + ` if (__local !== ${localLast}) {
2055
+ ` + ` ${localLast} = __local
2056
+ ` + (attr.bindable ? ` try { __inputAttrs.${attr.key} = __local } catch {}
2057
+ ${parentLast} = __inputAttrs.${attr.key}
2058
+ ` : "") + ` }
2059
+ ` + `})`);
2060
+ }
2061
+ } else {
2062
+ propsLines.push("const __props = __inputAttrs");
2063
+ }
2064
+ const propsObjectCode = "\t\t" + propsLines.join(`
2065
+ `);
2056
2066
  if (!body) {
2057
2067
  const baseMembers = [`get ${propName}() { return __props }`];
2058
2068
  if (hasParents)
@@ -2025,38 +2025,48 @@ ${createFnBody}
2025
2025
  }`;
2026
2026
  return configCode;
2027
2027
  }
2028
- function transformOriginBody(content, parents = [], attrDetails = [], propName = "props", svelteImports) {
2028
+ function transformOriginBody(content, parents = [], attrDetails = [], propName = "props", _svelteImports) {
2029
2029
  let body = content.trim();
2030
2030
  const hasParents = parents.length > 0;
2031
- const attrStateDeclarations = [];
2032
- const attrSyncEffects = [];
2033
- const attrGettersSetters = [];
2034
- if (attrDetails.length > 0 && svelteImports) {
2035
- svelteImports.add("untrack");
2036
- }
2037
- for (const attr of attrDetails) {
2038
- const defaultExpr = attr.hasDefault ? ` ?? ${attr.defaultValue}` : "";
2039
- attrStateDeclarations.push(`let __attr_${attr.key} = $state(__inputAttrs.${attr.key}${defaultExpr})`);
2040
- attrSyncEffects.push(`$effect(() => {
2041
- const __parentVal = __inputAttrs.${attr.key}
2042
- const __currentVal = untrack(() => __attr_${attr.key})
2043
- if (__parentVal !== __currentVal) {
2044
- __attr_${attr.key} = __parentVal${defaultExpr}
2045
- }
2046
- })`);
2047
- attrGettersSetters.push(`get ${attr.key}() { return __attr_${attr.key} }`);
2048
- attrGettersSetters.push(`set ${attr.key}(v) { __attr_${attr.key} = v; __inputAttrs.${attr.key} = v }`);
2049
- }
2050
- const attrStateSection = attrStateDeclarations.length > 0 ? attrStateDeclarations.join(`
2051
- `) + `
2052
- ` : "";
2053
- const attrEffectSection = attrSyncEffects.length > 0 ? attrSyncEffects.join(`
2054
- `) + `
2055
- ` : "";
2056
- const propsObjectCode = attrGettersSetters.length > 0 ? `${attrStateSection}${attrEffectSection}const __props = {
2057
- ${attrGettersSetters.join(`,
2058
- `)}
2059
- }` : ` const __props = __inputAttrs`;
2031
+ const propsLines = [];
2032
+ if (attrDetails.length > 0) {
2033
+ const initialProps = [];
2034
+ for (const attr of attrDetails) {
2035
+ const defaultExpr = attr.hasDefault ? ` ?? ${attr.defaultValue}` : "";
2036
+ initialProps.push(`${attr.key}: __inputAttrs.${attr.key}${defaultExpr}`);
2037
+ }
2038
+ propsLines.push(`let __props = $state({ ${initialProps.join(", ")} })`);
2039
+ for (const attr of attrDetails) {
2040
+ const defaultExpr = attr.hasDefault ? ` ?? ${attr.defaultValue}` : "";
2041
+ const parentLast = `__last_parent_${attr.key}`;
2042
+ const localLast = `__last_local_${attr.key}`;
2043
+ propsLines.push(`let ${parentLast} = __inputAttrs.${attr.key}`, `let ${localLast} = __props.${attr.key}`, `$effect(() => {
2044
+ ` + ` const __parent = __inputAttrs.${attr.key}
2045
+ ` + ` const __local = __props.${attr.key}
2046
+
2047
+ ` + ` // Parent -> local
2048
+ ` + ` if (__parent !== ${parentLast}) {
2049
+ ` + ` ${parentLast} = __parent
2050
+ ` + ` if (__local !== __parent) {
2051
+ ` + ` __props.${attr.key} = __parent${defaultExpr}
2052
+ ` + ` }
2053
+ ` + ` ${localLast} = __props.${attr.key}
2054
+ ` + ` return
2055
+ ` + ` }
2056
+
2057
+ ` + ` // Local -> parent
2058
+ ` + ` if (__local !== ${localLast}) {
2059
+ ` + ` ${localLast} = __local
2060
+ ` + (attr.bindable ? ` try { __inputAttrs.${attr.key} = __local } catch {}
2061
+ ${parentLast} = __inputAttrs.${attr.key}
2062
+ ` : "") + ` }
2063
+ ` + `})`);
2064
+ }
2065
+ } else {
2066
+ propsLines.push("const __props = __inputAttrs");
2067
+ }
2068
+ const propsObjectCode = "\t\t" + propsLines.join(`
2069
+ `);
2060
2070
  if (!body) {
2061
2071
  const baseMembers = [`get ${propName}() { return __props }`];
2062
2072
  if (hasParents)
@@ -2933,6 +2943,9 @@ async function transformScriptContent(source, options) {
2933
2943
  var EXTENSIONS = [".svelte", ".svelte.ts", ".svelte.js", ".js", ".ts"];
2934
2944
  function cleanupForLibrary(code, schemaCache) {
2935
2945
  let result = code;
2946
+ result = result.replace(/(?:var|const|let)\s+\$\w+\s*=\s*globalThis\.\$\w+(?:\s*,\s*\$\w+\s*=\s*globalThis\.\$\w+)*\s*;?\n?/g, "");
2947
+ result = result.replace(/(?:var|const|let)\s*\{\s*\$origin\s*(?:,\s*\$\w+\s*)*\}\s*=\s*globalThis\s*;?\n?/g, "");
2948
+ result = result.replace(/\/\/\s*@ts-ignore\s*-?\s*macros are transformed at build time\n?/g, "");
2936
2949
  result = result.replace(/\ntype \$\$Props = \$attrs\.Of<[^>]+>\n/g, `
2937
2950
  `);
2938
2951
  const attrsOfPattern = /(\}\s*:\s*)\$attrs\.Of<typeof (\w+)>(\s*=\s*\$props\(\))/g;
@@ -3063,10 +3076,12 @@ async function postProcess(options) {
3063
3076
  isComponent,
3064
3077
  schemaResolver
3065
3078
  });
3066
- if (transformed.changed) {
3079
+ let finalCode = cleanupForLibrary(transformed.changed ? transformed.code : source, schemaCache);
3080
+ const sourceToCompare = transformed.changed ? transformed.code : source;
3081
+ const cleanupChanged = finalCode !== sourceToCompare;
3082
+ if (transformed.changed || cleanupChanged) {
3067
3083
  result.filesProcessed++;
3068
3084
  result.files.push(filePath);
3069
- let finalCode = cleanupForLibrary(transformed.code, schemaCache);
3070
3085
  if (!dryRun) {
3071
3086
  fs.writeFileSync(filePath, finalCode, "utf-8");
3072
3087
  }
@@ -2021,38 +2021,48 @@ ${createFnBody}
2021
2021
  }`;
2022
2022
  return configCode;
2023
2023
  }
2024
- function transformOriginBody(content, parents = [], attrDetails = [], propName = "props", svelteImports) {
2024
+ function transformOriginBody(content, parents = [], attrDetails = [], propName = "props", _svelteImports) {
2025
2025
  let body = content.trim();
2026
2026
  const hasParents = parents.length > 0;
2027
- const attrStateDeclarations = [];
2028
- const attrSyncEffects = [];
2029
- const attrGettersSetters = [];
2030
- if (attrDetails.length > 0 && svelteImports) {
2031
- svelteImports.add("untrack");
2032
- }
2033
- for (const attr of attrDetails) {
2034
- const defaultExpr = attr.hasDefault ? ` ?? ${attr.defaultValue}` : "";
2035
- attrStateDeclarations.push(`let __attr_${attr.key} = $state(__inputAttrs.${attr.key}${defaultExpr})`);
2036
- attrSyncEffects.push(`$effect(() => {
2037
- const __parentVal = __inputAttrs.${attr.key}
2038
- const __currentVal = untrack(() => __attr_${attr.key})
2039
- if (__parentVal !== __currentVal) {
2040
- __attr_${attr.key} = __parentVal${defaultExpr}
2041
- }
2042
- })`);
2043
- attrGettersSetters.push(`get ${attr.key}() { return __attr_${attr.key} }`);
2044
- attrGettersSetters.push(`set ${attr.key}(v) { __attr_${attr.key} = v; __inputAttrs.${attr.key} = v }`);
2045
- }
2046
- const attrStateSection = attrStateDeclarations.length > 0 ? attrStateDeclarations.join(`
2047
- `) + `
2048
- ` : "";
2049
- const attrEffectSection = attrSyncEffects.length > 0 ? attrSyncEffects.join(`
2050
- `) + `
2051
- ` : "";
2052
- const propsObjectCode = attrGettersSetters.length > 0 ? `${attrStateSection}${attrEffectSection}const __props = {
2053
- ${attrGettersSetters.join(`,
2054
- `)}
2055
- }` : ` const __props = __inputAttrs`;
2027
+ const propsLines = [];
2028
+ if (attrDetails.length > 0) {
2029
+ const initialProps = [];
2030
+ for (const attr of attrDetails) {
2031
+ const defaultExpr = attr.hasDefault ? ` ?? ${attr.defaultValue}` : "";
2032
+ initialProps.push(`${attr.key}: __inputAttrs.${attr.key}${defaultExpr}`);
2033
+ }
2034
+ propsLines.push(`let __props = $state({ ${initialProps.join(", ")} })`);
2035
+ for (const attr of attrDetails) {
2036
+ const defaultExpr = attr.hasDefault ? ` ?? ${attr.defaultValue}` : "";
2037
+ const parentLast = `__last_parent_${attr.key}`;
2038
+ const localLast = `__last_local_${attr.key}`;
2039
+ propsLines.push(`let ${parentLast} = __inputAttrs.${attr.key}`, `let ${localLast} = __props.${attr.key}`, `$effect(() => {
2040
+ ` + ` const __parent = __inputAttrs.${attr.key}
2041
+ ` + ` const __local = __props.${attr.key}
2042
+
2043
+ ` + ` // Parent -> local
2044
+ ` + ` if (__parent !== ${parentLast}) {
2045
+ ` + ` ${parentLast} = __parent
2046
+ ` + ` if (__local !== __parent) {
2047
+ ` + ` __props.${attr.key} = __parent${defaultExpr}
2048
+ ` + ` }
2049
+ ` + ` ${localLast} = __props.${attr.key}
2050
+ ` + ` return
2051
+ ` + ` }
2052
+
2053
+ ` + ` // Local -> parent
2054
+ ` + ` if (__local !== ${localLast}) {
2055
+ ` + ` ${localLast} = __local
2056
+ ` + (attr.bindable ? ` try { __inputAttrs.${attr.key} = __local } catch {}
2057
+ ${parentLast} = __inputAttrs.${attr.key}
2058
+ ` : "") + ` }
2059
+ ` + `})`);
2060
+ }
2061
+ } else {
2062
+ propsLines.push("const __props = __inputAttrs");
2063
+ }
2064
+ const propsObjectCode = "\t\t" + propsLines.join(`
2065
+ `);
2056
2066
  if (!body) {
2057
2067
  const baseMembers = [`get ${propName}() { return __props }`];
2058
2068
  if (hasParents)
@@ -3475,24 +3475,39 @@ function __attrsFor(factory, rawAttrs) {
3475
3475
  const wrapper = {};
3476
3476
  const localValues = {};
3477
3477
  const hasLocalValue = {};
3478
- for (const key2 of Object.keys(schema)) {
3479
- Object.defineProperty(wrapper, key2, {
3478
+ const defineExposedProp = (key2, info) => {
3479
+ if (Object.prototype.hasOwnProperty.call(wrapper, key2))
3480
+ return;
3481
+ const base = {
3480
3482
  get() {
3481
- if (hasLocalValue[key2]) {
3483
+ if (hasLocalValue[key2])
3482
3484
  return localValues[key2];
3483
- }
3484
3485
  return rawAttrs[key2];
3485
3486
  },
3486
- set(value) {
3487
- localValues[key2] = value;
3488
- hasLocalValue[key2] = true;
3489
- try {
3490
- rawAttrs[key2] = value;
3491
- } catch {}
3492
- },
3493
3487
  enumerable: true,
3494
3488
  configurable: true
3495
- });
3489
+ };
3490
+ if (info.bindable) {
3491
+ Object.defineProperty(wrapper, key2, {
3492
+ ...base,
3493
+ set(value) {
3494
+ localValues[key2] = value;
3495
+ hasLocalValue[key2] = true;
3496
+ try {
3497
+ rawAttrs[key2] = value;
3498
+ } catch {}
3499
+ }
3500
+ });
3501
+ } else {
3502
+ Object.defineProperty(wrapper, key2, base);
3503
+ }
3504
+ };
3505
+ for (const [key2, info] of Object.entries(schema)) {
3506
+ const parentValue = rawAttrs[key2];
3507
+ const shouldExpose = !info.bindable || !info.hasDefault || parentValue !== undefined;
3508
+ if (shouldExpose) {
3509
+ defineExposedProp(key2, info);
3510
+ }
3496
3511
  }
3497
3512
  for (const sym of Object.getOwnPropertySymbols(rawAttrs)) {
3498
3513
  Object.defineProperty(wrapper, sym, {
@@ -3505,34 +3520,57 @@ function __attrsFor(factory, rawAttrs) {
3505
3520
  }
3506
3521
  return new Proxy(wrapper, {
3507
3522
  get(target, prop2) {
3508
- if (prop2 in target) {
3509
- return target[prop2];
3523
+ if (typeof prop2 === "string" && prop2 in schema) {
3524
+ if (hasLocalValue[prop2])
3525
+ return localValues[prop2];
3526
+ return rawAttrs[prop2];
3510
3527
  }
3528
+ if (prop2 in target)
3529
+ return target[prop2];
3511
3530
  return rawAttrs[prop2];
3512
3531
  },
3513
3532
  set(target, prop2, value) {
3533
+ if (typeof prop2 === "string" && prop2 in schema) {
3534
+ localValues[prop2] = value;
3535
+ hasLocalValue[prop2] = true;
3536
+ if (!Object.prototype.hasOwnProperty.call(wrapper, prop2)) {
3537
+ defineExposedProp(prop2, schema[prop2]);
3538
+ }
3539
+ if (schema[prop2].bindable) {
3540
+ try {
3541
+ rawAttrs[prop2] = value;
3542
+ } catch {}
3543
+ }
3544
+ return true;
3545
+ }
3514
3546
  if (prop2 in target) {
3515
- target[prop2] = value;
3547
+ try {
3548
+ target[prop2] = value;
3549
+ } catch {}
3516
3550
  return true;
3517
3551
  }
3518
3552
  try {
3519
3553
  rawAttrs[prop2] = value;
3520
3554
  } catch {
3521
- localValues[prop2] = value;
3522
- hasLocalValue[prop2] = true;
3555
+ if (typeof prop2 === "string") {
3556
+ localValues[prop2] = value;
3557
+ hasLocalValue[prop2] = true;
3558
+ }
3523
3559
  }
3524
3560
  return true;
3525
3561
  },
3526
3562
  has(target, prop2) {
3563
+ if (typeof prop2 === "string" && prop2 in schema) {
3564
+ return prop2 in target || hasLocalValue[prop2];
3565
+ }
3527
3566
  return prop2 in target || prop2 in rawAttrs;
3528
3567
  },
3529
3568
  ownKeys() {
3530
3569
  return [...Object.keys(wrapper), ...Object.getOwnPropertySymbols(rawAttrs)];
3531
3570
  },
3532
3571
  getOwnPropertyDescriptor(target, prop2) {
3533
- if (prop2 in target) {
3572
+ if (prop2 in target)
3534
3573
  return Object.getOwnPropertyDescriptor(target, prop2);
3535
- }
3536
3574
  return Object.getOwnPropertyDescriptor(rawAttrs, prop2);
3537
3575
  }
3538
3576
  });
@@ -75,7 +75,7 @@ export type UnwrapBindable<T> = T extends Bindable<infer U> ? U : T;
75
75
  */
76
76
  export interface OriginFactory<TInstance = unknown, TAttrs = Record<string, any>> {
77
77
  /** Create an instance with the given reactive attrs and optional init args */
78
- (attrs?: TAttrs | null, ...initArgs: any[]): TInstance;
78
+ (attrs?: TAttrs | null, ...initArgs: any[]): TInstance & OriginInstanceExtras;
79
79
  /** Marker to identify origin factories */
80
80
  readonly __origin: true;
81
81
  /** The attr schema extracted from the definition */
@@ -113,13 +113,33 @@ export interface AttrsFactory<_TAttrs = Record<string, any>> {
113
113
  /** Parent attr factories (for inheritance) */
114
114
  readonly __parents: AttrsFactory<any>[];
115
115
  }
116
+ /**
117
+ * Attachment registry returned by $attachments getter.
118
+ * Maps Symbol keys to attachment functions.
119
+ */
120
+ export type AttachmentRegistry = Record<symbol, (element: Element) => void | (() => void)>;
121
+ /**
122
+ * Properties added to all origin instances at runtime.
123
+ * These are injected by __createOrigin after __create returns.
124
+ */
125
+ export interface OriginInstanceExtras {
126
+ /**
127
+ * Attachment functions that can be spread onto elements.
128
+ * Returns an object with Symbol keys mapping to attachment functions.
129
+ *
130
+ * @example
131
+ * <div {...instance.$attachments}>
132
+ */
133
+ readonly $attachments: AttachmentRegistry;
134
+ }
116
135
  /**
117
136
  * The origin instance has:
118
137
  * - attrs: reactive attrs object (passed in)
119
138
  * - super: parent instance (if extending)
139
+ * - $attachments: attachment registry (injected by runtime)
120
140
  * - All other members from the definition (state, getters, methods)
121
141
  */
122
- export interface OriginInstance<TAttrs = Record<string, any>> {
142
+ export interface OriginInstance<TAttrs = Record<string, any>> extends OriginInstanceExtras {
123
143
  /** Reactive attrs object */
124
144
  readonly attrs: TAttrs;
125
145
  /** Parent instance (if extending) */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-origin",
3
- "version": "1.0.0-next.16",
3
+ "version": "1.0.0-next.18",
4
4
  "description": "Compiler-assisted state and prop ergonomics for Svelte 5",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",