Forms: exclude unminified build artifacts from production distribution#47044
Forms: exclude unminified build artifacts from production distribution#47044
Conversation
Replace the blanket `build/**` production-include in .gitattributes with explicit includes for *.php, *.min.js, and loader.js. This excludes unminified JS (~2.7 MB) and source maps from the production distribution. Add a SCRIPT_DEBUG fallback file that predefines the wp-build generated registration functions with file_exists checks, so SCRIPT_DEBUG=true gracefully falls back to .min.js when unminified files are absent. Also exclude /routes/** (source TSX/TS/SCSS compiled into build/).
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
There was a problem hiding this comment.
Pull request overview
This PR reduces the Forms package’s production footprint by tightening which build/ artifacts are included in production distributions, while adding a PHP fallback so SCRIPT_DEBUG=true doesn’t break when unminified assets are intentionally omitted.
Changes:
- Narrow
projects/packages/forms/.gitattributesproduction includes forbuild/to*.php,*.min.js, andloader.js, and exclude/routes/**sources from production. - Add a
wp-buildSCRIPT_DEBUG fallback file that prefers unminified assets only when they actually exist. - Load the fallback file before
build/build.phpwhen the wp-build admin page is visited.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| projects/packages/forms/src/dashboard/wp-build-script-debug-fallback.php | Adds SCRIPT_DEBUG-safe registration helpers that fall back to .min.js/.min.css when unminified files are missing. |
| projects/packages/forms/src/dashboard/class-dashboard.php | Loads the fallback file before wp-build’s generated build.php on the wp-build admin page. |
| projects/packages/forms/changelog/update-forms-assets | Documents the production distribution change. |
| projects/packages/forms/.gitattributes | Restricts which build artifacts are shipped to production and excludes /routes/** sources. |
projects/packages/forms/src/dashboard/wp-build-script-debug-fallback.php
Outdated
Show resolved
Hide resolved
projects/packages/forms/src/dashboard/wp-build-script-debug-fallback.php
Outdated
Show resolved
Hide resolved
projects/packages/forms/src/dashboard/wp-build-script-debug-fallback.php
Show resolved
Hide resolved
Code Coverage SummaryCoverage changed in 1 file.
1 file is newly checked for coverage.
Full summary · PHP report · JS report Coverage check overridden by
I don't care about code coverage for this PR
|
- Use defined( 'SCRIPT_DEBUG' ) guard for $default_version in scripts and styles registration to avoid PHP 8+ fatal on undefined constant. - Remove remove_action( 'wp_default_scripts', 'wp_default_script_modules' ) which was incorrectly copied from the template — our fallback should not prevent core from registering its default script modules.
projects/packages/forms/src/dashboard/wp-build-script-debug-fallback.php
Show resolved
Hide resolved
projects/packages/forms/src/dashboard/wp-build-script-debug-fallback.php
Show resolved
Hide resolved
… WP 6.8 compat - Add /build/**/*.min.css and /build/**/*.rtl.css to .gitattributes production-include so CSS build artifacts ship in production. - Add ABSPATH guard to wp-build-script-debug-fallback.php. - Move $default_version after extension/suffix detection so minified fallback files use the stable build version instead of time(). - Remove 5th arg from wp_register_script_module() (added in WP 6.9, but Jetpack supports 6.8). - Add unit tests for Dashboard::load_wp_build().
projects/packages/forms/src/dashboard/wp-build-script-debug-fallback.php
Show resolved
Hide resolved
projects/packages/forms/src/dashboard/wp-build-script-debug-fallback.php
Show resolved
Hide resolved
The load_wp_build tests were using assertions that could never fail:
function_exists() for a function that never exists, and assertTrue(true).
Replace with has_action('wp_default_scripts', ...) to verify the actual
observable side effect of loading the fallback file. Skip the missing-build
test locally when build/ exists to avoid false positives.
Why is the production build building unminified JS in the first place?
For i18n purposes1 and client-side debugging, someday it would probably be nice to start distributing source maps for all our JS. The down side, though, is that (last I checked, p1726069454468079-slack-C05Q5HSS013) it would increase the on-disk size of Jetpack by something like 30M, which seems likely to draw more "bloat" complaints. Footnotes
|
That I don't know, tbh. |
|
Tested locally and did not notice any regression. Nice to see the Gutenberg PR as well! |
projects/packages/forms/src/dashboard/wp-build-script-debug-fallback.php
Show resolved
Hide resolved
|
@simison - would you be fine with shipping this for Jetpack while the larger |
See FORMS-582 (partial)
Proposed changes:
build/** production-includein the forms package.gitattributeswith explicit includes for*.php,*.min.js, andloader.js. This excludes unminified JS (~2.7 MB, ~61K lines) and source maps from the production distribution.wp-build-script-debug-fallback.php) that predefines the wp-build auto-generated registration functions withfile_existschecks, soSCRIPT_DEBUG=truegracefully falls back to.min.jswhen unminified files are absent./routes/**source files (TSX/TS/SCSS compiled intobuild/) from production.How it works
The auto-generated PHP in
build/picks script extensions via:Without unminified files,
SCRIPT_DEBUG=truewould 404. The generated files all usefunction_exists()guards, so loading the fallback file beforebuild.phppredefines the functions with a safe version that checksfile_exists()before choosing the unminified path.Other information:
Jetpack product discussion
Does this pull request change what data or activity we track or use?
No.
Testing instructions:
jp build projects/packages/forms --depsgit check-attr production-include -- build/scripts/theme/index.js build/modules/boot/index.js build/routes/forms/content.js # All should show "production-include: unspecified"jetpack_forms_alphafilter enabled):SCRIPT_DEBUG=true+ unminified files present (dev): loads.jsSCRIPT_DEBUG=true+ unminified files absent (production): loads.min.jsSCRIPT_DEBUG=false: always loads.min.js