Skip to content

Commit

Permalink
Consolidate some config options under baseConfig export
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Jul 13, 2021
1 parent ca09082 commit a674d33
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 89 deletions.
16 changes: 2 additions & 14 deletions tools/webpack/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ const fastGlob = require( 'fast-glob' );
/**
* Internal dependencies
*/
const {
devtool,
mode,
moduleConfig,
optimization,
plugins,
stylesTransform,
watchOptions,
} = require( './shared' );
const { baseConfig, plugins, stylesTransform } = require( './shared' );

/*
* Matches a block's name in paths in the form
Expand Down Expand Up @@ -52,16 +44,14 @@ const createEntrypoints = () => {
};

module.exports = {
...baseConfig,
name: 'blocks',
optimization,
mode,
entry: createEntrypoints(),
output: {
devtoolNamespace: 'wp',
filename: './build/block-library/[name]/view.min.js',
path: join( __dirname, '..', '..' ),
},
module: moduleConfig,
plugins: [
...plugins,
new CopyWebpackPlugin( {
Expand Down Expand Up @@ -167,6 +157,4 @@ module.exports = {
),
} ),
].filter( Boolean ),
watchOptions,
devtool,
};
16 changes: 2 additions & 14 deletions tools/webpack/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@ const {
* Internal dependencies
*/
const { dependencies } = require( '../../package' );
const {
devtool,
mode,
moduleConfig,
optimization,
plugins,
stylesTransform,
watchOptions,
} = require( './shared' );
const { baseConfig, plugins, stylesTransform } = require( './shared' );

const WORDPRESS_NAMESPACE = '@wordpress/';
const BUNDLED_PACKAGES = [ '@wordpress/icons', '@wordpress/interface' ];
Expand All @@ -40,9 +32,8 @@ const gutenbergPackages = Object.keys( dependencies )
.map( ( packageName ) => packageName.replace( WORDPRESS_NAMESPACE, '' ) );

module.exports = {
...baseConfig,
name: 'packages',
optimization,
mode,
entry: gutenbergPackages.reduce( ( memo, packageName ) => {
return {
...memo,
Expand All @@ -56,7 +47,6 @@ module.exports = {
library: [ 'wp', '[camelName]' ],
libraryTarget: 'window',
},
module: moduleConfig,
plugins: [
...plugins,
new CustomTemplatedPathPlugin( {
Expand Down Expand Up @@ -84,6 +74,4 @@ module.exports = {
} ) ),
} ),
].filter( Boolean ),
watchOptions,
devtool,
};
120 changes: 59 additions & 61 deletions tools/webpack/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,62 +18,46 @@ const {
WP_DEVTOOL: devtool = mode === 'production' ? false : 'source-map',
} = process.env;

const stylesTransform = ( content ) => {
if ( mode === 'production' ) {
return postcss( [
require( 'cssnano' )( {
preset: [
'default',
{
discardComments: {
removeAll: true,
},
const baseConfig = {
optimization: {
// Only concatenate modules in production, when not analyzing bundles.
concatenateModules:
mode === 'production' && ! process.env.WP_BUNDLE_ANALYZER,
minimizer: [
new TerserPlugin( {
cache: true,
parallel: true,
sourceMap: mode !== 'production',
terserOptions: {
output: {
comments: /translators:/i,
},
compress: {
passes: 2,
},
mangle: {
reserved: [ '__', '_n', '_nx', '_x' ],
},
],
} ),
] )
.process( content, {
from: 'src/app.css',
to: 'dest/app.css',
} )
.then( ( result ) => result.css );
}
return content;
};

const optimization = {
// Only concatenate modules in production, when not analyzing bundles.
concatenateModules:
mode === 'production' && ! process.env.WP_BUNDLE_ANALYZER,
minimizer: [
new TerserPlugin( {
cache: true,
parallel: true,
sourceMap: mode !== 'production',
terserOptions: {
output: {
comments: /translators:/i,
},
compress: {
passes: 2,
},
mangle: {
reserved: [ '__', '_n', '_nx', '_x' ],
},
extractComments: false,
} ),
],
},
mode,
module: {
rules: compact( [
mode !== 'production' && {
test: /\.js$/,
use: require.resolve( 'source-map-loader' ),
enforce: 'pre',
},
extractComments: false,
} ),
],
};

const moduleConfig = {
rules: compact( [
mode !== 'production' && {
test: /\.js$/,
use: require.resolve( 'source-map-loader' ),
enforce: 'pre',
},
] ),
] ),
},
watchOptions: {
ignored: [ '**/node_modules', '**/packages/*/src' ],
aggregateTimeout: 500,
},
devtool,
};

const plugins = [
Expand All @@ -93,17 +77,31 @@ const plugins = [
mode === 'production' && new ReadableJsAssetsWebpackPlugin(),
];

const watchOptions = {
ignored: [ '**/node_modules', '**/packages/*/src' ],
aggregateTimeout: 500,
const stylesTransform = ( content ) => {
if ( 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;
};

module.exports = {
devtool,
mode,
moduleConfig,
optimization,
baseConfig,
plugins,
stylesTransform,
watchOptions,
};

0 comments on commit a674d33

Please sign in to comment.