Skip to content

Commit 6865c4e

Browse files
committed
WDSBT-20 - Add checks in webpack config to only build blocks if it's available
1 parent 6321eb1 commit 6865c4e

File tree

6 files changed

+26
-11
lines changed

6 files changed

+26
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ This will process JavaScript, SCSS, optimize images, and generate necessary file
277277

278278
2. Create an SCSS file with the exact filename as the block name you want to customize. This file will house your custom styles for that specific block.
279279

280-
3. Files within the `assets/scss/blocks/` directory are automatically enqueued, simplifying the integration of your custom styles into the WordPress block editor. Do not import these files into the main `index.scss`
280+
3. Files within the `assets/scss/blocks/core/` directory are automatically enqueued, simplifying the integration of your custom styles into the WordPress block editor. Do not import these files into the main `index.scss`
281281

282282
4. After adding your custom SCSS file, run the following command to compile and apply your changes:
283283

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// 'blocks/custom/' is for styling custom blocks added by plugins
22
// (e.g., shareaholic etc)
3-
// file should be named after the block's slug
3+
// Files added here will be added to the built style.css.

assets/scss/editor.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* -- Editor Stykes -- */

assets/scss/index.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
@import './components/components';
44
@import './layout/layout';
55
@import './pages/pages';
6+
@import './blocks/custom/custom';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
},
7575
"scripts": {
7676
"a11y": "node a11y.cjs",
77-
"build": "wp-scripts build --config webpack.config.js --webpack-src-dir=assets/blocks",
77+
"build": "wp-scripts build --config webpack.config.js --webpack-src-dir=assets/blocks --stats-error-details --progress",
7878
"create-block": "cd ./assets/blocks && npx @wordpress/create-block --namespace=wdsbt --template ../../inc/block-template --no-plugin",
7979
"format": "wp-scripts format && npm run format:php",
8080
"format:css": "wp-scripts format-style",

webpack.config.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ const StylelintPlugin = require('stylelint-webpack-plugin');
1010
const glob = require('glob');
1111
const postcssRTL = require('postcss-rtl');
1212

13+
// Function to check for the existence of files matching a pattern
14+
function hasFiles(pattern) {
15+
return glob.sync(pattern, { posix: true, dotRelative: true }).length > 0;
16+
}
17+
1318
// Dynamically generate entry points for each file inside 'assets/scss/blocks'
1419
const coreBlockEntryPaths = glob
15-
.sync('./assets/scss/blocks/**/*.scss', {
20+
.sync('./assets/scss/blocks/core/*.scss', {
1621
posix: true,
1722
dotRelative: true,
1823
})
@@ -52,30 +57,38 @@ const styleScssPaths = glob
5257
}, {});
5358

5459
// CopyPlugin patterns to include PHP and JSON files
55-
const copyPluginPatterns = [
56-
{
60+
const copyPluginPatterns = [];
61+
62+
// Only add PHP and JSON patterns if these files exist
63+
if (hasFiles('./assets/blocks/**/*.php')) {
64+
copyPluginPatterns.push({
5765
from: './assets/blocks/**/*.php',
5866
to: ({ context, absoluteFilename }) => {
5967
return absoluteFilename.replace(
6068
`${context}/assets/blocks/`,
6169
'../blocks/'
6270
);
6371
},
64-
},
65-
{
66-
from: './assets/blocks/**/*.json',
72+
});
73+
}
74+
75+
if (hasFiles('./assets/blocks/**/*.json')) {
76+
copyPluginPatterns.push({
77+
from: './assets/blocks/core/*.json',
6778
to: ({ context, absoluteFilename }) => {
6879
return absoluteFilename.replace(
6980
`${context}/assets/blocks/`,
7081
'../blocks/'
7182
);
7283
},
73-
},
74-
];
84+
});
85+
}
7586

7687
module.exports = {
7788
...defaultConfig,
7889
entry: {
90+
...defaultConfig.entry,
91+
admin: './assets/scss/editor.scss',
7992
index: './assets/js/index.js',
8093
variations: './assets/js/block-variations/index.js',
8194
filters: './assets/js/block-filters/index.js',

0 commit comments

Comments
 (0)