Skip to content

Commit

Permalink
fix: tell @marko/compiler about function event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Oct 3, 2024
1 parent f1ee0c9 commit 2fba2c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-years-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/tags-api-preview": patch
---

Use `meta.hasFunctionEventHandlers` from recent marko releases when we detect an event or change handler to ensure the template is marked for client side compilation.`
4 changes: 4 additions & 0 deletions src/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import type { types as t } from "@marko/compiler";
import wrapperComponent from "./wrapper-component";
import cachedValues from "./cached-values/transform";
import nativeTagVar from "./native-tag-var/transform";
import nativeTagHandlers from "./track-function-handlers";
import hoistTagVars from "./hoist-tag-vars/transform";
import featureDetection from "./feature-detection";
import tagBodyParameters from "./tag-body-parameters";
import customTagVar from "./custom-tag-var";
import assignmentsToChangeCall from "./assignments-to-change-call";
import attributeBindings from "./attribute-bindings";
import trackFunctionHandlers from "./track-function-handlers";

export default [
featureDetection,
Expand All @@ -17,6 +19,8 @@ export default [
hoistTagVars,
attributeBindings,
nativeTagVar,
nativeTagHandlers,
customTagVar,
tagBodyParameters,
trackFunctionHandlers,
] as t.Visitor[];
25 changes: 25 additions & 0 deletions src/transform/track-function-handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { types as t } from "@marko/compiler";
import isApi from "../util/is-api";
const eventNameReg = /^on[A-Z]/;
const changeNameReg = /Change$/;

export default {
MarkoTag(tag: t.NodePath<t.MarkoTag>) {
if (isApi(tag, "tags")) {
// Tells Marko that this tag uses event handlers.
const file = tag.hub.file;
const meta = file.metadata.marko as any;
if (!meta.hasFunctionEventHandlers) {
for (const attr of tag.node.attributes) {
if (
t.isMarkoAttribute(attr) &&
(eventNameReg.test(attr.name) || changeNameReg.test(attr.name))
) {
meta.hasFunctionEventHandlers = true;
break;
}
}
}
}
},
} as t.Visitor;

0 comments on commit 2fba2c9

Please sign in to comment.