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

Alpine.js doesn't work with Phoenix 1.6.2 because of too low esbuild target #4579

Closed
dsincl12 opened this issue Nov 5, 2021 · 6 comments
Closed

Comments

@dsincl12
Copy link

dsincl12 commented Nov 5, 2021

Environment

  • Elixir version (elixir -v): 1.12.3
  • Phoenix version (mix deps): 1.6.2
  • Operating system: WSL Ubuntu 20.04 or Windows 11

Expected behavior

Alpine functioning properly

Actual behavior

module.esm.js:1635 Alpine Expression Error: msg is not defined

Expression: "msg"

<p x-data=​"{msg:​ 'Alpine works!'}​" x-text=​"msg">​Alpine does not work :(​</p>​

After spending a day with this issue I finally figured out why it didn't work. Alpine.js doesn't work because of the low target version for esbuild (--target=es2016) in config.exs. Bumping the target to es2018 makes everything work like a charm :)

@derpycoder
Copy link

derpycoder commented Nov 6, 2021

I faced the same issue today, you are a lifesaver.

Thank you so much.

P.S. I wish Petal stack was a standard and would work out of the box, without so many setups.

@alaadahmed
Copy link

alaadahmed commented Nov 8, 2021

still doesn't work even after bump --target=es2018 as mentioned. It was working actually before updating phoenix :(

<div x-data="{msg: 'Alpine works!'}">
  <span x-text="msg">Alpine does not work</span>
</div>

@slashdotdash
Copy link

slashdotdash commented Nov 9, 2021

It took me a couple of hours to track down the root cause of the following AlpineJS v3.5.0 error:

Alpine Expression Error: func(...).catch is not a function

Expression: "{ isOpen: true }"

<div x-data="{ isOpen: true }">

The default esbuild configuration created by Phoenix 1.6.2 in config/config.exs, as below, uses es2016 as the target.

config :esbuild,
  version: "0.12.18",
  default: [
    args: ~w(js/app.js --bundle --target=es2016 --outdir=../priv/static/assets --external:/fonts/* --external:/images/*),
    cd: Path.expand("../assets", __DIR__),
    env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}
  ]

This doesn't work with Alpine v3 as it requires support for async/await which is in ECMAScript 2017 (see #2318).

Changing the target to es2017 fixes Alpine with Phoenix for me:

config :esbuild,
  version: "0.12.18",
  default: [
    args: ~w(js/app.js --bundle --target=es2017 --outdir=../priv/static/assets --external:/fonts/* --external:/images/*),
    cd: Path.expand("../assets", __DIR__),
    env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}
  ]

@cseeger
Copy link

cseeger commented Nov 9, 2021

@slashdotdash, just curious - why es2017 and not es2018 as described by @dsincl12?

@slashdotdash
Copy link

@slashdotdash, just curious - why es2017 and not es2018 as described by @dsincl12?

To allow support for slightly older browsers.

@dongennl
Copy link

Thank you so much. A whole morning....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants