Skip to content

Commit 5fadb73

Browse files
committed
Build/Test Tools: Ensure all built files are deleted as expected.
Block editor-related files can currently become stale or are not always deleted from `src` through the relevant `grunt clean` commands reliably. In the past, this primarily caused issues locally when a CSS file was copied from the `@wordpress/block-library` npm package into `src` and later removed from the package entirely. The result was a failing `grunt verify:old-files` task until the `grunt clean` command was run with the `--dev` flag. After [61438] this issue presented in new ways. Mainly, files would remain in the core.svn.wordpress.org build repository indefinitely unless explicitly deleted. [62051] brought the `grunt clean` tasks up to date, but there are still paths where files remain unexpectedly or have outdated contents after rebuilding. This can cause incomplete or inaccurate commits where built files subject to version control are not updated correctly, especially when changing the `gutenberg.sha` value in `package.json`. This change improves the build script to ensure that all files sourced from the zip file with assets built by the Gutenberg repository are always fresh and up to date, and any files that are deleted from the built zip file are also deleted from version control appropriately (in both the `develop` and `core` repositories). A handful of changes were required to accomplish this: - All Gutenberg-sourced outputs are written to `src/` regardless of `--dev`. In production builds, `build:gutenberg` runs before `build:files`, and `copy:files` propagates the tree to `build/`. - `gutenbergFiles` has been split into two different arrays: `gutenbergUnversionedFiles` and `gutenbergVersionedFiles`. The `src` argument for the `clean:gutenberg` task is dynamically populated at run time with a bare `grunt clean` cleaning only the unversioned subset (so version-controlled files are not unexpectedly deleted), and explicit `clean:gutenberg` (or any chain through `build:gutenberg`) cleans both, removing files deleted upstream from version control. - `clean:gutenberg` no longer wipes non-Gutenberg sourced files from `wp-includes/js/`. All file/path lists have been updated to only match files the related tasks are directly responsible for managing. - `tools/gutenberg/copy.js` has been added to `tsconfig.json` and brought under `tsc --build` strict-mode checking. The large `copyBlockAssets()` function was broken into one named function per asset type, each typed against the relevant `COPY_CONFIG` slice. The split is a code-clarity improvement, not a bug fix. This recommits [62525] to `trunk`. Props desrosj, westonruter, jorbin, adamsilverstein. Fixes #65452. git-svn-id: https://develop.svn.wordpress.org/trunk@62645 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0970efa commit 5fadb73

5 files changed

Lines changed: 371 additions & 222 deletions

File tree

‎Gruntfile.js‎

Lines changed: 90 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,43 @@ module.exports = function(grunt) {
4141
'wp-admin/css/colors/**/*.css',
4242
],
4343

44-
// Built js files, in /src or /build.
44+
// Built JavaScript files that do not belong to a more specific group.
4545
jsFiles = [
4646
'wp-admin/js/',
47-
'wp-includes/js/',
47+
'wp-includes/js/*',
48+
/*
49+
* This directory has shared responsibility and is managed through
50+
* gutenbergUnversionedFiles, webpackFiles, and copy:vendor-js.
51+
*/
52+
'!wp-includes/js/dist',
53+
'wp-includes/js/dist/vendor/*.js',
54+
// Managed by the Gutenberg-related tasks.
55+
'!wp-includes/js/dist/vendor/react-jsx-runtime*',
56+
],
57+
58+
// Files sourced from the Gutenberg repository built asset that are ignored by version control.
59+
gutenbergUnversionedFiles = [
60+
SOURCE_DIR + 'wp-includes/blocks/*/*.css',
61+
SOURCE_DIR + 'wp-includes/css/dist',
62+
SOURCE_DIR + 'wp-includes/js/dist/*.js',
63+
SOURCE_DIR + 'wp-includes/js/dist/script-modules',
64+
SOURCE_DIR + 'wp-includes/js/dist/vendor/react-jsx-runtime*',
4865
],
4966

50-
// All files copied from the Gutenberg repository excluded from version control.
51-
gutenbergFiles = [
52-
'wp-includes/js/dist',
53-
'wp-includes/css/dist',
54-
// Old location kept temporarily to ensure they are cleaned up.
55-
'wp-includes/icons',
67+
// Files sourced from the Gutenberg repository built asset that are managed through version control.
68+
gutenbergVersionedFiles = [
69+
// Block assets (block.json, top-level PHP, nested PHP helpers).
70+
SOURCE_DIR + 'wp-includes/blocks/*',
71+
'!' + SOURCE_DIR + 'wp-includes/blocks/index.php',
72+
SOURCE_DIR + 'wp-includes/images/icon-library',
73+
SOURCE_DIR + 'wp-includes/theme.json',
74+
SOURCE_DIR + 'wp-includes/theme-i18n.json',
75+
// Routes and pages.
76+
SOURCE_DIR + 'wp-includes/build',
77+
// PHP manifests generated by gutenberg:copy.
78+
SOURCE_DIR + 'wp-includes/assets/icon-library-manifest.php',
79+
SOURCE_DIR + 'wp-includes/assets/script-loader-packages.php',
80+
SOURCE_DIR + 'wp-includes/assets/script-modules-packages.php',
5681
],
5782

5883
// All files built by Webpack, in /src or /build.
@@ -241,10 +266,32 @@ module.exports = function(grunt) {
241266
return setFilePath( WORKING_DIR, file );
242267
} ),
243268

