Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tell @marko/compiler about function event handlers #66

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading