-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
feature: enable multiple file types per entry #2213
Conversation
i will come back to check on this in a day or two. If at a high level people are ok with this extension I will put a little spit shine on there and add the tests. |
There have been a lot of users who incorrectly put all of their frontend assets into the As Gaurav points out in those tickets, the
Edit: This is not just a webpackER problem, I have had similar problems in vanilla webpack as well. |
@jakeNiemiec thanks for the update. I am working through the details with the rest of my team with regards to the style-loader issue you raised. However I wanted to acknowledge that everything you and @gauravtiwari have said both here and in other tickets make good sense. The only thing I would ask you consider in addition to the great webpackER defaults/conventions, that we still allow people who want or need to use other features of webpack access to them. Without some kind of patch like mine you just can't do multi file entry points with webpackER (unless you monkeypatch or otherwise edit the code at runtime). I know there are workarounds where you can link your styles into your one JS entry point file but those don't work well for our use case. If we don't want this patch to go in as is perhaps we could open up some kind of a config or setting option to enable multi file entry points (opt-in) for the community. For those of us that don't need a JS render pipeline and just want sprinkles of JS (while keeping our packs to 1 per page with css) this multi file entry point was designed perfectly for that case. I'll drop another update once we have finished our regression testing however right now this path (especially with chunking) provides amazing speed improvements over the sprockets + turbolinks approach. |
Speaking as someone who rolls with a customized version of webpacker in production, I know where you are coming from. Think of entry: {
home: ['./home.js', './home.scss'],
account: ['./account.js', './account.scss']
}, Turns into: app/javascript:
├── packs: # Think of this as entry
│ └── home.js
│ └── home.scss
│ └── account.js
│ └── account.scss Then in your home view: <%= javascript_pack_tag 'home' %>
<%= stylesheet_pack_tag 'home' %> I'm pretty sure that even Could you tell me a bit about what you would like to change within the context of this flow? (e.g.: actual input/output vs desired input/output) |
I originally tried something very similar. We opted to go with a js and css file per page for our migration to webpack. We then import in libraries and styles into the JS and SCSS as necessary from outside of the packs directory. Which creates something very similar to what we are used to from the old sprockets days. With chunking enabled we effectively get super lightweight, intelligently cached, extraordinary simple convention for per page assets (not to far from what sprockets originally gave us but with all of the webpack bells & whistles) Here is our directory structure:
However when you compile a scss file you get some js file that overrides the js file you had intended. This is fixed however if you apply the patch I created as it explicitly tells webpack that you want both files (this is my working assumption based on the docs, I haven't confirmed that in the webpack(er) source yet) It is likely related to: If you have a way to get the same behavior (leveraging something like this https://webpack.js.org/guides/entry-advanced/) I would love to use it. (unrelated to this specific issue, but still exciting, if you apply this strategy your compile times for any given page with or without the dev server drop dramatically with the |
Rails/Basecamp devs have a new framework that matches what is seems like you want to do: https://github.com/stimulusjs/stimulus |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you create some tests?
@jakeNiemiec Of course I can add some tests, however does that mean you think there is a chance we will merge this PR/concept? Do you think we should do anything around config too or just leave it as is? WRT Stimulus: We have looked at that for a while and it is a cool idea and maybe we could try that out on one of our greenfield projects. For now however I need to get webpack(er) working with my 100s of view specific js and css. woohoo. haha |
It's not that your idea is without merit, it just feels like an XY_problem that cuts against the grain inherent to webpack (packing a bunch of files into just a few), but, if it does not negatively impact webpacker at all, I don't see a reason why it couldn't be merged.
Ideally, you could have a single pack file set up to lazy/dynamically load any of the hundreds of js/css files you need for a single view. This way, you can chunk/cache/prefetch/tree-shake your files and skip the webpack entry overhead. Read #1967 (comment) for a decent how-to on this. Otherwise, I feel like stimulus or even a basic |
Awesome, Ok i'll add some tests in there and try to get this ready to ship. WRT to the 100s of view specific js and css files. I think I may have miscommunicated. It's not that all of these files are included on the same view. Rather we have 100s of views with their own special combination of libs and specific data. We just want 1 scss and 1 js file per entry point and in those files we pull in whatever we need, then we let webpack do its chunking and tree shaking etc. I was just trying to convey we probably can't switch over to stimulus just yet on this large of a code base in one go (aka we still would love to have this feature). Many thanks @jakeNiemiec ! I'll get to work on this and try to have it ship shape by EOW. |
No problem, tests are currently broken so there's plenty of time 😅 |
@jakeNiemiec Changed impl to ensure underlying representation would not change for people using the single file entry points. Also noticed the testing for this module is pretty sparse, I know Rails usually keeps testing more "pragmatic" so I added the happy path for the added functionality, happy to add a few corner cases but may need to create a few more files. Let me know if you would like any edits (also I am not averse to you editing on my behalf if you prefer). 🍻Cheers 🍻 |
@jakeNiemiec just checking in on this guy. Anything else that we need to do? I have a branch that we are using that leverages these changes but I would like to see us merge this or make updates as required so we can move on |
@jakeNiemiec 2020 is here, I am just going through my list of open tickets. Any word on this? |
@jakeNiemiec sorry, where did we leave this again? I thought I saw a email come in from you about something regarding this. The base of this commit needs updating to the latest webpacker version. Is this fixed in some other way upstream now? |
@jakeNiemiec sorry made a mistake, reopening under #2476 , also includes updates from 4.2.2 rebased under it. |
Add support for multiple files per entry
Summary
It is possible to provide different types of files when using an array of values for entry to achieve separate bundles for CSS and JavaScript (and other) files in applications that are not using import for styles in JavaScript (pre Single Page Applications or different reasons).
For additional info: https://webpack.js.org/guides/entry-advanced/
Community Consideration
This is particularly useful if you are migrating from a per page
css/js organization as was common with rails <= 6
Note this has been a source of confusion and has been PR'd in the past as well. However no one pointed out this is a first class supported feature of webpack that is specifically built for static pages which rails does so well (see link above).
Here are just some of the PR/Issues from the past
PR changes
Seamlessly (without breaking support for the original single file per entry model) extends the entryPoint algo to extend entrypoints with multiple files when they are found.
So that you can get something like this
and get a single pack with page specific js and css.