From 95fec36507c0161210d3dbe8263451c518f34dec Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Grzegorz=20=28Greg=29=20Zi=C3=B3=C5=82kowski?=
 <grzegorz@gziolo.pl>
Date: Fri, 22 Feb 2019 20:01:05 +0100
Subject: [PATCH] Avoid mutating imported default config in webpack config
 (#14039)

---
 webpack.config.js | 150 +++++++++++++++++++++++-----------------------
 1 file changed, 75 insertions(+), 75 deletions(-)

diff --git a/webpack.config.js b/webpack.config.js
index a5dd63c3bd9822..1550b0712ab0d2 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -13,7 +13,7 @@ const { basename } = require( 'path' );
  */
 const CustomTemplatedPathPlugin = require( '@wordpress/custom-templated-path-webpack-plugin' );
 const LibraryExportDefaultPlugin = require( '@wordpress/library-export-default-webpack-plugin' );
-const config = require( '@wordpress/scripts/config/webpack.config' );
+const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
 const { camelCaseDash } = require( '@wordpress/scripts/utils' );
 
 /**
@@ -27,82 +27,82 @@ const gutenbergPackages = Object.keys( dependencies )
 	.filter( ( packageName ) => packageName.startsWith( WORDPRESS_NAMESPACE ) )
 	.map( ( packageName ) => packageName.replace( WORDPRESS_NAMESPACE, '' ) );
 
-config.entry = gutenbergPackages.reduce( ( memo, packageName ) => {
-	const name = camelCaseDash( packageName );
-	memo[ name ] = `./packages/${ packageName }`;
-	return memo;
-}, {} );
+module.exports = {
+	...defaultConfig,
+	entry: gutenbergPackages.reduce( ( memo, packageName ) => {
+		const name = camelCaseDash( packageName );
+		memo[ name ] = `./packages/${ packageName }`;
+		return memo;
+	}, {} ),
+	output: {
+		filename: './build/[basename]/index.js',
+		path: __dirname,
+		library: [ 'wp', '[name]' ],
+		libraryTarget: 'this',
+	},
+	plugins: [
+		...defaultConfig.plugins,
+		new DefinePlugin( {
+			// Inject the `GUTENBERG_PHASE` global, used for feature flagging.
+			// eslint-disable-next-line @wordpress/gutenberg-phase
+			'process.env.GUTENBERG_PHASE': JSON.stringify( parseInt( process.env.npm_package_config_GUTENBERG_PHASE, 10 ) || 1 ),
+		} ),
+		// Create RTL files with a -rtl suffix
+		new WebpackRTLPlugin( {
+			suffix: '-rtl',
+			minify: defaultConfig.mode === 'production' ? { safe: true } : false,
+		} ),
+		new CustomTemplatedPathPlugin( {
+			basename( path, data ) {
+				let rawRequest;
 
-config.output = {
-	filename: './build/[basename]/index.js',
-	path: __dirname,
-	library: [ 'wp', '[name]' ],
-	libraryTarget: 'this',
-};
-
-config.plugins.push(
-	new DefinePlugin( {
-		// Inject the `GUTENBERG_PHASE` global, used for feature flagging.
-		// eslint-disable-next-line @wordpress/gutenberg-phase
-		'process.env.GUTENBERG_PHASE': JSON.stringify( parseInt( process.env.npm_package_config_GUTENBERG_PHASE, 10 ) || 1 ),
-	} ),
-	// Create RTL files with a -rtl suffix
-	new WebpackRTLPlugin( {
-		suffix: '-rtl',
-		minify: config.mode === 'production' ? { safe: true } : false,
-	} ),
-	new CustomTemplatedPathPlugin( {
-		basename( path, data ) {
-			let rawRequest;
-
-			const entryModule = get( data, [ 'chunk', 'entryModule' ], {} );
-			switch ( entryModule.type ) {
-				case 'javascript/auto':
-					rawRequest = entryModule.rawRequest;
-					break;
-
-				case 'javascript/esm':
-					rawRequest = entryModule.rootModule.rawRequest;
-					break;
-			}
+				const entryModule = get( data, [ 'chunk', 'entryModule' ], {} );
+				switch ( entryModule.type ) {
+					case 'javascript/auto':
+						rawRequest = entryModule.rawRequest;
+						break;
 
-			if ( rawRequest ) {
-				return basename( rawRequest );
-			}
+					case 'javascript/esm':
+						rawRequest = entryModule.rootModule.rawRequest;
+						break;
+				}
 
-			return path;
-		},
-	} ),
-	new LibraryExportDefaultPlugin( [
-		'api-fetch',
-		'deprecated',
-		'dom-ready',
-		'redux-routine',
-		'token-list',
-	].map( camelCaseDash ) ),
-	new CopyWebpackPlugin(
-		gutenbergPackages.map( ( packageName ) => ( {
-			from: `./packages/${ packageName }/build-style/*.css`,
-			to: `./build/${ packageName }/`,
-			flatten: true,
-			transform: ( content ) => {
-				if ( config.mode === 'production' ) {
-					return postcss( [
-						require( 'cssnano' )( {
-							preset: [ 'default', {
-								discardComments: {
-									removeAll: true,
-								},
-							} ],
-						} ),
-					] )
-						.process( content, { from: 'src/app.css', to: 'dest/app.css' } )
-						.then( ( result ) => result.css );
+				if ( rawRequest ) {
+					return basename( rawRequest );
 				}
-				return content;
-			},
-		} ) )
-	)
-);
 
-module.exports = config;
+				return path;
+			},
+		} ),
+		new LibraryExportDefaultPlugin( [
+			'api-fetch',
+			'deprecated',
+			'dom-ready',
+			'redux-routine',
+			'token-list',
+		].map( camelCaseDash ) ),
+		new CopyWebpackPlugin(
+			gutenbergPackages.map( ( packageName ) => ( {
+				from: `./packages/${ packageName }/build-style/*.css`,
+				to: `./build/${ packageName }/`,
+				flatten: true,
+				transform: ( content ) => {
+					if ( defaultConfig.mode === 'production' ) {
+						return postcss( [
+							require( 'cssnano' )( {
+								preset: [ 'default', {
+									discardComments: {
+										removeAll: true,
+									},
+								} ],
+							} ),
+						] )
+							.process( content, { from: 'src/app.css', to: 'dest/app.css' } )
+							.then( ( result ) => result.css );
+					}
+					return content;
+				},
+			} ) )
+		),
+	],
+};