Skip to content

Commit 1de160b

Browse files
committed
WDSBT-20 - Fix build for core block styles and assets
1 parent 8b7577f commit 1de160b

File tree

5 files changed

+100
-8
lines changed

5 files changed

+100
-8
lines changed

babel.config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-env", "@babel/preset-react"]
3+
}

inc/hooks/enqueue-block-stylesheet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function enqueue_block_styles() {
8686
* @return array An array containing information about block stylesheets.
8787
*/
8888
function get_block_stylesheets() {
89-
$css_dir = get_stylesheet_directory() . '/build/css';
89+
$css_dir = get_stylesheet_directory() . '/assets/css';
9090
$exclude_stylesheets = array( 'style.css', 'style-rtl.css' );
9191

9292
$css_files = array_diff(
@@ -103,7 +103,7 @@ function ( $css_file ) use ( $css_dir ) {
103103

104104
return array(
105105
'path' => $css_file,
106-
'src' => str_replace( $css_dir, get_stylesheet_directory_uri() . '/build/css', $css_file ),
106+
'src' => str_replace( $css_dir, get_stylesheet_directory_uri() . '/assets/css', $css_file ),
107107
);
108108
},
109109
$css_files

inc/setup/scripts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function scripts() {
2525
}
2626

2727
// Register styles & scripts.
28-
wp_enqueue_style( 'wdsbt-styles', get_stylesheet_directory_uri() . '/build/style.css', [], $asset_file['version'] );
28+
wp_enqueue_style( 'wdsbt-styles', get_stylesheet_directory_uri() . '/build/css/style.css', [], $asset_file['version'] );
2929
wp_enqueue_script( 'wdsbt-scripts', get_stylesheet_directory_uri() . '/build/js/index.js', $asset_file['dependencies'], $asset_file['version'], true );
3030
}
3131
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\scripts' );

inc/setup/setup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function setup() {
3131

3232
// Gutenberg editor styles support.
3333
add_theme_support( 'editor-styles' );
34-
add_editor_style( 'build/style.css' );
34+
add_editor_style( 'build/css/style.css' );
3535

3636
remove_action( 'wp_footer', 'the_block_template_skip_link' );
3737

webpack.config.js

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const path = require('path');
22
const defaultConfig = require('@wordpress/scripts/config/webpack.config');
3+
const RemovePlugin = require('remove-files-webpack-plugin');
34
const CopyPlugin = require('copy-webpack-plugin');
45
const SVGSpritemapPlugin = require('svg-spritemap-webpack-plugin');
56
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
67
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
78
const ESLintPlugin = require('eslint-webpack-plugin');
89
const StylelintPlugin = require('stylelint-webpack-plugin');
9-
const { glob } = require('glob');
10+
const glob = require('glob');
1011
const postcssRTL = require('postcss-rtl');
1112

1213
// Dynamically generate entry points for each file inside 'assets/scss/blocks'
@@ -21,17 +22,58 @@ const coreBlockEntryPaths = glob
2122
return acc;
2223
}, {});
2324

25+
// Dynamically generate entry points for each block
26+
const blockEntryPaths = glob
27+
.sync('./assets/blocks/**/index.js', { posix: true, dotRelative: true })
28+
.reduce((acc, filePath) => {
29+
const entryKey = filePath
30+
.replace('./assets/blocks/', '')
31+
.replace('/index.js', '');
32+
acc[`../blocks/${entryKey}`] = filePath;
33+
return acc;
34+
}, {});
35+
36+
const blockScssPaths = glob
37+
.sync('./assets/blocks/**/style.scss', { posix: true, dotRelative: true })
38+
.reduce((acc, filePath) => {
39+
const entryKey = filePath
40+
.replace('./assets/blocks/', '')
41+
.replace('/style.scss', '');
42+
acc[`../blocks/${entryKey}`] = filePath;
43+
return acc;
44+
}, {});
45+
46+
const styleScssPaths = glob
47+
.sync('./assets/scss/index.scss', { posix: true, dotRelative: true })
48+
.reduce((acc, filePath) => {
49+
const entryKey = 'style';
50+
acc[`css/${entryKey}`] = filePath;
51+
return acc;
52+
}, {});
53+
2454
module.exports = {
2555
...defaultConfig,
2656
entry: {
27-
style: './assets/scss/index.scss',
2857
index: './assets/js/index.js',
2958
variations: './assets/js/block-variations/index.js',
3059
filters: './assets/js/block-filters/index.js',
60+
...styleScssPaths,
61+
...blockEntryPaths,
62+
...blockScssPaths,
3163
...coreBlockEntryPaths,
3264
},
3365
output: {
34-
filename: 'js/[name].js',
66+
filename: (pathData) => {
67+
const entryName = pathData.chunk.name;
68+
if (
69+
entryName.includes('css/blocks') ||
70+
blockEntryPaths[entryName] ||
71+
blockScssPaths[entryName]
72+
) {
73+
return '[name].js';
74+
}
75+
return 'js/[name].js';
76+
},
3577
path: path.resolve(__dirname, 'build'),
3678
},
3779
module: {
@@ -83,13 +125,36 @@ module.exports = {
83125
filename: 'fonts/[name][ext]',
84126
},
85127
},
128+
{
129+
test: /\.js$/,
130+
exclude: /node_modules/,
131+
use: {
132+
loader: 'babel-loader',
133+
options: {
134+
presets: ['@babel/preset-env', '@babel/preset-react'],
135+
},
136+
},
137+
},
86138
],
87139
},
88140
plugins: [
89141
...defaultConfig.plugins,
90142

91143
new MiniCssExtractPlugin({
92-
filename: '[name].css', // Output LTR styles to build/css directory
144+
filename: (pathData) => {
145+
const entryName = pathData.chunk.name;
146+
if (
147+
entryName.includes('css/blocks') ||
148+
blockEntryPaths[entryName] ||
149+
blockScssPaths[entryName]
150+
) {
151+
return '[name].css';
152+
}
153+
if (entryName === 'css/style') {
154+
return 'css/style.css';
155+
}
156+
return '[name].css';
157+
},
93158
}),
94159

95160
new CopyPlugin({
@@ -124,6 +189,30 @@ module.exports = {
124189
},
125190
}),
126191

192+
new RemovePlugin({
193+
after: {
194+
log: false,
195+
test: [
196+
{
197+
folder: path.resolve(__dirname, 'build'),
198+
method: (absoluteItemPath) => {
199+
return new RegExp(/\.js/, 'm').test(
200+
absoluteItemPath
201+
);
202+
},
203+
},
204+
{
205+
folder: path.resolve(__dirname, 'build'),
206+
method: (absoluteItemPath) => {
207+
return new RegExp(/\.php$/, 'm').test(
208+
absoluteItemPath
209+
);
210+
},
211+
},
212+
],
213+
},
214+
}),
215+
127216
new CleanWebpackPlugin(),
128217
new ESLintPlugin(),
129218
new StylelintPlugin(),

0 commit comments

Comments
 (0)