Skip to content

Instantly share code, notes, and snippets.

@phpbits
Last active October 12, 2022 12:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phpbits/bda180bc7593fc89bc302797b48019fd to your computer and use it in GitHub Desktop.
Save phpbits/bda180bc7593fc89bc302797b48019fd to your computer and use it in GitHub Desktop.
Webpack config for `wp-scripts` package with postcss processing to handle css files. Learn more here : https://jeffreycarandang.com/create-gutenberg-block-plugin-wp-scripts-postcss-build/
const defaultConfig = require( './node_modules/@wordpress/scripts/config/webpack.config.js' );
const path = require( 'path' );
const postcssPresetEnv = require( 'postcss-preset-env' );
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
const IgnoreEmitPlugin = require( 'ignore-emit-webpack-plugin' );
const production = process.env.NODE_ENV === '';
module.exports = {
...defaultConfig,
entry: {
index: path.resolve( process.cwd(), 'src', 'index.js' ),
style: path.resolve( process.cwd(), 'src', 'style.scss' ),
editor: path.resolve( process.cwd(), 'src', 'editor.scss' ),
},
optimization: {
...defaultConfig.optimization,
splitChunks: {
cacheGroups: {
editor: {
name: 'editor',
test: /editor\.(sc|sa|c)ss$/,
chunks: 'all',
enforce: true,
},
style: {
name: 'style',
test: /style\.(sc|sa|c)ss$/,
chunks: 'all',
enforce: true,
},
default: false,
},
},
},
module: {
...defaultConfig.module,
rules: [
...defaultConfig.module.rules,
{
test: /\.(sc|sa|c)ss$/,
exclude: /node_modules/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
sourceMap: ! production,
},
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: () => [
postcssPresetEnv( {
stage: 3,
features: {
'custom-media-queries': {
preserve: false,
},
'custom-properties': {
preserve: true,
},
'nesting-rules': true,
},
} ),
],
},
},
{
loader: 'sass-loader',
options: {
sourceMap: ! production,
},
},
],
},
],
},
plugins: [
...defaultConfig.plugins,
new MiniCssExtractPlugin( {
filename: '[name].css',
} ),
new IgnoreEmitPlugin( [ 'editor.js', 'style.js' ] ),
],
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment