Skip to content

Commit 98785ab

Browse files
aduthmchowning
authored andcommitted
Scripts: Optimize default Webpack configuration (WordPress#14860)
* 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
1 parent b189c1b commit 98785ab

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

package-lock.json

Lines changed: 26 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/scripts/config/webpack.config.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@ const externals = [
6666
const isProduction = process.env.NODE_ENV === 'production';
6767
const mode = isProduction ? 'production' : 'development';
6868

69-
const getBabelLoaderOptions = () => hasBabelConfig() ? {} : {
70-
babelrc: false,
71-
configFile: false,
72-
presets: [ require.resolve( '@wordpress/babel-preset-default' ) ],
73-
};
74-
7569
const config = {
7670
mode,
7771
entry: {
@@ -89,18 +83,29 @@ const config = {
8983
},
9084
module: {
9185
rules: [
92-
{
93-
test: /\.js$/,
94-
use: require.resolve( 'source-map-loader' ),
95-
enforce: 'pre',
96-
},
9786
{
9887
test: /\.js$/,
9988
exclude: /node_modules/,
100-
use: {
101-
loader: require.resolve( 'babel-loader' ),
102-
options: getBabelLoaderOptions(),
103-
},
89+
use: [
90+
require.resolve( 'thread-loader' ),
91+
{
92+
loader: require.resolve( 'babel-loader' ),
93+
options: {
94+
// Babel uses a directory within local node_modules
95+
// by default. Use the environment variable option
96+
// to enable more persistent caching.
97+
cacheDirectory: process.env.BABEL_CACHE_DIRECTORY || true,
98+
99+
// Provide a fallback configuration if there's not
100+
// one explicitly available in the project.
101+
...( ! hasBabelConfig() && {
102+
babelrc: false,
103+
configFile: false,
104+
presets: [ require.resolve( '@wordpress/babel-preset-default' ) ],
105+
} ),
106+
},
107+
},
108+
],
104109
},
105110
],
106111
},
@@ -121,6 +126,11 @@ if ( ! isProduction ) {
121126
// WP_DEVTOOL global variable controls how source maps are generated.
122127
// See: https://webpack.js.org/configuration/devtool/#devtool.
123128
config.devtool = process.env.WP_DEVTOOL || 'source-map';
129+
config.module.rules.unshift( {
130+
test: /\.js$/,
131+
use: require.resolve( 'source-map-loader' ),
132+
enforce: 'pre',
133+
} );
124134
}
125135

126136
module.exports = config;

packages/scripts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"source-map-loader": "^0.2.4",
5151
"stylelint": "^9.10.1",
5252
"stylelint-config-wordpress": "^13.1.0",
53+
"thread-loader": "^2.1.2",
5354
"webpack": "4.8.3",
5455
"webpack-bundle-analyzer": "^3.0.3",
5556
"webpack-cli": "^3.1.2",

0 commit comments

Comments
 (0)