-
Notifications
You must be signed in to change notification settings - Fork 800
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
Add Javascript and TypeScript build for jetpack-wpcom-mu #34158
Merged
Merged
Changes from 36 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
12335c0
Jetpack Mu WPcom: Migrate Error Handling
obenland 9159934
Update script paths
obenland 5900f65
Add initial JS build code for jetpack-mu-wpcom
daledupreez 2f9d0c3
changelog
daledupreez 56441c4
Loosen dependencies
daledupreez 102379d
Add build/ to .gitignore
daledupreez de56417
Add clean script
daledupreez 5135060
Add dependencies for error-reporting
daledupreez 6f90e42
Update versions
daledupreez e1a1b30
Changelog
daledupreez 1934951
Add build scripts
daledupreez aa4ff50
Try using PKG_DIR
obenland e9498ed
Add build/ directory to production output
daledupreez 9c210ab
Import namespaced class
obenland 55f4420
Merge branch 'add/js-build-for-jetpack-wpcom-mu' of https://github.co…
obenland bcf0831
Bail early if ETK has enqueued its script
obenland d606b39
Switch to more direct webpack build
daledupreez 2edd95c
Remove @wordpress/scripts and dependencies
daledupreez 284fd65
Ignore the .cache directory
daledupreez c0aa7a9
Update to using base dir and file to enqueue scripts
obenland f9e2410
Merge branch 'add/js-build-for-jetpack-wpcom-mu' of https://github.co…
obenland df9cb53
Remove JS/TS file exclusion and update build dir path
daledupreez c9ad3da
Ensure we set up production builds correctly
daledupreez 232e5a9
Merge remote-tracking branch 'origin/trunk' into add/js-build-for-jet…
daledupreez 90b72ea
Add docs for new build commands
obenland ec003ec
Better reflect folder structure
obenland 13c5e76
Reset change pnpm-lock.yaml
daledupreez 6d861f4
Update pnpm lock file
daledupreez f968f67
Update mu-wpcom-plugin
daledupreez 9e0791f
Launchpad: Add `new_prompt` query param to Write first post task (#34…
sixhours 2c0a191
Subscribe modal: remove filters and enable for Jetpack sites (#33909)
simison 9ee445e
Merge branch 'trunk' into add/js-build-for-jetpack-wpcom-mu
daledupreez 882158e
Add trailing slash
obenland 348f3c4
Merge branch 'add/js-build-for-jetpack-wpcom-mu' of https://github.co…
obenland 1696a90
Update build docs
daledupreez 519deda
Merge branch 'trunk' into add/js-build-for-jetpack-wpcom-mu
okmttdhr 7dbc2d0
Merge branch 'trunk' into add/js-build-for-jetpack-wpcom-mu
okmttdhr 5ae7897
Update pnpm-lock.yaml
okmttdhr 5e1e8c8
Update composer.lock
okmttdhr 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.cache/ | ||
src/build/ | ||
vendor/ | ||
node_modules/ | ||
wordpress/ |
This file contains 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
4 changes: 4 additions & 0 deletions
4
projects/packages/jetpack-mu-wpcom/changelog/add-js-build-for-jetpack-wpcom-mu
This file contains 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: minor | ||
Type: added | ||
|
||
Add initial JS and TS build logic |
4 changes: 4 additions & 0 deletions
4
projects/packages/jetpack-mu-wpcom/changelog/migrate-etk-error-handling
This file contains 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: minor | ||
Type: added | ||
|
||
Added Editor error handling from ETK |
This file contains 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 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 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
153 changes: 153 additions & 0 deletions
153
projects/packages/jetpack-mu-wpcom/src/features/error-reporting/error-reporting.php
This file contains 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,153 @@ | ||
<?php | ||
/** | ||
* Error reporting from wp-admin / Gutenberg context for Simple Sites and WoA. | ||
* | ||
* @package automattic/jetpack-mu-wpcom | ||
*/ | ||
|
||
use Automattic\Jetpack\Jetpack_Mu_Wpcom; | ||
|
||
/** | ||
* Whether the site is eligible for Error Reporting, which is a feature that's specific to WPCOM. | ||
* | ||
* By default, sites should not be eligible. | ||
* | ||
* @return bool True if current site is eligible for error reporting, false otherwise. | ||
*/ | ||
function wpcom_is_site_eligible_for_error_reporting() { | ||
/** | ||
* Can be used to toggle the Error Reporting functionality. | ||
* | ||
* @param bool true if Error Reporting should be enabled, false otherwise. | ||
*/ | ||
return apply_filters( 'a8c_enable_error_reporting', false ); | ||
} | ||
|
||
/** | ||
* Inline error handler that will capture errors before the main handler has a chance to. | ||
* | ||
* Errors are pushed to a global array called `_jsErr` which is then verified in the main handler. | ||
* | ||
* @see index.js | ||
*/ | ||
function wpcom_head_error_handler() { | ||
?><script type="text/javascript"> | ||
window._headJsErrorHandler = function( errEvent ) { | ||
window._jsErr = window._jsErr || []; | ||
window._jsErr.push( errEvent ); | ||
} | ||
window.addEventListener( 'error', window._headJsErrorHandler ); | ||
</script> | ||
<?php | ||
} | ||
|
||
/** | ||
* Limit the attribute to script elements that point to scripts served from s0.wp.com. | ||
* | ||
* We might want to add stats.wp.com and widgets.wp.com here, too. See https://wp.me/pMz3w-cCq#comment-86959. | ||
* "Staticized" (aka minified or concatenaded) scripts don't go through this pipeline, so they are not processed | ||
* by this filter. The attribute is added to those directly in jsconcat, see D57238-code. | ||
* | ||
* @param string $tag String containing the def of a script tag. | ||
*/ | ||
function wpcom_add_crossorigin_to_script_elements( $tag ) { | ||
$end_of_tag = strpos( $tag, '>' ); | ||
if ( false === $end_of_tag ) { | ||
return $tag; | ||
} | ||
|
||
/* | ||
* Get JUST the <script ...> tag, not anything else. $tag can include the content of the script as well. | ||
* Assumes that $tag begins with <script..., which does seem to be the case in our testing. | ||
*/ | ||
$script_tag = substr( $tag, 0, $end_of_tag + 1 ); | ||
|
||
// If the src of that script tag points to an internal domain, set crossorigin=anonymous. | ||
if ( preg_match( '/<script.*src=.*(s0\.wp\.com|stats\.wp\.com|widgets\.wp\.com).*>/', $script_tag ) ) { // phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript | ||
// Update the src of the <script...> tag. | ||
$new_tag = str_replace( ' src=', " crossorigin='anonymous' src=", $script_tag ); | ||
|
||
// Then, find the original script_tag within the ENTIRE $tag, and replace it with the updated version. Now the script includes crossorigin=anonymous. | ||
return str_replace( $script_tag, $new_tag, $tag ); | ||
} | ||
|
||
return $tag; | ||
} | ||
|
||
/** | ||
* Temporary function to feature flag Sentry by segment. | ||
* | ||
* We'll be testing it on production (simple sites) for a while to see if it's feasible to | ||
* activate it for all sites and perhaps get rid of our custom solution. If it works well, | ||
* we'll activate for all simple sites and look into activating it for WoA, too. | ||
* | ||
* @param int $user_id The user id. | ||
* @return bool | ||
*/ | ||
function wpcom_user_in_sentry_test_segment( $user_id ) { | ||
$current_segment = 10; // Segment of existing users that will get this feature in %. | ||
$user_segment = $user_id % 100; | ||
|
||
/* | ||
* We get the last two digits of the user id and that will be used to decide in what | ||
* segment the user is. i.e if current_segment is 10, then only ids that end in < 10 | ||
* will be considered part of the segment. | ||
*/ | ||
return $user_segment < $current_segment; | ||
} | ||
|
||
/** | ||
* Return whether Sentry should be activated for a given user. | ||
* | ||
* In this phase, a12s have the possibility of configuring what error reporter to use | ||
* through the sticker. a12s should not be covered by the segment logic. | ||
* | ||
* Regular users have the error reporter chosen based on the segmentation logic, only. | ||
* | ||
* @param int $user_id The user id. Used to check if the user is A8C or in the Sentry test segment. | ||
* @param int $blog_id The blog ID. Usually the value of `get_current_blog_id`. Used to check if the sticker is applied if user is A8C. | ||
*/ | ||
function wpcom_should_activate_sentry( $user_id, $blog_id ) { | ||
return ( is_automattician( $user_id ) && has_blog_sticker( 'error-reporting-use-sentry', $blog_id ) ) | ||
|| ( ! is_automattician( $user_id ) && wpcom_user_in_sentry_test_segment( $user_id ) ); | ||
} | ||
|
||
/** | ||
* Enqueue assets | ||
*/ | ||
function wpcom_enqueue_error_reporting_script() { | ||
// Bail if ETK has enqueued its script. | ||
if ( wp_script_is( 'a8c-fse-error-reporting-script' ) ) { | ||
return; | ||
} | ||
|
||
$asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/error-reporting/error-reporting.asset.php'; | ||
$script_dependencies = isset( $asset_file['dependencies'] ) ? $asset_file['dependencies'] : array(); | ||
$script_version = isset( $asset_file['version'] ) ? $asset_file['version'] : filemtime( Jetpack_Mu_Wpcom::BASE_DIR . 'build/error-reporting/error-reporting.js' ); | ||
$script_id = 'wpcom-error-reporting-script'; | ||
|
||
wp_enqueue_script( | ||
$script_id, | ||
plugins_url( 'build/error-reporting/error-reporting.js', Jetpack_Mu_Wpcom::BASE_FILE ), | ||
$script_dependencies, | ||
$script_version, | ||
true | ||
); | ||
|
||
wp_localize_script( | ||
$script_id, | ||
'WPcom_Error_Reporting_Config', | ||
array( | ||
'shouldActivateSentry' => wpcom_should_activate_sentry( get_current_user_id(), get_current_blog_id() ) ? 'true' : 'false', | ||
'releaseName' => defined( 'WPCOM_DEPLOYED_GIT_HASH' ) ? 'WPCOM_' . WPCOM_DEPLOYED_GIT_HASH : 'WPCOM_NO_RELEASE', | ||
) | ||
); | ||
} | ||
|
||
if ( wpcom_is_site_eligible_for_error_reporting() ) { | ||
add_action( 'admin_print_scripts', 'wpcom_head_error_handler', 0 ); | ||
add_filter( 'script_loader_tag', 'wpcom_add_crossorigin_to_script_elements', 99, 2 ); | ||
|
||
// We load as last as possible for performance reasons. The head handler will capture errors until the main handler is loaded. | ||
add_action( 'admin_enqueue_scripts', 'wpcom_enqueue_error_reporting_script', 100 ); | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I correct in understanding that this feature is disabled because user_id on Atomic is not linked to WordPress.com? I'm asking for my learning purposes 🙂 @daledupreez @obenland
PCYsg-Ewq-p2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@okmttdhr, I think that @fullofcaffeine is best-placed to comment on why we're only running this on WPCOM Simple and haven't rolled the functionality out more broadly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @okmttdhr! Yes, it's not running on AT. The original plan is to run on AT at some point, and we tried - see p4TIVU-9DI-p2#comment-10922 - but there were some problems. At the time, though, we hadn't implemented Sentry yet, and Sentry should work fine on AT, too. The challenge, though is how to treat errors from AT as AT is the wild west :D - sites can run all sorts of third-party code - so we should have a RFC before proceeding, and Sentry atm is not top-priority for WPCOM, AFAIK.