@farm.js/react 0.1.0-beta.39 → 0.1.0-beta.40

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/vite.mjs CHANGED
@@ -8,7 +8,6 @@ import { resolve, dirname, relative, sep, isAbsolute } from 'path';
8
8
  var DELEGATABLE_KEYED_ROW_EVENTS = /* @__PURE__ */ new Set([
9
9
  "onBeforeInput",
10
10
  "onBlur",
11
- "onChange",
12
11
  "onClick",
13
12
  "onCompositionEnd",
14
13
  "onCompositionStart",
@@ -50,16 +49,17 @@ var DELEGATABLE_KEYED_ROW_EVENTS = /* @__PURE__ */ new Set([
50
49
  "onTouchStart",
51
50
  "onWheel"
52
51
  ]);
53
- function canDelegateKeyedRowEvents(plan) {
52
+ function canDelegateKeyedRowEvents(plan, withoutReact = false) {
54
53
  if (plan.events.length === 0 || plan.conditionals.length > 0) return false;
55
54
  if (plan.source.openingElement.attributes.some(
56
55
  (attribute) => types.isJSXAttribute(attribute) && types.isJSXIdentifier(attribute.name) && /^on[A-Z]/.test(attribute.name.name)
57
56
  )) {
58
57
  return false;
59
58
  }
60
- return plan.events.every(
61
- (event) => DELEGATABLE_KEYED_ROW_EVENTS.has(event.name.replace(/Capture$/, ""))
62
- );
59
+ return plan.events.every((event) => {
60
+ const name = event.name.replace(/Capture$/, "");
61
+ return DELEGATABLE_KEYED_ROW_EVENTS.has(name) || withoutReact && name === "onChange";
62
+ });
63
63
  }
64
64
  var COMPILER_RUNTIME_FEATURE_EXPORTS = {
65
65
  conditional: "conditionalRuntimeFeature",
@@ -563,9 +563,14 @@ function validateDerivedExpression(expression, safeGlobals, additionalSafeCall)
563
563
  AssignmentExpression: (path) => reject("assignments", path),
564
564
  AwaitExpression: (path) => reject("await expressions", path),
565
565
  CallExpression(path) {
566
- if (!isSafeCompilerCall(path.node, safeGlobals) && !additionalSafeCall?.(path.node)) {
567
- reject("function calls", path);
566
+ if (isSafeCompilerCall(path.node, safeGlobals) || additionalSafeCall?.(path.node)) return;
567
+ if (isCollectionPipelineLength(path)) {
568
+ const unsupported2 = validateCollectionPipeline(path.node, safeGlobals);
569
+ if (unsupported2) reject(unsupported2, path);
570
+ else path.skip();
571
+ return;
568
572
  }
573
+ reject("function calls", path);
569
574
  },
570
575
  ClassExpression: (path) => reject("class expressions", path),
571
576
  Function: (path) => reject("function expressions", path),
@@ -3029,6 +3034,173 @@ function validateCollectionCallback(callback, method, safeGlobals) {
3029
3034
  const unsupported = validateDerivedExpression(value, safeGlobals);
3030
3035
  return unsupported ? `${method} callbacks cannot use ${unsupported}` : void 0;
3031
3036
  }
3037
+ function isPrimitiveLiteral(node) {
3038
+ return types.isStringLiteral(node) || types.isNumericLiteral(node) || types.isBooleanLiteral(node) || types.isNullLiteral(node);
3039
+ }
3040
+ function inlinableChild(name, programPath) {
3041
+ const binding = programPath.scope.getBinding(name);
3042
+ if (!binding?.constant || binding.kind !== "hoisted" && binding.kind !== "const") return;
3043
+ const referencedOnlyAsTag = binding.referencePaths.every(
3044
+ (reference) => reference.isJSXIdentifier() || reference.parentPath?.isExportSpecifier() || reference.parentPath?.isExportDefaultDeclaration()
3045
+ );
3046
+ if (!referencedOnlyAsTag) return;
3047
+ const functionPath = binding.path.isFunctionDeclaration() ? binding.path : binding.path.isVariableDeclarator() ? binding.path.get("init") : void 0;
3048
+ if (!functionPath || Array.isArray(functionPath) || !(functionPath.isFunctionDeclaration() || functionPath.isArrowFunctionExpression() || functionPath.isFunctionExpression())) {
3049
+ return;
3050
+ }
3051
+ const fn = functionPath.node;
3052
+ if (fn.async || fn.generator || fn.params.length > 1) return;
3053
+ const props = /* @__PURE__ */ new Map();
3054
+ const param = fn.params[0];
3055
+ if (param) {
3056
+ if (!types.isObjectPattern(param)) return;
3057
+ for (const property of param.properties) {
3058
+ if (!types.isObjectProperty(property) || property.computed || !types.isIdentifier(property.key) || !types.isIdentifier(property.value) || property.key.name === "children") {
3059
+ return;
3060
+ }
3061
+ props.set(property.key.name, property.value.name);
3062
+ }
3063
+ }
3064
+ const body = fn.body;
3065
+ const returned = types.isJSXElement(body) ? body : types.isBlockStatement(body) && body.body.length === 1 && types.isReturnStatement(body.body[0]) && types.isJSXElement(body.body[0].argument) ? body.body[0].argument : void 0;
3066
+ if (!returned) return;
3067
+ let unsafe = false;
3068
+ const localNames = /* @__PURE__ */ new Set();
3069
+ const freeNames = /* @__PURE__ */ new Set();
3070
+ functionPath.traverse({
3071
+ CallExpression(path) {
3072
+ const callee = path.node.callee;
3073
+ const calleeName = types.isIdentifier(callee) ? callee.name : types.isMemberExpression(callee) && !callee.computed && types.isIdentifier(callee.property) ? callee.property.name : "";
3074
+ if (/^use([A-Z0-9]|$)/.test(calleeName)) unsafe = true;
3075
+ },
3076
+ ThisExpression() {
3077
+ unsafe = true;
3078
+ },
3079
+ Identifier(path) {
3080
+ if (path.node.name === "arguments") unsafe = true;
3081
+ },
3082
+ JSXIdentifier(path) {
3083
+ if (path.node.name === name) unsafe = true;
3084
+ },
3085
+ Scopable(path) {
3086
+ for (const local of Object.keys(path.scope.bindings)) localNames.add(local);
3087
+ },
3088
+ ReferencedIdentifier(path) {
3089
+ const reference = path.node.name;
3090
+ const owner = path.scope.getBinding(reference);
3091
+ if (!owner || owner.scope === programPath.scope) freeNames.add(reference);
3092
+ }
3093
+ });
3094
+ if (unsafe) return;
3095
+ for (const local of props.values()) localNames.delete(local);
3096
+ return { props, root: returned, scope: functionPath.scope, localNames, freeNames };
3097
+ }
3098
+ function isDuplicableProp(expression, safeGlobals) {
3099
+ return types.isArrowFunctionExpression(expression) || types.isFunctionExpression(expression) || validateDerivedExpression(expression, safeGlobals) === void 0;
3100
+ }
3101
+ function inlineStatelessChildren(component, programPath, safeGlobals) {
3102
+ let budget = 64;
3103
+ component.traverse({
3104
+ JSXElement(path) {
3105
+ const tag = path.node.openingElement.name;
3106
+ if (!types.isJSXIdentifier(tag) || !isComponentName(tag.name) || budget <= 0) return;
3107
+ if (path.scope.getBinding(tag.name) !== programPath.scope.getBinding(tag.name)) return;
3108
+ const child = inlinableChild(tag.name, programPath);
3109
+ if (!child || meaningfulJsxChildren(path.node).length > 0) return;
3110
+ const values = /* @__PURE__ */ new Map();
3111
+ for (const attribute of path.node.openingElement.attributes) {
3112
+ if (!types.isJSXAttribute(attribute) || !types.isJSXIdentifier(attribute.name)) return;
3113
+ const prop = attribute.name.name;
3114
+ if (prop === "key" || prop === "ref" || prop === "children") return;
3115
+ const value = attribute.value;
3116
+ const expression = value === null || value === void 0 ? types.booleanLiteral(true) : types.isStringLiteral(value) ? types.stringLiteral(value.value) : types.isJSXExpressionContainer(value) && types.isExpression(value.expression) ? value.expression : void 0;
3117
+ if (!expression) return;
3118
+ values.set(prop, expression);
3119
+ }
3120
+ for (const [prop, expression] of values) {
3121
+ if (!child.props.has(prop) && !isDuplicableProp(expression, safeGlobals)) return;
3122
+ }
3123
+ for (const name of child.freeNames) {
3124
+ if (child.props.has(name) || [...child.props.values()].includes(name)) continue;
3125
+ if (path.scope.getBinding(name) !== child.scope.getBinding(name)) return;
3126
+ }
3127
+ const root = types.cloneNode(child.root, true);
3128
+ const file = expressionFile(root);
3129
+ const counts = /* @__PURE__ */ new Map();
3130
+ const localToProp = new Map([...child.props].map(([prop, local]) => [local, prop]));
3131
+ let rejected = false;
3132
+ traverse(file, {
3133
+ ReferencedIdentifier(reference) {
3134
+ const prop = localToProp.get(reference.node.name);
3135
+ if (!prop || reference.scope.hasBinding(reference.node.name, { noGlobals: true })) return;
3136
+ if (types.isJSXIdentifier(reference.node)) rejected = true;
3137
+ counts.set(prop, (counts.get(prop) ?? 0) + 1);
3138
+ }
3139
+ });
3140
+ for (const [prop, count] of counts) {
3141
+ const value = values.get(prop);
3142
+ if (!value) continue;
3143
+ if (count > 1 && !isDuplicableProp(value, safeGlobals)) rejected = true;
3144
+ traverse(expressionFile(types.cloneNode(value, true)), {
3145
+ ReferencedIdentifier(reference) {
3146
+ if (child.localNames.has(reference.node.name)) rejected = true;
3147
+ }
3148
+ });
3149
+ }
3150
+ if (rejected) return;
3151
+ traverse(file, {
3152
+ ReferencedIdentifier(reference) {
3153
+ const prop = localToProp.get(reference.node.name);
3154
+ if (!prop || reference.scope.hasBinding(reference.node.name, { noGlobals: true })) return;
3155
+ const value = values.get(prop);
3156
+ const replacement = value ? types.cloneNode(value, true) : types.unaryExpression("void", types.numericLiteral(0));
3157
+ reference.replaceWith(replacement);
3158
+ reference.skip();
3159
+ }
3160
+ });
3161
+ budget -= 1;
3162
+ path.replaceWith(file.program.body[0].expression);
3163
+ }
3164
+ });
3165
+ }
3166
+ function hoistConstantListCollections(component, programPath) {
3167
+ const hoisted = [];
3168
+ component.traverse({
3169
+ CallExpression(path) {
3170
+ if (!isMapCall(path.node) || !path.findParent((parent) => parent.isJSXExpressionContainer()))
3171
+ return;
3172
+ const calls = [path.node];
3173
+ let source = path.node.callee.object;
3174
+ while (collectionPipelineMethod(source)) {
3175
+ calls.push(source);
3176
+ source = source.callee.object;
3177
+ }
3178
+ if (!types.isArrayExpression(source) || !source.elements.every(isPrimitiveLiteral)) return;
3179
+ const callbacksReachOnlyItems = calls.every(
3180
+ (call) => call.arguments.every(
3181
+ (argument) => !types.isFunction(argument) || (types.isArrowFunctionExpression(argument) || types.isFunctionExpression(argument)) && argument.params.length <= 2
3182
+ ) && call.arguments.every(
3183
+ (argument) => types.isFunction(argument) || isPrimitiveLiteral(argument)
3184
+ )
3185
+ );
3186
+ if (!callbacksReachOnlyItems) return;
3187
+ const identifier = programPath.scope.generateUidIdentifier("farmListItems");
3188
+ hoisted.push(
3189
+ types.variableDeclaration("const", [types.variableDeclarator(identifier, types.cloneNode(source))])
3190
+ );
3191
+ const owner = calls[calls.length - 1].callee;
3192
+ owner.object = types.cloneNode(identifier);
3193
+ }
3194
+ });
3195
+ if (hoisted.length === 0) return;
3196
+ const lastImport = programPath.get("body").filter((statement) => statement.isImportDeclaration()).at(-1);
3197
+ if (lastImport) lastImport.insertAfter(hoisted);
3198
+ else programPath.unshiftContainer("body", hoisted);
3199
+ }
3200
+ function isCollectionPipelineLength(path) {
3201
+ const parent = path.parent;
3202
+ return types.isMemberExpression(parent) && parent.object === path.node && !parent.computed && types.isIdentifier(parent.property, { name: "length" }) && collectionPipelineMethod(path.node) !== void 0;
3203
+ }
3032
3204
  function validateCollectionPipeline(expression, safeGlobals) {
3033
3205
  const method = collectionPipelineMethod(expression);
3034
3206
  if (!method || !types.isCallExpression(expression)) {
@@ -3327,6 +3499,47 @@ function reactOwnedAttribute(name, attribute) {
3327
3499
  if (name === "defaultValue" || name === "defaultChecked") return true;
3328
3500
  return (name === "spellCheck" || name === "draggable" || name === "contentEditable") && !types.isStringLiteral(attribute.value);
3329
3501
  }
3502
+ function keyedRowEventHandler(attribute) {
3503
+ if (!types.isJSXExpressionContainer(attribute.value) || !types.isArrowFunctionExpression(attribute.value.expression) && !types.isFunctionExpression(attribute.value.expression)) {
3504
+ return "interactive compiled keyed rows require inline synchronous event handlers";
3505
+ }
3506
+ const handler = attribute.value.expression;
3507
+ if (handler.async || handler.generator) {
3508
+ return "interactive compiled keyed row handlers must be synchronous";
3509
+ }
3510
+ if (containsDirectHookCall(handler)) {
3511
+ return "Hooks cannot be called inside compiled keyed row handlers";
3512
+ }
3513
+ const unsupported = validateKeyedRowEventHandler(handler);
3514
+ if (unsupported) return `interactive compiled keyed row handlers cannot use ${unsupported}`;
3515
+ return handler;
3516
+ }
3517
+ function staticKeyedRowEvents(row, compilerSetters) {
3518
+ const events = [];
3519
+ const visit = (element, path) => {
3520
+ for (const attribute of element.openingElement.attributes) {
3521
+ if (!types.isJSXAttribute(attribute)) continue;
3522
+ const name = jsxAttributeName(attribute);
3523
+ if (!name || !/^on[A-Z]/.test(name)) continue;
3524
+ const handler = keyedRowEventHandler(attribute);
3525
+ if (typeof handler === "string") return false;
3526
+ events.push({
3527
+ id: events.length,
3528
+ path: [...path],
3529
+ name,
3530
+ value: stabilizeDeferredEventCurrentTarget(handler, compilerSetters)
3531
+ });
3532
+ }
3533
+ let elementIndex = 0;
3534
+ for (const child of element.children) {
3535
+ if (!types.isJSXElement(child)) continue;
3536
+ if (!visit(child, [...path, elementIndex])) return false;
3537
+ elementIndex += 1;
3538
+ }
3539
+ return true;
3540
+ };
3541
+ return visit(row, []) ? events : void 0;
3542
+ }
3330
3543
  function analyzeKeyedRowTree(row, renderCallback, safeGlobals, compilerSetters, keyedTargetReactiveByValue) {
3331
3544
  if (renderCallback.async || renderCallback.generator || renderCallback.params.length < 1 || renderCallback.params.length > 2 || !types.isIdentifier(renderCallback.params[0]) || renderCallback.params[1] !== void 0 && !types.isIdentifier(renderCallback.params[1])) {
3332
3545
  return {
@@ -3387,20 +3600,8 @@ function analyzeKeyedRowTree(row, renderCallback, safeGlobals, compilerSetters,
3387
3600
  }
3388
3601
  if (name === "key") continue;
3389
3602
  if (/^on[A-Z]/.test(name)) {
3390
- if (!types.isJSXExpressionContainer(attribute.value) || !types.isArrowFunctionExpression(attribute.value.expression) && !types.isFunctionExpression(attribute.value.expression)) {
3391
- return "interactive compiled keyed rows require inline synchronous event handlers";
3392
- }
3393
- const handler = attribute.value.expression;
3394
- if (handler.async || handler.generator) {
3395
- return "interactive compiled keyed row handlers must be synchronous";
3396
- }
3397
- if (containsDirectHookCall(handler)) {
3398
- return "Hooks cannot be called inside compiled keyed row handlers";
3399
- }
3400
- const unsupported2 = validateKeyedRowEventHandler(handler);
3401
- if (unsupported2) {
3402
- return `interactive compiled keyed row handlers cannot use ${unsupported2}`;
3403
- }
3603
+ const handler = keyedRowEventHandler(attribute);
3604
+ if (typeof handler === "string") return handler;
3404
3605
  events.push({
3405
3606
  id: events.length,
3406
3607
  path: [...path],
@@ -3689,7 +3890,7 @@ function analyzeKeyedRowsContainer(container, statesByValue, safeGlobals, listNa
3689
3890
  bindings
3690
3891
  };
3691
3892
  }
3692
- function analyzeKeyedRangesContainer(container, statesByValue, safeGlobals, listNames, allowSingleRange = false) {
3893
+ function analyzeKeyedRangesContainer(container, statesByValue, safeGlobals, listNames, allowSingleRange = false, delegateWithoutReact = false) {
3693
3894
  if (container.openingElement.attributes.some(
3694
3895
  (attribute) => types.isJSXSpreadAttribute(attribute) || types.isJSXAttribute(attribute) && types.isJSXIdentifier(attribute.name) && (attribute.name.name === "ref" || attribute.name.name === "dangerouslySetInnerHTML")
3695
3896
  )) {
@@ -3704,7 +3905,13 @@ function analyzeKeyedRangesContainer(container, statesByValue, safeGlobals, list
3704
3905
  for (const child of children) {
3705
3906
  const range = analyzeKeyedRowChild(child, statesByValue, safeGlobals, listNames);
3706
3907
  if (range) {
3707
- if (range.events.length > 0 || range.conditionals.length > 0) return void 0;
3908
+ if (range.conditionals.length > 0) return void 0;
3909
+ if (range.events.length > 0 && !canDelegateKeyedRowEvents(
3910
+ { events: range.events, conditionals: [], source: container },
3911
+ delegateWithoutReact
3912
+ )) {
3913
+ return void 0;
3914
+ }
3708
3915
  ranges.push({
3709
3916
  before: staticBefore,
3710
3917
  source: range.source,
@@ -3713,6 +3920,7 @@ function analyzeKeyedRangesContainer(container, statesByValue, safeGlobals, list
3713
3920
  renderCallback: range.renderCallback,
3714
3921
  row: range.row,
3715
3922
  bindings: range.bindings,
3923
+ ...range.events.length > 0 ? { events: range.events } : {},
3716
3924
  syntax: range.syntax
3717
3925
  });
3718
3926
  for (const dependency of range.dependencies) dependencies.add(dependency);
@@ -3870,7 +4078,7 @@ function analyzeHostConditionalTree(branch, safeGlobals, allowStaticEvents = fal
3870
4078
  const reason = visit(branch, []);
3871
4079
  return reason ? { reason } : { bindings };
3872
4080
  }
3873
- function analyzeRecursiveHostTree(branch, statesByValue, safeGlobals, listNames, parent, allocateId) {
4081
+ function analyzeRecursiveHostTree(branch, statesByValue, safeGlobals, listNames, parent, allocateId, delegatedEvents = /* @__PURE__ */ new Set()) {
3874
4082
  const bindings = [];
3875
4083
  const plans = [];
3876
4084
  const blocks = /* @__PURE__ */ new Map();
@@ -3901,6 +4109,7 @@ function analyzeRecursiveHostTree(branch, statesByValue, safeGlobals, listNames,
3901
4109
  return `recursive compiler-owned block ${name} requires React ownership`;
3902
4110
  }
3903
4111
  if (/^on[A-Z]/.test(name)) {
4112
+ if (delegatedEvents.has(attribute)) continue;
3904
4113
  return "recursive compiler-owned block events require React ownership";
3905
4114
  }
3906
4115
  if (!types.isJSXExpressionContainer(attribute.value) || types.isJSXEmptyExpression(attribute.value.expression)) {
@@ -3959,7 +4168,8 @@ function analyzeRecursiveHostTree(branch, statesByValue, safeGlobals, listNames,
3959
4168
  safeGlobals,
3960
4169
  listNames,
3961
4170
  id,
3962
- allocateId
4171
+ allocateId,
4172
+ delegatedEvents
3963
4173
  );
3964
4174
  if (mixed) {
3965
4175
  plans.push(...mixed.nestedPlans);
@@ -4009,7 +4219,8 @@ function analyzeRecursiveHostTree(branch, statesByValue, safeGlobals, listNames,
4009
4219
  safeGlobals,
4010
4220
  listNames,
4011
4221
  owner,
4012
- allocateId
4222
+ allocateId,
4223
+ delegatedEvents
4013
4224
  );
4014
4225
  if (nested.reason) return nested.reason;
4015
4226
  plans.push(...nested.plans);
@@ -4161,7 +4372,8 @@ function analyzeRecursiveHostTree(branch, statesByValue, safeGlobals, listNames,
4161
4372
  safeGlobals,
4162
4373
  listNames,
4163
4374
  owner,
4164
- allocateId
4375
+ allocateId,
4376
+ delegatedEvents
4165
4377
  );
4166
4378
  if (nested.reason) return nested.reason;
4167
4379
  plans.push(...nested.plans);
@@ -4231,19 +4443,80 @@ function analyzeRecursiveHostTree(branch, statesByValue, safeGlobals, listNames,
4231
4443
  const reason = visit(branch, [], parent);
4232
4444
  return { bindings, plans, blocks, reason };
4233
4445
  }
4234
- function analyzeCompilerOwnedKeyedRowHostBlocks(shape, statesByValue, safeGlobals, listNames, parent, allocateId) {
4235
- if (shape.events.length > 0) return void 0;
4446
+ function jsxCanRenderElements(expression) {
4447
+ let found = false;
4448
+ traverse(expressionFile(cloneExpression(expression)), {
4449
+ JSXElement(path) {
4450
+ found = true;
4451
+ path.stop();
4452
+ },
4453
+ JSXFragment(path) {
4454
+ found = true;
4455
+ path.stop();
4456
+ }
4457
+ });
4458
+ return found;
4459
+ }
4460
+ function keyedRowEventPathIsStable(row, path) {
4461
+ let element = row;
4462
+ for (const index of path) {
4463
+ let seen = 0;
4464
+ let next;
4465
+ for (const child of element.children) {
4466
+ if (types.isJSXElement(child)) {
4467
+ if (!isHostElement(child)) return false;
4468
+ if (seen === index) {
4469
+ next = child;
4470
+ break;
4471
+ }
4472
+ seen += 1;
4473
+ continue;
4474
+ }
4475
+ if (types.isJSXFragment(child)) return false;
4476
+ if (types.isJSXExpressionContainer(child) && !types.isJSXEmptyExpression(child.expression) && jsxCanRenderElements(child.expression)) {
4477
+ return false;
4478
+ }
4479
+ }
4480
+ if (!next) return false;
4481
+ element = next;
4482
+ }
4483
+ return true;
4484
+ }
4485
+ function analyzeCompilerOwnedKeyedRowHostBlocks(shape, container, statesByValue, safeGlobals, listNames, parent, allocateId, delegateWithoutReact = false) {
4486
+ const events = shape.rowReason ? staticKeyedRowEvents(
4487
+ shape.row,
4488
+ new Set([...statesByValue.values()].map((state) => state.setterName))
4489
+ ) : shape.events;
4490
+ if (!events || events.length > 0 && (!canDelegateKeyedRowEvents(
4491
+ { events, conditionals: [], source: container },
4492
+ delegateWithoutReact
4493
+ ) || !events.every((event) => keyedRowEventPathIsStable(shape.row, event.path)))) {
4494
+ return void 0;
4495
+ }
4236
4496
  const row = types.cloneNode(shape.row, true);
4237
4497
  row.openingElement.attributes = row.openingElement.attributes.filter(
4238
4498
  (attribute) => !types.isJSXAttribute(attribute) || jsxAttributeName(attribute) !== "key"
4239
4499
  );
4500
+ const delegatedEvents = /* @__PURE__ */ new Set();
4501
+ for (const event of events) {
4502
+ let element = row;
4503
+ for (const index of event.path) {
4504
+ element = element.children.filter((child) => types.isJSXElement(child))[index];
4505
+ }
4506
+ for (const attribute of element.openingElement.attributes) {
4507
+ if (types.isJSXAttribute(attribute) && jsxAttributeName(attribute) === event.name) {
4508
+ delegatedEvents.add(attribute);
4509
+ }
4510
+ }
4511
+ }
4240
4512
  const analysis = analyzeRecursiveHostTree(
4241
4513
  row,
4242
4514
  statesByValue,
4243
4515
  safeGlobals,
4244
4516
  listNames,
4245
4517
  parent,
4246
- allocateId
4518
+ allocateId,
4519
+ delegatedEvents
4247
4520
  );
4248
4521
  if (analysis.reason || analysis.plans.length === 0) {
4249
4522
  return void 0;
@@ -4252,7 +4525,8 @@ function analyzeCompilerOwnedKeyedRowHostBlocks(shape, statesByValue, safeGlobal
4252
4525
  row,
4253
4526
  bindings: analysis.bindings,
4254
4527
  descriptorBlocks: analysis.blocks,
4255
- nestedPlans: analysis.plans
4528
+ nestedPlans: analysis.plans,
4529
+ events
4256
4530
  };
4257
4531
  }
4258
4532
  function analyzeHostConditionalContainer(container, statesByValue, safeGlobals, listNames, parent, allocateId) {
@@ -4416,7 +4690,7 @@ function analyzeConditionalRangesContainer(container, statesByValue, safeGlobals
4416
4690
  nestedPlans
4417
4691
  };
4418
4692
  }
4419
- function analyzeMixedRangesContainer(container, statesByValue, safeGlobals, listNames, parent, allocateId) {
4693
+ function analyzeMixedRangesContainer(container, statesByValue, safeGlobals, listNames, parent, allocateId, delegatedEvents = /* @__PURE__ */ new Set()) {
4420
4694
  const containerName = container.openingElement.name;
4421
4695
  if (!types.isJSXIdentifier(containerName) || !/^[a-z]/.test(containerName.name) || isForeignContent(container) || container.openingElement.attributes.some(
4422
4696
  (attribute) => types.isJSXSpreadAttribute(attribute) || types.isJSXAttribute(attribute) && types.isJSXIdentifier(attribute.name) && (attribute.name.name === "ref" || attribute.name.name === "dangerouslySetInnerHTML")
@@ -4531,7 +4805,8 @@ function analyzeMixedRangesContainer(container, statesByValue, safeGlobals, list
4531
4805
  safeGlobals,
4532
4806
  listNames,
4533
4807
  parent,
4534
- allocateId
4808
+ allocateId,
4809
+ delegatedEvents
4535
4810
  );
4536
4811
  if (staticAnalysis.reason) return void 0;
4537
4812
  for (const binding of staticAnalysis.bindings) {
@@ -4796,7 +5071,7 @@ function uniqueLocalIdentifier(base, nodes) {
4796
5071
  while (names.has(name)) name = `${base}${suffix++}`;
4797
5072
  return types.identifier(name);
4798
5073
  }
4799
- function rewriteKeyedRowEventAttributes(row, events, eventFactory, item, index) {
5074
+ function rewriteKeyedRowEventAttributes(row, events, eventFactory, item, index, extraArguments = []) {
4800
5075
  const eventByLocation = new Map(
4801
5076
  events.map((event) => [`${event.path.join(".")}:${event.name}`, event])
4802
5077
  );
@@ -4811,7 +5086,8 @@ function rewriteKeyedRowEventAttributes(row, events, eventFactory, item, index)
4811
5086
  types.callExpression(types.cloneNode(eventFactory), [
4812
5087
  types.cloneNode(item),
4813
5088
  types.cloneNode(index),
4814
- types.numericLiteral(event.id)
5089
+ types.numericLiteral(event.id),
5090
+ ...extraArguments.map((argument) => types.cloneNode(argument))
4815
5091
  ])
4816
5092
  );
4817
5093
  }
@@ -5038,6 +5314,7 @@ function keyedRowsBoundary(blockRuntime, plan, keyedMapUpdateHints, keyedArrayFi
5038
5314
  ...plan.conditionals.map((conditional) => conditional.test)
5039
5315
  ]);
5040
5316
  const delegatedEvents = canDelegateKeyedRowEvents(plan);
5317
+ const delegatedWithoutReact = !delegatedEvents && canDelegateKeyedRowEvents(plan, true);
5041
5318
  const bindingTargets = keyedRowBindingTargets(plan);
5042
5319
  const renderSource = keyedRowsRenderSource(plan, eventFactory, conditionalFactory);
5043
5320
  const renderFactoryParameters = plan.conditionals.length > 0 ? [types.cloneNode(eventFactory), types.cloneNode(conditionalFactory)] : plan.events.length > 0 ? [types.cloneNode(eventFactory)] : [];
@@ -5134,9 +5411,11 @@ function keyedRowsBoundary(blockRuntime, plan, keyedMapUpdateHints, keyedArrayFi
5134
5411
  )
5135
5412
  )
5136
5413
  ),
5137
- ...delegatedEvents ? [
5414
+ ...delegatedEvents || delegatedWithoutReact ? [
5138
5415
  types.jsxAttribute(
5139
- types.jsxIdentifier("delegateEvents"),
5416
+ types.jsxIdentifier(
5417
+ delegatedEvents ? "delegateEvents" : "delegateEventsWithoutReact"
5418
+ ),
5140
5419
  types.jsxExpressionContainer(types.booleanLiteral(true))
5141
5420
  )
5142
5421
  ] : []
@@ -5197,15 +5476,66 @@ function keyedRangeObject(range, descriptorBlocks = /* @__PURE__ */ new Map()) {
5197
5476
  types.arrayExpression(
5198
5477
  range.bindings.map((binding) => keyedRowBindingObject(binding, parameters))
5199
5478
  )
5200
- )
5479
+ ),
5480
+ ...range.events?.length ? [
5481
+ types.objectProperty(
5482
+ types.identifier("events"),
5483
+ types.arrayExpression(range.events.map((event) => keyedRowEventObject(event, parameters)))
5484
+ )
5485
+ ] : []
5201
5486
  ]);
5202
5487
  }
5488
+ function rewriteKeyedRangeEvents(source, plan, eventFactory) {
5489
+ let rangeIndex = 0;
5490
+ for (const child of meaningfulJsxChildren(source)) {
5491
+ if (types.isJSXElement(child) && isHostElement(child)) continue;
5492
+ const range = plan.ranges[rangeIndex];
5493
+ const currentRange = rangeIndex;
5494
+ rangeIndex += 1;
5495
+ if (!range?.events?.length) continue;
5496
+ let callback;
5497
+ if (types.isJSXExpressionContainer(child) && !types.isJSXEmptyExpression(child.expression) && isMapCall(child.expression)) {
5498
+ const candidate = child.expression.arguments[0];
5499
+ if (types.isArrowFunctionExpression(candidate) || types.isFunctionExpression(candidate)) {
5500
+ callback = candidate;
5501
+ }
5502
+ } else if (types.isJSXElement(child)) {
5503
+ const renderChild = meaningfulJsxChildren(child)[0];
5504
+ if (types.isJSXExpressionContainer(renderChild) && (types.isArrowFunctionExpression(renderChild.expression) || types.isFunctionExpression(renderChild.expression))) {
5505
+ callback = renderChild.expression;
5506
+ }
5507
+ }
5508
+ const row = callback && returnedExpression(callback);
5509
+ const item = callback?.params[0];
5510
+ const index = callback?.params[1];
5511
+ if (!types.isJSXElement(row) || !types.isIdentifier(item)) {
5512
+ throw new Error("keyed range events must belong to an inline row callback");
5513
+ }
5514
+ rewriteKeyedRowEventAttributes(
5515
+ row,
5516
+ range.events,
5517
+ eventFactory,
5518
+ item,
5519
+ types.isIdentifier(index) ? index : types.numericLiteral(0),
5520
+ [types.numericLiteral(currentRange)]
5521
+ );
5522
+ }
5523
+ }
5203
5524
  function keyedRangesBoundary(blockRuntime, plan) {
5204
5525
  const name = types.jsxMemberExpression(
5205
5526
  types.jsxIdentifier(blockRuntime.name),
5206
5527
  types.jsxIdentifier("KeyedRanges")
5207
5528
  );
5208
5529
  const source = types.cloneNode(plan.source, true);
5530
+ const hasEvents = plan.ranges.some((range) => range.events?.length);
5531
+ const delegatedEvents = plan.ranges.every(
5532
+ (range) => !range.events?.length || canDelegateKeyedRowEvents({ events: range.events, conditionals: [], source: plan.source })
5533
+ );
5534
+ const eventFactory = uniqueLocalIdentifier("_farmRowEvent", [
5535
+ plan.source,
5536
+ ...plan.ranges.flatMap((range) => (range.events || []).map((event) => event.value))
5537
+ ]);
5538
+ if (hasEvents) rewriteKeyedRangeEvents(source, plan, eventFactory);
5209
5539
  let rootRef;
5210
5540
  source.openingElement.attributes = source.openingElement.attributes.filter((attribute) => {
5211
5541
  if (!types.isJSXAttribute(attribute) || jsxAttributeName(attribute) !== "ref" || !types.isJSXExpressionContainer(attribute.value) || types.isJSXEmptyExpression(attribute.value.expression)) {
@@ -5221,8 +5551,16 @@ function keyedRangesBoundary(blockRuntime, plan) {
5221
5551
  types.jsxAttribute(types.jsxIdentifier("id"), types.jsxExpressionContainer(types.numericLiteral(plan.id))),
5222
5552
  types.jsxAttribute(
5223
5553
  types.jsxIdentifier("render"),
5224
- types.jsxExpressionContainer(types.arrowFunctionExpression([], source))
5554
+ types.jsxExpressionContainer(
5555
+ types.arrowFunctionExpression(hasEvents ? [types.cloneNode(eventFactory)] : [], source)
5556
+ )
5225
5557
  ),
5558
+ ...hasEvents ? [
5559
+ types.jsxAttribute(
5560
+ types.jsxIdentifier(delegatedEvents ? "delegateEvents" : "delegateEventsWithoutReact"),
5561
+ types.jsxExpressionContainer(types.booleanLiteral(true))
5562
+ )
5563
+ ] : [],
5226
5564
  ...rootRef ? [types.jsxAttribute(types.jsxIdentifier("rootRef"), types.jsxExpressionContainer(rootRef))] : [],
5227
5565
  types.jsxAttribute(
5228
5566
  types.jsxIdentifier("ranges"),
@@ -5512,7 +5850,7 @@ function componentIslandBoundary(blockRuntime, plan, source) {
5512
5850
  true
5513
5851
  );
5514
5852
  }
5515
- function analyzeComposableBlocks(root, statesByValue, safeGlobals, listNames, allowedComponentNames) {
5853
+ function analyzeComposableBlocks(root, statesByValue, safeGlobals, listNames, allowedComponentNames, delegateWithoutReact) {
5516
5854
  const plans = [];
5517
5855
  const componentElements = /* @__PURE__ */ new Set();
5518
5856
  const conditionalExpressions = /* @__PURE__ */ new Set();
@@ -5646,7 +5984,14 @@ function analyzeComposableBlocks(root, statesByValue, safeGlobals, listNames, al
5646
5984
  continue;
5647
5985
  }
5648
5986
  nextId = mixedRangeStart;
5649
- const keyedRanges = insideConditional ? void 0 : analyzeKeyedRangesContainer(child, statesByValue, safeGlobals, listNames);
5987
+ const keyedRanges = insideConditional ? void 0 : analyzeKeyedRangesContainer(
5988
+ child,
5989
+ statesByValue,
5990
+ safeGlobals,
5991
+ listNames,
5992
+ false,
5993
+ delegateWithoutReact
5994
+ );
5650
5995
  if (keyedRanges) {
5651
5996
  plans.push({
5652
5997
  kind: "keyed-ranges",
@@ -5724,11 +6069,13 @@ function analyzeComposableBlocks(root, statesByValue, safeGlobals, listNames, al
5724
6069
  const nestedStart = nextId;
5725
6070
  const hostBlocks = analyzeCompilerOwnedKeyedRowHostBlocks(
5726
6071
  keyedRows,
6072
+ child,
5727
6073
  statesByValue,
5728
6074
  safeGlobals,
5729
6075
  listNames,
5730
6076
  keyedRowsId,
5731
- () => nextId++
6077
+ () => nextId++,
6078
+ delegateWithoutReact
5732
6079
  );
5733
6080
  if (!hostBlocks) nextId = nestedStart;
5734
6081
  if (keyedRows.rowReason && !hostBlocks) {
@@ -5751,7 +6098,7 @@ function analyzeComposableBlocks(root, statesByValue, safeGlobals, listNames, al
5751
6098
  renderCallback: keyedRows.renderCallback,
5752
6099
  row: hostBlocks?.row || keyedRows.row,
5753
6100
  bindings: hostBlocks?.bindings || keyedRows.bindings,
5754
- events: keyedRows.events,
6101
+ events: hostBlocks?.events || keyedRows.events,
5755
6102
  conditionals: hostBlocks ? [] : keyedRows.conditionals,
5756
6103
  descriptorBlocks: hostBlocks?.descriptorBlocks,
5757
6104
  syntax: keyedRows.syntax
@@ -5897,7 +6244,14 @@ function analyzeComposableBlocks(root, statesByValue, safeGlobals, listNames, al
5897
6244
  };
5898
6245
  }
5899
6246
  nextId = rootMixedRangeStart;
5900
- const rootRanges = analyzeKeyedRangesContainer(root, statesByValue, safeGlobals, listNames, true);
6247
+ const rootRanges = analyzeKeyedRangesContainer(
6248
+ root,
6249
+ statesByValue,
6250
+ safeGlobals,
6251
+ listNames,
6252
+ true,
6253
+ delegateWithoutReact
6254
+ );
5901
6255
  if (rootRanges) {
5902
6256
  plans.push({
5903
6257
  kind: "keyed-ranges",
@@ -6405,7 +6759,7 @@ function wrapperElement(definitionIdentifier, props) {
6405
6759
  const attributes = props ? [types.jsxSpreadAttribute(types.cloneNode(props))] : [];
6406
6760
  return types.jsxElement(types.jsxOpeningElement(name, attributes, true), null, [], true);
6407
6761
  }
6408
- function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateIdentifier, keyedArrayAppendIdentifier, keyedArrayFilterIdentifier, keyedArrayMapPipelineIdentifier, keyedArrayMappedStructuralAppendIdentifier, keyedArrayStructuralAppendMapPipelineIdentifier, keyedArrayMappedStructuralPrependIdentifier, keyedArrayStructuralPrependMapPipelineIdentifier, keyedArrayQueuedMapPipelineIdentifier, keyedArrayRollingWindowMapPipelineIdentifier, keyedArrayMappedRollingWindowIdentifier, keyedArrayMapReorderIdentifier, keyedArrayMappedStructuralIdentifier, keyedArrayPrependIdentifier, keyedArrayPositionIdentifier, keyedArrayBatchInsertIdentifier, keyedArrayWindowReplaceIdentifier, keyedArrayReorderIdentifier, keyedArrayStructuralReorderIdentifier, keyedArraySortIdentifier, keyedArrayStructuralSortIdentifier, keyedArrayRollingWindowIdentifier, keyedArraySliceIdentifier, keyedCollectionUpdateIdentifier, keyedCollectionMutationIdentifier, runtimeFeatureIdentifiers, usedRuntimeFeatures, optimizationCounts, compilerUsage, useStateNames, reactNames, listNames, moduleId, reactivity) {
6762
+ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateIdentifier, keyedArrayAppendIdentifier, keyedArrayFilterIdentifier, keyedArrayMapPipelineIdentifier, keyedArrayMappedStructuralAppendIdentifier, keyedArrayStructuralAppendMapPipelineIdentifier, keyedArrayMappedStructuralPrependIdentifier, keyedArrayStructuralPrependMapPipelineIdentifier, keyedArrayQueuedMapPipelineIdentifier, keyedArrayRollingWindowMapPipelineIdentifier, keyedArrayMappedRollingWindowIdentifier, keyedArrayMapReorderIdentifier, keyedArrayMappedStructuralIdentifier, keyedArrayPrependIdentifier, keyedArrayPositionIdentifier, keyedArrayBatchInsertIdentifier, keyedArrayWindowReplaceIdentifier, keyedArrayReorderIdentifier, keyedArrayStructuralReorderIdentifier, keyedArraySortIdentifier, keyedArrayStructuralSortIdentifier, keyedArrayRollingWindowIdentifier, keyedArraySliceIdentifier, keyedCollectionUpdateIdentifier, keyedCollectionMutationIdentifier, runtimeFeatureIdentifiers, usedRuntimeFeatures, optimizationCounts, compilerUsage, useStateNames, reactNames, listNames, moduleId, reactivity, delegateWithoutReact) {
6409
6763
  const { path, name, statementPath } = candidate;
6410
6764
  if (path.node.async || path.node.generator)
6411
6765
  return "async and generator components are not supported";
@@ -6591,7 +6945,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
6591
6945
  reactiveByValue,
6592
6946
  safeGlobals,
6593
6947
  listNames,
6594
- allowedComponentNames
6948
+ allowedComponentNames,
6949
+ delegateWithoutReact
6595
6950
  );
6596
6951
  let analysis = analyzeHostTree(
6597
6952
  expandedReactiveRoot,
@@ -6611,7 +6966,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
6611
6966
  reactiveByValue,
6612
6967
  safeGlobals,
6613
6968
  listNames,
6614
- allowedComponentNames
6969
+ allowedComponentNames,
6970
+ delegateWithoutReact
6615
6971
  );
6616
6972
  analysis = analyzeHostTree(
6617
6973
  expandedReactiveRoot,
@@ -6644,7 +7000,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
6644
7000
  reactiveByValue,
6645
7001
  safeGlobals,
6646
7002
  listNames,
6647
- allowedComponentNames
7003
+ allowedComponentNames,
7004
+ delegateWithoutReact
6648
7005
  );
6649
7006
  const hintedAnalysis = analyzeHostTree(
6650
7007
  hintedRoot.root,
@@ -6685,7 +7042,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
6685
7042
  reactiveByValue,
6686
7043
  safeGlobals,
6687
7044
  listNames,
6688
- allowedComponentNames
7045
+ allowedComponentNames,
7046
+ delegateWithoutReact
6689
7047
  );
6690
7048
  const hintedAnalysis = analyzeHostTree(
6691
7049
  appendHintedRoot.root,
@@ -6727,7 +7085,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
6727
7085
  reactiveByValue,
6728
7086
  safeGlobals,
6729
7087
  listNames,
6730
- allowedComponentNames
7088
+ allowedComponentNames,
7089
+ delegateWithoutReact
6731
7090
  );
6732
7091
  const hintedAnalysis = analyzeHostTree(
6733
7092
  prependHintedRoot.root,
@@ -6763,7 +7122,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
6763
7122
  reactiveByValue,
6764
7123
  safeGlobals,
6765
7124
  listNames,
6766
- allowedComponentNames
7125
+ allowedComponentNames,
7126
+ delegateWithoutReact
6767
7127
  );
6768
7128
  const hintedAnalysis = analyzeHostTree(
6769
7129
  rollingWindowHintedRoot.root,
@@ -6802,7 +7162,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
6802
7162
  reactiveByValue,
6803
7163
  safeGlobals,
6804
7164
  listNames,
6805
- allowedComponentNames
7165
+ allowedComponentNames,
7166
+ delegateWithoutReact
6806
7167
  );
6807
7168
  const hintedAnalysis = analyzeHostTree(
6808
7169
  positionHintedRoot.root,
@@ -6857,7 +7218,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
6857
7218
  reactiveByValue,
6858
7219
  safeGlobals,
6859
7220
  listNames,
6860
- allowedComponentNames
7221
+ allowedComponentNames,
7222
+ delegateWithoutReact
6861
7223
  );
6862
7224
  const hintedAnalysis = analyzeHostTree(
6863
7225
  reorderPipelineHintedRoot.root,
@@ -6906,7 +7268,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
6906
7268
  reactiveByValue,
6907
7269
  safeGlobals,
6908
7270
  listNames,
6909
- allowedComponentNames
7271
+ allowedComponentNames,
7272
+ delegateWithoutReact
6910
7273
  );
6911
7274
  const hintedAnalysis = analyzeHostTree(
6912
7275
  reorderHintedRoot.root,
@@ -6944,7 +7307,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
6944
7307
  reactiveByValue,
6945
7308
  safeGlobals,
6946
7309
  listNames,
6947
- allowedComponentNames
7310
+ allowedComponentNames,
7311
+ delegateWithoutReact
6948
7312
  );
6949
7313
  const hintedAnalysis = analyzeHostTree(
6950
7314
  sortHintedRoot.root,
@@ -6982,7 +7346,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
6982
7346
  reactiveByValue,
6983
7347
  safeGlobals,
6984
7348
  listNames,
6985
- allowedComponentNames
7349
+ allowedComponentNames,
7350
+ delegateWithoutReact
6986
7351
  );
6987
7352
  const hintedAnalysis = analyzeHostTree(
6988
7353
  sliceHintedRoot.root,
@@ -7020,7 +7385,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
7020
7385
  reactiveByValue,
7021
7386
  safeGlobals,
7022
7387
  listNames,
7023
- allowedComponentNames
7388
+ allowedComponentNames,
7389
+ delegateWithoutReact
7024
7390
  );
7025
7391
  const hintedAnalysis = analyzeHostTree(
7026
7392
  filterHintedRoot.root,
@@ -7060,7 +7426,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
7060
7426
  reactiveByValue,
7061
7427
  safeGlobals,
7062
7428
  listNames,
7063
- allowedComponentNames
7429
+ allowedComponentNames,
7430
+ delegateWithoutReact
7064
7431
  );
7065
7432
  const hintedAnalysis = analyzeHostTree(
7066
7433
  queuedMappedStructuralHintedRoot.root,
@@ -7107,7 +7474,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
7107
7474
  reactiveByValue,
7108
7475
  safeGlobals,
7109
7476
  listNames,
7110
- allowedComponentNames
7477
+ allowedComponentNames,
7478
+ delegateWithoutReact
7111
7479
  );
7112
7480
  const hintedAnalysis = analyzeHostTree(
7113
7481
  queuedStructuralMapHintedRoot.root,
@@ -7154,7 +7522,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
7154
7522
  reactiveByValue,
7155
7523
  safeGlobals,
7156
7524
  listNames,
7157
- allowedComponentNames
7525
+ allowedComponentNames,
7526
+ delegateWithoutReact
7158
7527
  );
7159
7528
  const hintedAnalysis = analyzeHostTree(
7160
7529
  queuedStructuralPrependMapHintedRoot.root,
@@ -7197,7 +7566,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
7197
7566
  reactiveByValue,
7198
7567
  safeGlobals,
7199
7568
  listNames,
7200
- allowedComponentNames
7569
+ allowedComponentNames,
7570
+ delegateWithoutReact
7201
7571
  );
7202
7572
  const hintedAnalysis = analyzeHostTree(
7203
7573
  queuedRollingWindowMapHintedRoot.root,
@@ -7258,7 +7628,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
7258
7628
  reactiveByValue,
7259
7629
  safeGlobals,
7260
7630
  listNames,
7261
- allowedComponentNames
7631
+ allowedComponentNames,
7632
+ delegateWithoutReact
7262
7633
  );
7263
7634
  const hintedAnalysis = analyzeHostTree(
7264
7635
  queuedReorderMapHintedRoot.root,
@@ -7301,7 +7672,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
7301
7672
  reactiveByValue,
7302
7673
  safeGlobals,
7303
7674
  listNames,
7304
- allowedComponentNames
7675
+ allowedComponentNames,
7676
+ delegateWithoutReact
7305
7677
  );
7306
7678
  const hintedAnalysis = analyzeHostTree(
7307
7679
  queuedStructuralReorderHintedRoot.root,
@@ -7357,7 +7729,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
7357
7729
  reactiveByValue,
7358
7730
  safeGlobals,
7359
7731
  listNames,
7360
- allowedComponentNames
7732
+ allowedComponentNames,
7733
+ delegateWithoutReact
7361
7734
  );
7362
7735
  const hintedAnalysis = analyzeHostTree(
7363
7736
  keyedCollectionHintedRoot.root,
@@ -7723,6 +8096,16 @@ async function compileReactModule(code, id, options) {
7723
8096
  const runtimeFeatureIdentifiers = /* @__PURE__ */ new Map();
7724
8097
  const usedRuntimeFeatures = /* @__PURE__ */ new Set();
7725
8098
  for (const candidate of candidates) {
8099
+ hoistConstantListCollections(candidate.path, programPath);
8100
+ inlineStatelessChildren(
8101
+ candidate.path,
8102
+ programPath,
8103
+ new Set(
8104
+ [...SAFE_GLOBAL_CALLS, "Math"].filter(
8105
+ (name) => !candidate.path.scope.getBinding(name)
8106
+ )
8107
+ )
8108
+ );
7726
8109
  const reason = compileCandidate(
7727
8110
  candidate,
7728
8111
  createComponentIdentifier,
@@ -7759,7 +8142,8 @@ async function compileReactModule(code, id, options) {
7759
8142
  reactNames,
7760
8143
  listNames,
7761
8144
  id,
7762
- options.reactivity
8145
+ options.reactivity,
8146
+ options.compiledRoot !== false
7763
8147
  );
7764
8148
  if (reason) {
7765
8149
  diagnostics.push({
@@ -8268,7 +8652,11 @@ function normalizeReactCompilerOptions(value) {
8268
8652
  }
8269
8653
  const reportFile = normalizeCompilerReportFile(options.reportFile);
8270
8654
  const report = options.report === true || options.reportFile !== void 0;
8271
- return { mode, directive, onUnsupported, reactivity, report, reportFile };
8655
+ if (options.compiledRoot !== void 0 && typeof options.compiledRoot !== "boolean") {
8656
+ throw new TypeError("The React compiler compiledRoot option must be a boolean.");
8657
+ }
8658
+ const compiledRoot = options.compiledRoot !== false;
8659
+ return { mode, directive, onUnsupported, reactivity, report, reportFile, compiledRoot };
8272
8660
  }
8273
8661
 
8274
8662
  // src/vite.ts
@@ -8282,6 +8670,24 @@ function readCompilerOptions(rendererOptions) {
8282
8670
  }
8283
8671
  return normalizeReactCompilerOptions(compiler);
8284
8672
  }
8673
+ function rewriteCompiledRootImports(code, program) {
8674
+ const edits = [];
8675
+ for (const statement of program.body) {
8676
+ const node = statement;
8677
+ if (node.type !== "ImportDeclaration" || node.importKind === "type" || node.source?.value !== "react-dom/client" || !node.specifiers?.length || !node.specifiers.every(
8678
+ (specifier) => specifier.type === "ImportSpecifier" && (specifier.imported?.name ?? specifier.imported?.value) === "createRoot"
8679
+ )) {
8680
+ continue;
8681
+ }
8682
+ edits.push(node.source);
8683
+ }
8684
+ if (edits.length === 0) return null;
8685
+ let result = code;
8686
+ for (const literal of edits.sort((left, right) => right.start - left.start)) {
8687
+ result = `${result.slice(0, literal.start)}"@farm.js/react/compiled-root"${result.slice(literal.end)}`;
8688
+ }
8689
+ return result;
8690
+ }
8285
8691
  function createFarmRendererPlugin(options = {}) {
8286
8692
  const compilerOptions = readCompilerOptions(options.rendererOptions);
8287
8693
  if (!compilerOptions) return [];
@@ -8337,10 +8743,25 @@ function createFarmRendererPlugin(options = {}) {
8337
8743
  reportObservations.values()
8338
8744
  );
8339
8745
  }
8746
+ },
8747
+ {
8748
+ // Runs after TypeScript and JSX are compiled, so the module parses as JavaScript.
8749
+ name: "farm:react-compiled-root",
8750
+ transform(code, id, transformOptions) {
8751
+ if (compilerOptions.compiledRoot === false || options.ssr || transformOptions?.ssr) {
8752
+ return null;
8753
+ }
8754
+ if (!code.includes("react-dom/client")) return null;
8755
+ const cleanId = normalizePath(id.split("?", 1)[0]);
8756
+ if (!/\.[cm]?[jt]sx?$/.test(cleanId) || cleanId.includes("/node_modules/") || projectRoot && !cleanId.startsWith(`${projectRoot}/`)) {
8757
+ return null;
8758
+ }
8759
+ return rewriteCompiledRootImports(code, this.parse(code));
8760
+ }
8340
8761
  }
8341
8762
  ];
8342
8763
  }
8343
8764
 
8344
- export { createFarmRendererPlugin };
8765
+ export { createFarmRendererPlugin, rewriteCompiledRootImports };
8345
8766
  //# sourceMappingURL=vite.mjs.map
8346
8767
  //# sourceMappingURL=vite.mjs.map