Created
July 19, 2023 21:23
-
-
Save justintadlock/043f7fedb436a80174da27ee0c5fa53b to your computer and use it in GitHub Desktop.
Theme webpack config with multiple stylesheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// --------------------------------------------------------------------------------- | |
// 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