babel-plugin-essor 0.0.16-beta.3 → 0.0.16-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -22142,15 +22142,94 @@ function styleToString(style) {
22142
22142
  function shouldResolveStaticCollection(attrName) {
22143
22143
  return attrName !== "ref" && !attrName.startsWith("bind:") && !normalizeEventName(attrName);
22144
22144
  }
22145
+ function normalizeTemplateAttrName(attrName) {
22146
+ var _a;
22147
+ return (_a = propsToAttrMap[attrName]) != null ? _a : attrName;
22148
+ }
22149
+ function createStaticAttr(attrName, value, order) {
22150
+ return {
22151
+ attr: { name: attrName, value },
22152
+ order,
22153
+ templateName: normalizeTemplateAttrName(attrName)
22154
+ };
22155
+ }
22156
+ function createDynamicAttr(attrPath, attrName, value, effectKind, order) {
22157
+ return {
22158
+ attr: {
22159
+ path: attrPath,
22160
+ name: attrName,
22161
+ value,
22162
+ effectKind
22163
+ },
22164
+ order,
22165
+ templateName: normalizeTemplateAttrName(attrName)
22166
+ };
22167
+ }
22168
+ function resolveStaticExpressionValue(attrName, expressionPath) {
22169
+ if (!shouldResolveStaticCollection(attrName)) {
22170
+ return void 0;
22171
+ }
22172
+ const evaluated = expressionPath.evaluate();
22173
+ if (!evaluated.confident || !isSerializableStaticValue(evaluated.value)) {
22174
+ return void 0;
22175
+ }
22176
+ let value = evaluated.value;
22177
+ if (attrName === "style" && isPlainObject(value)) {
22178
+ value = styleToString(value);
22179
+ }
22180
+ if (isString(value) || isBoolean(value)) {
22181
+ return value;
22182
+ }
22183
+ if (isNumber(value)) {
22184
+ return String(value);
22185
+ }
22186
+ return void 0;
22187
+ }
22188
+ function parseExpressionAttribute(attrPath, attrName, expression, order, staticAttrs, dynamicAttrs) {
22189
+ const expressionPath = attrPath.get("value.expression");
22190
+ const staticValue = resolveStaticExpressionValue(attrName, expressionPath);
22191
+ if (staticValue !== void 0) {
22192
+ staticAttrs.push(createStaticAttr(attrName, staticValue, order));
22193
+ return;
22194
+ }
22195
+ const classified = classifyAttrValue(expression);
22196
+ dynamicAttrs.push(
22197
+ createDynamicAttr(attrPath, attrName, classified.expression, classified.kind, order)
22198
+ );
22199
+ }
22200
+ function finalizeParsedAttributes(staticAttrs, dynamicAttrs, spreadAttrs) {
22201
+ if (dynamicAttrs.length === 0) {
22202
+ return {
22203
+ staticAttrs: staticAttrs.map((entry) => entry.attr),
22204
+ dynamicAttrs: [],
22205
+ spreadAttrs
22206
+ };
22207
+ }
22208
+ const dynamicNames = new Set(dynamicAttrs.map((entry) => entry.templateName));
22209
+ const lastExplicitOrder = /* @__PURE__ */ new Map();
22210
+ for (const entry of [...staticAttrs, ...dynamicAttrs]) {
22211
+ if (!dynamicNames.has(entry.templateName)) continue;
22212
+ const current = lastExplicitOrder.get(entry.templateName);
22213
+ if (current == null || entry.order > current) {
22214
+ lastExplicitOrder.set(entry.templateName, entry.order);
22215
+ }
22216
+ }
22217
+ const keepsWinningExplicitAttr = (entry) => !dynamicNames.has(entry.templateName) || entry.order === lastExplicitOrder.get(entry.templateName);
22218
+ return {
22219
+ staticAttrs: staticAttrs.filter(keepsWinningExplicitAttr).map((entry) => entry.attr),
22220
+ dynamicAttrs: dynamicAttrs.filter(keepsWinningExplicitAttr).map((entry) => entry.attr),
22221
+ spreadAttrs
22222
+ };
22223
+ }
22145
22224
  function parseAttributes(path2) {
22146
22225
  const staticAttrs = [];
22147
22226
  const dynamicAttrs = [];
22148
22227
  const spreadAttrs = [];
22149
22228
  if (!path2.isJSXElement()) {
22150
- return { staticAttrs, dynamicAttrs, spreadAttrs };
22229
+ return { staticAttrs: [], dynamicAttrs: [], spreadAttrs };
22151
22230
  }
22152
22231
  const attributes = path2.get("openingElement.attributes");
22153
- for (const attrPath of attributes) {
22232
+ for (const [order, attrPath] of attributes.entries()) {
22154
22233
  if (attrPath.isJSXSpreadAttribute()) {
22155
22234
  spreadAttrs.push({
22156
22235
  value: attrPath.node.argument,
@@ -22158,14 +22237,17 @@ function parseAttributes(path2) {
22158
22237
  });
22159
22238
  continue;
22160
22239
  }
22240
+ if (!attrPath.isJSXAttribute()) {
22241
+ continue;
22242
+ }
22161
22243
  const attr = attrPath.node;
22162
22244
  const attrName = getJSXName(attr.name);
22163
22245
  if (!attr.value) {
22164
- staticAttrs.push({ name: attrName, value: true });
22246
+ staticAttrs.push(createStaticAttr(attrName, true, order));
22165
22247
  continue;
22166
22248
  }
22167
22249
  if (core.types.isStringLiteral(attr.value)) {
22168
- staticAttrs.push({ name: attrName, value: attr.value.value });
22250
+ staticAttrs.push(createStaticAttr(attrName, attr.value.value, order));
22169
22251
  continue;
22170
22252
  }
22171
22253
  if (core.types.isJSXExpressionContainer(attr.value)) {
@@ -22173,36 +22255,10 @@ function parseAttributes(path2) {
22173
22255
  if (core.types.isJSXEmptyExpression(expression)) {
22174
22256
  continue;
22175
22257
  }
22176
- const expressionPath = attrPath.get(
22177
- "value.expression"
22178
- );
22179
- const evaluated = expressionPath.evaluate();
22180
- if (evaluated.confident && isSerializableStaticValue(evaluated.value)) {
22181
- if (shouldResolveStaticCollection(attrName)) {
22182
- let value2 = evaluated.value;
22183
- if (attrName === "style" && isPlainObject(value2)) {
22184
- value2 = styleToString(value2);
22185
- }
22186
- if (isString(value2) || isBoolean(value2) || isNumber(value2)) {
22187
- staticAttrs.push({
22188
- name: attrName,
22189
- value: isBoolean(value2) ? value2 : String(value2)
22190
- });
22191
- continue;
22192
- }
22193
- }
22194
- }
22195
- const value = expression;
22196
- const classified = classifyAttrValue(value);
22197
- dynamicAttrs.push({
22198
- path: attrPath,
22199
- name: attrName,
22200
- value: classified.expression,
22201
- effectKind: classified.kind
22202
- });
22258
+ parseExpressionAttribute(attrPath, attrName, expression, order, staticAttrs, dynamicAttrs);
22203
22259
  }
22204
22260
  }
22205
- return { staticAttrs, dynamicAttrs, spreadAttrs };
22261
+ return finalizeParsedAttributes(staticAttrs, dynamicAttrs, spreadAttrs);
22206
22262
  }
22207
22263
 
22208
22264
  // src/jsx/ir.ts
@@ -22804,7 +22860,13 @@ function generateServer(ir, ctx) {
22804
22860
  // src/jsx/client.ts
22805
22861
  init_cjs_shims();
22806
22862
  var MEMO_STATE_ID = "_p$";
22807
- var MEMO_NEXT_VALUE_ID = "_v$0";
22863
+ var MEMO_NEXT_VALUE_ID = "_v$";
22864
+ function isDynamicChild(node) {
22865
+ return node.type === 3 /* EXPRESSION */ || node.type === 1 /* COMPONENT */ || node.type === 4 /* FOR */;
22866
+ }
22867
+ function hasOwnEffects(element) {
22868
+ return element.dynamicAttrs.length > 0 || element.events.length > 0 || element.spreads.length > 0 || element.ref != null || element.binds.length > 0;
22869
+ }
22808
22870
  function buildTemplateAndFlatten(root, mode) {
22809
22871
  const nodes = [];
22810
22872
  let template = "";
@@ -22820,22 +22882,19 @@ function buildTemplateAndFlatten(root, mode) {
22820
22882
  irElement: element,
22821
22883
  parentId,
22822
22884
  childIndex,
22823
- needsRef: element.dynamicAttrs.length > 0 || element.events.length > 0 || element.spreads.length > 0 || element.ref != null || element.binds.length > 0
22885
+ needsRef: hasOwnEffects(element)
22824
22886
  });
22825
22887
  return;
22826
22888
  }
22827
22889
  template += `<${element.tag}${attrs}>`;
22828
- const hasDynamicChild = element.children.some(
22829
- (c) => c.type === 3 /* EXPRESSION */ || c.type === 1 /* COMPONENT */ || c.type === 4 /* FOR */
22830
- );
22831
- const hasOwnEffects = element.dynamicAttrs.length > 0 || element.events.length > 0 || element.spreads.length > 0 || element.ref != null || element.binds.length > 0;
22890
+ const hasDynamicChildren = element.children.some(isDynamicChild);
22832
22891
  nodes.push({
22833
22892
  id: myId,
22834
22893
  kind: "element",
22835
22894
  irElement: element,
22836
22895
  parentId,
22837
22896
  childIndex,
22838
- needsRef: hasOwnEffects || hasDynamicChild
22897
+ needsRef: hasOwnEffects(element) || hasDynamicChildren
22839
22898
  });
22840
22899
  let templateChildIndex = 0;
22841
22900
  for (const child of element.children) {
@@ -22908,7 +22967,6 @@ function planNavigation(nodes) {
22908
22967
  }
22909
22968
  }
22910
22969
  function ensureNavigated(node) {
22911
- var _a;
22912
22970
  if (navigated.has(node.id)) return;
22913
22971
  if (node.parentId >= 0 && !navigated.has(node.parentId)) {
22914
22972
  const parent = nodeMap.get(node.parentId);
@@ -22918,8 +22976,7 @@ function planNavigation(nodes) {
22918
22976
  steps.push({
22919
22977
  nodeId: node.id,
22920
22978
  type: "child",
22921
- fromNodeId: node.parentId,
22922
- distance: 0
22979
+ fromNodeId: node.parentId
22923
22980
  });
22924
22981
  navigated.add(node.id);
22925
22982
  return;
@@ -22940,26 +22997,16 @@ function planNavigation(nodes) {
22940
22997
  nodeId: node.id,
22941
22998
  type: "next",
22942
22999
  fromNodeId: prevNav.id,
22943
- distance: node.childIndex - prevNav.childIndex
23000
+ index: node.childIndex - prevNav.childIndex
22944
23001
  });
22945
23002
  navigated.add(node.id);
22946
23003
  return;
22947
23004
  }
22948
- const firstChild = parentSiblings == null ? void 0 : parentSiblings.get(0);
22949
- if (firstChild && !navigated.has(firstChild.id)) {
22950
- steps.push({
22951
- nodeId: firstChild.id,
22952
- type: "child",
22953
- fromNodeId: node.parentId,
22954
- distance: 0
22955
- });
22956
- navigated.add(firstChild.id);
22957
- }
22958
23005
  steps.push({
22959
23006
  nodeId: node.id,
22960
- type: "next",
22961
- fromNodeId: (_a = firstChild == null ? void 0 : firstChild.id) != null ? _a : node.parentId,
22962
- distance: node.childIndex
23007
+ type: "nthChild",
23008
+ fromNodeId: node.parentId,
23009
+ index: node.childIndex
22963
23010
  });
22964
23011
  navigated.add(node.id);
22965
23012
  }
@@ -22970,8 +23017,16 @@ function planNavigation(nodes) {
22970
23017
  }
22971
23018
  return steps;
22972
23019
  }
23020
+ function createNavigationExpression(step, fromExpr) {
23021
+ if (step.type === "child") {
23022
+ return core.types.callExpression(useImport("child"), [fromExpr]);
23023
+ }
23024
+ if (step.type === "nthChild") {
23025
+ return core.types.callExpression(useImport("nthChild"), [fromExpr, core.types.numericLiteral(step.index)]);
23026
+ }
23027
+ return core.types.callExpression(useImport("next"), [fromExpr, core.types.numericLiteral(step.index)]);
23028
+ }
22973
23029
  function generateElement(node, state) {
22974
- var _a;
22975
23030
  const { mode } = state;
22976
23031
  if (isStaticSubtree(node)) {
22977
23032
  const tmplId2 = registerTemplate(buildStaticTemplateString(node, mode));
@@ -22987,76 +23042,77 @@ function generateElement(node, state) {
22987
23042
  );
22988
23043
  const varMap = /* @__PURE__ */ new Map();
22989
23044
  varMap.set(0, rootId);
22990
- for (const step of navSteps) {
23045
+ emitNavigationDeclarations(navSteps, varMap, rootId, body);
23046
+ const pendingMemoPatches = [];
23047
+ emitElementEffects(flatNodes, varMap, state, body, pendingMemoPatches);
23048
+ emitMergedMemoEffect(pendingMemoPatches, body);
23049
+ emitDynamicChildInserts(flatNodes, varMap, state, body);
23050
+ body.push(core.types.returnStatement(rootId));
23051
+ return core.types.callExpression(core.types.arrowFunctionExpression([], core.types.blockStatement(body)), []);
23052
+ }
23053
+ function emitNavigationDeclarations(steps, varMap, rootId, body) {
23054
+ var _a;
23055
+ for (const step of steps) {
22991
23056
  const varName = genUid("n$");
22992
23057
  const fromExpr = (_a = varMap.get(step.fromNodeId)) != null ? _a : rootId;
22993
- let navExpr;
22994
- if (step.type === "child") {
22995
- navExpr = core.types.callExpression(useImport("child"), [fromExpr]);
22996
- } else {
22997
- navExpr = core.types.callExpression(useImport("next"), [fromExpr, core.types.numericLiteral(step.distance)]);
22998
- }
23058
+ const navExpr = createNavigationExpression(step, fromExpr);
22999
23059
  body.push(core.types.variableDeclaration("const", [core.types.variableDeclarator(varName, navExpr)]));
23000
23060
  varMap.set(step.nodeId, varName);
23001
23061
  }
23062
+ }
23063
+ function emitElementEffects(flatNodes, varMap, state, body, pendingMemoPatches) {
23002
23064
  for (const flatNode of flatNodes) {
23003
23065
  if (flatNode.kind !== "element" || !flatNode.irElement) continue;
23004
- const el = flatNode.irElement;
23005
- const elExpr = varMap.get(flatNode.id);
23006
- if (!elExpr) continue;
23007
- for (const event of el.events) {
23008
- emitEvent(event, elExpr, body);
23009
- }
23010
- if (el.ref) {
23011
- body.push(core.types.expressionStatement(createRefExpression(elExpr, el.ref.value)));
23012
- }
23013
- for (const bind of el.binds) {
23014
- emitBind(bind, elExpr, body);
23015
- }
23016
- for (const attr of el.dynamicAttrs) {
23017
- emitDynamicAttr(attr, elExpr, body, state);
23018
- }
23019
- for (const spread of el.spreads) {
23020
- emitSpread(spread, elExpr, body, state);
23021
- }
23066
+ const target = varMap.get(flatNode.id);
23067
+ if (!target) continue;
23068
+ emitElementEffect(flatNode.irElement, target, state, body, pendingMemoPatches);
23069
+ }
23070
+ }
23071
+ function emitElementEffect(element, target, state, body, pendingMemoPatches) {
23072
+ for (const event of element.events) {
23073
+ emitEvent(event, target, body);
23074
+ }
23075
+ if (element.ref) {
23076
+ body.push(core.types.expressionStatement(createRefExpression(target, element.ref.value)));
23077
+ }
23078
+ for (const bind of element.binds) {
23079
+ emitBind(bind, target, body);
23022
23080
  }
23081
+ for (const attr of element.dynamicAttrs) {
23082
+ emitDynamicAttr(attr, target, body, state, pendingMemoPatches);
23083
+ }
23084
+ for (const spread of element.spreads) {
23085
+ emitSpread(spread, target, body, state, pendingMemoPatches);
23086
+ }
23087
+ }
23088
+ function emitDynamicChildInserts(flatNodes, varMap, state, body) {
23023
23089
  for (const flatNode of flatNodes) {
23024
23090
  if (flatNode.kind !== "anchor" || !flatNode.dynamicChild) continue;
23025
- const parentExpr = varMap.get(flatNode.parentId);
23026
- const anchorExpr = varMap.get(flatNode.id);
23027
- if (!parentExpr || !anchorExpr) continue;
23028
- const child = flatNode.dynamicChild;
23029
- if (child.type === 3 /* EXPRESSION */) {
23030
- body.push(
23031
- core.types.expressionStatement(
23032
- core.types.callExpression(useImport("insert"), [
23033
- parentExpr,
23034
- core.types.arrowFunctionExpression([], core.types.cloneNode(child.value, true)),
23035
- anchorExpr
23036
- ])
23037
- )
23038
- );
23039
- } else if (child.type === 1 /* COMPONENT */) {
23040
- const componentExpr = generateComponent(child, state);
23041
- body.push(
23042
- core.types.expressionStatement(
23043
- core.types.callExpression(useImport("insert"), [parentExpr, componentExpr, anchorExpr])
23044
- )
23045
- );
23046
- } else if (child.type === 4 /* FOR */) {
23047
- body.push(
23048
- core.types.expressionStatement(
23049
- core.types.callExpression(useImport("insert"), [
23050
- parentExpr,
23051
- generateFor(child, state),
23052
- anchorExpr
23053
- ])
23054
- )
23055
- );
23056
- }
23091
+ const parent = varMap.get(flatNode.parentId);
23092
+ const anchor = varMap.get(flatNode.id);
23093
+ if (!parent || !anchor) continue;
23094
+ body.push(
23095
+ core.types.expressionStatement(createInsertCall(parent, flatNode.dynamicChild, anchor, state))
23096
+ );
23097
+ }
23098
+ }
23099
+ function createInsertCall(parent, child, anchor, state) {
23100
+ if (child.type === 4 /* FOR */) {
23101
+ const insert = useImport("insert");
23102
+ return core.types.callExpression(insert, [parent, generateFor(child, state), anchor]);
23103
+ }
23104
+ const childExpression = createDynamicChildExpression(child, state);
23105
+ return core.types.callExpression(useImport("insert"), [parent, childExpression, anchor]);
23106
+ }
23107
+ function createDynamicChildExpression(child, state) {
23108
+ switch (child.type) {
23109
+ case 3 /* EXPRESSION */:
23110
+ return core.types.arrowFunctionExpression([], core.types.cloneNode(child.value, true));
23111
+ case 1 /* COMPONENT */:
23112
+ return generateComponent(child, state);
23113
+ case 4 /* FOR */:
23114
+ return generateFor(child, state);
23057
23115
  }
23058
- body.push(core.types.returnStatement(rootId));
23059
- return core.types.callExpression(core.types.arrowFunctionExpression([], core.types.blockStatement(body)), []);
23060
23116
  }
23061
23117
  function emitEvent(event, target, body) {
23062
23118
  if (event.delegated) {
@@ -23097,65 +23153,75 @@ function emitBind(bind, target, body) {
23097
23153
  if (modifiersArg) args.push(core.types.cloneNode(modifiersArg));
23098
23154
  body.push(core.types.expressionStatement(core.types.callExpression(useImport("bindElement"), args)));
23099
23155
  }
23100
- function emitDynamicAttr(attr, target, body, state) {
23156
+ function emitDynamicAttr(attr, target, body, state, pendingMemoPatches) {
23101
23157
  emitPatchOrEffect(
23102
23158
  target,
23103
23159
  attr.name,
23104
23160
  attr.value,
23105
23161
  attr.kind,
23106
23162
  body,
23107
- state,
23108
- () => createEffectKey(attr.name, state.effectIndex++)
23163
+ () => createEffectKey(attr.name, state.effectIndex++),
23164
+ pendingMemoPatches
23109
23165
  );
23110
23166
  }
23111
- function emitSpread(spread, target, body, state) {
23167
+ function emitSpread(spread, target, body, state, pendingMemoPatches) {
23112
23168
  emitPatchOrEffect(
23113
23169
  target,
23114
23170
  "_$spread$",
23115
23171
  spread.value,
23116
23172
  spread.kind,
23117
23173
  body,
23118
- state,
23119
- () => createSpreadEffectKey(state.effectIndex++)
23174
+ () => createSpreadEffectKey(state.effectIndex++),
23175
+ pendingMemoPatches
23120
23176
  );
23121
23177
  }
23122
- function emitPatchOrEffect(target, attrName, value, kind, body, state, getEffectKey) {
23178
+ function emitPatchOrEffect(target, attrName, value, kind, body, getEffectKey, pendingMemoPatches) {
23123
23179
  if (kind === "static") {
23124
23180
  body.push(core.types.expressionStatement(createPatchCall(useImport, target, attrName, value)));
23125
23181
  return;
23126
23182
  }
23127
- const effectKey = getEffectKey();
23128
- emitMemoEffect(effectKey, target, attrName, value, body);
23129
- }
23130
- function emitMemoEffect(effectKey, target, attrName, value, body) {
23131
- const effectState = core.types.memberExpression(core.types.identifier(MEMO_STATE_ID), core.types.identifier(effectKey));
23132
- const valueId = core.types.identifier(MEMO_NEXT_VALUE_ID);
23133
- const updateCall = createPatchCall(useImport, target, attrName, value, {
23134
- previousValue: effectState,
23135
- nextValue: core.types.assignmentExpression("=", effectState, valueId)
23183
+ pendingMemoPatches.push({
23184
+ effectKey: getEffectKey(),
23185
+ target,
23186
+ attrName,
23187
+ value
23136
23188
  });
23189
+ }
23190
+ function emitMergedMemoEffect(patches, body) {
23191
+ if (patches.length === 0) return;
23192
+ const memoStateId = core.types.identifier(MEMO_STATE_ID);
23193
+ const effectBody = patches.flatMap((patch) => createMemoPatchStatements(patch, memoStateId));
23194
+ effectBody.push(core.types.returnStatement(memoStateId));
23137
23195
  body.push(
23138
23196
  core.types.expressionStatement(
23139
23197
  core.types.callExpression(useImport("memoEffect"), [
23140
- core.types.arrowFunctionExpression(
23141
- [core.types.identifier(MEMO_STATE_ID)],
23142
- core.types.blockStatement([
23143
- core.types.variableDeclaration("var", [core.types.variableDeclarator(valueId, value)]),
23144
- core.types.expressionStatement(
23145
- core.types.logicalExpression(
23146
- "&&",
23147
- core.types.binaryExpression("!==", valueId, effectState),
23148
- updateCall
23149
- )
23150
- ),
23151
- core.types.returnStatement(core.types.identifier(MEMO_STATE_ID))
23152
- ])
23153
- ),
23154
- core.types.objectExpression([core.types.objectProperty(core.types.identifier(effectKey), core.types.objectExpression([]))])
23198
+ core.types.arrowFunctionExpression([memoStateId], core.types.blockStatement(effectBody)),
23199
+ createMemoInitialState(patches)
23155
23200
  ])
23156
23201
  )
23157
23202
  );
23158
23203
  }
23204
+ function createMemoPatchStatements(patch, memoStateId) {
23205
+ const effectState = core.types.memberExpression(memoStateId, core.types.identifier(patch.effectKey));
23206
+ const valueId = genUid(MEMO_NEXT_VALUE_ID);
23207
+ const updateCall = createPatchCall(useImport, patch.target, patch.attrName, valueId, {
23208
+ previousValue: effectState,
23209
+ nextValue: core.types.assignmentExpression("=", effectState, valueId)
23210
+ });
23211
+ return [
23212
+ core.types.variableDeclaration("var", [core.types.variableDeclarator(valueId, core.types.cloneNode(patch.value, true))]),
23213
+ core.types.expressionStatement(
23214
+ core.types.logicalExpression("&&", core.types.binaryExpression("!==", valueId, effectState), updateCall)
23215
+ )
23216
+ ];
23217
+ }
23218
+ function createMemoInitialState(patches) {
23219
+ return core.types.objectExpression(
23220
+ patches.map(
23221
+ (patch) => core.types.objectProperty(core.types.identifier(patch.effectKey), core.types.identifier("undefined"))
23222
+ )
23223
+ );
23224
+ }
23159
23225
  function generateComponent(node, state) {
23160
23226
  const renderedChildren = renderChildExpressions(
23161
23227
  node.children,