-
I want to set up a modern environment for theme development. I would like to use wp-scripts to automatically process the JS, CSS, SCSS as it is done for blocks. I really miss guides on how to work with themes. |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 11 replies
-
Hi @cawa-93, There are various ways to build JS and SCSS for a theme, but below is an example of a script that I often use.
{
"name": "my-theme",
"scripts": {
"start:sass": "sass src/sass/one.scss:build/css/one.css src/sass/two.scss:build/css/two.css --watch",
"start:js": "wp-scripts start src/js/one.js src/js/two.js --output-path=build/js",
"build:sass": "sass src/sass/one.scss:build/css/one.css src/sass/two.scss:build/css/two.css --no-source-map --style compressed",
"build:js": "wp-scripts build src/js/one.js src/js/two.js --output-path=build/js",
"clean": "rimraf build"
},
"devDependencies": {
"@wordpress/scripts": "^26.13.0"
}
} |
Beta Was this translation helpful? Give feedback.
-
You might find this article by Justin Tadlock helpful: There are plenty more really good articles on that blog, under the "Theme" tag, but also for other development topics. |
Beta Was this translation helpful? Give feedback.
-
I agree with @vicobot-0815 that Justin's tutorial post is very helpful. And good news! There is also now (as of January 2024) official documentation for using the |
Beta Was this translation helpful? Give feedback.
-
I have also just switched to working with wp-scripts. I found it helpful that pnpm workspaces come in handy when working with wp-scripts on plugins and themes at the same time. |
Beta Was this translation helpful? Give feedback.
-
Does anyone have any pointer on how to use |
Beta Was this translation helpful? Give feedback.
-
You could import assets in javascript, but that is a limited option (if they are importable - not all assets could be imported with import). |
Beta Was this translation helpful? Give feedback.
-
I am also working with
And what I want in my
My solution was creating
|
Beta Was this translation helpful? Give feedback.
-
If you're not building blocks in the theme then change the package.json script to build to a The In the
Package.json: {
"scripts": {
"build": "wp-scripts build --webpack-src-dir=src --output-path=assets",
"start": "wp-scripts start --webpack-src-dir=src --output-path=assets",
"theme-zip": "wp-scripts plugin-zip"
},
// This tells theme-zip which files to include in the zip folder
"files": [
"acf-json/",
"assets/",
"inc/",
"languages/",
"parts/",
"patterns/",
"templates/",
"*.php",
"README.md",
"LICENSE",
"screenshot.png",
"style.css",
"theme.json"
],
} // WordPress webpack config - webpack.config.js
import wpConfig from '@wordpress/scripts/config/webpack.config.js';
// Plugins
import RemoveEmptyScriptsPlugin from 'webpack-remove-empty-scripts';
import CopyPlugin from 'copy-webpack-plugin';
// Utilities
import path from 'path';
/**
* Exports the custom webpack config.
*
* @since 1.0.0
*/
export default {
...wpConfig,
entry: {
'js/block-filters': path.resolve(
process.cwd(),
'src/js',
'block-filters.js'
),
'js/block-styles': path.resolve(
process.cwd(),
'src/js',
'block-styles.js'
),
'js/block-formats': path.resolve(
process.cwd(),
'src/js',
'block-formats.js'
),
'css/global': path.resolve(process.cwd(), 'src/scss', 'global.scss'),
'css/editor': path.resolve(process.cwd(), 'src/scss', 'editor.scss'),
},
plugins: [
// Include WordPress plugins but filter out RTL plugin
...wpConfig.plugins.filter(
(plugin) => plugin.constructor.name !== 'RtlCssPlugin'
),
// Remove empty JS files after WordPress asset generation
new RemoveEmptyScriptsPlugin({
stage: RemoveEmptyScriptsPlugin.STAGE_AFTER_PROCESS_PLUGINS,
}),
// Copy static assets
new CopyPlugin({
patterns: [
{
from: './src/fonts',
to: './fonts',
},
],
}),
],
performance: {
maxAssetSize: 512000,
},
}; /**
* Enqueue theme styles.
*/
function theme_enqueue_styles()
{
$asset = include THEME_DIR . '/assets/css/global.asset.php';
// Global styles for frontend
wp_enqueue_style(
'theme-global-styles',
THEME_URI . '/assets/css/global.css',
$asset['dependencies'],
$asset['version']
);
}
add_action('wp_enqueue_scripts', 'theme_enqueue_styles');
/**
* Register styles with the editor
*/
function theme_editor_styles()
{
add_editor_style([
THEME_URI . '/assets/css/global.css',
]);
}
add_action('after_setup_theme', 'theme_editor_styles');
/**
* Enqueue editor styles and scripts
*/
function theme_enqueue_block_editor_assets()
{
$filterAsset = include THEME_DIR . '/assets/js/block-filters.asset.php';
$editorAsset = include THEME_DIR . '/assets/css/editor.asset.php';
$blockFormatAsset = include THEME_DIR . '/assets/js/block-formats.asset.php';
wp_enqueue_style(
'theme-editor-styles',
THEME_URI . '/assets/css/editor.css',
$editorAsset['dependencies'],
$editorAsset['version']
);
wp_enqueue_script(
'theme-block-filters',
THEME_URI . '/assets/js/block-filters.js',
$filterAsset['dependencies'],
$filterAsset['version'],
true
);
wp_enqueue_script(
'theme-block-formats',
THEME_URI . '/assets/js/block-formats.js',
array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor', 'wp-rich-text'),
$blockFormatAsset['version'],
true
);
wp_enqueue_script(
'theme-block-styles',
THEME_URI . '/assets/js/block-styles.js',
array('wp-blocks', 'wp-dom-ready'),
$blockFormatAsset['version'],
true
);
}
add_action('enqueue_block_editor_assets', 'theme_enqueue_block_editor_assets'); |
Beta Was this translation helpful? Give feedback.
I agree with @vicobot-0815 that Justin's tutorial post is very helpful. And good news! There is also now (as of January 2024) official documentation for using the
wp-scripts
package for a theme build process. That can be found here: https://developer.wordpress.org/themes/advanced-topics/build-process/