@wordpress/build 0.27.0 → 0.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,24 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.28.0 (2026-11-18)
6
+
7
+ ### Enhancements
8
+
9
+ - In watch mode, copy a changed PHP file from `wpCopyFiles` to the build on its own instead of rebuilding its whole package, so the change reaches the build in milliseconds rather than seconds. Adding or removing a PHP file still rebuilds the package ([#86271](https://github.com/WordPress/gutenberg/pull/86271)).
10
+
11
+ ### Bug Fixes
12
+
13
+ - Exit the generated PHP files when they are accessed directly, print the inline styles and scripts of generated pages through the WordPress enqueue functions, and escape the message shown when a user cannot access a page. Plugins shipping the build now pass the WordPress.org Plugin Directory review and Plugin Check ([#86699](https://github.com/WordPress/gutenberg/pull/86699)).
14
+ - Require PostCSS `^8.5.33` for CSS parser fixes and source-map file-loading protections ([#86899](https://github.com/WordPress/gutenberg/pull/86899)).
15
+ - Let a full-page screen show the admin bar again. The page template's rule that hides it now loads after the page's own styles, so it has zero specificity to keep it overridable ([#86992](https://github.com/WordPress/gutenberg/pull/86992)).
16
+ - Load a `wp-admin` page's scripts and styles only on its own screen, not on a Core screen whose screen ID matches the page slug. The `dashboard` page no longer hides the content of the Core Dashboard (`index.php`) ([#86995](https://github.com/WordPress/gutenberg/pull/86995)).
17
+
18
+ ### Internal
19
+
20
+ - Widen the `@wordpress/theme` peer dependency range to include 5.x ([#86720](https://github.com/WordPress/gutenberg/pull/86720)).
21
+ - Update the Vitest development dependency to 5.1.0 ([#86509](https://github.com/WordPress/gutenberg/pull/86509)).
22
+
5
23
  ## 0.27.0 (2026-11-04)
6
24
 
7
25
  ### Enhancements
package/README.md CHANGED
@@ -137,20 +137,37 @@ Custom SCSS entry point patterns:
137
137
 
138
138
  ### `wpCopyFiles`
139
139
 
140
- Files to copy with optional PHP transformations:
140
+ Files to copy to `build/scripts/{package}/`, with optional PHP transformations:
141
141
 
142
142
  ```json
143
143
  {
144
- "wpCopyFiles": [
145
- {
146
- "from": "src/index.php",
147
- "to": "build/index.php",
148
- "transform": "php"
144
+ "wpCopyFiles": {
145
+ "files": [ "src/**/*.php", "src/*/block.json" ],
146
+ "transforms": {
147
+ "php": {
148
+ "functionPrefix": "gutenberg_",
149
+ "classSuffix": "_Gutenberg",
150
+ "prefixFunctions": [ "wp_apply_colors_support" ],
151
+ "suffixClasses": [ "WP_Navigation_Block_Renderer" ],
152
+ "addActionPriority": 20,
153
+ "flattenIndexFiles": true
154
+ }
149
155
  }
150
- ]
156
+ }
151
157
  }
152
158
  ```
153
159
 
160
+ `files` are glob patterns relative to the package, and each file keeps its path relative to `src/`. The PHP transforms apply only to `.php` files, and are skipped when `IS_WORDPRESS_CORE` is set:
161
+
162
+ - `functionPrefix`: Prefix for the functions a file defines, replacing a `wp_` prefix, and for their uses.
163
+ - `prefixFunctions`: Other functions to prefix with `functionPrefix`.
164
+ - `classSuffix` and `suffixClasses`: Suffix to add to the given classes.
165
+ - `addActionPriority`: Priority to add to the `init` action registering a block.
166
+ - `filenameSuffix`: Suffix for the copied file's name, e.g. `-gutenberg` for `class-wp-style-engine-gutenberg.php`.
167
+ - `flattenIndexFiles`: Copy `{name}/index.php` to `{name}.php`.
168
+
169
+ In watch mode, a change to one of these PHP files copies only that file to the build, without rebuilding the package. Adding or removing a file rebuilds the package, but the build's copy of a removed file is not deleted.
170
+
154
171
  ### `wpWorkers`
155
172
 
156
173
  Worker bundle definitions for packages that need self-contained Web Worker files.
@@ -480,7 +497,7 @@ routes/
480
497
  stage.tsx # Main content component
481
498
  inspector.tsx # Optional sidebar component
482
499
  canvas.tsx # Optional custom canvas component
483
- route.tsx # Optional lifecycle hooks (beforeLoad, loader, canvas)
500
+ route.tsx # Optional lifecycle hooks (beforeLoad, loader, canvas, title)
484
501
  ```
485
502
 
486
503
  ### Route Configuration
@@ -550,6 +567,7 @@ Export a `route` object with optional hooks. Each hook receives `{ params, searc
550
567
  - `CanvasData` (`{ postType, postId, isPreview?, editLink? }`) → default WordPress editor canvas
551
568
  - `null` → custom `canvas.tsx` component (if provided)
552
569
  - `undefined` or omitted → no canvas
570
+ - **`title`** — Runs in parallel with `loader`. Returns the page's title as a string, or a promise of a string. After each navigation within the application, the title is announced to screen readers and used to update the document title. Every route should define it: without it, screen reader users do not hear that the page changed, and the browser tab keeps the title of the previous page.
553
571
 
554
572
  ```tsx
555
573
  export const route = {
@@ -574,6 +592,10 @@ export const route = {
574
592
  // Return undefined to show no canvas
575
593
  // return undefined;
576
594
  },
595
+ title: ( { params, search } ) => {
596
+ // Return the page title
597
+ return __( 'Settings', 'my-plugin' );
598
+ },
577
599
  };
578
600
  ```
579
601
 
package/lib/build.mjs CHANGED
@@ -23,7 +23,12 @@ import {
23
23
  getPhpReplacements,
24
24
  renderTemplateToString,
25
25
  } from './php-generator.mjs';
26
- import { getPackageInfo, getPackageInfoFromFile } from './package-utils.mjs';
26
+ import {
27
+ boolConfigVal,
28
+ getPackageInfo,
29
+ getPackageInfoFromFile,
30
+ } from './package-utils.mjs';
31
+ import { copyPackageFile, getCopyFiles } from './copy-files.mjs';
27
32
  import { getBrowserslistQueries } from './browserslist.mjs';
28
33
  import { getSourceFileGlob, isTestSourceFile } from './source-files.mjs';
29
34
  import { createWordpressExternalsPlugin } from './wordpress-externals-plugin.mjs';
@@ -110,20 +115,6 @@ const PAGES = WP_PLUGIN_CONFIG.pages || [];
110
115
  // enforce this themselves.
111
116
  const DEFAULT_PAGE_CAPABILITY = 'manage_options';
112
117
 
113
- /**
114
- * Interprets a configuration value as a boolean, where `"true"` and `"1"`
115
- * are considered true while all other values are false.
116
- *
117
- * @param {string|undefined} value The configuration value to interpret.
118
- * @return {boolean|undefined} Boolean interpretation of the given configuration value, or undefined if not set.
119
- */
120
- const boolConfigVal = ( value ) => {
121
- if ( value === undefined ) {
122
- return undefined;
123
- }
124
- return [ 'true', '1' ].includes( value.toLowerCase() );
125
- };
126
-
127
118
  const baseDefine = {
128
119
  'globalThis.IS_GUTENBERG_PLUGIN': JSON.stringify(
129
120
  boolConfigVal( process.env.IS_GUTENBERG_PLUGIN ) ??
@@ -419,65 +410,6 @@ function normalizePath( p ) {
419
410
  return p.replace( /\\/g, '/' );
420
411
  }
421
412
 
422
- function transformPhpContent( content, transforms ) {
423
- const {
424
- functionPrefix = '',
425
- classSuffix = '',
426
- prefixFunctions = [],
427
- suffixClasses = [],
428
- addActionPriority,
429
- } = transforms;
430
-
431
- content = content.toString();
432
-
433
- /*
434
- * Transforms are used to modify PHP files that are committed to version
435
- * control in their wordpress-develop state (`wp_` function prefixes, `WP_`
436
- * class prefixes, etc.). When building for WordPress Core, it's not
437
- * necessary to perform these steps.
438
- */
439
- if (
440
- boolConfigVal( process.env.IS_WORDPRESS_CORE ) ??
441
- boolConfigVal( process.env.npm_package_config_IS_WORDPRESS_CORE )
442
- ) {
443
- return content;
444
- }
445
-
446
- if ( prefixFunctions.length ) {
447
- content = content.replace(
448
- new RegExp( prefixFunctions.join( '|' ), 'g' ),
449
- ( match ) => `${ functionPrefix }${ match.replace( /^wp_/, '' ) }`
450
- );
451
- }
452
-
453
- if ( suffixClasses.length ) {
454
- content = content.replace(
455
- new RegExp( suffixClasses.join( '|' ), 'g' ),
456
- ( match ) => `${ match }${ classSuffix }`
457
- );
458
- }
459
-
460
- if ( functionPrefix ) {
461
- content = Array.from(
462
- content.matchAll( /^\s*function ([^\(]+)/gm )
463
- ).reduce( ( result, [ , functionName ] ) => {
464
- return result.replace(
465
- new RegExp( functionName + '(?![a-zA-Z0-9_])', 'g' ),
466
- ( match ) => functionPrefix + match.replace( /^wp_/, '' )
467
- );
468
- }, content );
469
- }
470
-
471
- if ( addActionPriority ) {
472
- content = content.replace(
473
- /(add_action\(\s*'init',\s*'gutenberg_register_block_[^']+'(?!,))/,
474
- `$1, ${ addActionPriority }`
475
- );
476
- }
477
-
478
- return content;
479
- }
480
-
481
413
  /**
482
414
  * Plugin to handle moment-timezone aliases.
483
415
  * Redirects moment-timezone imports to use pre-built bundles with limited data.
@@ -901,76 +833,15 @@ async function bundlePackage( packageName, options = {} ) {
901
833
  }
902
834
  }
903
835
 
904
- if ( packageJson.wpCopyFiles ) {
905
- const { files, transforms = {} } = packageJson.wpCopyFiles;
906
- const packageSourceDir = path.join( packageDir, 'src' );
907
- const outputDir = path.join( BUILD_DIR, 'scripts', packageName );
908
-
909
- for ( const filePattern of files ) {
910
- const matchedFiles = await glob(
911
- normalizePath( path.join( packageDir, filePattern ) )
912
- );
913
-
914
- for ( const sourceFile of matchedFiles ) {
915
- const relativePath = path.relative(
916
- packageSourceDir,
917
- sourceFile
918
- );
919
- const destPath = path.join( outputDir, relativePath );
920
- const destDir = path.dirname( destPath );
921
-
922
- if ( sourceFile.endsWith( '.php' ) && transforms.php ) {
923
- builds.push(
924
- ( async () => {
925
- let finalPath = destPath;
926
- let finalDir = destDir;
927
-
928
- const content = await readFile(
929
- sourceFile,
930
- 'utf8'
931
- );
932
- const transformed = transformPhpContent(
933
- content,
934
- transforms.php
935
- );
936
-
937
- if ( transforms.php.filenameSuffix ) {
938
- const ext = path.extname( destPath );
939
- const base = path.basename( destPath, ext );
940
- finalPath = path.join(
941
- destDir,
942
- `${ base }${ transforms.php.filenameSuffix }${ ext }`
943
- );
944
- }
945
-
946
- // Check if we should flatten index.php files
947
- if (
948
- transforms.php.flattenIndexFiles &&
949
- path.basename( sourceFile ) === 'index.php'
950
- ) {
951
- // Flatten: button/index.php → button.php
952
- const parentDir = path.dirname( finalPath );
953
- const blockName = path.basename( parentDir );
954
- finalPath = path.join(
955
- path.dirname( parentDir ),
956
- `${ blockName }.php`
957
- );
958
- finalDir = path.dirname( finalPath );
959
- }
960
-
961
- await mkdir( finalDir, { recursive: true } );
962
- await writeFile( finalPath, transformed );
963
- } )()
964
- );
965
- } else {
966
- builds.push(
967
- mkdir( destDir, { recursive: true } ).then( () =>
968
- copyFile( sourceFile, destPath )
969
- )
970
- );
971
- }
972
- }
973
- }
836
+ for ( const sourceFile of await getCopyFiles( packageDir, packageJson ) ) {
837
+ builds.push(
838
+ copyPackageFile(
839
+ sourceFile,
840
+ packageDir,
841
+ path.join( BUILD_DIR, 'scripts', packageName ),
842
+ packageJson
843
+ )
844
+ );
974
845
  }
975
846
 
976
847
  if ( builds.length === 0 ) {
@@ -2721,6 +2592,7 @@ async function watchMode() {
2721
2592
  * Rebuild a package and any affected scripts/modules.
2722
2593
  *
2723
2594
  * @param {string} packageName Package to rebuild (short name).
2595
+ * @return {Promise<boolean>} Whether the package itself was rebuilt.
2724
2596
  */
2725
2597
  async function rebuildPackage( packageName ) {
2726
2598
  try {
@@ -2777,8 +2649,10 @@ async function watchMode() {
2777
2649
  );
2778
2650
  }
2779
2651
  }
2652
+ return true;
2780
2653
  } catch ( error ) {
2781
2654
  console.log( `❌ ${ packageName } - Error: ${ error.message }` );
2655
+ return false;
2782
2656
  }
2783
2657
  }
2784
2658
 
@@ -2800,6 +2674,58 @@ async function watchMode() {
2800
2674
  }
2801
2675
  }
2802
2676
 
2677
+ /**
2678
+ * Copy a changed PHP file to the build, without rebuilding its package.
2679
+ *
2680
+ * @param {string} filename Absolute path of the PHP file.
2681
+ */
2682
+ async function recopyPhpFile( filename ) {
2683
+ const packageName = getPackageName( filename );
2684
+ const relativePath = path.relative( PACKAGES_DIR, filename );
2685
+ try {
2686
+ const startTime = Date.now();
2687
+ await copyPackageFile(
2688
+ filename,
2689
+ path.join( PACKAGES_DIR, packageName ),
2690
+ path.join( BUILD_DIR, 'scripts', packageName ),
2691
+ fullToPackageJson.get( shortToFull.get( packageName ) )
2692
+ );
2693
+ const copyTime = Date.now() - startTime;
2694
+ console.log( `✅ ${ relativePath } (copied) (${ copyTime }ms)` );
2695
+ } catch ( error ) {
2696
+ // The file was removed since; its removal rebuilds the package.
2697
+ if ( error.code === 'ENOENT' ) {
2698
+ return;
2699
+ }
2700
+ console.log( `❌ ${ relativePath } - Error: ${ error.message }` );
2701
+ }
2702
+ }
2703
+
2704
+ /*
2705
+ * The PHP files each package copies to the build with `wpCopyFiles`, by
2706
+ * package name. Only the build's copy of such a file depends on it, so a
2707
+ * change to it needs the file copied again, not its package rebuilt.
2708
+ */
2709
+ const copiedPhpFiles = new Map();
2710
+
2711
+ /**
2712
+ * Find the PHP files a package copies to the build with `wpCopyFiles`.
2713
+ *
2714
+ * @param {string} packageName Package name (short name).
2715
+ */
2716
+ async function updateCopiedPhpFiles( packageName ) {
2717
+ const copyFiles = await getCopyFiles(
2718
+ path.join( PACKAGES_DIR, packageName ),
2719
+ fullToPackageJson.get( shortToFull.get( packageName ) )
2720
+ );
2721
+ copiedPhpFiles.set(
2722
+ packageName,
2723
+ new Set( copyFiles.filter( ( file ) => file.endsWith( '.php' ) ) )
2724
+ );
2725
+ }
2726
+
2727
+ await Promise.all( PACKAGES.map( updateCopiedPhpFiles ) );
2728
+
2803
2729
  async function processNextRebuild() {
2804
2730
  if ( needsRebuild.size === 0 ) {
2805
2731
  isRebuilding = false;
@@ -2809,6 +2735,9 @@ async function watchMode() {
2809
2735
  const itemsToRebuild = Array.from( needsRebuild );
2810
2736
  needsRebuild.clear();
2811
2737
 
2738
+ const phpFilesToCopy = [];
2739
+ const rebuiltPackages = new Set();
2740
+
2812
2741
  for ( const item of itemsToRebuild ) {
2813
2742
  // Check if it's a route (prefixed with 'route:')
2814
2743
  if ( item.startsWith( 'route:' ) ) {
@@ -2817,8 +2746,17 @@ async function watchMode() {
2817
2746
  } else if ( item.startsWith( 'widget:' ) ) {
2818
2747
  const widgetDirName = item.slice( 7 ); // Remove 'widget:' prefix
2819
2748
  await rebuildWidget( widgetDirName );
2820
- } else {
2821
- await rebuildPackage( item );
2749
+ } else if ( item.startsWith( 'php:' ) ) {
2750
+ phpFilesToCopy.push( item.slice( 4 ) ); // Remove 'php:' prefix
2751
+ } else if ( await rebuildPackage( item ) ) {
2752
+ rebuiltPackages.add( item );
2753
+ }
2754
+ }
2755
+
2756
+ // Copy the PHP files last, unless a rebuild of their package did.
2757
+ for ( const file of phpFilesToCopy ) {
2758
+ if ( ! rebuiltPackages.has( getPackageName( file ) ) ) {
2759
+ await recopyPhpFile( file );
2822
2760
  }
2823
2761
  }
2824
2762
 
@@ -2860,7 +2798,7 @@ async function watchMode() {
2860
2798
  console.error( '❌ Watcher error:', error );
2861
2799
  } );
2862
2800
 
2863
- const handleFileChange = async ( filename ) => {
2801
+ const handleFileChange = async ( filename, event ) => {
2864
2802
  if ( ! isPackageSourceFile( filename ) ) {
2865
2803
  return;
2866
2804
  }
@@ -2870,19 +2808,47 @@ async function watchMode() {
2870
2808
  return;
2871
2809
  }
2872
2810
 
2811
+ /*
2812
+ * A changed PHP file only needs copying again. An added or removed
2813
+ * one still rebuilds its package, which copies all of its files.
2814
+ */
2815
+ const file = normalizePath( path.resolve( filename ) );
2816
+ const item =
2817
+ event === 'change' && copiedPhpFiles.get( packageName )?.has( file )
2818
+ ? `php:${ file }`
2819
+ : packageName;
2820
+
2821
+ /*
2822
+ * Find the package's copied PHP files again when one may have been
2823
+ * added or removed. Until that finishes, a change to an added file
2824
+ * rebuilds the package, which copies it too.
2825
+ */
2826
+ if ( event !== 'change' && file.endsWith( '.php' ) ) {
2827
+ updateCopiedPhpFiles( packageName ).catch( ( error ) => {
2828
+ console.log(
2829
+ `❌ ${ packageName } - Error finding copied PHP files: ${ error.message }`
2830
+ );
2831
+ } );
2832
+ }
2833
+
2834
+ // Queue the item, so that it is not written while a rebuild is running.
2873
2835
  if ( isRebuilding ) {
2874
- needsRebuild.add( packageName );
2836
+ needsRebuild.add( item );
2875
2837
  return;
2876
2838
  }
2877
2839
 
2878
2840
  isRebuilding = true;
2879
- await rebuildPackage( packageName );
2841
+ needsRebuild.add( item );
2880
2842
  await processNextRebuild();
2881
2843
  };
2882
2844
 
2883
- watcher.on( 'change', handleFileChange );
2884
- watcher.on( 'add', handleFileChange );
2885
- watcher.on( 'unlink', handleFileChange );
2845
+ watcher.on( 'change', ( filename ) =>
2846
+ handleFileChange( filename, 'change' )
2847
+ );
2848
+ watcher.on( 'add', ( filename ) => handleFileChange( filename, 'add' ) );
2849
+ watcher.on( 'unlink', ( filename ) =>
2850
+ handleFileChange( filename, 'unlink' )
2851
+ );
2886
2852
 
2887
2853
  // Watch route files if routes exist
2888
2854
  if ( allRoutes.length > 0 ) {
@@ -0,0 +1,165 @@
1
+ import { readFile, writeFile, copyFile, mkdir } from 'fs/promises';
2
+ import path from 'path';
3
+ import glob from 'fast-glob';
4
+ import { boolConfigVal } from './package-utils.mjs';
5
+
6
+ /**
7
+ * @typedef {Object} PhpTransforms
8
+ *
9
+ * @property {string} [functionPrefix] Prefix for the functions a PHP file defines, replacing a `wp_` prefix.
10
+ * @property {string} [classSuffix] Suffix for the classes in `suffixClasses`.
11
+ * @property {string[]} [prefixFunctions] Other functions to prefix with `functionPrefix`.
12
+ * @property {string[]} [suffixClasses] Classes to suffix with `classSuffix`.
13
+ * @property {number} [addActionPriority] Priority to add to the `init` action registering a block.
14
+ * @property {string} [filenameSuffix] Suffix for the copied file's name.
15
+ * @property {boolean} [flattenIndexFiles] Whether to copy `{name}/index.php` to `{name}.php`.
16
+ */
17
+
18
+ /**
19
+ * @typedef {Object} CopyFiles
20
+ *
21
+ * @property {string[]} files Glob patterns of the files to copy, relative to the package.
22
+ * @property {{ php?: PhpTransforms }} [transforms] Transforms to apply to the copied files.
23
+ */
24
+
25
+ /**
26
+ * @typedef {{ wpCopyFiles?: CopyFiles }} CopyFilesPackageJson
27
+ */
28
+
29
+ /**
30
+ * Apply a package's `wpCopyFiles` PHP transforms to a PHP file's content.
31
+ *
32
+ * @param {string} content PHP file content.
33
+ * @param {PhpTransforms} transforms The `wpCopyFiles.transforms.php` configuration.
34
+ * @return {string} Transformed content.
35
+ */
36
+ export function transformPhpContent( content, transforms ) {
37
+ const {
38
+ functionPrefix = '',
39
+ classSuffix = '',
40
+ prefixFunctions = [],
41
+ suffixClasses = [],
42
+ addActionPriority,
43
+ } = transforms;
44
+
45
+ content = content.toString();
46
+
47
+ /*
48
+ * Transforms are used to modify PHP files that are committed to version
49
+ * control in their wordpress-develop state (`wp_` function prefixes, `WP_`
50
+ * class prefixes, etc.). When building for WordPress Core, it's not
51
+ * necessary to perform these steps.
52
+ */
53
+ if (
54
+ boolConfigVal( process.env.IS_WORDPRESS_CORE ) ??
55
+ boolConfigVal( process.env.npm_package_config_IS_WORDPRESS_CORE )
56
+ ) {
57
+ return content;
58
+ }
59
+
60
+ if ( prefixFunctions.length ) {
61
+ content = content.replace(
62
+ new RegExp( prefixFunctions.join( '|' ), 'g' ),
63
+ ( match ) => `${ functionPrefix }${ match.replace( /^wp_/, '' ) }`
64
+ );
65
+ }
66
+
67
+ if ( suffixClasses.length ) {
68
+ content = content.replace(
69
+ new RegExp( suffixClasses.join( '|' ), 'g' ),
70
+ ( match ) => `${ match }${ classSuffix }`
71
+ );
72
+ }
73
+
74
+ if ( functionPrefix ) {
75
+ content = Array.from(
76
+ content.matchAll( /^\s*function ([^\(]+)/gm )
77
+ ).reduce( ( result, [ , functionName ] ) => {
78
+ return result.replace(
79
+ new RegExp( functionName + '(?![a-zA-Z0-9_])', 'g' ),
80
+ ( match ) => functionPrefix + match.replace( /^wp_/, '' )
81
+ );
82
+ }, content );
83
+ }
84
+
85
+ if ( addActionPriority ) {
86
+ content = content.replace(
87
+ /(add_action\(\s*'init',\s*'gutenberg_register_block_[^']+'(?!,))/,
88
+ `$1, ${ addActionPriority }`
89
+ );
90
+ }
91
+
92
+ return content;
93
+ }
94
+
95
+ /**
96
+ * Find the source files a package copies to the build with `wpCopyFiles`.
97
+ *
98
+ * @param {string} packageDir Package directory.
99
+ * @param {CopyFilesPackageJson} packageJson Package's package.json.
100
+ * @return {Promise<string[]>} Absolute paths of the files to copy.
101
+ */
102
+ export async function getCopyFiles( packageDir, packageJson ) {
103
+ if ( ! packageJson.wpCopyFiles ) {
104
+ return [];
105
+ }
106
+ // Glob relative to the package, so that its path is not read as a pattern.
107
+ return glob( packageJson.wpCopyFiles.files, {
108
+ cwd: packageDir,
109
+ absolute: true,
110
+ } );
111
+ }
112
+
113
+ /**
114
+ * Copy one of a package's `wpCopyFiles` to the build, transforming PHP files.
115
+ *
116
+ * @param {string} sourceFile Absolute path of the file to copy.
117
+ * @param {string} packageDir Package directory.
118
+ * @param {string} outputDir Directory the package's files are copied to.
119
+ * @param {CopyFilesPackageJson} packageJson Package's package.json.
120
+ */
121
+ export async function copyPackageFile(
122
+ sourceFile,
123
+ packageDir,
124
+ outputDir,
125
+ packageJson
126
+ ) {
127
+ const transforms = packageJson.wpCopyFiles?.transforms ?? {};
128
+ const relativePath = path.relative(
129
+ path.join( packageDir, 'src' ),
130
+ sourceFile
131
+ );
132
+ let destPath = path.join( outputDir, relativePath );
133
+
134
+ if ( ! sourceFile.endsWith( '.php' ) || ! transforms.php ) {
135
+ await mkdir( path.dirname( destPath ), { recursive: true } );
136
+ await copyFile( sourceFile, destPath );
137
+ return;
138
+ }
139
+
140
+ const content = await readFile( sourceFile, 'utf8' );
141
+ const transformed = transformPhpContent( content, transforms.php );
142
+
143
+ if ( transforms.php.filenameSuffix ) {
144
+ const ext = path.extname( destPath );
145
+ const base = path.basename( destPath, ext );
146
+ destPath = path.join(
147
+ path.dirname( destPath ),
148
+ `${ base }${ transforms.php.filenameSuffix }${ ext }`
149
+ );
150
+ }
151
+
152
+ // Check if we should flatten index.php files
153
+ if (
154
+ transforms.php.flattenIndexFiles &&
155
+ path.basename( sourceFile ) === 'index.php'
156
+ ) {
157
+ // Flatten: button/index.php → button.php
158
+ const parentDir = path.dirname( destPath );
159
+ const blockName = path.basename( parentDir );
160
+ destPath = path.join( path.dirname( parentDir ), `${ blockName }.php` );
161
+ }
162
+
163
+ await mkdir( path.dirname( destPath ), { recursive: true } );
164
+ await writeFile( destPath, transformed );
165
+ }
@@ -124,3 +124,17 @@ export function getPackageInfoFromFile( packageJsonPath ) {
124
124
  packagePathCache.set( packageJsonPath, packageJson );
125
125
  return packageJson;
126
126
  }
127
+
128
+ /**
129
+ * Interprets a configuration value as a boolean, where `"true"` and `"1"`
130
+ * are considered true while all other values are false.
131
+ *
132
+ * @param {string|undefined} value The configuration value to interpret.
133
+ * @return {boolean|undefined} Boolean interpretation of the given configuration value, or undefined if not set.
134
+ */
135
+ export const boolConfigVal = ( value ) => {
136
+ if ( value === undefined ) {
137
+ return undefined;
138
+ }
139
+ return [ 'true', '1' ].includes( value.toLowerCase() );
140
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/build",
3
- "version": "0.27.0",
3
+ "version": "0.28.0",
4
4
  "description": "Build tool for WordPress plugins.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -41,9 +41,9 @@
41
41
  "wp-build": "./lib/build.mjs"
42
42
  },
43
43
  "dependencies": {
44
- "@wordpress/browserslist-config": "^7.1.0",
45
- "@wordpress/style-runtime": "^0.15.0",
46
- "@wordpress/theme-build-tools": "^1.0.0",
44
+ "@wordpress/browserslist-config": "^7.2.0",
45
+ "@wordpress/style-runtime": "^0.16.0",
46
+ "@wordpress/theme-build-tools": "^1.1.0",
47
47
  "autoprefixer": "^10.4.21",
48
48
  "browserslist": "^4.28.7",
49
49
  "browserslist-to-esbuild": "^2.1.1",
@@ -52,7 +52,7 @@
52
52
  "cssnano": "^6.0.1",
53
53
  "fast-glob": "^3.2.7",
54
54
  "moment-timezone": "^0.5.40",
55
- "postcss": "^8.4.38",
55
+ "postcss": "^8.5.33",
56
56
  "postcss-modules": "^6.0.1",
57
57
  "rolldown": "^1.2.15",
58
58
  "rtlcss": "^4.3.0",
@@ -60,11 +60,11 @@
60
60
  },
61
61
  "devDependencies": {
62
62
  "@types/node": "^24.13.3",
63
- "@wordpress/theme": "^4.0.0",
64
- "vitest": "^5.0.5"
63
+ "@wordpress/theme": "^5.0.0",
64
+ "vitest": "^5.1.0"
65
65
  },
66
66
  "peerDependencies": {
67
- "@wordpress/theme": "^3.0.0 || ^4.0.0"
67
+ "@wordpress/theme": "^3.0.0 || ^4.0.0 || ^5.0.0"
68
68
  },
69
69
  "peerDependenciesMeta": {
70
70
  "@wordpress/theme": {
@@ -74,5 +74,5 @@
74
74
  "publishConfig": {
75
75
  "access": "public"
76
76
  },
77
- "gitHead": "9b70363b9f93cff0583a1f3153738d383cf5a49e"
77
+ "gitHead": "1000cdf1dffd5b5e69f75cc4360aa76db8252264"
78
78
  }
@@ -6,6 +6,10 @@
6
6
  * @package {{PREFIX}}
7
7
  */
8
8
 
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
9
13
  // Load script module registration.
10
14
  $modules_file = __DIR__ . '/modules.php';
11
15
  if ( file_exists( $modules_file ) ) {
@@ -8,6 +8,10 @@
8
8
  * @package {{PREFIX}}
9
9
  */
10
10
 
11
+ if ( ! defined( 'ABSPATH' ) ) {
12
+ exit;
13
+ }
14
+
11
15
  return array(
12
16
  'version' => '{{VERSION}}',
13
17
  'build_url' => {{BASE_URL}},
@@ -6,6 +6,10 @@
6
6
  * @package {{PREFIX}}
7
7
  */
8
8
 
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
9
13
  /**
10
14
  * Register all script modules.
11
15
  */
@@ -6,6 +6,10 @@
6
6
  * @package {{PREFIX}}
7
7
  */
8
8
 
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
9
13
  return array(
10
14
  {{MODULES}}
11
15
  );
@@ -10,6 +10,10 @@
10
10
  * @package {{PREFIX}}
11
11
  */
12
12
 
13
+ if ( ! defined( 'ABSPATH' ) ) {
14
+ exit;
15
+ }
16
+
13
17
  // Global storage for {{PAGE_SLUG}} routes and menu items
14
18
  global ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_wp_admin_routes, ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_wp_admin_menu_items;
15
19
  ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_wp_admin_routes = array();
@@ -134,10 +138,17 @@ function {{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_wp_admin_enqueue_scripts( $hook_suf
134
138
  // Check all possible ways this page can be accessed:
135
139
  // 1. Menu page via admin.php?page={{PAGE_SLUG}}-wp-admin (plugin)
136
140
  // 2. Direct file via {{PAGE_SLUG}}.php (Core) - screen ID will be '{{PAGE_SLUG}}'
141
+ // The screen ID alone is not enough: a Core screen can share it with the page
142
+ // slug (for example, index.php has the screen ID 'dashboard').
137
143
  $current_screen = get_current_screen();
138
144
  $is_our_page = (
139
145
  ( isset( $_GET['page'] ) && '{{PAGE_SLUG}}-wp-admin' === $_GET['page'] ) || // phpcs:ignore WordPress.Security.NonceVerification.Recommended
140
- ( $current_screen && '{{PAGE_SLUG}}' === $current_screen->id )
146
+ (
147
+ $current_screen &&
148
+ '{{PAGE_SLUG}}' === $current_screen->id &&
149
+ isset( $GLOBALS['pagenow'] ) &&
150
+ '{{PAGE_SLUG}}.php' === $GLOBALS['pagenow']
151
+ )
141
152
  );
142
153
 
143
154
  if ( ! $is_our_page ) {
@@ -217,6 +228,58 @@ function {{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_wp_admin_enqueue_scripts( $hook_suf
217
228
  );
218
229
  wp_register_style( '{{PAGE_SLUG}}-wp-admin-prerequisites', false, $style_dependencies, $asset['version'] );
219
230
 
231
+ // Critical styles to prevent layout shifts.
232
+ $page_styles = <<<'CSS'
233
+ #wpwrap {
234
+ overflow-y: auto;
235
+ }
236
+ body.js {
237
+ background: #fff;
238
+ }
239
+
240
+ /* Reset wp-admin padding */
241
+ body.js #wpcontent {
242
+ padding-inline-start: 0;
243
+ }
244
+ body.js #wpbody-content {
245
+ padding-bottom: 0;
246
+ }
247
+
248
+ /* Hide legacy admin elements */
249
+ body.js #wpbody-content > div:not(#{{PAGE_SLUG}}-wp-admin-app):not(#screen-meta) {
250
+ display: none;
251
+ }
252
+ body.js #wpfooter {
253
+ display: none;
254
+ }
255
+
256
+ /* Accessibility regions */
257
+ .a11y-speak-region {
258
+ inset-inline-start: -1px;
259
+ top: -1px;
260
+ }
261
+
262
+ /* Admin menu indicators */
263
+ ul#adminmenu a.wp-has-current-submenu::after,
264
+ ul#adminmenu > li.current > a.current::after {
265
+ border-inline-end-color: #fff;
266
+ }
267
+
268
+ /* Media frame fix */
269
+ .media-frame select.attachment-filters:last-of-type {
270
+ width: auto;
271
+ max-width: 100%;
272
+ }
273
+
274
+ /* Responsive overflow fix for #wpwrap */
275
+ @media (min-width: 782px) {
276
+ #wpwrap {
277
+ overflow-y: initial;
278
+ }
279
+ }
280
+ CSS;
281
+ wp_add_inline_style( '{{PAGE_SLUG}}-wp-admin-prerequisites', $page_styles );
282
+
220
283
  // Build dependencies for {{PAGE_SLUG}}-wp-admin module
221
284
  $boot_dependencies = array(
222
285
  array(
@@ -280,61 +343,11 @@ function {{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_wp_admin_enqueue_scripts( $hook_suf
280
343
  */
281
344
  function {{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_wp_admin_render_page() {
282
345
  ?>
283
- <style>
284
- /* Critical styles to prevent layout shifts - inlined for immediate application */
285
-
286
- #wpwrap {
287
- overflow-y: auto;
288
- }
289
- body.js {
290
- background: #fff;
291
- }
292
-
293
- /* Reset wp-admin padding */
294
- body.js #wpcontent {
295
- padding-inline-start: 0;
296
- }
297
- body.js #wpbody-content {
298
- padding-bottom: 0;
299
- }
300
-
301
- /* Hide legacy admin elements */
302
- body.js #wpbody-content > div:not(#{{PAGE_SLUG}}-wp-admin-app):not(#screen-meta) {
303
- display: none;
304
- }
305
- body.js #wpfooter {
306
- display: none;
307
- }
308
-
309
- /* Accessibility regions */
310
- .a11y-speak-region {
311
- inset-inline-start: -1px;
312
- top: -1px;
313
- }
314
-
315
- /* Admin menu indicators */
316
- ul#adminmenu a.wp-has-current-submenu::after,
317
- ul#adminmenu > li.current > a.current::after {
318
- border-inline-end-color: #fff;
319
- }
320
-
321
- /* Media frame fix */
322
- .media-frame select.attachment-filters:last-of-type {
323
- width: auto;
324
- max-width: 100%;
325
- }
326
-
327
- /* Responsive overflow fix for #wpwrap */
328
- @media (min-width: 782px) {
329
- #wpwrap {
330
- overflow-y: initial;
331
- }
332
- }
333
- </style>
334
346
  <div class="wrap hide-if-js">
335
347
  <h1 class="wp-heading-inline"><?php echo esc_html( get_admin_page_title() ); ?></h1>
336
348
  <?php
337
349
  wp_admin_notice(
350
+ // phpcs:ignore WordPress.WP.I18n.MissingArgDomain -- Core string, translated in the default text domain.
338
351
  __( 'This screen requires JavaScript. Enable JavaScript in your browser settings and reload the page.' ),
339
352
  array( 'type' => 'error' )
340
353
  );
@@ -7,6 +7,10 @@
7
7
  * @package {{PREFIX}}
8
8
  */
9
9
 
10
+ if ( ! defined( 'ABSPATH' ) ) {
11
+ exit;
12
+ }
13
+
10
14
  // Global storage for {{PAGE_SLUG}} routes and menu items
11
15
  global ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_routes, ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_menu_items;
12
16
  ${{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_routes = array();
@@ -189,6 +193,23 @@ function {{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_render_page() {
189
193
  );
190
194
  wp_register_style( '{{PAGE_SLUG}}-prerequisites', false, $style_dependencies, $asset['version'] );
191
195
 
196
+ // Base styles for the full-page screen, which renders without the wp-admin stylesheets.
197
+ // `:where()` lets a page that shows the admin bar override the rule hiding it.
198
+ $page_styles = <<<'CSS'
199
+ html {
200
+ background: #f1f1f1;
201
+ color: #444;
202
+ font-family: -apple-system, system-ui, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
203
+ font-size: 13px;
204
+ line-height: 1.4em;
205
+ }
206
+ body {
207
+ margin: 0;
208
+ }
209
+ :where(#wpadminbar) { display: none; }
210
+ CSS;
211
+ wp_add_inline_style( '{{PAGE_SLUG}}-prerequisites', $page_styles );
212
+
192
213
  // Build dependencies for {{PAGE_SLUG}} module
193
214
  $boot_dependencies = array(
194
215
  array(
@@ -252,19 +273,6 @@ function {{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_render_page() {
252
273
  <meta charset="<?php bloginfo( 'charset' ); ?>">
253
274
  <meta name="viewport" content="width=device-width, initial-scale=1">
254
275
  <title><?php echo esc_html( get_admin_page_title() ); ?></title>
255
- <style>
256
- html {
257
- background: #f1f1f1;
258
- color: #444;
259
- font-family: -apple-system, system-ui, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
260
- font-size: 13px;
261
- line-height: 1.4em;
262
- }
263
- body {
264
- margin: 0;
265
- }
266
- #wpadminbar { display: none; }
267
- </style>
268
276
  <?php
269
277
  global $hook_suffix;
270
278
  // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
@@ -285,17 +293,14 @@ function {{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_render_page() {
285
293
  <body class="{{PAGE_SLUG}} no-js">
286
294
  <?php
287
295
  // BEGIN see wp-admin/admin-header.php
288
- ?>
289
- <script type="text/javascript">
290
- document.body.className = document.body.className.replace( 'no-js', 'js' );
291
- </script>
292
- <?php
296
+ wp_print_inline_script_tag( "document.body.className = document.body.className.replace( 'no-js', 'js' );" );
293
297
  // END see wp-admin/admin-header.php
294
298
  ?>
295
299
  <div class="wrap hide-if-js" style="margin: 20px;">
296
300
  <h1 class="wp-heading-inline"><?php echo esc_html( get_admin_page_title() ); ?></h1>
297
301
  <?php
298
302
  wp_admin_notice(
303
+ // phpcs:ignore WordPress.WP.I18n.MissingArgDomain -- Core string, translated in the default text domain.
299
304
  __( 'This screen requires JavaScript. Enable JavaScript in your browser settings and reload the page.' ),
300
305
  array( 'type' => 'error' )
301
306
  );
@@ -343,7 +348,8 @@ function {{PREFIX}}_{{PAGE_SLUG_UNDERSCORE}}_intercept_render() {
343
348
 
344
349
  if ( ! current_user_can( '{{CAPABILITY}}' ) ) {
345
350
  wp_die(
346
- __( 'Sorry, you are not allowed to access this page.' ),
351
+ // phpcs:ignore WordPress.WP.I18n.MissingArgDomain -- Core string, translated in the default text domain.
352
+ esc_html__( 'Sorry, you are not allowed to access this page.' ),
347
353
  403
348
354
  );
349
355
  }
@@ -6,4 +6,8 @@
6
6
  * @package {{PREFIX}}
7
7
  */
8
8
 
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
9
13
  {{PAGE_INCLUDES}}
@@ -6,6 +6,10 @@
6
6
  * @package {{PREFIX}}
7
7
  */
8
8
 
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
9
13
  return array(
10
14
  {{ROUTES}}
11
15
  );
@@ -7,6 +7,10 @@
7
7
  * @package {{PREFIX}}
8
8
  */
9
9
 
10
+ if ( ! defined( 'ABSPATH' ) ) {
11
+ exit;
12
+ }
13
+
10
14
  // Load routes registry
11
15
  $routes_file = __DIR__ . '/routes/registry.php';
12
16
  if ( ! file_exists( $routes_file ) ) {
@@ -6,6 +6,10 @@
6
6
  * @package {{PREFIX}}
7
7
  */
8
8
 
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
9
13
  /**
10
14
  * Registers a script according to `wp_register_script`. Honors this request by
11
15
  * reassigning internal dependency properties of any script handle already
@@ -6,6 +6,10 @@
6
6
  * @package {{PREFIX}}
7
7
  */
8
8
 
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
9
13
  return array(
10
14
  {{SCRIPTS}}
11
15
  );
@@ -9,6 +9,10 @@
9
9
  * @package {{PREFIX}}
10
10
  */
11
11
 
12
+ if ( ! defined( 'ABSPATH' ) ) {
13
+ exit;
14
+ }
15
+
12
16
  /**
13
17
  * Registers a style according to `wp_register_style`. Honors this request by
14
18
  * deregistering any style by the same handler before registration.
@@ -6,6 +6,10 @@
6
6
  * @package {{PREFIX}}
7
7
  */
8
8
 
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
9
13
  return array(
10
14
  {{STYLES}}
11
15
  );
@@ -7,6 +7,10 @@
7
7
  * @package {{PREFIX}}
8
8
  */
9
9
 
10
+ if ( ! defined( 'ABSPATH' ) ) {
11
+ exit;
12
+ }
13
+
10
14
  /**
11
15
  * Register widget script modules.
12
16
  *
@@ -6,6 +6,10 @@
6
6
  * @package {{PREFIX}}
7
7
  */
8
8
 
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
9
13
  return array(
10
14
  {{WIDGETS}}
11
15
  );