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
Request: New dist/controller.min.js that only includes Controller. #638
Comments
I'm able to workaround this by adding |
Hey @dkniffin, I just checked how that might work. Technically this is possible, but I'm wondering if it wouldn't make more sense if you instead tree-shake the bundle on your end. Technically the tool should be smart enough to pull out the Constant and extract the related "dependencies" with it. Or is there any reason why you wouldn't want to go that route? |
@marcoroth maybe my app is configured wrong, but it's not tree-shaking everything else out currently. Do you know if there's a config for that in webpacker? |
@dkniffin did you have a chance to look at https://webpack.js.org/guides/tree-shaking? Edit: there's also this good article on the topic: https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366 |
@marcoroth I took a look at that. I'll need to dig in more to my config and see if something is misconfigured. I'm definitely doing But if I understand the webpack options correctly, only |
@dkniffin the tree-shaking should be smart enough to figure out which parts to keep even if the sources are minified. The |
@marcoroth I found out that my project was not configured correctly for tree-shaking. I tested by adding all of It turns out I was missing HOWEVER, once I got that enabled, it did not make a difference on the size of Stimulus inside my packs that only use |
It would be interesting to see if this would also help for your case here: #623 (comment) If you would do something like: import { Controller } from "@hotwired/stimulus/dist/core/controller" |
@marcoroth yep! That made a huge difference. So, tree shaking isn't working with the full module, but it does work with the individual file. So I think my original request here is still valid. Is that something you'd consider including in the build? |
@marcoroth actually, I just looked into the I think somewhere in my configs, tree-shaking is not properly configured. But I'm not sure where. I tested by adding a function in one of my own files with a ton of lorem-ipsum text. Then I import another function from that file. I'd expect that function to be removed, but it's not. I don't understand. Wepback(er)... 😩 But I did test with the dev build and import you suggested, and without |
Using #686 you should now be able to import the controller class like: import { Controller } from "@hotwired/stimulus/dist/core/controller" The tree-shake config depends on the bundler you are using. But with having the classes available by themselves should allow any bundler to tree-shake the files and imports accordingly. Feel free to re-open this issue if you still think we need something else in Stimulus. Thank you! |
In version |
@markhealey they will ship with the next version of Stimulus. In the meantime, you can install the latest dev-build to already make use of them today:
|
Over on https://discuss.hotwired.dev/t/advice-request-stimulus-webpack-best-practices-bundle-splitting-for-multi-page-ssr-site/612/12 we're discussing how to split apart stimulus controllers into separate packs in webpack. I think we've landed on a good way to do that:
This works. However, it means that Stimulus is loaded into each pack, increasing the size of each pack unnecessarily. The only thing we need from Stimulus in there is
Controller
.To fix this, I want to request creating a new file
dist/controller.min.js
so that we can doimport { Controller } from '@hotwired/stimulus/dist/controller.js'
The text was updated successfully, but these errors were encountered: