Skip to content

Commit

Permalink
Avoid mutating imported default config in webpack config (#14039)
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Feb 22, 2019
1 parent 666989f commit 2d39089
Showing 1 changed file with 75 additions and 75 deletions.
150 changes: 75 additions & 75 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

/**
Expand All @@ -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;
},
} ) )
),
],
};

0 comments on commit 2d39089

Please sign in to comment.