Skip to content

Commit 090e18e

Browse files
committed
Consolidate block-json building
1 parent f9aae7e commit 090e18e

File tree

2 files changed

+32
-90
lines changed

2 files changed

+32
-90
lines changed

Gruntfile.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,23 +1630,6 @@ module.exports = function(grunt) {
16301630
}
16311631
} );
16321632

1633-
grunt.registerTask( 'copy:block-json', 'Copies block.json file contents to block-json.php.', function() {
1634-
var blocks = {};
1635-
grunt.file.recurse( SOURCE_DIR + 'wp-includes/blocks', function( abspath, rootdir, subdir, filename ) {
1636-
if ( /^block\.json$/.test( filename ) ) {
1637-
blocks[ subdir ] = grunt.file.readJSON( abspath );
1638-
}
1639-
} );
1640-
grunt.file.write(
1641-
SOURCE_DIR + 'wp-includes/blocks/blocks-json.php',
1642-
'<?php return ' + json2php.make( {
1643-
linebreak: '\n',
1644-
indent: ' ',
1645-
shortArraySyntax: false
1646-
} )( blocks ) + ';'
1647-
);
1648-
} );
1649-
16501633
grunt.registerTask( 'copy:js', [
16511634
'copy:npm-packages',
16521635
'copy:vendor-js',
@@ -1784,7 +1767,6 @@ module.exports = function(grunt) {
17841767
grunt.registerTask( 'build:files', [
17851768
'clean:files',
17861769
'copy:files',
1787-
'copy:block-json',
17881770
'copy:version',
17891771
] );
17901772

tools/gutenberg/copy-gutenberg-build.js

Lines changed: 32 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
const fs = require( 'fs' );
1313
const path = require( 'path' );
14+
const json2php = require( 'json2php' );
1415

1516
// Paths
1617
const rootDir = path.resolve( __dirname, '../..' );
@@ -281,11 +282,18 @@ function generateScriptModulesPackages() {
281282

282283
processDirectory( modulesDir, modulesDir );
283284

284-
// Generate both minified and non-minified PHP files
285-
const phpContentMin = `<?php return ${ phpEncode( assetsMin ) };
286-
`;
287-
const phpContentRegular = `<?php return ${ phpEncode( assetsRegular ) };
288-
`;
285+
// Generate both minified and non-minified PHP files using json2php
286+
const phpContentMin = '<?php return ' + json2php.make( {
287+
linebreak: '\n',
288+
indent: ' ',
289+
shortArraySyntax: false
290+
} )( assetsMin ) + ';';
291+
292+
const phpContentRegular = '<?php return ' + json2php.make( {
293+
linebreak: '\n',
294+
indent: ' ',
295+
shortArraySyntax: false
296+
} )( assetsRegular ) + ';';
289297

290298
const outputPathMin = path.join( wpIncludesDir, 'assets/script-modules-packages.min.php' );
291299
const outputPathRegular = path.join( wpIncludesDir, 'assets/script-modules-packages.php' );
@@ -354,11 +362,18 @@ function generateScriptLoaderPackages() {
354362
}
355363
}
356364

357-
// Generate both minified and non-minified PHP files
358-
const phpContentMin = `<?php return ${ phpEncode( assetsMin ) };
359-
`;
360-
const phpContentRegular = `<?php return ${ phpEncode( assetsRegular ) };
361-
`;
365+
// Generate both minified and non-minified PHP files using json2php
366+
const phpContentMin = '<?php return ' + json2php.make( {
367+
linebreak: '\n',
368+
indent: ' ',
369+
shortArraySyntax: false
370+
} )( assetsMin ) + ';';
371+
372+
const phpContentRegular = '<?php return ' + json2php.make( {
373+
linebreak: '\n',
374+
indent: ' ',
375+
shortArraySyntax: false
376+
} )( assetsRegular ) + ';';
362377

363378
const outputPathMin = path.join( wpIncludesDir, 'assets/script-loader-packages.min.php' );
364379
const outputPathRegular = path.join( wpIncludesDir, 'assets/script-loader-packages.php' );
@@ -460,6 +475,7 @@ ${ staticBlocks.map( name => `\t'${ name }',` ).join( '\n' ) }
460475
/**
461476
* Generate blocks-json.php from all block.json files.
462477
* Reads all block.json files and combines them into a single PHP array.
478+
* Uses json2php to maintain consistency with Core's formatting.
463479
*/
464480
function generateBlocksJson() {
465481
const blocksDir = path.join( wpIncludesDir, 'blocks' );
@@ -489,9 +505,12 @@ function generateBlocksJson() {
489505
}
490506
}
491507

492-
// Generate the PHP file content
493-
const phpContent = `<?php return ${ phpEncode( blocks ) };
494-
`;
508+
// Generate the PHP file content using json2php for consistent formatting
509+
const phpContent = '<?php return ' + json2php.make( {
510+
linebreak: '\n',
511+
indent: ' ',
512+
shortArraySyntax: false
513+
} )( blocks ) + ';';
495514

496515
fs.writeFileSync(
497516
path.join( wpIncludesDir, 'blocks/blocks-json.php' ),
@@ -664,65 +683,6 @@ function parsePHPArray( phpArrayContent ) {
664683
}
665684
}
666685

667-
/**
668-
* Convert JavaScript value to PHP representation.
669-
* Similar to json2php used in Gruntfile.
670-
*
671-
* @param {*} value - Value to encode.
672-
* @param {number} indent - Indentation level.
673-
* @return {string} PHP representation.
674-
*/
675-
function phpEncode( value, indent = 0 ) {
676-
const indentStr = ' '.repeat( indent );
677-
const nextIndentStr = ' '.repeat( indent + 1 );
678-
679-
if ( value === null ) {
680-
return 'null';
681-
}
682-
683-
if ( typeof value === 'boolean' ) {
684-
return value ? 'true' : 'false';
685-
}
686-
687-
if ( typeof value === 'number' ) {
688-
return String( value );
689-
}
690-
691-
if ( typeof value === 'string' ) {
692-
// Escape single quotes and backslashes
693-
const escaped = value
694-
.replace( /\\/g, '\\\\' )
695-
.replace( /'/g, "\\'" );
696-
return `'${ escaped }'`;
697-
}
698-
699-
if ( Array.isArray( value ) ) {
700-
if ( value.length === 0 ) {
701-
return 'array()';
702-
}
703-
704-
const items = value.map( item => `${ nextIndentStr }${ phpEncode( item, indent + 1 ) }` );
705-
return `array(\n${ items.join( ',\n' ) }\n${ indentStr })`;
706-
}
707-
708-
if ( typeof value === 'object' ) {
709-
const keys = Object.keys( value );
710-
711-
if ( keys.length === 0 ) {
712-
return 'array()';
713-
}
714-
715-
const items = keys.map( key => {
716-
const phpKey = phpEncode( key, indent + 1 );
717-
const phpValue = phpEncode( value[ key ], indent + 1 );
718-
return `${ nextIndentStr }${ phpKey } => ${ phpValue }`;
719-
} );
720-
721-
return `array(\n${ items.join( ',\n' ) }\n${ indentStr })`;
722-
}
723-
724-
return 'null';
725-
}
726686

727687
/**
728688
* Transform PHP file contents to work in Core.

0 commit comments

Comments
 (0)