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

Make the bundle smaller than 20M #2

Closed
7 of 10 tasks
adamziel opened this issue Sep 2, 2022 · 6 comments
Closed
7 of 10 tasks

Make the bundle smaller than 20M #2

adamziel opened this issue Sep 2, 2022 · 6 comments

Comments

@adamziel
Copy link
Collaborator

adamziel commented Sep 2, 2022

The largest files to load for the initial paint are:

  • wp.data – 13M – the WordPress installation and all its php files
  • php-web.wasm – 6.2M – the WebAssembly PHP build
  • wp.js – 182k – the emscripten-generated JS loader for wp.data
  • php-web.js – 153k – the emscripten-generated JS loader for php-web.wasm
  • php-webworker.js – 153k – same as above, but for the webworker backend

Let's shrink them as much as possible.

Here's a few ideas:

  • Minify the variable names in the PHP files
  • Minify the JSON files
  • Remove non-english translations
  • Identify PHP dependencies to remove, e.g. libxml2 doesn't seem necessary
    • libxml2 was removed from the default build in db0a477
  • Compress the .data and .wasm files using brotli compression – props to @eliot-akira. Edit: Web servers take care of it and most browsers support the brotli compression!
  • Consider disabling script/style concatenation #39 – done in Don't include static files in the build. #43 – reduced wp.data from 43M to 13M!
  • Minify the PHP files
  • Remove themes other than twentytwentytwo
  • Remove unminified JS and CSS files
  • Remove the .eot, .gif, .htaccess, .md, .mp4, .png, .scss, .stylelintignore, .svg, .ttf, .txt, .woff, and .woff2 files
@adamziel
Copy link
Collaborator Author

adamziel commented Sep 2, 2022

The file extensions seen inside of the bundle are:

; find ./ -type f  | awk -F '.' '{print $NF}' | sort | uniq
//wp-includes/sodium_compat/LICENSE
crt
css
eot
gif
htaccess
html
jpg
js
json
md
mp4
php
png
scss
stylelintignore
svg
ttf
txt
woff
woff2
xml

@adamziel adamziel changed the title Make the bundle smaller than 50mb [Browser] Make the bundle smaller than 50mb Sep 22, 2022
@eliot-akira
Copy link
Collaborator

eliot-akira commented Oct 3, 2022

Here's an idea I saw today in Golang's documentation about reducing the size of WASM files.

https://github.com/golang/go/wiki/WebAssembly#reducing-the-size-of-wasm-files

They recommend brotli compression, which produces the smallest file size compared to others (gzip or zopfli).

EDIT: Oops, never mind - this issue is about the file wp.data, which is not a WASM binary. Leaving the comment here anyway, the information could be useful elsewhere.

adamziel added a commit that referenced this issue Oct 14, 2022
Define 'CONCATENATE_SCRIPTS' as false in `wp-config.php` and removes most static assets from the `wp.data` build script. This makes for an incredible size decrease from 44M to 13M. See #39 and #2 for more details.

Co-authored-by: Adam Zieliński <adam@adamziel.com>
@adamziel adamziel changed the title [Browser] Make the bundle smaller than 50mb [Browser] Make the bundle smaller than 13M Oct 14, 2022
@adamziel adamziel changed the title [Browser] Make the bundle smaller than 13M [Browser] Make the bundle smaller than 20M Oct 14, 2022
@adamziel
Copy link
Collaborator Author

@eliot-akira I just played with brotli compression and the results are impressive:

  • php-web.wasm went from 6.2M to 1.5M (gzip gives 2.2M)
  • wp.data went from 13M to 3.3M (gzip gives 5.2M)

https://caniuse.com/brotli says it's supported on most major browsers – it shouldn't be that hard to serve brotli-compressed version of this files by default and offer a gzip fallback for unsupported browsers.

@eliot-akira
Copy link
Collaborator

Cool! Glad the information about brotli turned out to be useful. Just saw PR #43, amazing.

@adamziel adamziel changed the title [Browser] Make the bundle smaller than 20M Make the bundle smaller than 20M Oct 22, 2022
@krishenriksen
Copy link

krishenriksen commented Nov 4, 2022

Brotli compress/decompress of php.wasm + wp.data

php.wasm - 1.58 MB
wp.data - 1.44 MB

#66

It loads fast enough now for you to even remove the Progress Bar.

