-
Notifications
You must be signed in to change notification settings - Fork 860
Forms: exclude unminified build artifacts from production distribution #47044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kraftbj
wants to merge
6
commits into
trunk
Choose a base branch
from
update/forms-assets
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d6f9540
Exclude unminified build artifacts from production distribution
kraftbj 99cab46
Fix bare SCRIPT_DEBUG usage and remove stray remove_action
kraftbj 5cc13ec
Address PR review: CSS gitattributes, ABSPATH guard, version caching,…
kraftbj fb99211
Replace meaningless test assertions with has_action() checks
kraftbj 8ce71c8
Merge remote-tracking branch 'origin/trunk' into update/forms-assets
kraftbj 5f64132
Merge remote-tracking branch 'origin/trunk' into update/forms-assets
kraftbj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: changed | ||
|
|
||
| Exclude unminified build artifacts from production distribution to reduce plugin size. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
276 changes: 276 additions & 0 deletions
276
projects/packages/forms/src/dashboard/wp-build-script-debug-fallback.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,276 @@ | ||
| <?php | ||
| /** | ||
| * SCRIPT_DEBUG fallback for wp-build generated functions. | ||
| * | ||
| * Production distributions exclude unminified JS files to reduce plugin size. | ||
kraftbj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * The auto-generated code in build/ uses SCRIPT_DEBUG to pick .js vs .min.js | ||
| * without checking whether the unminified file exists, which would cause 404s | ||
| * in production when SCRIPT_DEBUG is enabled. | ||
| * | ||
| * This file predefines those functions with a file_exists fallback. Because the | ||
| * generated code wraps each function in function_exists(), our definitions take | ||
| * precedence when this file is loaded before build.php. | ||
| * | ||
| * @package automattic/jetpack-forms | ||
| */ | ||
|
|
||
| if ( ! function_exists( 'jetpack_forms_override_script' ) ) { | ||
| /** | ||
| * Registers a script according to `wp_register_script`, honoring any existing | ||
| * registration by reassigning its internal properties rather than deregistering. | ||
| * | ||
| * @param WP_Scripts $scripts WP_Scripts instance. | ||
| * @param string $handle Script handle. | ||
| * @param string $src Full URL or root-relative path. | ||
| * @param array $deps Dependencies. | ||
| * @param string|bool|null $ver Version string. | ||
| * @param bool $in_footer Whether to enqueue in footer. | ||
| */ | ||
| function jetpack_forms_override_script( $scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) { | ||
| $script = $scripts->query( $handle, 'registered' ); | ||
| if ( $script ) { | ||
| $script->src = $src; | ||
| $script->deps = $deps; | ||
| $script->ver = $ver; | ||
| $script->args = $in_footer ? 1 : null; | ||
| } else { | ||
| $scripts->add( $handle, $src, $deps, $ver, ( $in_footer ? 1 : null ) ); | ||
| } | ||
|
|
||
| if ( in_array( 'wp-i18n', $deps, true ) ) { | ||
| $scripts->set_translations( $handle ); | ||
kraftbj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| if ( ! function_exists( 'jetpack_forms_register_package_scripts' ) ) { | ||
| /** | ||
| * Register all package scripts with SCRIPT_DEBUG file_exists fallback. | ||
| * | ||
| * @param WP_Scripts $scripts WP_Scripts instance. | ||
| */ | ||
| function jetpack_forms_register_package_scripts( $scripts ) { | ||
| $build_dir = dirname( __DIR__, 2 ) . '/build'; | ||
| $build_constants = require $build_dir . '/constants.php'; | ||
| $default_version = ! SCRIPT_DEBUG ? $build_constants['version'] : time(); | ||
kraftbj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| $scripts_dir = $build_dir . '/scripts'; | ||
| $scripts_file = $scripts_dir . '/registry.php'; | ||
|
|
||
| if ( ! file_exists( $scripts_file ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $scripts_data = require $scripts_file; | ||
|
|
||
| $extension = '.min.js'; | ||
| if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { | ||
| if ( ! empty( $scripts_data[0]['path'] ) ) { | ||
| $unminified_path = $scripts_dir . '/' . $scripts_data[0]['path'] . '.js'; | ||
| if ( file_exists( $unminified_path ) ) { | ||
| $extension = '.js'; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| foreach ( $scripts_data as $script_data ) { | ||
| $asset_file = $scripts_dir . '/' . $script_data['asset']; | ||
| $asset = file_exists( $asset_file ) ? require $asset_file : array(); | ||
| $dependencies = $asset['dependencies'] ?? array(); | ||
| $version = $asset['version'] ?? $default_version; | ||
|
|
||
kraftbj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| jetpack_forms_override_script( | ||
| $scripts, | ||
| $script_data['handle'], | ||
| $build_constants['build_url'] . 'scripts/' . $script_data['path'] . $extension, | ||
| $dependencies, | ||
| $version, | ||
| true | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| add_action( 'wp_default_scripts', 'jetpack_forms_register_package_scripts' ); | ||
| } | ||
|
|
||
| if ( ! function_exists( 'jetpack_forms_register_script_modules' ) ) { | ||
| /** | ||
| * Register all script modules with SCRIPT_DEBUG file_exists fallback. | ||
| */ | ||
| function jetpack_forms_register_script_modules() { | ||
| $build_dir = dirname( __DIR__, 2 ) . '/build'; | ||
| $build_constants = require $build_dir . '/constants.php'; | ||
| $modules_dir = $build_dir . '/modules'; | ||
| $modules_file = $modules_dir . '/registry.php'; | ||
|
|
||
| if ( ! file_exists( $modules_file ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $modules = require $modules_file; | ||
| $base_url = $build_constants['build_url'] . 'modules/'; | ||
| $extension = '.min.js'; | ||
| if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { | ||
| if ( ! empty( $modules[0]['path'] ) ) { | ||
| $unminified_path = $modules_dir . '/' . $modules[0]['path'] . '.js'; | ||
| if ( file_exists( $unminified_path ) ) { | ||
| $extension = '.js'; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| foreach ( $modules as $module ) { | ||
kraftbj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $asset_path = $modules_dir . '/' . $module['asset']; | ||
| $asset = file_exists( $asset_path ) ? require $asset_path : array(); | ||
|
|
||
| wp_register_script_module( | ||
|
Check failure on line 126 in projects/packages/forms/src/dashboard/wp-build-script-debug-fallback.php
|
||
| $module['id'], | ||
| $base_url . $module['path'] . $extension, | ||
| $asset['module_dependencies'] ?? array(), | ||
| $asset['version'] ?? false, | ||
| array( | ||
| 'fetchpriority' => 'low', | ||
| 'in_footer' => true, | ||
| ) | ||
| ); | ||
kraftbj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| add_action( 'wp_default_scripts', 'jetpack_forms_register_script_modules' ); | ||
| remove_action( 'wp_default_scripts', 'wp_default_script_modules' ); | ||
| } | ||
kraftbj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if ( ! function_exists( 'jetpack_forms_register_page_routes' ) ) { | ||
| /** | ||
| * Register routes for a page with SCRIPT_DEBUG file_exists fallback. | ||
| * | ||
| * @param array $page_routes Array of route data for the page. | ||
| * @param string $register_function_name Name of the function to call for registering each route. | ||
| */ | ||
| function jetpack_forms_register_page_routes( $page_routes, $register_function_name ) { | ||
| $build_dir = dirname( __DIR__, 2 ) . '/build'; | ||
| $build_constants = require $build_dir . '/constants.php'; | ||
|
|
||
| foreach ( $page_routes as $route ) { | ||
| $content_handle = null; | ||
| $route_handle = null; | ||
|
|
||
| // Register content module if exists. | ||
| if ( $route['has_content'] ) { | ||
| $content_asset_path = $build_dir . "/routes/{$route['name']}/content.min.asset.php"; | ||
| if ( file_exists( $content_asset_path ) ) { | ||
| $content_asset = require $content_asset_path; | ||
| $content_handle = 'jetpack-forms/routes/' . $route['name'] . '/content'; | ||
| $extension = '.min.js'; | ||
| if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { | ||
| $content_unminified = $build_dir . '/routes/' . $route['name'] . '/content.js'; | ||
| if ( file_exists( $content_unminified ) ) { | ||
| $extension = '.js'; | ||
| } | ||
| } | ||
| wp_register_script_module( | ||
| $content_handle, | ||
| $build_constants['build_url'] . 'routes/' . $route['name'] . '/content' . $extension, | ||
| $content_asset['module_dependencies'] ?? array(), | ||
| $content_asset['version'] ?? false | ||
| ); | ||
kraftbj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| // Register route module if exists. | ||
| if ( $route['has_route'] ) { | ||
| $route_asset_path = $build_dir . "/routes/{$route['name']}/route.min.asset.php"; | ||
| if ( file_exists( $route_asset_path ) ) { | ||
| $route_asset = require $route_asset_path; | ||
| $route_handle = 'jetpack-forms/routes/' . $route['name'] . '/route'; | ||
| $extension = '.min.js'; | ||
| if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { | ||
| $route_unminified = $build_dir . '/routes/' . $route['name'] . '/route.js'; | ||
| if ( file_exists( $route_unminified ) ) { | ||
| $extension = '.js'; | ||
| } | ||
| } | ||
| wp_register_script_module( | ||
| $route_handle, | ||
| $build_constants['build_url'] . 'routes/' . $route['name'] . '/route' . $extension, | ||
| $route_asset['module_dependencies'] ?? array(), | ||
| $route_asset['version'] ?? false | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Register route with page. | ||
| if ( function_exists( $register_function_name ) ) { | ||
| call_user_func( $register_function_name, $route['path'], $content_handle, $route_handle ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ( ! function_exists( 'jetpack_forms_override_style' ) ) { | ||
| /** | ||
| * Registers a style, deregistering any existing style by the same handle first. | ||
| * | ||
| * @param WP_Styles $styles WP_Styles instance. | ||
| * @param string $handle Stylesheet handle. | ||
| * @param string $src Full URL or root-relative path. | ||
| * @param array $deps Dependencies. | ||
| * @param string|bool|null $ver Version string. | ||
| * @param string $media Media type. | ||
| */ | ||
| function jetpack_forms_override_style( $styles, $handle, $src, $deps = array(), $ver = false, $media = 'all' ) { | ||
| $style = $styles->query( $handle, 'registered' ); | ||
| if ( $style ) { | ||
| $styles->remove( $handle ); | ||
| } | ||
| $styles->add( $handle, $src, $deps, $ver, $media ); | ||
| } | ||
| } | ||
|
|
||
| if ( ! function_exists( 'jetpack_forms_register_package_styles' ) ) { | ||
| /** | ||
| * Register all package styles with SCRIPT_DEBUG file_exists fallback. | ||
| * | ||
| * @param WP_Styles $styles WP_Styles instance. | ||
| */ | ||
| function jetpack_forms_register_package_styles( $styles ) { | ||
| $build_dir = dirname( __DIR__, 2 ) . '/build'; | ||
| $build_constants = require $build_dir . '/constants.php'; | ||
| $default_version = ! SCRIPT_DEBUG ? $build_constants['version'] : time(); | ||
kraftbj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
kraftbj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $styles_dir = $build_dir . '/styles'; | ||
| $styles_file = $styles_dir . '/registry.php'; | ||
|
|
||
| if ( ! file_exists( $styles_file ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $styles_data = require $styles_file; | ||
|
|
||
| $suffix = '.min'; | ||
| if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { | ||
| if ( ! empty( $styles_data[0]['path'] ) ) { | ||
| $unminified_path = $styles_dir . '/' . $styles_data[0]['path'] . '.css'; | ||
| if ( file_exists( $unminified_path ) ) { | ||
| $suffix = ''; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| foreach ( $styles_data as $style_data ) { | ||
| jetpack_forms_override_style( | ||
| $styles, | ||
| $style_data['handle'], | ||
| $build_constants['build_url'] . 'styles/' . $style_data['path'] . $suffix . '.css', | ||
| $style_data['dependencies'], | ||
| $default_version | ||
kraftbj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| // Enable RTL support. | ||
| $styles->add_data( $style_data['handle'], 'rtl', 'replace' ); | ||
| $styles->add_data( $style_data['handle'], 'suffix', $suffix ); | ||
| } | ||
| } | ||
|
|
||
| add_action( 'wp_default_styles', 'jetpack_forms_register_package_styles' ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.