Skip to content

Commit a7d27a8

Browse files
authored
Add developer documentation (#60)
Document the public and private APIs of all the packages in this repository. This commit is polluted with code refactoring since many ideas stood out as overly complex while writing the docs. There isn't much value in separating them out at this point so they're here. Follow-up work: * Iterate on the docs, fix typos, gather more feedback, reword confusing fragments. * Generate a proper reference doc in a markdown format. It should include the top-level module documentation, function documentation, and the classes + their methods. I'm not sure which tool can handle that correctly. * Add the missing `wordpress-wasm` module documentation * Fill-in any remaining blanks
1 parent 02ccafe commit a7d27a8

30 files changed

+2295
-879
lines changed

README.md

Lines changed: 33 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,56 @@
11
# WordPress in the browser!
22

3-
See
4-
[the WordPress.org blog post explaining why is this useful and how does it work](https://make.wordpress.org/core/2022/09/23/client-side-webassembly-wordpress-with-no-server/) for more details. The README doc below is short&sweet for now. Also, [explore the early preview on StackBlitz](https://stackblitz.com/edit/wp-plugin-playground).
5-
6-
![](demo.gif)
7-
8-
9-
## Running the demo
10-
11-
This repository ships with a pre-built demo that you can just run!
3+
WordPress.wasm is a client-side WordPress that runs without a PHP server thanks to the magic of WebAssembly.
124

13-
1. Clone this repo
14-
2. Run `npm install && npm run dev`
15-
3. Visit http://127.0.0.1:8777/
5+
[See the live demo!](https://wasm.wordpress.net/wordpress.html)
166

17-
If you want to build the assembly yourself, follow the instructions below.
18-
19-
This repo draws inspiration from https://github.com/seanmorris/php-wasm and uses https://github.com/aaemnnosttv/wp-sqlite-db
20-
21-
## Building the assembly
22-
23-
The build process is split into automated parts. The scripts below create a docker image with the necessary tools, and build PHP as WebAssembly.
24-
25-
### Building WASM PHP for the web
7+
![](demo.gif)
268

27-
```bash
28-
npm run build:php:web
29-
```
30-
### Building WASM PHP for node.js
9+
Table of contents:
3110

32-
```bash
33-
npm run build:php:node
34-
```
11+
- [Why is this useful?](#why-is-this-useful)
12+
- [Getting started](#getting-started)
13+
- [Architecture overview](#architecture-overview)
3514

36-
## Building custom WordPress bundle
15+
## Why is this useful?
3716

38-
If you'd like to customize the packaged WordPress installation, update
39-
the build script at `packages/wordpress-wasm/wordpress/prepare-wordpress.sh`. It generates a bundle called `wp.data`.
17+
I'm glad you asked – WordPress.wasm is a big deal!
4018

41-
For example, before the comment "Prepare WordPress static files":
19+
WordPress.wasm can power:
4220

43-
- Download and extract a plugin zip package
44-
- Move it into `wp-content/mu-plugins`
45-
- Add a file that loads the plugin
21+
* Runeditable code examples in your documentation or course
22+
* Plugin and theme demos in a private WordPress instance where the user is already logged in as admin
23+
* Easily switching PHP and WordPress version when testing
24+
* Replaying and fixing the failed CI tests right in the browser
4625

47-
```bash
48-
plugin_file=example-plugin.1.0.0.zip
49-
wget https://downloads.wordpress.org/plugin/$plugin_file
50-
unzip $plugin_file
51-
rm $plugin_file
26+
WordPress.wasm provides a strong foundation for the above use-cases but does not implement them just yet. This project is still in its early days and needs contributors. Become a contributor today and help us make these tools a reality!
5227

53-
mv example-plugin wordpress/wp-content/plugins/
28+
See
29+
[the WordPress.org blog post](https://make.wordpress.org/core/2022/09/23/client-side-webassembly-wordpress-with-no-server/) to learn about the vision, other use-cases, and the visuals.
5430

55-
echo "<?php
56-
require_once ABSPATH.'wp-content/mu-plugins/example-plugin/example-plugin.php';
57-
" > wp-content/mu-plugins/index.php
58-
```
31+
### Getting started
5932

60-
To generate a new bundle, run:
33+
You can run WordPress.wasm as follows:
6134

62-
```bash
63-
npm run build:wp
35+
```js
36+
git clone https://github.com/WordPress/wordpress-wasm
37+
cd wordpress-wasm
38+
npm install
39+
npm run dev
6440
```
6541

66-
## How does it work?
67-
68-
This repo uses four magic ingredients to make WordPress work in the browser:
69-
70-
1. A WordPress configured to use SQLite instead of MySQL. This is possible thanks to https://github.com/aaemnnosttv/wp-sqlite-db.
71-
2. A PHP 8.0 compiled with SQLite3 support into WebAssembly.
72-
3. A PHP + WordPress WebAssembly bundle created using the emscripten toolkit.
73-
4. A service worker that loads the bundle and dispatches the regular HTTP traffic to the in-memory WordPress instance.
74-
75-
The static files (.js, .css, etc.) are served directly from the host filesystem, not from the WebAssembly bundle.
76-
77-
The work is *heavily* inspired by https://github.com/seanmorris/php-wasm.
78-
79-
## Limitations
80-
81-
The worker applies a series of strange patches to WordPress, it's unclear why they're needed at the moment.
82-
83-
The site editor does not work at the moment. The block editor does, though.
42+
A browser should open and take you to your very own client-side WordPress at http://127.0.0.1:8777/wordpress.html!
8443

85-
PHP cannot communicate with WordPress.org so the plugin directory etc does not work.
44+
As of today, the best way to play with WordPress.wasm is to directly modify the cloned files – [packages/wordpress-wasm/](./packages/wordpress-wasm) is a good place to start.
8645

87-
The sqlite database lives in the memory and the changes only live as long as the service worker.
46+
Why aren't there any npm packages? This is a new project and didn't get there yet – all the efforts were focused on getting it to work. If you'd like to see public npm packages sooner than later, you are more than welcome to contribute.
8847

89-
## Future work
48+
## Architecture overview
9049

91-
* Fix the workarounds mentioned above
92-
* Remove the static files from the wasm bundle
93-
* Remove unnecessary PHP extensions to lower the bundle size
50+
WordPress.wasm is made of the following building blocks:
9451

52+
* [php-wasm](./packages/php-wasm) – a configurable PHP->WebAssembly build pipeline, convenient JavaScript bindings, and a PHP server implemented in JavaScript.
53+
* [php-wasm-browser](./packages/php-wasm-browser) – tools to run real PHP apps in the browser via `php-wasm`, like a Service Worker implementation or Worker Threads to run the PHP runtime concurrently.
54+
* [wordpress-wasm](./packages/wordpress-wasm) – runs WordPress in the browser using the other two packages.
9555

56+
Consult the linked README.md files in each of these packages to learn more.

build-docs.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
npx docgen packages/php-wasm/src/php.js --output packages/php-wasm/README.md --to-section php.js 1 ↵
4+
npx docgen packages/php-wasm/src/php-server.js --output packages/php-wasm/README.md --to-section php-server.js
5+
npx docgen packages/php-wasm/src/php-browser.js --output packages/php-wasm/README.md --to-section php-browser.js
6+
7+
npx docgen packages/php-wasm-browser/src/emscripten-download-monitor.js --output packages/php-wasm-browser/README.md --to-section Utilities --ignore defaul
8+
npx docgen packages/php-wasm-browser/src/worker-thread.js --output packages/php-wasm-browser/README.md --to-section 'Worker Thread API ' --ignore 'loadPHPWithProgress|currentBackend'

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
"yargs": "^17.5.1"
4141
},
4242
"devDependencies": {
43+
"@wordpress/docgen": "^1.29.0",
4344
"@wordpress/eslint-plugin": "^13.0.0",
44-
"live-server": "^1.2.2",
4545
"chokidar-cli": "^3.0.0",
4646
"esbuild": "^0.15.5",
4747
"eslint": "8.22.0",
@@ -53,6 +53,7 @@
5353
"gulp": "^4.0.2",
5454
"gulp-rename": "^2.0.0",
5555
"gulp-replace": "^1.1.3",
56+
"live-server": "^1.2.2",
5657
"npm-run-all": "^4.1.5",
5758
"prettier": "^2.7.1",
5859
"request": "^2.88.2"

0 commit comments

Comments
 (0)