From 2fba2c9df78462b7b645a483a23f25acb7a4a3df Mon Sep 17 00:00:00 2001 From: dpiercey Date: Thu, 3 Oct 2024 15:24:46 -0700 Subject: [PATCH] fix: tell @marko/compiler about function event handlers --- .changeset/fifty-years-sneeze.md | 5 +++++ src/transform/index.ts | 4 ++++ src/transform/track-function-handlers.ts | 25 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 .changeset/fifty-years-sneeze.md create mode 100644 src/transform/track-function-handlers.ts diff --git a/.changeset/fifty-years-sneeze.md b/.changeset/fifty-years-sneeze.md new file mode 100644 index 0000000..01616a1 --- /dev/null +++ b/.changeset/fifty-years-sneeze.md @@ -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.` diff --git a/src/transform/index.ts b/src/transform/index.ts index 5133ed1..21909a4 100644 --- a/src/transform/index.ts +++ b/src/transform/index.ts @@ -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, @@ -17,6 +19,8 @@ export default [ hoistTagVars, attributeBindings, nativeTagVar, + nativeTagHandlers, customTagVar, tagBodyParameters, + trackFunctionHandlers, ] as t.Visitor[]; diff --git a/src/transform/track-function-handlers.ts b/src/transform/track-function-handlers.ts new file mode 100644 index 0000000..797501d --- /dev/null +++ b/src/transform/track-function-handlers.ts @@ -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) { + 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;