Skip to content

Instantly share code, notes, and snippets.

@justintadlock
Created July 19, 2023 21:23
Show Gist options
  • Save justintadlock/043f7fedb436a80174da27ee0c5fa53b to your computer and use it in GitHub Desktop.
Save justintadlock/043f7fedb436a80174da27ee0c5fa53b to your computer and use it in GitHub Desktop.
Theme webpack config with multiple stylesheets
// ---------------------------------------------------------------------------------
// Be sure to install:
// npm install @wordpress/scripts glob path webpack-remove-empty-scripts --save-dev
// ---------------------------------------------------------------------------------
// WordPress webpack config.
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
// Plugins.
const RemoveEmptyScriptsPlugin = require( 'webpack-remove-empty-scripts' );
// Utilities.
const path = require( 'path' );
const { globSync } = require( 'glob' );
// Gets all of the block stylesheets, which are enqueued separately and inlined
// into the `<head>` area by WordPress. These should not be bundled together.
const blockStylesheets = () => globSync( './resources/scss/blocks/core/*.scss' ).reduce(
( files, filepath ) => {
const name = path.parse( filepath ).name;
files[ `css/blocks/core/${ name }` ] = path.resolve(
process.cwd(),
'resources/scss/blocks/core',
`${ name }.scss`
);
return files;
}, {}
);
module.exports = {
...defaultConfig,
...{
entry: {
'js/editor': path.resolve( process.cwd(), 'resources/js', 'editor.js' ),
'css/screen': path.resolve( process.cwd(), 'resources/scss', 'screen.scss' ),
'css/editor': path.resolve( process.cwd(), 'resources/scss', 'editor.scss' ),
...blockStylesheets()
},
plugins: [
...defaultConfig.plugins,
new RemoveEmptyScriptsPlugin( {
stage: RemoveEmptyScriptsPlugin.STAGE_AFTER_PROCESS_PLUGINS
} )
]
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment