marko-js / tags-api-preview

Preview the Marko 6 "Tags API" in Marko 5!
MIT License
23 stars 7 forks source link

Using a spread and function attribute compiles to code that reassigns a const #56

Closed AngusMorton closed 3 months ago

AngusMorton commented 3 months ago

Version: 0.7.4

The following marko template, when compiled for the server, will compile into code that reassigns a const variable.

<let/foobar="foo"/>
<div
  ...input
  onClick() { foobar = "bar"; }
/>

Compiles to:

const foobar = "foo";
var _meta = {};
out.w(
  `<div${_marko_attrs(
    _extractHandlers({
      ...input,
      onClick: function () {
        foobar = "bar";
      },
    })
  )}></div>`
);

This is a problem for us because we deploy to Cloudflare Pages and when wrangler checks the final server code (_worker.js), it throws an error because of the const reassignment.

Ideally, Cloudflare wouldn't throw this error because the handlers never run(as far as I can tell), but here we are. If you think it makes more sense to try and resolve this on the Cloudflare Pages/wrangler side I'm happy to do that instead.

Possible Fix

The easiest fix would be to change the let tag to output a let instead of a const on the server. I'm happy to make that change if it's the correct change.

Playground https://markojs.com/playground/#NobwRAdghgtgpmAXGAlhAJnAHgOhlAJwGsB7MAGjAAcoAXACyTAHoBjEmKkiOCWgZ2ZpMufMTKV2fXrSYAeADZxazAGYkSAI0IBeADph1JA8wB8eiHPQoAbhYAE9nM7RUArrQf3uAYQUpWIgAKAEp7EHsjbQJ7HXsDaIMAbnsAXwszMFSAXSA

DylanPiercey commented 3 months ago

@AngusMorton I think changing the output to a let on the server could be good. Feel free to give it a shot, I'm happy to give pointers here or on discord 😃