diff --git a/package.json b/package.json index 36d46c8d9b316..6a83f3b0a9789 100644 --- a/package.json +++ b/package.json @@ -125,6 +125,7 @@ "shallow-equals": "1.0.0", "shallowequal": "1.1.0", "simple-git": "1.113.0", + "source-map-loader": "0.2.4", "sprintf-js": "1.1.1", "stylelint-config-wordpress": "13.1.0", "uuid": "3.3.2", diff --git a/webpack.config.js b/webpack.config.js index ba130ce0992d4..3de56a819e0de 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -4,7 +4,7 @@ const { DefinePlugin } = require( 'webpack' ); const CopyWebpackPlugin = require( 'copy-webpack-plugin' ); const postcss = require( 'postcss' ); -const { get, escapeRegExp } = require( 'lodash' ); +const { get, escapeRegExp, compact } = require( 'lodash' ); const { basename, sep } = require( 'path' ); /** @@ -12,7 +12,7 @@ const { basename, sep } = require( 'path' ); */ const CustomTemplatedPathPlugin = require( '@wordpress/custom-templated-path-webpack-plugin' ); const LibraryExportDefaultPlugin = require( '@wordpress/library-export-default-webpack-plugin' ); -const defaultConfig = require( '@wordpress/scripts/config/webpack.config' ); +const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' ); const { camelCaseDash } = require( '@wordpress/scripts/utils' ); /** @@ -20,6 +20,11 @@ const { camelCaseDash } = require( '@wordpress/scripts/utils' ); */ const { dependencies } = require( './package' ); +const { + NODE_ENV: mode = 'development', + WP_DEVTOOL: devtool = ( mode === 'production' ? false : 'source-map' ), +} = process.env; + const WORDPRESS_NAMESPACE = '@wordpress/'; const gutenbergPackages = Object.keys( dependencies ) @@ -27,7 +32,7 @@ const gutenbergPackages = Object.keys( dependencies ) .map( ( packageName ) => packageName.replace( WORDPRESS_NAMESPACE, '' ) ); module.exports = { - ...defaultConfig, + mode, entry: gutenbergPackages.reduce( ( memo, packageName ) => { const name = camelCaseDash( packageName ); memo[ name ] = `./packages/${ packageName }`; @@ -39,8 +44,16 @@ module.exports = { library: [ 'wp', '[name]' ], libraryTarget: 'this', }, + module: { + rules: compact( [ + mode !== 'production' && { + test: /\.js$/, + use: require.resolve( 'source-map-loader' ), + enforce: 'pre', + }, + ] ), + }, plugins: [ - ...defaultConfig.plugins, new DefinePlugin( { // Inject the `GUTENBERG_PHASE` global, used for feature flagging. // eslint-disable-next-line @wordpress/gutenberg-phase @@ -82,7 +95,7 @@ module.exports = { to: `./build/${ packageName }/`, flatten: true, transform: ( content ) => { - if ( defaultConfig.mode === 'production' ) { + if ( mode === 'production' ) { return postcss( [ require( 'cssnano' )( { preset: [ 'default', { @@ -134,5 +147,7 @@ module.exports = { }, }, ] ), + new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ), ], + devtool, };