Repository navigation
Scripts: Optimize default Webpack configuration - #14860
Conversation
gziolo
left a comment
There was a problem hiding this comment.
It looks like a set of good improvements to land. I will give it a spin later this week and compare performance. Thank you for working on it 👍
| options: getBabelLoaderOptions(), | ||
| }, | ||
| use: [ | ||
| 'thread-loader', |
There was a problem hiding this comment.
over on Calypso, we found that the optimal number wasn't CPU-1, but more like CPU / 2. MIght be worth experimenting with a bit.
There was a problem hiding this comment.
over on Calypso, we found that the optimal number wasn't CPU-1, but more like CPU / 2. MIght be worth experimenting with a bit.
Hey Ben, thanks for checking in. I didn't note it in the original comments, but I did tinker with this value slightly, though in my case I tried increasing it to the exact number of CPUs, and had observed a degraded benefit. I hadn't considered lowering it, but will give it a try.
Is there a pull request or issue which I could reference for that and/or other settings you'd explored?
There was a problem hiding this comment.
|
|
||
| // Provide a fallback configuration if there's not | ||
| // one explicitly available in the project. | ||
| ...( ! hasBabelConfig() && { |
There was a problem hiding this comment.
I had never liked the options weren't colocated with the loader, so I welcome this change!
|
This is the result of my testing:
I've also played with what Ben suggested (a static number of workers from 1 to 4 + a dynamic number as in calypso): it didn't render any improvement over the PR here. Interestingly, this is how my cores behave (tested it several times and can repro consistently, in an Intel® Core™ i7-5600U CPU @ 2.60GHz × 4). In In this PR, they seem to work in a more coordinated fashion: |
294c42e to
ba48f76
Compare
| // WP_DEVTOOL global variable controls how source maps are generated. | ||
| // See: https://webpack.js.org/configuration/devtool/#devtool. | ||
| config.devtool = process.env.WP_DEVTOOL || 'source-map'; | ||
| config.module.rules.unshift( { |
There was a problem hiding this comment.
It might be push as well. I don't know if order matters. Better safe than sorry :)
oandregal
left a comment
There was a problem hiding this comment.
We need to make sure thread-loader is looked up in @wordpress/scripts node_modules folder.
oandregal
left a comment
There was a problem hiding this comment.
OK, this is now ready. Commited a change to fix the issue I reported on my previous review.
Tested it by using it in an external plugin and it works. No webpack output difference detected. Sanity-checked that Gutenberg works fine.
Let's save everyone's seconds for good! Life is so precious, we shouldn't be waiting for Webpack.
* Scripts: Use thread-loader in Babel compilation * Scripts: Include Sourcemap Loader only in development environments * Scripts: Use cache directory for Babel loader * Fix package-lock.json to stop installing packages locally * Tell webpack to look for thread-loader within the scripts node_modules folder
|
CHANGELOG notes should have been included here. I committed them directly to master in c8695f7 . |


This pull request seeks to try a few optimizations for the default Webpack configuration. In my testing using
time ./node_modules/.bin/wp-scripts build, the approximate result in my environment is a net build time decrease of ~38% (18.5s to 11.5s) uncached to ~50% (18.5s to 9.3s) cached.The optimizations are three:
thread-loaderto complement Babel compilation. On its own, with a default configuration, this seemed to reduce build time by approximately 2-3 seconds. The result may vary by machine depending on the number of cores available (default threading isnumCPU - 1workers).babel-loader'scacheDirectoryoption. This too appeared to reduce build time by approximately 2-3 seconds further. By default, this uses a cache directory local to the project'snode_modules. Thus, it will not have an immediate benefit on a fresh clone, or ifnode_modulesis removed for any reason. I've included an option to direct this to a more persistent cache directory, which is partly self-serving for one of my own projects where I'd like to use a more persistent cache.source-map-loaderfrom production builds. This had a slightly more significant impact to reduce build time by a further 3-4 seconds. My reading of thesource-map-loaderdocumentation is that it is intended to complement thedevtooloption, which we already configure only for non-production environments.A few others I considered, but did not include:
output.pathinfo).References:
Testing instructions:
You may consider to compare build times between master and this branch:
(Note: Babel loader's
cacheDirectorywon't have an impact the first time it's run. You can look to see whether a cache exists asnode_modules/.cache/babel-loader)Otherwise, verify there are no regressions in the expected output from build scripts (runs in browser, no sourcemap/devtool regression).