-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
feat: Improved HMR and Cache management on Webpack 5 #136
Conversation
# Conflicts: # package.json # src/next-transpile-modules.js
# Conflicts: # src/next-transpile-modules.js
Better cache management within webpack Faster HMR Simpler resolution compared to before
src/next-transpile-modules.js
Outdated
}); | ||
|
||
const snapshot = Object.assign({}, config.snapshot); | ||
const mainPkg = require(pkgUp.sync()); |
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.
for better caching, it might be worth traversing over the transpiled modules package.json files (the symlinked ones at least)
@@ -187,6 +188,30 @@ const withTmInitializer = (modules = [], options = {}) => { | |||
`**node_modules/{${modules.map((mod) => `!(${mod})`).join(',')}}/**/*`, | |||
]; | |||
|
|||
if (isWebpack5) { | |||
const checkForTranspiledModules = (currentPath) => |
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.
we need to check if its symlinked or not before excluding it from webpack cache, otherwise we are rebuilding uncached modules that are simply in ES6, but not in the monorepo as a workspace package
@martpie this improves HMR build speed alot, for your review |
Thank you very much, can you fix the pipeline? It looks like your removed some changes I've made to the README as well |
A couple of additional things:
|
Ok, good news, I've managed to make it work. There was an issue in my setup. So now, Webpack 5 HMR works, but is unoptimized as you probably realized when tweaking |
@martpie im going to copy paste an update for you. Right now this works for my company and is monorepo heavy so you might want to take some inspiration from it, but it might not work outside yarn workspaces, likely because of the set of package resolutions i do upfront. Other improvements, I've got ESM and CJS resolving and webpack HMR cache seems to be acceptable |
Highlights here This solves for packages like
Match and unmatch are not reliable enough with the current resolutions path. For example i have dirname transpiles everything in src, but not pages. So i need to go up and resolve to the root package then match an unmatch against its module root.
Also i see you've added funding to this project, ill be signing up as a personal sponsor, this isn't easy project :) |
I am still working through a this error though.
|
So let me get this straight: the strategy is to identify the location of the package that won't change (meaning, all the packages, except the ones that are transpiled), finding them (they can be at really different locations due to yarn workspaces etc) and adding them to managed paths to improve the cache performances. Right? |
Yeah. Would you wanna do a zoom call, perhaps we can combine our knowledge and solution something out. Working on my fork has been one of the more interesting challenges and how NTM actually manipulates next has been a huge unlocker for me to use similar conventions to do the same elsewhere. Basically we only need to find paths to package.json files that will not change. Webpack, by default, has managedPaths:[/node_modules/] If we were able to link them to another directory and have webpack resolve node_modules + some other directory it might work around manually managing cache - tho this might cause node resolution issues outside webpack Check out the snapshot documentation on webpack docs. One problem my implementation has is that CI runs out of memory if I use the cache manager. So I can only do it in dev. The reason is because I'm setting cache to memory, and it would be good to check how to, if possible get file system to work. With your latest work. Removing unmatch, solved all remaining issue I had with css errors. Enhanced resolve works. But only for cjs packages. So if it's pure ESM, there's no module, main field. So I have to try, catch (like you do, but then try and resolve to package.json) So I resolve transpiled modules, check if they are symlinked - if so remove the from the cache, then keep searching for other modules that need transpilation. Allow webpack to cache those. So we almost have to do a lookup on all packages that are being used and manually manage them. In all cases I don't need to pass files, just the package.json - which Webpack will know what to do with after. Tobias initially said we should try and make resolve symlinks work. So instead of using node modules at all, it resolves to the actual location we edit in the monorepo. I tried this but ran into loader errors saying modules can't be outside x directory or whatever. I know you're moving away from regex, but that means that work for cache management since it takes either absolute paths or regex Right now my fork, there's a open or in it for resolve symlinks with my latest work. I use this project heavily, we are in the middle of company wide webpack and next 10 upgrades. NTM has been getting a lot of attention as of late. But my fork has hmr working well enough that it's around as fast as webpack 4 was. But I know it's slower than it's supposed to be - scss seems to be very slow or I'm not caching some styles correctly. Also my projects are very very large - so it's hard to tell if my cache management is off or if the size of my projects are just that big. Anyways would love to collaborate with you on a solid solution - i do have a lot of stake in this plugin. |
Without managing the paths. Webpack HMR rebuilds everything attached to changed file and parent. Since next no longer uses its own cache plugin as much in WP5, we have to make Webpack cache the right stuff ourselves otherwise it's extremely slow on HMR. Try setting cache=false and you'll see haha. So right now I'm finding dependencies (not dev deps) then resolving them. Then everything else that passes webpack goes through findPkg to get the root of that package and manage it |
Better cache management within webpack
Faster HMR
Simpler resolution compared to before