@adamziel
Copy link
Collaborator Author

adamziel commented Nov 23, 2022

We're down to 3MB - 4MB for both php.wasm and wp.data! The actual bundles are about 15MB at the moment, but the webservers apply Brotli compression which dramatically reduces the amount of data transferred over the wire. Furthermore, #66 explores Brotli at JavaScript level to support non-webserver environments and more aggressive compression levels.

I'm going to close this issue for now, feel free to reopen at any point if needed.

jdevalk added a commit to jdevalk/wordpress-playground that referenced this issue Feb 6, 2024
Using `''` instead of `null` to prevent the following error:

```json
{
“type”:“deprecated”,
“message”:“http_build_query(): Passing null to parameter WordPress#2 ($numeric_prefix) of type string is deprecated”,“file”:“wp-content/mu-plugins/playground-includes/wp_http_fetch.php”,“line”:113,“stack”:[“http_build_query()“,”Wp_Http_Fetch_Base::format_get()“,”Wp_Http_Fetch_Base->request()“,”WpOrg\Requests\Requests::request()“,”WP_Http->request()“,”WP_Http->get()“,”wp_remote_get()“,”WP_Community_Events->get_events()“,”wp_ajax_get_community_events()“,”do_action(‘wp_ajax_get-community-events’)“],“component”:“MU Plugin: playground-includes”
}
```
adamziel pushed a commit that referenced this issue Feb 6, 2024
Using `''` instead of `null` in Wp_Http_Fetch to prevent the following error:

```json
{
“type”:“deprecated”,
“message”:“http_build_query(): Passing null to parameter #2 ($numeric_prefix) of type string is deprecated”,“file”:“wp-content/mu-plugins/playground-includes/wp_http_fetch.php”,“line”:113,“stack”:[“http_build_query()“,”Wp_Http_Fetch_Base::format_get()“,”Wp_Http_Fetch_Base->request()“,”WpOrg\Requests\Requests::request()“,”WP_Http->request()“,”WP_Http->get()“,”wp_remote_get()“,”WP_Community_Events->get_events()“,”wp_ajax_get_community_events()“,”do_action(‘wp_ajax_get-community-events’)“],“component”:“MU Plugin: playground-includes”
}
```
bgrgicak pushed a commit that referenced this issue Jun 2, 2024
## What is this PR doing?

Similar to #1357, but taking a deeper cut at it, as import expects to be
run within an admin interface.

## What problem is it solving?

The following fatal will be triggered:
```
PHP Fatal error:  Uncaught Error: Call to undefined function wp_read_audio_metadata() in /wordpress/wp-admin/includes/image.php:2
Stack trace:
#0 /wordpress/wp-content/plugins/WordPress-Importer-master/class-wxr-importer.php(1067): wp_generate_attachment_metadata(821, '/wordpress/wp-c...')
#1 /wordpress/wp-content/plugins/WordPress-Importer-master/class-wxr-importer.php(861): WXR_Importer->process_attachment(Array, Array, 'https://raw.git...')
#2 /wordpress/wp-content/plugins/WordPress-Importer-master/class-wxr-importer.php(383): WXR_Importer->process_post(Array, Array, Array, Array)
#3 /internal/eval.php(20): WXR_Importer->import('/tmp/import.wxr')
#4 {main}
  thrown in /wordpress/wp-admin/includes/image.php on line 2
```

Fixes #1444 (Since found this issue)

## How is the problem addressed?

Including includes/admin.php which pulls in all admin-related
functionality, rather than just the cropping functionality.

Alternatively, this could've been done by selectively importing
`includes/media.php` as well as `includes/image.php`, but I anticipate
that core is likely to include other related admin functions.

## Testing Instructions

Note: This is an untested change.

Try running
https://playground.wordpress.net/builder/builder.html#{%22preferredVersions%22:{%22php%22:%228.0%22,%22wp%22:%22latest%22},%22phpExtensionBundles%22:[%22kitchen-sink%22],%22features%22:{%22networking%22:true},%22steps%22:[{%22step%22:%22login%22,%22username%22:%22admin%22,%22password%22:%22password%22},{%22step%22:%22importWxr%22,%22file%22:{%22resource%22:%22url%22,%22url%22:%22https://raw.githubusercontent.com/WordPress/theme-test-data/try/cors-accessible-images/themeunittestdata.wordpress.xml%22}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants