Skip to content
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 Web Worker Offloading module #556

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
93fdcc9
Add partytown package in dependencies
thelovekesh Mar 27, 2022
7780635
Add script to copy partytown js files inside module
thelovekesh Mar 27, 2022
fdef498
Add partytown scripts
thelovekesh Mar 27, 2022
3a51604
Add web worker module under javascript scope
thelovekesh Mar 28, 2022
6c80047
Add tests for web worker module
thelovekesh Mar 28, 2022
2251f4a
Update tests
thelovekesh Mar 28, 2022
8bbf34a
Update module-i18n file with Web Worker Module
thelovekesh Mar 28, 2022
2a65e27
Update tests for Web Worker Module
thelovekesh Mar 28, 2022
cbc172a
Update assert function for checking has_action
thelovekesh Apr 5, 2022
ead61f0
Fix package-lock file
thelovekesh Oct 13, 2022
db90a04
Add perflab_ prefix in functions and filters
thelovekesh Oct 13, 2022
28b5fab
Update in_array strict comparision to true
thelovekesh Oct 13, 2022
9ea44b2
Fix grammar
thelovekesh Oct 13, 2022
805b8bc
Refactor code to get partytown scripts from single helper function
thelovekesh Oct 13, 2022
c96f06c
Update module name
thelovekesh Oct 13, 2022
d299c3a
Update module-i18n file
thelovekesh Oct 13, 2022
0de9671
Add event forward array in partytown config
thelovekesh Oct 13, 2022
aa5479a
Remove partytown files
thelovekesh Oct 13, 2022
a9bc409
Add GH workflow steps to include Partytown library files at plugin re…
thelovekesh Oct 13, 2022
134e092
Update directory name for more clarity about module
thelovekesh Oct 13, 2022
b92d7a9
Update partytown copylib command
thelovekesh Oct 13, 2022
f6de312
Add partytown lib path to gitignore
thelovekesh Oct 13, 2022
96e3033
Add partytown copylib command at prepare hook
thelovekesh Oct 13, 2022
d18fc66
Update tests path for partytown module
thelovekesh Oct 13, 2022
eb71def
Fix tests prefix and undefined method
thelovekesh Oct 13, 2022
bfeefb5
Bump `@builder.io/partytown` from 0.5.1 to 0.5.4
thelovekesh Jan 20, 2024
3ac9488
Update closures to be static
thelovekesh Jan 20, 2024
3a1bd8f
Add method visibility
thelovekesh Jan 20, 2024
33774cd
Update since annotation to use n.e.x.t format
thelovekesh Jan 20, 2024
a6c40b5
Add js to focus areas
thelovekesh Jan 20, 2024
7f14576
Update module-i18n.php
thelovekesh Jan 20, 2024
a17ba9f
Add .vscode to ignored list
thelovekesh Jan 20, 2024
aad2200
Update partytown config and script enqueuing
thelovekesh Jan 20, 2024
846248f
Update partytown module test cases
thelovekesh Jan 20, 2024
38a5781
Update partytown module script handling
thelovekesh Jan 20, 2024
47ddefd
Add cli command to copy vendor libraries
thelovekesh Jan 20, 2024
0ad4560
Hook copy-vendor-libs command on build-plugins command
thelovekesh Jan 20, 2024
def2055
Add config for copying vendor libs for different modules
thelovekesh Jan 20, 2024
e0e8dd4
Update plugin step before release
thelovekesh Jan 20, 2024
283f6f3
Bump `@builder.io/partytown` from 0.5.4 to 0.8.2
thelovekesh Jan 20, 2024
9c5feef
Add condition to load Partytown module based on ssl requirement
thelovekesh Jan 26, 2024
3804345
Add errors in the admin notices for Partytown Module
thelovekesh Jan 26, 2024
e899107
Add condition to exit early if module is accessed directly
thelovekesh Jan 26, 2024
2c7a51f
Remove file exists check for partytown library
thelovekesh Jan 26, 2024
6159750
Add .eslintignore
thelovekesh Jan 26, 2024
15f5bf5
Remove pluggable functions from can-load.php file
thelovekesh Feb 3, 2024
b2babb6
Update module description
thelovekesh Feb 3, 2024
335daaa
Move Partytown Web Worker module to js-and-css focus area
thelovekesh Feb 3, 2024
d482043
Revert "Add cli command to copy vendor libraries"
thelovekesh Feb 3, 2024
f8bc701
Revert "Hook copy-vendor-libs command on build-plugins command"
thelovekesh Feb 3, 2024
0058897
Add copy-webpack-plugin
thelovekesh Jan 26, 2024
073a813
Add webpackbar
thelovekesh Jan 26, 2024
21721c8
Bump @babel/traverse
thelovekesh Jan 26, 2024
5a83f39
Add webpack base config
thelovekesh Jan 26, 2024
9ff7b82
Add wp-scripts build script
thelovekesh Jan 26, 2024
5dfcfdf
Add webpack config for copying partytown lib
thelovekesh Feb 3, 2024
d8c779c
Update build command
thelovekesh Feb 3, 2024
7afe9e9
Update package-lock.json
thelovekesh Feb 3, 2024
5bbcd2c
Update build command in workflows
thelovekesh Feb 3, 2024
efd16cb
Add .asset.php files to exclude from PHPCS
thelovekesh Jan 26, 2024
77f3907
Update module-i18n.php
thelovekesh Feb 3, 2024
6b0b88f
Update plugin path to remove any trailing slashes
thelovekesh Feb 3, 2024
41f4c9a
Update partytown tests path
thelovekesh Feb 3, 2024
1f46191
Update partytown module tests cases
thelovekesh Feb 3, 2024
35412eb
Update partytown module tests cases to reset wp scripts global state …
thelovekesh Feb 3, 2024
0095e8f
Update substr offset to keep the logic clean
thelovekesh Feb 3, 2024
195886b
Add @thelovekesh
thelovekesh Feb 3, 2024
f9f2a5e
Add build assets steps in workflows
thelovekesh Feb 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/*.min.js
**/node_modules/**
**/vendor/**
build/*
modules/js-and-css/partytown-web-worker/assets/js/partytown/*
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
/modules/js-and-css/audit-enqueued-assets @manuelRod
/tests/modules/js-and-css/audit-enqueued-assets @manuelRod
/tests/testdata/modules/js-and-css/audit-enqueued-assets @manuelRod
/modules/js-and-css/partytown-web-worker @thelovekesh
/tests/modules/js-and-css/partytown-web-worker @thelovekesh
westonruter marked this conversation as resolved.
Show resolved Hide resolved

# Module: Dominant Color Images
/modules/images/dominant-color-images @pbearne @spacedmonkey
Expand Down
16 changes: 16 additions & 0 deletions .github/workflows/deploy-dotorg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3.5.0
with:
node-version-file: ".nvmrc"
cache: npm

- name: Install dependencies
run: npm ci
env:
CI: true

- name: Build Plugin
run: npm run build

- name: WordPress plugin deploy
uses: 10up/action-wordpress-plugin-deploy@stable
env:
SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
SVN_USERNAME: ${{ secrets.SVN_USERNAME }}
SLUG: performance-lab

- name: Upload release assets
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/php-test-standalone-plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ jobs:
cache: npm
- name: npm install
run: npm ci
- name: Build plugin assets
run: npm run build
- name: General debug information
run: |
npm --version
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/php-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ jobs:
cache: npm
- name: npm install
run: npm ci
- name: Build plugin assets
run: npm run build
- name: Install WordPress
run: npm run wp-env start
# Note that `composer update` is required instead of `composer install`
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local.properties
.cproject
.buildpath
nbproject/
.vscode

############
## Build
Expand All @@ -33,6 +34,7 @@ build-cs/composer.lock
node_modules/
vendor/

modules/js-and-css/partytown-web-worker/assets/js/partytown
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will necessitate introducing a .distignore file to replace the .gitattributes file as explained in #893 (comment).

############
## OSes
############
Expand Down
2 changes: 2 additions & 0 deletions module-i18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
_x( 'Creates WebP versions for new JPEG image uploads if supported by the server.', 'module description', 'performance-lab' ),
_x( 'Enqueued Assets Health Check', 'module name', 'performance-lab' ),
_x( 'Adds a CSS and JS resource check in Site Health status.', 'module description', 'performance-lab' ),
_x( 'Partytown Web Worker', 'module name', 'performance-lab' ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_x( 'Partytown Web Worker', 'module name', 'performance-lab' ),
_x( 'Web Worker Offloading', 'module name', 'performance-lab' ),

_x( 'Add support for running JavaScript in a separate Web Worker with the help of the Partytown library.', 'module description', 'performance-lab' ),
_x( 'Autoloaded Options Health Check', 'module name', 'performance-lab' ),
_x( 'Adds a check for autoloaded options in Site Health status.', 'module description', 'performance-lab' ),
);
Expand Down
41 changes: 41 additions & 0 deletions modules/js-and-css/partytown-web-worker/can-load.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Can load function to determine if Partytown Web Worker module can be loaded.
*
* @since n.e.x.t
* @package partytown-web-worker
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

return static function () {
$errors = new WP_Error();

// Check for Partytown JavaScript library.
if ( ! file_exists( __DIR__ . '/assets/js/partytown/partytown.js' ) ) {
$errors->add(
'partytown_web_worker_library_missing',
sprintf(
/* translators: %s: npm commands. */
__( 'Partytown library is missing from the plugin. Please do "%s" to install it.', 'performance-lab' ),
'npm install && npm run build'
)
);
}

