-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wp-scripts with CSS only entry points? #54467
Comments
I'm using npx postcss src/block-mods/**/*.css --base src/block-mods --dir build/block-mods Anyway. I think this must be a quite common scenario for theme builders? Another thing I dislike is the filenames and chunks |
Here's what I use, based on https://gist.github.com/justintadlock/043f7fedb436a80174da27ee0c5fa53b webpack.config.js // WordPress webpack config.
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
// Plugins.
const RemoveEmptyScriptsPlugin = require( 'webpack-remove-empty-scripts' );
const CopyPlugin = require( 'copy-webpack-plugin' );
// Utilities.
const path = require( 'path' );
const { globSync } = require( 'fast-glob' );
// Stylesheets entrypoints.
const stylesheetEntryPoints = () =>
globSync( './assets/css/**/*.{css,scss}' ).reduce( ( files, filepath ) => {
const relativePath = path.relative(
path.resolve( process.cwd(), 'assets/css' ),
filepath
);
// Exclude files that are already imported.
if ( ! path.basename( filepath ).startsWith( '_' ) ) {
const destinationPath = `../css/${ relativePath.replace(
/\.(css|scss)$/,
''
) }`;
files[ destinationPath ] = filepath;
}
return files;
}, {} );
// Scripts entrypoints.
const scriptsEntryPoints = () =>
globSync( './assets/js/**/*.js' ).reduce( ( files, filepath ) => {
const relativePath = path.relative(
path.resolve( process.cwd(), 'assets/js' ),
filepath
);
// Exclude files that are already imported.
if ( ! relativePath.startsWith( 'imports/' ) ) {
const destinationPath = `../js/${ relativePath.replace(
/\.js$/,
''
) }`;
files[ destinationPath ] = filepath;
}
return files;
}, {} );
module.exports = {
...defaultConfig,
...{
entry: {
...defaultConfig.entry(),
...stylesheetEntryPoints(),
...scriptsEntryPoints(),
},
plugins: [
...defaultConfig.plugins,
new RemoveEmptyScriptsPlugin( {
stage: RemoveEmptyScriptsPlugin.STAGE_AFTER_PROCESS_PLUGINS,
} ),
new CopyPlugin( {
patterns: [
{ from: 'assets/fonts', to: '../fonts' },
{ from: 'assets/images', to: '../images' },
],
} ),
],
},
}; package.json {
"devDependencies": {
"@wordpress/scripts": "^26.16.0",
"path": "^0.12.7",
"webpack-remove-empty-scripts": "^1.0.4"
},
"prettier": "@wordpress/prettier-config",
"scripts": {
"build": "wp-scripts build --webpack-src-dir=blocks --output-path=dist/blocks --webpack-copy-php",
"check-engines": "wp-scripts check-engines",
"format": "wp-scripts format",
"lint:css": "wp-scripts lint-style",
"lint:js": "wp-scripts lint-js",
"start": "wp-scripts start --webpack-src-dir=blocks --output-path=dist/blocks --webpack-copy-php"
}
} Maybe the @wordpress/scripts could include the remove empty scripts plugin and have additional flags to ease it's usage for themes assets like you describe (core blocks overrides or javascript files). |
I left a comment on a similar topic today #55936 (comment). Let's explore how we can expand the automatic discovery of JS and CSS entry points for both plugins and themes in The only challenge would be arriving at the patterns for detecting CSS files. Are there any existing patterns? |
This is not an exhaustive research, but it appears that When people build bespoke themes and choose to include custom blocks in their theme, I don't know where they put their blocks. I would say either a From https://github.com/10up/wp-scaffold/tree/trunk/themes/10up-theme/
From https://github.com/justintadlock/first-draft/
From https://github.com/OllieWP/ollie
From https://github.com/wpengine/frost
From https://github.com/WordPress/twentytwentyfour/
From https://github.com/WordPress/twentytwentyone/
|
I use the same code then @benoitchantre but I found it in the gutenberg source. I think it would be already helpful if there is a simple helper function we can import in our webpack.config.js and set the glob path with a argument. I believe the the |
What problem does this address?
I'm using wp-scripts for my plugin. I have a folder
block-mods
contain only additional css loading for core blocks with PHPwp_enqueue_block_style
function. Now the problem is my CSS use PostCSS and i would like to have some preprocessing, but how is it possible since its not included in block.json or any javascript?What is your proposed solution?
I would like to see better customize options for wp-scripts, especially entry points, chunk names and PHP files to copy.
Maybe have a json configuration could be a option? Currently my workaround is using a custom
webpack.config.js
and I hope someone come up with a better idea?The text was updated successfully, but these errors were encountered: