Skip to content

Commit 034169d

Browse files
committed
Fix block CSS building
1 parent fe07bf3 commit 034169d

File tree

1 file changed

+105
-142
lines changed

1 file changed

+105
-142
lines changed

tools/gutenberg/copy-gutenberg-build.js

Lines changed: 105 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,28 @@ const COPY_CONFIG = {
7575
},
7676

7777
// Blocks (to wp-includes/blocks/)
78-
// Note: Copies JS/CSS/JSON from build, PHP from packages source (no prefixes)
78+
// Unified configuration for all block types
7979
blocks: {
80-
source: 'scripts/block-library',
8180
destination: 'blocks',
82-
copyAll: true,
83-
excludePHP: true, // PHP files copied separately from packages source
84-
excludeExperimental: true, // Skip experimental blocks
81+
sources: [
82+
{
83+
// Block library blocks
84+
name: 'block-library',
85+
scripts: 'scripts/block-library',
86+
styles: 'styles/block-library',
87+
php: 'block-library/src',
88+
},
89+
{
90+
// Widget blocks
91+
name: 'widgets',
92+
scripts: 'scripts/widgets/blocks',
93+
styles: 'styles/widgets',
94+
php: 'widgets/src/blocks',
95+
},
96+
],
8597
},
8698

87-
// PHP source files (copied from packages, not build, to avoid Gutenberg prefixes)
99+
// PHP source files (non-block files, copied from packages)
88100
phpSource: {
89101
files: [
90102
{
@@ -96,34 +108,8 @@ const COPY_CONFIG = {
96108
'class-wp-block-parser-frame.php',
97109
],
98110
destination: '', // Root of wp-includes
99-
transform: false, // These don't need path transformations
111+
transform: false,
100112
},
101-
{
102-
// Block library PHP files
103-
package: 'block-library/src',
104-
pattern: '*/index.php', // Each block directory
105-
destination: 'blocks',
106-
renamePattern: { from: '/index.php', to: '.php' }, // comment-template/index.php → comment-template.php
107-
transform: true,
108-
},
109-
{
110-
// Widgets PHP files
111-
package: 'widgets/src/blocks',
112-
files: [
113-
{ from: 'legacy-widget/index.php', to: 'blocks/legacy-widget.php' },
114-
{ from: 'widget-group/index.php', to: 'blocks/widget-group.php' },
115-
],
116-
transform: true,
117-
},
118-
],
119-
},
120-
121-
// Widget block.json files (from build, not source)
122-
widgetBlockJson: {
123-
source: 'scripts/widgets/blocks',
124-
files: [
125-
{ from: 'legacy-widget/block.json', to: 'blocks/legacy-widget/block.json' },
126-
{ from: 'widget-group/block.json', to: 'blocks/widget-group/block.json' },
127113
],
128114
},
129115

@@ -210,6 +196,81 @@ function copyDirectory( src, dest, transform = null, options = {} ) {
210196
}
211197
}
212198

199+
/**
200+
* Copy all assets for blocks from Gutenberg to Core.
201+
* Handles scripts, styles, PHP, and JSON for all block types in a unified way.
202+
*
203+
* @param {Object} config - Block configuration from COPY_CONFIG.blocks
204+
*/
205+
function copyBlockAssets( config ) {
206+
const blocksDest = path.join( wpIncludesDir, config.destination );
207+
208+
for ( const source of config.sources ) {
209+
const scriptsSrc = path.join( gutenbergBuildDir, source.scripts );
210+
const stylesSrc = path.join( gutenbergBuildDir, source.styles );
211+
const phpSrc = path.join( gutenbergPackagesDir, source.php );
212+
213+
if ( ! fs.existsSync( scriptsSrc ) ) {
214+
continue;
215+
}
216+
217+
// Get all block directories from the scripts source
218+
const blockDirs = fs.readdirSync( scriptsSrc, { withFileTypes: true } )
219+
.filter( entry => entry.isDirectory() )
220+
.map( entry => entry.name );
221+
222+
for ( const blockName of blockDirs ) {
223+
// Skip experimental blocks
224+
const blockJsonPath = path.join( scriptsSrc, blockName, 'block.json' );
225+
if ( isExperimentalBlock( blockJsonPath ) ) {
226+
continue;
227+
}
228+
229+
const blockDest = path.join( blocksDest, blockName );
230+
fs.mkdirSync( blockDest, { recursive: true } );
231+
232+
// 1. Copy scripts/JSON (everything except PHP)
233+
const blockScriptsSrc = path.join( scriptsSrc, blockName );
234+
if ( fs.existsSync( blockScriptsSrc ) ) {
235+
const files = fs.readdirSync( blockScriptsSrc );
236+
for ( const file of files ) {
237+
if ( file.endsWith( '.php' ) ) {
238+
continue; // Skip PHP, copied from packages
239+
}
240+
fs.copyFileSync(
241+
path.join( blockScriptsSrc, file ),
242+
path.join( blockDest, file )
243+
);
244+
}
245+
}
246+
247+
// 2. Copy styles (if they exist in per-block directory)
248+
const blockStylesSrc = path.join( stylesSrc, blockName );
249+
if ( fs.existsSync( blockStylesSrc ) ) {
250+
const cssFiles = fs.readdirSync( blockStylesSrc )
251+
.filter( file => file.endsWith( '.css' ) );
252+
for ( const cssFile of cssFiles ) {
253+
fs.copyFileSync(
254+
path.join( blockStylesSrc, cssFile ),
255+
path.join( blockDest, cssFile )
256+
);
257+
}
258+
}
259+
260+
// 3. Copy and transform PHP from packages
261+
const blockPhpSrc = path.join( phpSrc, blockName, 'index.php' );
262+
if ( fs.existsSync( blockPhpSrc ) ) {
263+
const phpDest = path.join( wpIncludesDir, config.destination, `${ blockName }.php` );
264+
let content = fs.readFileSync( blockPhpSrc, 'utf8' );
265+
content = transformPHPContent( content, blockPhpSrc, phpDest );
266+
fs.writeFileSync( phpDest, content );
267+
}
268+
}
269+
270+
console.log( ` ✅ ${ source.name } blocks copied (${ blockDirs.length } blocks)` );
271+
}
272+
}
273+
213274
/**
214275
* Generate script-modules-packages.min.php from individual asset files.
215276
* Reads all view.min.asset.php files from modules/block-library and combines them
@@ -882,33 +943,13 @@ async function main() {
882943
console.log( ' ✅ Styles copied' );
883944
}
884945

885-
// 5. Copy blocks (excluding PHP - copied separately from source)
946+
// 5. Copy blocks (unified: scripts, styles, PHP, JSON)
886947
console.log( '\n📦 Copying blocks...' );
887-
const blocksConfig = COPY_CONFIG.blocks;
888-
const blocksSrc = path.join( gutenbergBuildDir, blocksConfig.source );
889-
const blocksDest = path.join( wpIncludesDir, blocksConfig.destination );
890-
891-
if ( fs.existsSync( blocksSrc ) ) {
892-
// Transform function to remove source map comments from blocks/index.js and index.min.js
893-
const blocksTransform = ( content, srcPath, destPath ) => {
894-
// Only process blocks/index.js and blocks/index.min.js
895-
if ( destPath.endsWith( path.join( 'blocks', 'index.js' ) ) ||
896-
destPath.endsWith( path.join( 'blocks', 'index.min.js' ) ) ) {
897-
// Remove sourceMappingURL comment
898-
return content.replace( /\/\/# sourceMappingURL=.*$/m, '' ).trimEnd();
899-
}
900-
return content;
901-
};
902-
903-
copyDirectory( blocksSrc, blocksDest, blocksTransform, {
904-
excludePHP: blocksConfig.excludePHP,
905-
excludeExperimental: blocksConfig.excludeExperimental,
906-
} );
907-
console.log( ' ✅ Blocks copied (JS/CSS/JSON, excluding experimental)' );
908-
}
948+
const blocksDest = path.join( wpIncludesDir, COPY_CONFIG.blocks.destination );
949+
copyBlockAssets( COPY_CONFIG.blocks );
909950

910-
// 6. Copy PHP source files (from packages, to avoid Gutenberg prefixes)
911-
console.log( '\n📦 Copying PHP source files...' );
951+
// 6. Copy non-block PHP source files (from packages)
952+
console.log( '\n📦 Copying non-block PHP files...' );
912953
const phpSourceConfig = COPY_CONFIG.phpSource;
913954

914955
for ( const fileGroup of phpSourceConfig.files ) {
@@ -919,49 +960,11 @@ async function main() {
919960
continue;
920961
}
921962

922-
// Simple file list
923-
if ( fileGroup.files && Array.isArray( fileGroup.files ) && typeof fileGroup.files[0] === 'string' ) {
924-
for ( const file of fileGroup.files ) {
925-
const src = path.join( packageSrc, file );
926-
const dest = path.join( wpIncludesDir, fileGroup.destination, file );
927-
928-
if ( fs.existsSync( src ) ) {
929-
fs.mkdirSync( path.dirname( dest ), { recursive: true } );
930-
let content = fs.readFileSync( src, 'utf8' );
931-
932-
if ( fileGroup.transform ) {
933-
content = transformPHPContent( content, src, dest );
934-
}
935-
936-
fs.writeFileSync( dest, content );
937-
console.log( ` ✅ ${ file }` );
938-
}
939-
}
940-
}
941-
942-
// Pattern-based (e.g., */index.php for all block directories)
943-
if ( fileGroup.pattern ) {
944-
const entries = fs.readdirSync( packageSrc, { withFileTypes: true } );
945-
946-
for ( const entry of entries ) {
947-
if ( ! entry.isDirectory() ) continue;
948-
949-
// Check if this block is experimental
950-
const blockJsonPath = path.join( packageSrc, entry.name, 'block.json' );
951-
if ( isExperimentalBlock( blockJsonPath ) ) {
952-
continue;
953-
}
954-
955-
const src = path.join( packageSrc, entry.name, 'index.php' );
956-
if ( ! fs.existsSync( src ) ) continue;
957-
958-
// Apply rename pattern: comment-template/index.php → comment-template.php
959-
const destFileName = fileGroup.renamePattern
960-
? entry.name + fileGroup.renamePattern.to
961-
: entry.name + '/index.php';
962-
963-
const dest = path.join( wpIncludesDir, fileGroup.destination, destFileName );
963+
for ( const file of fileGroup.files ) {
964+
const src = path.join( packageSrc, file );
965+
const dest = path.join( wpIncludesDir, fileGroup.destination, file );
964966

967+
if ( fs.existsSync( src ) ) {
965968
fs.mkdirSync( path.dirname( dest ), { recursive: true } );
966969
let content = fs.readFileSync( src, 'utf8' );
967970

@@ -971,51 +974,11 @@ async function main() {
971974

972975
fs.writeFileSync( dest, content );
973976
}
974-
console.log( ` ✅ ${ fileGroup.package } PHP files (excluding experimental)` );
975-
}
976-
977-
// Files with from/to mapping (widgets)
978-
if ( fileGroup.files && Array.isArray( fileGroup.files ) && typeof fileGroup.files[0] === 'object' ) {
979-
for ( const fileMap of fileGroup.files ) {
980-
const src = path.join( packageSrc, fileMap.from );
981-
const dest = path.join( wpIncludesDir, fileMap.to );
982-
983-
if ( fs.existsSync( src ) ) {
984-
fs.mkdirSync( path.dirname( dest ), { recursive: true } );
985-
let content = fs.readFileSync( src, 'utf8' );
986-
987-
if ( fileGroup.transform ) {
988-
content = transformPHPContent( content, src, dest );
989-
}
990-
991-
fs.writeFileSync( dest, content );
992-
console.log( ` ✅ ${ fileMap.to }` );
993-
}
994-
}
995-
}
996-
}
997-
998-
// 7. Copy widget block.json files (from build)
999-
console.log( '\n📦 Copying widget block.json files...' );
1000-
const widgetBlockJsonConfig = COPY_CONFIG.widgetBlockJson;
1001-
const widgetSrc = path.join( gutenbergBuildDir, widgetBlockJsonConfig.source );
1002-
1003-
if ( fs.existsSync( widgetSrc ) ) {
1004-
for ( const fileMap of widgetBlockJsonConfig.files ) {
1005-
const src = path.join( widgetSrc, fileMap.from );
1006-
const dest = path.join( wpIncludesDir, fileMap.to );
1007-
1008-
if ( fs.existsSync( src ) ) {
1009-
fs.mkdirSync( path.dirname( dest ), { recursive: true } );
1010-
fs.copyFileSync( src, dest );
1011-
console.log( ` ✅ ${ fileMap.to }` );
1012-
} else {
1013-
console.log( ` ⚠️ Not found: ${ fileMap.from }` );
1014-
}
1015977
}
978+
console.log( ` ✅ ${ fileGroup.package } (${ fileGroup.files.length } files)` );
1016979
}
1017980

1018-
// 8. Copy theme JSON files (from Gutenberg lib directory)
981+
// 7. Copy theme JSON files (from Gutenberg lib directory)
1019982
console.log( '\n📦 Copying theme JSON files...' );
1020983
const themeJsonConfig = COPY_CONFIG.themeJson;
1021984
const gutenbergLibDir = path.join( gutenbergDir, 'lib' );

0 commit comments

Comments
 (0)