// Check for HTTPS.
if (
! is_ssl()
&&
'localhost' !== wp_parse_url( home_url(), PHP_URL_HOST )
) {
$errors->add(
'partytown_web_worker_ssl_error',
__( 'Partytown Web Worker Module requires a secure HTTPS connection to be enabled.', 'performance-lab' )
);
}

return $errors->has_errors() ? $errors : true;
};
139 changes: 139 additions & 0 deletions modules/js-and-css/partytown-web-worker/load.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php
/**
* Module Name: Partytown Web Worker
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to call the module Partytown?

IMHO usage of the Partytown library is just an implementation detail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. A less-specific name would be "Web Worker Offloading". I asked Bard for some alternative names, and it suggested "Threadshift". I like that, but it is trademarked.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. If you have a certain name in mind, do let me know, and I will edit it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about Web Worker Offloading for now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM, some more names:

  • Scripts offloading/offloader (in the description add a reference to Web Worker and/or Partytown).
  • Third-party JS offloader
  • Non-critical JS offloader

I will defer name selection to you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are some good options but I like Web Worker Offloading.

The JS being offloaded may not be third-party. It may also be critical, but just offloaded so it can run in another thread.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Module Name: Partytown Web Worker
* Module Name: Web Worker Offloading

* Description: Add support for running JavaScript in a separate Web Worker with the help of the Partytown library.
* Experimental: Yes
*
* @since n.e.x.t
* @package performance-lab
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* PartyTown Configuration
*
* @since n.e.x.t
* @see https://partytown.builder.io/configuration
* @return array
*/
function perflab_partytown_web_worker_configuration() {
$plugin_dir = plugin_dir_path( __FILE__ );
$content_dir_basename = basename( WP_CONTENT_DIR );

$config = array(
'lib' => substr( $plugin_dir, strpos( $plugin_dir, $content_dir_basename ) - 1 ) . 'assets/js/partytown/',
'forward' => array(),
);

/**
* Add configuration for PartyTown.
*
* @since n.e.x.t
* @see <https://partytown.builder.io/configuration>.
* @param array $config Configuration for PartyTown.
* @return array
*/
return apply_filters( 'perflab_partytown_configuration', $config );
}

/**
* Initialize PartyTown
*
* @since n.e.x.t
* @return void
*/
function perflab_partytown_web_worker_init() {
$partytown_js = file_get_contents( __DIR__ . '/assets/js/partytown/partytown.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request.

wp_register_script(
'partytown',
'',
array(),
PERFLAB_VERSION,
array( 'in_footer' => false )
);

wp_add_inline_script(
'partytown',
sprintf(
'window.partytown = %s;',
wp_json_encode( perflab_partytown_web_worker_configuration() )
),
'before'
);

wp_add_inline_script(
'partytown',
$partytown_js,
'after'
);
}
add_action( 'wp_enqueue_scripts', 'perflab_partytown_web_worker_init' );

/**
* Helper function to get all scripts tags which has `partytown` dependency.
*
* @since n.e.x.t
*
* @return array Array of script handles with `partytown` dependency.
*/
function perflab_get_partytown_handles() {
global $wp_scripts;

$partytown_handles = array();
foreach ( $wp_scripts->registered as $handle => $script ) {
if ( ! empty( $script->deps ) && in_array( 'partytown', $script->deps, true ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, but let's say you have an existing script in a theme like:

wp_enqueue_script( 'foo', 'https://example.com/foo.js' );

In order to opt-in this script to use Partytown, you'd have to do this if you wanted to opt-in from a separate plugin:

wp_scripts()->registered['foo']->deps[] = 'partytown';

Is that right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that should do the trick.

$partytown_handles[] = $handle;
}
}

return $partytown_handles;
}

/**
* Update script type for handles having `partytown` as dependency.
*
* @since n.e.x.t
*
* @param string $tag Script tag.
* @param string $handle Script handle.
* @param string $src Script source.
*
* @return string $tag Script tag with type="text/partytown".
*/
function perflab_partytown_web_worker_update_script_type( $tag, $handle, $src ) {
global $wp_scripts;

$partytown_handles = perflab_get_partytown_handles();

if ( in_array( $handle, $partytown_handles, true ) ) {
$before_script = $wp_scripts->get_inline_script_data( $handle, 'before' );
$after_script = $wp_scripts->get_inline_script_data( $handle, 'after' );

if ( ! empty( $before_script ) || ! empty( $after_script ) ) {
_doing_it_wrong(
'wp_add_inline_script',
sprintf(
/* translators: %s: script handle */
esc_html__( 'Cannot add inline script "%s" to scripts with a "partytown" dependency. Script will continue to load in the main thread.', 'performance-lab' ),
'<a href="https://developer.wordpress.org/reference/functions/wp_add_inline_script/">wp_add_inline_script()</a>'
),
esc_html( PERFLAB_VERSION )
);
} else {
$tag = wp_get_script_tag(
array(
'type' => 'text/partytown',
'src' => $src,
'handle' => $handle . '-js',
)
);
}
}

return $tag;
}
add_filter( 'script_loader_tag', 'perflab_partytown_web_worker_update_script_type', 10, 3 );
Loading
Loading