-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Extensions don't have UMD wrapper #1690
Comments
I'm going to close this issue. If you're already using a bundler then it is not a huge hassle to configure your bundler to import htmx properly. I don't think this is worth polluting the I'm open to re-opening it if there's a vocal constituency that feels differently. |
Re-opening per discussion in #1692
Importing htmx extensions into the Vite build system which is not how we recommend installing htmx. I understand that some people do it that way. But in that case they are already using a build system, so the advantage of us having already UMD-ifyed the extension is basically nil, because the purpose of wrapping htmx core in a UMD wrapper is to allow people to import it natively in the browser, no matter which module system they're using. So from my perspective, if you want to bundle htmx, it's not crazy to ask you to use a plugin that will bundle all that js for you. |
I opened the original issue about this problem, and while I don't have a strong opinion on the solutions you're discussing, I don't think it's currently clear enough from the documentation that using npm and a build system is not recommended or has drawbacks. I think it's a reasonable requirement to have users install that Vite plugin, but if that's the "solution" it should be made more clear in the installation instructions, maybe alongside the existing npm/webpack instructions. |
Yeah, I use htmx with npm just entirely to serve the I'm also open to htmx-side solutions, but speaking for myself, I don't love the two solutions that I've seen so far. Appending htmx to the window (in addition to the document) feels like a weird hack, and wrapping all the extension in a UMD module is clunky. On some level, I'm concerned it's a fools errand to alter the htmx source to meet the demands for various build systems, rather than provide some reasonable common denominator (a JS file) that they can work with. |
The UMD wrapper does exactly the same when in browser environment and the current documentation https://htmx.org/docs/#webpack pointing you in the same direction.
Of course, my fix is not the perfect solution, but it introduces much less code than any other solution. And I agree, we should update the documentation in this section. |
Yes, and I appreciate that!
Don't we basically just need to update this and say "applies to Vite as well"? |
I don't think that is enough. You have to explain that you need to expose |
Workaround via webpack // webpack.config.js
import webpack from 'webpack';
/** @type {import("webpack").Configuration} */
export default {
entry: './src/index.js',
[...]
+ plugins: [
+ new webpack.ProvidePlugin({
+ htmx: 'htmx.org',
+ }),
+ ],
}; |
I'm running into this as well with vite. I think the easiest solution atm is to just manually copy in the JS and import it. I tried both suggested methods above and as the linked (closed) discussion says I also never see the |
It should be made possible to import htmx in ESM without any globals. It can be done by placing a suitable ESM file in the package and pointing to it in package.json |
Needs to use htmx with window, because it doesn't have a UMD wrapper (bigskysoftware/htmx#1690) window.htmx = htmx;
@xhaggi @alexpetros @matteocontrini @anbraten @danthegoodman1 @silverwind import 'htmx.org';
import 'htmx-ext-extension-name'; // Replace `extension-name` with the name of the extension The way I have implemented it mirrors the deployment of the core HTMX package. Please check out the PR bigskysoftware/htmx-extensions#123 and hopefully this issue can be closed after it's merged. @Telroshan this issue should be moved to htmx-extensions repository. |
At the moment you need some workarounds to use an extension with Vite, Rollup or other build tools because of a missing UMD wrapper. Furthermore, the extensions are not minified like
htmx.js
.Workaround via window
Create a local
htmx.js
with the following content:Import this file before importing an extension.
Workaround via rollup-plugin-inject (Vite)
The text was updated successfully, but these errors were encountered: