Skip to content

Add developer documentation #60

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

Merged
merged 39 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
02ccafe
Rebuild php
adamziel Oct 25, 2022
79034fd
WIP Developer documentation
adamziel Oct 17, 2022
12c87ee
Draft docs for the php-wasm module
adamziel Oct 22, 2022
4ebb374
Adjust the server code example
adamziel Oct 22, 2022
3db6929
Document the build pipeline
adamziel Oct 23, 2022
5bfb617
Document php.js
adamziel Oct 25, 2022
d0d511e
Document and cleanup the PHPServer class
adamziel Oct 25, 2022
fc2defa
Move @typedef declaration to the bottom
adamziel Oct 25, 2022
e77abe7
Document PHPBrowser
adamziel Oct 25, 2022
05ef279
Update the code examples
adamziel Oct 25, 2022
e4c9281
Document pib_eval.c
adamziel Oct 25, 2022
3fa0957
Document and cleanup pib_eval.c
adamziel Oct 25, 2022
7bc55c8
Document PHP and PHPServer in more details
adamziel Oct 25, 2022
9c8b683
MVP of php-wasm package documentation
adamziel Oct 25, 2022
108654e
First draft of the php-wasm-browser docs
adamziel Oct 25, 2022
e3f3262
Rename worker thread „environments” to „backends” and „backends” to „…
adamziel Oct 25, 2022
1971b9d
Document the php-wasm-browser architecture overview
adamziel Oct 25, 2022
ceadf6b
Document messaging.js
adamziel Oct 25, 2022
379c78b
Remove the concept of postMessageHandler
adamziel Oct 25, 2022
e02d505
Simplify the code example in messaging.js
adamziel Oct 26, 2022
c185e2f
Document scopes
adamziel Oct 26, 2022
ffb968e
Rename `messageId` to `requestId` and `reply` to `response`
adamziel Oct 26, 2022
eeddfb7
Document emscripten-download-monitor.js
adamziel Oct 26, 2022
ddb677e
Remove the Service Worker Routing section as it’s covered in other pl…
adamziel Oct 26, 2022
633f55a
Don’t export the DEFAULT_RESPONSE_TIMEOUT
adamziel Oct 26, 2022
455e9fb
Document loadPHPWithProgress
adamziel Oct 26, 2022
00193d3
Rename urls.js to scopes.js
adamziel Oct 26, 2022
e7ce76c
Document scope.js
adamziel Oct 26, 2022
3e1a0f1
Document getPathQueryFragment
adamziel Oct 26, 2022
af22746
Document initializePHPWorkerThread
adamziel Oct 26, 2022
4cafc6a
Direct communication between the service worker and the worker thread
adamziel Oct 26, 2022
2f52212
Clarify the data flow between the different threads
adamziel Oct 26, 2022
07dc780
Correctly convert HOSTNAME to HOST in php-server
adamziel Oct 26, 2022
221ee74
Explain the worker thread backends and rename startPHPWorkerThread to…
adamziel Oct 26, 2022
f5c79bf
Rename the docs to README.md in their respective packages
adamziel Oct 26, 2022
10b329d
Fix the table of contents links
adamziel Oct 26, 2022
e7005f9
Adjust the header to match the toc
adamziel Oct 26, 2022
8835408
Correct typos
adamziel Oct 26, 2022
1db8420
Post-process the README files with docgen
adamziel Oct 26, 2022
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
105 changes: 33 additions & 72 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,95 +1,56 @@
# WordPress in the browser!

See
[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).

![](demo.gif)


## Running the demo

This repository ships with a pre-built demo that you can just run!
WordPress.wasm is a client-side WordPress that runs without a PHP server thanks to the magic of WebAssembly.

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

If you want to build the assembly yourself, follow the instructions below.

This repo draws inspiration from https://github.com/seanmorris/php-wasm and uses https://github.com/aaemnnosttv/wp-sqlite-db

## Building the assembly

The build process is split into automated parts. The scripts below create a docker image with the necessary tools, and build PHP as WebAssembly.

### Building WASM PHP for the web
![](demo.gif)

```bash
npm run build:php:web
```
### Building WASM PHP for node.js
Table of contents:

```bash
npm run build:php:node
```
- [Why is this useful?](#why-is-this-useful)
- [Getting started](#getting-started)
- [Architecture overview](#architecture-overview)

## Building custom WordPress bundle
## Why is this useful?

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

For example, before the comment "Prepare WordPress static files":
WordPress.wasm can power:

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

```bash
plugin_file=example-plugin.1.0.0.zip
wget https://downloads.wordpress.org/plugin/$plugin_file
unzip $plugin_file
rm $plugin_file
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!

mv example-plugin wordpress/wp-content/plugins/
See
[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.

echo "<?php
require_once ABSPATH.'wp-content/mu-plugins/example-plugin/example-plugin.php';
" > wp-content/mu-plugins/index.php
```
### Getting started

To generate a new bundle, run:
You can run WordPress.wasm as follows:

```bash
npm run build:wp
```js
git clone https://github.com/WordPress/wordpress-wasm
cd wordpress-wasm
npm install
npm run dev
```

## How does it work?

This repo uses four magic ingredients to make WordPress work in the browser:

1. A WordPress configured to use SQLite instead of MySQL. This is possible thanks to https://github.com/aaemnnosttv/wp-sqlite-db.
2. A PHP 8.0 compiled with SQLite3 support into WebAssembly.
3. A PHP + WordPress WebAssembly bundle created using the emscripten toolkit.
4. A service worker that loads the bundle and dispatches the regular HTTP traffic to the in-memory WordPress instance.

The static files (.js, .css, etc.) are served directly from the host filesystem, not from the WebAssembly bundle.

The work is *heavily* inspired by https://github.com/seanmorris/php-wasm.

## Limitations

The worker applies a series of strange patches to WordPress, it's unclear why they're needed at the moment.

The site editor does not work at the moment. The block editor does, though.
A browser should open and take you to your very own client-side WordPress at http://127.0.0.1:8777/wordpress.html!

PHP cannot communicate with WordPress.org so the plugin directory etc does not work.
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.

The sqlite database lives in the memory and the changes only live as long as the service worker.
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.

## Future work
## Architecture overview

* Fix the workarounds mentioned above
* Remove the static files from the wasm bundle
* Remove unnecessary PHP extensions to lower the bundle size
WordPress.wasm is made of the following building blocks:

* [php-wasm](./packages/php-wasm) – a configurable PHP->WebAssembly build pipeline, convenient JavaScript bindings, and a PHP server implemented in JavaScript.
* [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.
* [wordpress-wasm](./packages/wordpress-wasm) – runs WordPress in the browser using the other two packages.

Consult the linked README.md files in each of these packages to learn more.
8 changes: 8 additions & 0 deletions build-docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

npx docgen packages/php-wasm/src/php.js --output packages/php-wasm/README.md --to-section php.js 1 ↵
npx docgen packages/php-wasm/src/php-server.js --output packages/php-wasm/README.md --to-section php-server.js
npx docgen packages/php-wasm/src/php-browser.js --output packages/php-wasm/README.md --to-section php-browser.js

npx docgen packages/php-wasm-browser/src/emscripten-download-monitor.js --output packages/php-wasm-browser/README.md --to-section Utilities --ignore defaul
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'
4 changes: 2 additions & 2 deletions build/php.js

Large diffs are not rendered by default.

Binary file modified build/php.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"yargs": "^17.5.1"
},
"devDependencies": {
"@wordpress/docgen": "^1.29.0",
"@wordpress/eslint-plugin": "^13.0.0",
"live-server": "^1.2.2",
"chokidar-cli": "^3.0.0",
"esbuild": "^0.15.5",
"eslint": "8.22.0",
Expand All @@ -53,6 +53,7 @@
"gulp": "^4.0.2",
"gulp-rename": "^2.0.0",
"gulp-replace": "^1.1.3",
"live-server": "^1.2.2",
"npm-run-all": "^4.1.5",
"prettier": "^2.7.1",
"request": "^2.88.2"
Expand Down
Loading