htmx icon indicating copy to clipboard operation
htmx copied to clipboard

Update webpack and Vite istallation instructions for htmx 2.0

Open restless opened this issue 1 year ago • 11 comments

Description

The webpack installation instructions are no longer valid for htmx 2.0. There are also no installation instructions for Vite. This is affecting multiple people, judging from the issue tracker.

Corresponding issue:

  • https://github.com/bigskysoftware/htmx/issues/2628
  • https://github.com/bigskysoftware/htmx/issues/2657
  • https://github.com/bigskysoftware/htmx/issues/2659

Testing

It's a documentation update and the only way to test it is to follow the new guidelines. These new guidelines are what works for me and other people (see issues listed above).

Checklist

  • [x] I have read the contribution guidelines
  • [x] I have targeted this PR against the correct branch (master for website changes, dev for source changes)
  • [x] This is either a bugfix, a documentation update, or a new feature that has been explicitly approved via an issue
  • [x] I ran the test suite locally (npm run test) and verified that it succeeded

restless avatar Jun 24 '24 08:06 restless

I think this should include

import inject from '@rollup/plugin-inject'

Darkproduct avatar Jun 24 '24 14:06 Darkproduct

Also see bigskysoftware/htmx-extensions#128

Darkproduct avatar Jun 24 '24 14:06 Darkproduct

Thanks @Darkproduct I've rewritten the docs to include your suggestions. To summarize there are the following options:

  1. simply import htmx - it'll work then but won't allow you to use plugins or htmx.methods due to lack of the htmx global variable
  2. import htmx and manually assign it to the window.htmx global variable
  3. in case of webpack inject htmx via plugin
  4. in case of Vite inject htmx via plugin

restless avatar Jun 25 '24 06:06 restless

I am changing the main in package.json to be the esm file:

https://github.com/bigskysoftware/htmx/blob/52b357089db95cea13411999a6b7913f0f0bc39b/package.json#L23

Does this change these instructions at all? (Sorry I know nothing about Webpack or Vite)

1cg avatar Jun 27 '24 19:06 1cg

@1cg great to hear. Yes, the change allows us to simply inject/provide htmx.org instead of the dist/htmx.esm.js version. Let me push the updated docs version that reflects that change.

restless avatar Jun 28 '24 06:06 restless

Is this possible in esbuild?

I had to use a work around like so: https://discord.com/channels/725789699527933952/1252928586206154753/1252933134396428348

jackielii avatar Jul 08 '24 17:07 jackielii

Is this possible in esbuild?

I had to use a work around like so: https://discord.com/channels/725789699527933952/1252928586206154753/1252933134396428348

@jackielii I don't use esbuild directly, however define (https://esbuild.github.io/api/#define) looks promising. Have you tried it?

restless avatar Jul 12 '24 06:07 restless

This is going to be related too: https://github.com/bigskysoftware/htmx/issues/2629#issuecomment-2226447245

alexpetros avatar Jul 13 '24 18:07 alexpetros

I have not yet had success getting HonoX+Vite+Htmx working with these instructions, but updated guidance is important. :+1:

Update:

Ok, I was able to get this working but not with the pretty import for htmx. (the separate shim file also worked, but so does this inline approach in my limited testing). Am I doing something wrong?

I am using htmx 2.0.4 and htmx-ext-preload 2.1.1.

Not working Working
Pretty import
htmx is undefined :(
Not as pretty import
window.htmx is manually set
htmx is defined :tada:
Screenshot 2025-04-14 145806 Screenshot 2025-04-14 145652

gitblit avatar Apr 14 '25 18:04 gitblit

Hello all, does this PR need updating? I agree on removing "probably" and being firmer in what we recommend. Unfortunately, my JS build-fu is (intentionally) very weak. Looking for guidance!

1cg avatar Jun 02 '25 17:06 1cg

@1cg I've updated the PR. It now reflects changes implemented by @marisst in #123 and makes the docs much simpler.

I tested it with my Vite based setup and it works well. I believe it'll work with other bundlers, like webpack, too as there are no extra steps (providing/injecting htmx) needed anymore.

restless avatar Jun 02 '25 21:06 restless