diff --git a/packages/scripts/CHANGELOG.md b/packages/scripts/CHANGELOG.md index 59936867ebb9b..bc12fc5744a69 100644 --- a/packages/scripts/CHANGELOG.md +++ b/packages/scripts/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### New Features + +- The `WP_DEVTOOL` environment variable can now be used to set the Webpack devtool option for sourcemaps in production builds ([#46812](https://github.com/WordPress/gutenberg/pull/46812)). Previously, this only worked for development builds. + ## 25.3.0 (2023-02-01) ## 25.2.0 (2023-01-11) diff --git a/packages/scripts/config/webpack.config.js b/packages/scripts/config/webpack.config.js index 5347e126827cf..39af649f7e6e0 100644 --- a/packages/scripts/config/webpack.config.js +++ b/packages/scripts/config/webpack.config.js @@ -296,16 +296,15 @@ const config = { }, }; +// WP_DEVTOOL global variable controls how source maps are generated. +// See: https://webpack.js.org/configuration/devtool/#devtool. +if ( process.env.WP_DEVTOOL ) { + config.devtool = process.env.WP_DEVTOOL; +} + if ( ! isProduction ) { - // 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( { - test: /\.(j|t)sx?$/, - exclude: [ /node_modules/ ], - use: require.resolve( 'source-map-loader' ), - enforce: 'pre', - } ); + // Set default sourcemap mode if it wasn't set by WP_DEVTOOL. + config.devtool = config.devtool || 'source-map'; config.devServer = { devMiddleware: { writeToDisk: true, @@ -323,4 +322,14 @@ if ( ! isProduction ) { }; } +// Add source-map-loader if devtool is set, whether in dev mode or not. +if ( config.devtool ) { + config.module.rules.unshift( { + test: /\.(j|t)sx?$/, + exclude: [ /node_modules/ ], + use: require.resolve( 'source-map-loader' ), + enforce: 'pre', + } ); +} + module.exports = config;