Javascript setup with jsbundling-rails and esbuild #571
-
Hello, We are using jsbundling-rails and esbuild to bundle the JS code in our app, and I am having a hard time figuring out how to enable Pagy's Javascript features with our setup. I've read the docs at https://ddnexus.github.io/pagy/docs/api/javascript/setup/ and understand that not providing a npm package is a deliberate decision, but this poses a bit of a problem for us. AFAICT, the only way to integrate the Pagy JS helpers in our app would be to bypass esbuild and set up Sprockets to process the Pagy JS file, as described in this StackOverflow answer. If we do this, the event handling initialization code also has to be set up in a custom file processed by Sprockets: it can't be done via Stimulus, because Stimulus code is handled by esbuild, which has no knowledge of Pagy in this setup. As much as I'd like to use Pagy's JS, the above is not a viable solution for us. Am I missing something obvious? If not, would the maintainers reconsider shipping Pagy's JS code as an npm package? This is after all what Turbo itself does, as well as some other Ruby gems like ahoy. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
@olivier-thatch Sorry, Javascript handling is a Babel of different possible/impossible setups, and I am very far to remember them all, so here are a few concepts that should help. First of all: the reason why pagy is not a npm package is because the JS code is strictly coupled with, and depending from the ruby code for each specific version. In other words, with a npm package for pagy... you would have to update the npm package with the same identical version of the pagy gem each time you do a Now, the general concept to make JS work with pagy in a bundled environment is quite simple: you have to instruct your bundle system to bundle the You can consider (configure and use) the pagy module almost exactly as one of your own JS module written in your app. The only difference is that the pagy module is not in a path of your app, but it's in After you "convince" your builder about looking into the pagy path, and/or bundling the Please, research how to configure your specific bundle system and let us know (for the benefit of other users), or feel free to ask more questions/details. |
Beta Was this translation helpful? Give feedback.
-
Perhaps this demo app may help: https://github.com/benkoshy/pagy-esbuild Especially look at how it is built: look at the package.json file: https://github.com/benkoshy/pagy-esbuild/blob/f4234b664caf0d9ccf08963da522a2be253bbbd4/package.json#L10 I admit, in light of newer information, it may not be the canonical approach. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the quick reply and docs update! Unfortunately it doesn't quite work for us, because we're also using TypeScript for our Stimulus controllers and while esbuild supports I tried using path mapping, but:
Anyway, I understand that this is getting way too specific and not something that can (or ought to) be covered by the docs. I might take another stab at this when I have the time, and if I do manage to get it to work, I'll post the results here. Feel free to close the discussion for now. Really appreciate the help you've provided! |
Beta Was this translation helpful? Give feedback.
Fixed in 1605b22. The Javascript Setup doc should be complete now.