forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy.js
More file actions
647 lines (557 loc) · 17.5 KB
/
copy.js
File metadata and controls
647 lines (557 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#!/usr/bin/env node
/**
* Copy Gutenberg Build Script
*
* This script copies and transforms Gutenberg's build output to WordPress Core.
* It handles path transformations from plugin structure to Core structure.
*
* @package WordPress
*/
const fs = require( 'fs' );
const path = require( 'path' );
const json2php = require( 'json2php' );
const { fromString } = require( 'php-array-reader' );
// Paths.
const rootDir = path.resolve( __dirname, '../..' );
const gutenbergDir = path.join( rootDir, 'gutenberg' );
const gutenbergBuildDir = path.join( gutenbergDir, 'build' );
/*
* Determine build target from command line argument (--dev or --build-dir).
* Default to 'src' for development.
*/
const args = process.argv.slice( 2 );
const buildDirArg = args.find( ( arg ) => arg.startsWith( '--build-dir=' ) );
const buildTarget = buildDirArg
? buildDirArg.split( '=' )[ 1 ]
: args.includes( '--dev' )
? 'src'
: 'build';
const wpIncludesDir = path.join( rootDir, buildTarget, 'wp-includes' );
/**
* Copy configuration.
* Defines what to copy from Gutenberg build and where it goes in Core.
*/
const COPY_CONFIG = {
// JavaScript packages (to wp-includes/js/dist/).
scripts: {
source: 'scripts',
destination: 'js/dist',
copyDirectories: true,
// Rename vendors/ to vendor/ when copying.
directoryRenames: {
vendors: 'vendor',
},
},
/*
* Blocks (to wp-includes/blocks/).
* Unified configuration for all block types.
*/
blocks: {
destination: 'blocks',
sources: [
{
// Block library blocks.
name: 'block-library',
scripts: 'scripts/block-library',
styles: 'styles/block-library',
php: 'scripts/block-library',
},
{
// Widget blocks.
name: 'widgets',
scripts: 'scripts/widgets/blocks',
styles: 'styles/widgets',
php: 'scripts/widgets/blocks',
},
],
},
};
/**
* Given a path to a PHP file which returns a single value, converts that
* value into a native JavaScript value (limited by JSON serialization).
*
* @throws Error when PHP source file unable to be read or parsed.
*
* @param {string} phpFilepath Absolute path of PHP file returning a single value.
* @return {Object|Array} JavaScript representation of value from input file.
*/
function readReturnedValueFromPHPFile( phpFilepath ) {
const content = fs.readFileSync( phpFilepath, 'utf8' );
return fromString( content );
}
/**
* Check if a block is experimental by reading its block.json.
*
* @param {string} blockJsonPath - Path to block.json file.
* @return {boolean} True if block is experimental.
*/
function isExperimentalBlock( blockJsonPath ) {
try {
if ( ! fs.existsSync( blockJsonPath ) ) {
return false;
}
const blockJson = JSON.parse(
fs.readFileSync( blockJsonPath, 'utf8' )
);
return !! blockJson.__experimental;
} catch ( error ) {
return false;
}
}
/**
* Copy all assets for blocks from Gutenberg to Core.
* Handles scripts, styles, PHP, and JSON for all block types in a unified way.
*
* @param {Object} config - Block configuration from COPY_CONFIG.blocks
*/
function copyBlockAssets( config ) {
const blocksDest = path.join( wpIncludesDir, config.destination );
for ( const source of config.sources ) {
const scriptsSrc = path.join( gutenbergBuildDir, source.scripts );
const stylesSrc = path.join( gutenbergBuildDir, source.styles );
const phpSrc = path.join( gutenbergBuildDir, source.php );
if ( ! fs.existsSync( scriptsSrc ) ) {
continue;
}
// Get all block directories from the scripts source.
const blockDirs = fs
.readdirSync( scriptsSrc, { withFileTypes: true } )
.filter( ( entry ) => entry.isDirectory() )
.map( ( entry ) => entry.name );
for ( const blockName of blockDirs ) {
// Skip experimental blocks.
const blockJsonPath = path.join(
scriptsSrc,
blockName,
'block.json'
);
if ( isExperimentalBlock( blockJsonPath ) ) {
continue;
}
const blockDest = path.join( blocksDest, blockName );
fs.mkdirSync( blockDest, { recursive: true } );
// 1. Copy scripts/JSON (everything except PHP)
const blockScriptsSrc = path.join( scriptsSrc, blockName );
if ( fs.existsSync( blockScriptsSrc ) ) {
fs.cpSync(
blockScriptsSrc,
blockDest,
{
recursive: true,
// Skip PHP, copied from build in steps 3 & 4.
filter: f => ! f.endsWith( '.php' ),
}
);
}
// 2. Copy styles (if they exist in per-block directory)
const blockStylesSrc = path.join( stylesSrc, blockName );
if ( fs.existsSync( blockStylesSrc ) ) {
const cssFiles = fs
.readdirSync( blockStylesSrc )
.filter( ( file ) => file.endsWith( '.css' ) );
for ( const cssFile of cssFiles ) {
fs.copyFileSync(
path.join( blockStylesSrc, cssFile ),
path.join( blockDest, cssFile )
);
}
}
// 3. Copy PHP from build
const blockPhpSrc = path.join( phpSrc, `${ blockName }.php` );
const phpDest = path.join(
wpIncludesDir,
config.destination,
`${ blockName }.php`
);
if ( fs.existsSync( blockPhpSrc ) ) {
fs.copyFileSync( blockPhpSrc, phpDest );
}
// 4. Copy PHP subdirectories from build (e.g., navigation-link/shared/*.php)
const blockPhpDir = path.join( phpSrc, blockName );
if ( fs.existsSync( blockPhpDir ) ) {
const rootIndex = path.join( blockPhpDir, 'index.php' );
fs.cpSync( blockPhpDir, blockDest, {
recursive: true,
filter: function hasPhpFiles( src ) {
const stat = fs.statSync( src );
if ( stat.isDirectory() ) {
return fs.readdirSync( src, { withFileTypes: true } ).some(
( entry ) => hasPhpFiles( path.join( src, entry.name ) )
);
}
// Copy PHP files, but skip root index.php (handled by step 3).
return src.endsWith( '.php' ) && src !== rootIndex;
},
} );
}
}
console.log(
` ✅ ${ source.name } blocks copied (${ blockDirs.length } blocks)`
);
}
}
/**
* Generate script-modules-packages.php from individual asset files.
* Recursively scans the Gutenberg modules/ directory for *.min.asset.php files
* and combines their contents into a single PHP file.
*/
function generateScriptModulesPackages() {
const modulesDir = path.join( gutenbergBuildDir, 'modules' );
const assets = {};
/**
* Recursively process directory to find .asset.php files.
*
* @param {string} dir - Directory to process.
* @param {string} baseDir - Base directory for relative paths.
*/
function processDirectory( dir, baseDir ) {
if ( ! fs.existsSync( dir ) ) {
return;
}
const entries = fs.readdirSync( dir, { withFileTypes: true } );
for ( const entry of entries ) {
const fullPath = path.join( dir, entry.name );
if ( entry.isDirectory() ) {
// Skip plugin-only packages (e.g., vips/wasm) that should not be in Core.
if ( entry.name === 'vips' ) {
continue;
}
processDirectory( fullPath, baseDir );
} else if ( entry.name.endsWith( '.min.asset.php' ) ) {
const relativePath = path.relative( baseDir, fullPath );
// Normalize path separators to forward slashes for cross-platform consistency.
const normalizedPath = relativePath
.split( path.sep )
.join( '/' );
const jsPath = normalizedPath
.replace( /\.asset\.php$/, '.js' )
.replace( /\.min\.js$/, '.js' );
try {
const assetData = readReturnedValueFromPHPFile( fullPath );
assets[ jsPath ] = assetData;
} catch ( error ) {
console.error(
` ⚠️ Error reading ${ relativePath }:`,
error.message
);
}
}
}
}
processDirectory( modulesDir, modulesDir );
const phpContent =
'<?php return ' +
json2php.make( {
linebreak: '\n',
indent: '\t',
shortArraySyntax: false,
} )( assets ) +
';';
const outputPath = path.join(
wpIncludesDir,
'assets/script-modules-packages.php'
);
fs.mkdirSync( path.dirname( outputPath ), { recursive: true } );
fs.writeFileSync( outputPath, phpContent );
console.log(
` ✅ Generated with ${ Object.keys( assets ).length } modules`
);
}
/**
* Generate script-loader-packages.php from individual asset files.
* Reads all .min.asset.php files from scripts/ and combines them into a PHP file for script registration.
*/
function generateScriptLoaderPackages() {
const scriptsDir = path.join( gutenbergBuildDir, 'scripts' );
const assets = {};
if ( ! fs.existsSync( scriptsDir ) ) {
console.log( ' ⚠️ Scripts directory not found' );
return;
}
const entries = fs.readdirSync( scriptsDir, { withFileTypes: true } );
for ( const entry of entries ) {
if ( ! entry.isDirectory() ) {
continue;
}
const assetFile = path.join(
scriptsDir,
entry.name,
'index.min.asset.php'
);
if ( ! fs.existsSync( assetFile ) ) {
continue;
}
try {
const assetData = readReturnedValueFromPHPFile( assetFile );
// For regular scripts, use dependencies as-is.
if ( ! assetData.dependencies ) {
assetData.dependencies = [];
}
// Strip plugin-only module dependencies (e.g., vips) that are not in Core.
if ( Array.isArray( assetData.module_dependencies ) ) {
assetData.module_dependencies =
assetData.module_dependencies.filter(
( dep ) =>
! ( dep.id || dep ).startsWith(
'@wordpress/vips'
)
);
}
assets[ `${ entry.name }.js` ] = assetData;
} catch ( error ) {
console.error(
` ⚠️ Error reading ${ entry.name }/index.min.asset.php:`,
error.message
);
}
}
const phpContent =
'<?php return ' +
json2php.make( {
linebreak: '\n',
indent: '\t',
shortArraySyntax: false,
} )( assets ) +
';';
const outputPath = path.join(
wpIncludesDir,
'assets/script-loader-packages.php'
);
fs.mkdirSync( path.dirname( outputPath ), { recursive: true } );
fs.writeFileSync( outputPath, phpContent );
console.log(
` ✅ Generated with ${ Object.keys( assets ).length } packages`
);
}
/**
* Generate require-dynamic-blocks.php and require-static-blocks.php.
* Reads all block.json files from wp-includes/blocks and categorizes them.
* Only includes blocks from block-library, not widgets.
*/
function generateBlockRegistrationFiles() {
const blocksDir = path.join( wpIncludesDir, 'blocks' );
const dynamicBlocks = [];
const staticBlocks = [];
// Widget blocks to exclude (from @wordpress/widgets package).
const widgetBlocks = [ 'legacy-widget', 'widget-group' ];
if ( ! fs.existsSync( blocksDir ) ) {
console.error( ' ⚠️ Blocks directory not found' );
return;
}
const entries = fs.readdirSync( blocksDir, { withFileTypes: true } );
for ( const entry of entries ) {
if ( ! entry.isDirectory() ) {
continue;
}
// Skip widget blocks.
if ( widgetBlocks.includes( entry.name ) ) {
continue;
}
const blockDir = path.join( blocksDir, entry.name );
const blockJsonPath = path.join( blockDir, 'block.json' );
const phpFilePath = path.join( blocksDir, `${ entry.name }.php` );
// Skip if block.json doesn't exist.
if ( ! fs.existsSync( blockJsonPath ) ) {
continue;
}
// Check if it's experimental.
if ( isExperimentalBlock( blockJsonPath ) ) {
continue;
}
// Determine if it's dynamic (has a PHP file).
if ( fs.existsSync( phpFilePath ) ) {
dynamicBlocks.push( entry.name );
} else {
staticBlocks.push( entry.name );
}
}
// Sort alphabetically.
dynamicBlocks.sort();
staticBlocks.sort();
// Generate require-dynamic-blocks.php.
const dynamicContent = `<?php
// This file was autogenerated by tools/gutenberg/copy.js, do not change manually!
// Requires files for dynamic blocks necessary for core blocks registration.
${ dynamicBlocks
.map(
( name ) => `require_once ABSPATH . WPINC . '/blocks/${ name }.php';`
)
.join( '\n' ) }
`;
fs.writeFileSync(
path.join( wpIncludesDir, 'blocks/require-dynamic-blocks.php' ),
dynamicContent
);
// Generate require-static-blocks.php.
const staticContent = `<?php
// This file was autogenerated by tools/gutenberg/copy.js, do not change manually!
// Returns folder names for static blocks necessary for core blocks registration.
return array(
${ staticBlocks.map( ( name ) => `\t'${ name }',` ).join( '\n' ) }
);
`;
fs.writeFileSync(
path.join( wpIncludesDir, 'blocks/require-static-blocks.php' ),
staticContent
);
console.log(
` ✅ Generated: ${ dynamicBlocks.length } dynamic, ${ staticBlocks.length } static blocks`
);
}
/**
* Generate blocks-json.php from all block.json files.
* Reads all block.json files and combines them into a single PHP array.
* Uses json2php to maintain consistency with Core's formatting.
*/
function generateBlocksJson() {
const blocksDir = path.join( wpIncludesDir, 'blocks' );
const blocks = {};
if ( ! fs.existsSync( blocksDir ) ) {
console.error( ' ⚠️ Blocks directory not found' );
return;
}
const entries = fs.readdirSync( blocksDir, { withFileTypes: true } );
for ( const entry of entries ) {
if ( ! entry.isDirectory() ) {
continue;
}
const blockJsonPath = path.join( blocksDir, entry.name, 'block.json' );
if ( fs.existsSync( blockJsonPath ) ) {
try {
const blockJson = JSON.parse(
fs.readFileSync( blockJsonPath, 'utf8' )
);
blocks[ entry.name ] = blockJson;
} catch ( error ) {
console.error(
` ⚠️ Error reading ${ entry.name }/block.json:`,
error.message
);
}
}
}
// Generate the PHP file content using json2php for consistent formatting.
const phpContent =
'<?php return ' +
json2php.make( {
linebreak: '\n',
indent: '\t',
shortArraySyntax: false,
} )( blocks ) +
';';
fs.writeFileSync(
path.join( wpIncludesDir, 'blocks/blocks-json.php' ),
phpContent
);
console.log(
` ✅ Generated with ${ Object.keys( blocks ).length } blocks`
);
}
/**
* Main execution function.
*/
async function main() {
console.log( `📦 Copying Gutenberg build to ${ buildTarget }/...` );
if ( ! fs.existsSync( gutenbergBuildDir ) ) {
console.error( '❌ Gutenberg build directory not found' );
console.error( ' Run: npm run grunt gutenberg:download' );
process.exit( 1 );
}
// 1. Copy JavaScript packages.
console.log( '\n📦 Copying JavaScript packages...' );
const scriptsConfig = COPY_CONFIG.scripts;
const scriptsSrc = path.join( gutenbergBuildDir, scriptsConfig.source );
const scriptsDest = path.join( wpIncludesDir, scriptsConfig.destination );
if ( fs.existsSync( scriptsSrc ) ) {
const entries = fs.readdirSync( scriptsSrc, { withFileTypes: true } );
for ( const entry of entries ) {
const src = path.join( scriptsSrc, entry.name );
if ( entry.isDirectory() ) {
// Check if this should be copied as a directory (like vendors/).
if (
scriptsConfig.copyDirectories &&
scriptsConfig.directoryRenames &&
scriptsConfig.directoryRenames[ entry.name ]
) {
/*
* Copy special directories with rename (vendors/ → vendor/).
* Only copy react-jsx-runtime from vendors (react and react-dom come from Core's node_modules).
*/
const destName =
scriptsConfig.directoryRenames[ entry.name ];
const dest = path.join( scriptsDest, destName );
if ( entry.name === 'vendors' ) {
// Only copy react-jsx-runtime files, skip react and react-dom.
const vendorFiles = fs.readdirSync( src );
let copiedCount = 0;
fs.mkdirSync( dest, { recursive: true } );
for ( const file of vendorFiles ) {
if (
file.startsWith( 'react-jsx-runtime' ) &&
file.endsWith( '.js' )
) {
const srcFile = path.join( src, file );
const destFile = path.join( dest, file );
fs.copyFileSync( srcFile, destFile );
copiedCount++;
}
}
console.log(
` ✅ ${ entry.name }/ → ${ destName }/ (react-jsx-runtime only, ${ copiedCount } files)`
);
}
} else {
/*
* Flatten package structure: package-name/index.js → package-name.js.
* This matches Core's expected file structure.
*/
const packageFiles = fs.readdirSync( src );
for ( const file of packageFiles ) {
if (
/^index\.(js|min\.js)$/.test( file )
) {
const srcFile = path.join( src, file );
// Replace 'index.' with 'package-name.'.
const destFile = file.replace(
/^index\./,
`${ entry.name }.`
);
const destPath = path.join( scriptsDest, destFile );
fs.mkdirSync( path.dirname( destPath ), {
recursive: true,
} );
fs.copyFileSync( srcFile, destPath );
}
}
}
} else if ( entry.isFile() && entry.name.endsWith( '.js' ) ) {
// Copy root-level JS files.
const dest = path.join( scriptsDest, entry.name );
fs.mkdirSync( path.dirname( dest ), { recursive: true } );
fs.copyFileSync( src, dest );
}
}
console.log( ' ✅ JavaScript packages copied' );
}
// 2. Copy blocks (unified: scripts, styles, PHP, JSON).
console.log( '\n📦 Copying blocks...' );
copyBlockAssets( COPY_CONFIG.blocks );
// 3. Generate script-modules-packages.php from individual asset files.
console.log( '\n📦 Generating script-modules-packages.php...' );
generateScriptModulesPackages();
// 4. Generate script-loader-packages.php.
console.log( '\n📦 Generating script-loader-packages.php...' );
generateScriptLoaderPackages();
// 5. Generate require-dynamic-blocks.php and require-static-blocks.php.
console.log( '\n📦 Generating block registration files...' );
generateBlockRegistrationFiles();
// 6. Generate blocks-json.php from block.json files.
console.log( '\n📦 Generating blocks-json.php...' );
generateBlocksJson();
console.log( '\n✅ Copy complete!' );
}
// Run main function.
main().catch( ( error ) => {
console.error( '❌ Unexpected error:', error );
process.exit( 1 );
} );