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

Update webpack and Vite istallation instructions for htmx 2.0 #2668

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

restless
Copy link

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:

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

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

@Darkproduct
Copy link

I think this should include

import inject from '@rollup/plugin-inject'

@Darkproduct
Copy link

Also see #1690

@Telroshan Telroshan added the documentation Improvements or additions to documentation label Jun 25, 2024
@restless
Copy link
Author

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

@1cg
Copy link
Contributor

1cg commented Jun 27, 2024

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

"main": "dist/htmx.esm.js",

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

@restless
Copy link
Author

@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.

@jackielii
Copy link
Contributor

jackielii commented Jul 8, 2024

Is this possible in esbuild?

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

@Telroshan Telroshan added the 2.0 label Jul 12, 2024
@restless
Copy link
Author

restless commented Jul 12, 2024

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?

@alexpetros
Copy link
Collaborator

This is going to be related too: #2629 (comment)

Copy link

@TeachMeHowToUse2FA TeachMeHowToUse2FA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

import 'htmx.org';
```

However, you will probably need the global `htmx` variable, which gives your code access to methods like

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I may offer a suggestion:

The use of "probably" is unclear to beginners; You give a very concrete example right after (the use of htmx plugins), but is this the only scenario?

www/content/docs.md Outdated Show resolved Hide resolved
Co-authored-by: Théophile Choutri de Tarlé <[email protected]>
build: {...},
plugins: [
inject({
htmx: 'htmx.org',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be: 'htmx.org/dist/htmx.esm'?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plooney81 As per @1cg comment here: #2668 (comment) htmx.org uses htmx.esm.js by default now.
Please see: https://github.com/bigskysoftware/htmx/blob/master/package.json#L23C3-L23C30

In other words htmx.org should work (and works for me) well.

Copy link

@databyte databyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This mimics my setup.

import './htmx.js';

// now you can import extensions
import 'htmx-ext-debug/debug.js';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are describing a workaround which requires creating htmx.js file to inject htmx into the window scope, so that the extensions can use it. It has to be done in a separate file, because ESM imports are asynchronous.

I have created a PR in the extensions repository, which eliminates the need of this workaround. With the new change extensions can be imported like this:

import `htmx.org`;
import `htmx-ext-extension-name`; // Replace `extension-name` with the extension name

It relies on the ESM import mechanism supported by most bundlers, including Webpack, Vite and Rollup.

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

Successfully merging this pull request may close these issues.