244-
// Clean files built by the tools/gutenberg scripts.
245-
gutenberg: gutenbergFiles.map( function( file ) {
246-
return setFilePath( WORKING_DIR, file );
247-
}),
269+
/*
270+
* Clean files sourced from the downloaded zip file built by the Gutenberg repository.
271+
*
272+
* All files originating from the Gutenberg repository's built assets (both tracked and untracked by version
273+
* control) are deleted when `clean:gutenberg` is explicitly called. This ensures that versioned files that
274+
* have been deleted upstream are also removed from version control in this repository.
275+
*
276+
* When `clean:gutenberg` is not explicitly called and run through `grunt clean`, only ignored files are
277+
* cleaned.
278+
*/
279+
gutenberg: {
280+
get src() {
281+
const cli = grunt.cli.tasks;
282+
// Preserve versioned files only when running bare `grunt clean`.
283+
const isBareCleanSweep =
284+
cli.includes( 'clean' ) &&
285+
! cli.includes( 'clean:gutenberg' );
286+
287+
if ( isBareCleanSweep ) {
288+
return gutenbergUnversionedFiles;
289+
} else {
290+
return gutenbergUnversionedFiles.concat( gutenbergVersionedFiles );
291+
}
292+
},
293+
},
294+
248295
dynamic: {
249296
dot: true,
250297
expand: true,
@@ -289,7 +336,6 @@ module.exports = function(grunt) {
289336
expand: true,
290337
cwd: SOURCE_DIR,
291338
src: buildFiles.concat( [
292-
'!wp-includes/assets/**', // Assets is extracted into separate copy tasks.
293339
'!js/**', // JavaScript is extracted into separate copy tasks.
294340
'!.{svn,git}', // Exclude version control folders.
295341
'!wp-includes/version.php', // Exclude version.php.
@@ -666,24 +712,18 @@ module.exports = function(grunt) {
666712
'constants.php',
667713
'pages/**/*.php',
668714
],
669-
dest: WORKING_DIR + 'wp-includes/build/',
715+
dest: SOURCE_DIR + 'wp-includes/build/',
670716
} ],
671717
},
672718
/*
673-
* Only copy files relevant to the routes specified in the registry file.
674-
*
675-
* While the registry file does not contain any experimental routes, the `gutenberg/build/routes` directory
676-
* includes the files for all registered routes. Only the files related to the routes specified in the
677-
* registry should be included in the WordPress build.
678-
*
679-
* The `src` list is populated at task runtime by `routes:setup`, which reads the registry after
680-
* `gutenberg:download` has run. See the `routes:setup` task registration for implementation details.
719+
* The list of route source files is populated from the contents of the registry.php file at task runtime by
720+
* `routes:setup`.
681721
*/
682722
routes: {
683723
expand: true,
684724
cwd: 'gutenberg/build',
685725
src: [],
686-
dest: WORKING_DIR + 'wp-includes/build/',
726+
dest: SOURCE_DIR + 'wp-includes/build/',
687727
},
688728
'gutenberg-js': {
689729
files: [ {
@@ -692,7 +732,7 @@ module.exports = function(grunt) {
692732
src: [
693733
'pages/**/*.js',
694734
],
695-
dest: WORKING_DIR + 'wp-includes/build/',
735+
dest: SOURCE_DIR + 'wp-includes/build/',
696736
} ],
697737
},
698738
'gutenberg-modules': {
@@ -706,7 +746,7 @@ module.exports = function(grunt) {
706746
// with no debugging value over the minified versions.
707747
'!vips/!(*.min).js',
708748
],
709-
dest: WORKING_DIR + 'wp-includes/js/dist/script-modules/',
749+
dest: SOURCE_DIR + 'wp-includes/js/dist/script-modules/',
710750
} ],
711751
},
712752
'gutenberg-styles': {
@@ -719,7 +759,7 @@ module.exports = function(grunt) {
719759
// Per-block CSS is copied to wp-includes/blocks/ by tools/gutenberg/copy.js.
720760
'!block-library/*/**',
721761
],
722-
dest: WORKING_DIR + 'wp-includes/css/dist/',
762+
dest: SOURCE_DIR + 'wp-includes/css/dist/',
723763
} ],
724764
},
725765
'gutenberg-theme-json': {
@@ -738,20 +778,20 @@ module.exports = function(grunt) {
738778
files: [
739779
{
740780
src: 'gutenberg/lib/theme.json',
741-
dest: WORKING_DIR + 'wp-includes/theme.json',
781+
dest: SOURCE_DIR + 'wp-includes/theme.json',
742782
},
743783
{
744784
src: 'gutenberg/lib/theme-i18n.json',
745-
dest: WORKING_DIR + 'wp-includes/theme-i18n.json',
785+
dest: SOURCE_DIR + 'wp-includes/theme-i18n.json',
746786
},
747787
],
748788
},
749789
'icon-library-images': {
750790
files: [ {
751791
expand: true,
752792
cwd: 'gutenberg/packages/icons/src/library',
753-
src: '*.svg',
754-
dest: WORKING_DIR + 'wp-includes/images/icon-library',
793+
src: [ '*.svg' ],
794+
dest: SOURCE_DIR + 'wp-includes/images/icon-library',
755795
} ],
756796
},
757797
'icon-library-manifest': {
@@ -773,7 +813,7 @@ module.exports = function(grunt) {
773813
},
774814
files: [ {
775815
src: 'gutenberg/packages/icons/src/manifest.php',
776-
dest: WORKING_DIR + 'wp-includes/assets/icon-library-manifest.php',
816+
dest: SOURCE_DIR + 'wp-includes/assets/icon-library-manifest.php',
777817
} ],
778818
},
779819
},
@@ -1667,7 +1707,7 @@ module.exports = function(grunt) {
16671707
*/
16681708
grunt.util.spawn( {
16691709
grunt: true,
1670-
args: [ 'build:gutenberg', '--dev' ],
1710+
args: [ 'build:gutenberg' ],
16711711
opts: { stdio: 'inherit' }
16721712
}, function( buildError ) {
16731713
done( ! buildError );
@@ -1677,10 +1717,9 @@ module.exports = function(grunt) {
16771717

16781718
grunt.registerTask( 'gutenberg:copy', 'Copies Gutenberg JS packages and block assets to WordPress Core.', function() {
16791719
const done = this.async();
1680-
const buildDir = grunt.option( 'dev' ) ? 'src' : 'build';
16811720
grunt.util.spawn( {
16821721
cmd: 'node',
1683-
args: [ 'tools/gutenberg/copy.js', `--build-dir=${ buildDir }` ],
1722+
args: [ 'tools/gutenberg/copy.js' ],
16841723
opts: { stdio: 'inherit' }
16851724
}, function( error ) {
16861725
done( ! error );
@@ -2164,10 +2203,23 @@ module.exports = function(grunt) {
21642203
} );
21652204
} );
21662205

2167-
grunt.registerTask( 'build:gutenberg', [
2168-
'copy:gutenberg-php',
2206+
// Detects and copies stable routes.
2207+
grunt.registerTask( 'build:routes', [
21692208
'routes:setup',
21702209
'copy:routes',
2210+
] );
2211+
2212+
/*
2213+
* Refresh the Gutenberg-sourced content in src/.
2214+
*
2215+
* clean:gutenberg must run first to ensure files removed upstream are purged.
2216+
*
2217+
* Because all of these tasks write to src/, the outcome is identical for build and build:dev.
2218+
*/
2219+
grunt.registerTask( 'build:gutenberg', [
2220+
'clean:gutenberg',
2221+
'copy:gutenberg-php',
2222+
'build:routes',
21712223
'copy:gutenberg-js',
21722224
'gutenberg:copy',
21732225
'copy:gutenberg-modules',
@@ -2181,21 +2233,21 @@ module.exports = function(grunt) {
21812233
if ( grunt.option( 'dev' ) ) {
21822234
grunt.task.run( [
21832235
'gutenberg:verify',
2236+
'build:gutenberg',
21842237
'build:js',
21852238
'build:css',
21862239
'build:codemirror',
2187-
'build:gutenberg',
21882240
'build:certificates'
21892241
] );
21902242
} else {
21912243
grunt.task.run( [
21922244
'gutenberg:verify',
2245+
'build:gutenberg',
21932246
'build:certificates',
21942247
'build:files',
21952248
'build:js',
21962249
'build:css',
21972250
'build:codemirror',
2198-
'build:gutenberg',
21992251
'replace:source-maps',
22002252
'verify:build'
22012253
] );

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,6 @@
141141
"typecheck:php": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan",
142142
"gutenberg:copy": "node tools/gutenberg/copy.js",
143143
"gutenberg:verify": "node tools/gutenberg/utils.js",
144-
"gutenberg:download": "node tools/gutenberg/download.js && grunt build:gutenberg --dev"
144+
"gutenberg:download": "node tools/gutenberg/download.js && grunt build:gutenberg"
145145
}
146146
}

0 commit comments

Comments
 (0)