Skip to content

Commit

Permalink
Use folders instead of separate npm packages (#70)
Browse files Browse the repository at this point in the history
## Description

This PR greatly simplifies the repository setup by getting rid of the separate `package.json` files as they made the setup unnecessarily complex.

### Before this PR

* Multiple `tsconfig.json` setups meant we needed to rebuild types in a particular order to even get the dev environment to work.
* Esbuild needed two steps to first build each package separately and only then build the entire project. Worse, step one used ES Modules and step two used CommonJS. Using `tsconfig.json` made it extra hard. Substituting env variables caused conflicts.
* Generating the documentation required 8 separate configuration files
* Adding a new package was tedious. It required adding new `package.json`, `tsconfig.json`, `api-documenter.json`, adjusting all the build pipelines, ensuring the build steps are triggered in the correct order etc.

### After this PR

* There are two `tsconfig.json` files in the repo root and that's it. One is for the main-thread code and the other is for the worker code.
* ESbuild runs in a single step and is configured with a single JS object.
* There are no more documentation generator configs, it's all automated.
* Introducing a new section of the repository is as simple as creating a new directory. There's no need to think about configs, build pipelines etc
  • Loading branch information
adamziel committed Nov 15, 2022
1 parent 72c3806 commit 44289e6
Show file tree
Hide file tree
Showing 157 changed files with 1,490 additions and 191,018 deletions.
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
node_modules
package-lock.json
build/*
build-api/*
build-api
build-wp
build-php
build-types
build-scripts
!build/wp-admin
!build/wp-content
!build/wp-includes
!build/php.wasm
!build/php.js
!build/wp.data
!build/wp.js
packages/*/build*
packages/*/build*/*
*.tsbuildinfo
tsdoc-metadata.json

433 changes: 0 additions & 433 deletions api-extractor-base.json

This file was deleted.

22 changes: 11 additions & 11 deletions build/wp.data

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions build/wp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 32 additions & 10 deletions docs/using-php-in-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ startPHP<!-- -->(\

Initializes the PHP runtime with the given arguments and data dependencies.

This function handles the entire PHP initialization pipeline. In particular, it: * Instantiates the Emscripten PHP module * Wires it together with the data dependencies and loads them * Ensures is all happens in a correct order * Waits until the entire loading sequence is finished
This function handles the entire PHP initialization pipeline. In particular, it:
* Instantiates the Emscripten PHP module
* Wires it together with the data dependencies and loads them
* Ensures is all happens in a correct order
* Waits until the entire loading sequence is finished

Basic usage:

Expand All @@ -139,7 +143,10 @@ Basic usage:
```
**The `/php.js` module:**

In the basic usage example, `php.js` is **not** a vanilla Emscripten module. Instead, it's an ESM module that wraps the regular Emscripten output and adds some extra functionality. It's generated by the Dockerfile shipped with this repo. Here's the API it provides:
In the basic usage example, `php.js` is **not** a vanilla Emscripten module. Instead,
it's an ESM module that wraps the regular Emscripten output and adds some
extra functionality. It's generated by the Dockerfile shipped with this repo.
Here's the API it provides:

```js
// php.wasm size in bytes:
Expand All @@ -153,9 +160,11 @@ export default function(jsEnv, emscriptenModuleArgs) {}
```
**PHP Filesystem:**

Once initialized, the PHP has its own filesystem separate from the project files. It's provided by [Emscripten and uses its FS library](https://emscripten.org/docs/api_reference/Filesystem-API.html).
Once initialized, the PHP has its own filesystem separate from the project
files. It's provided by [Emscripten and uses its FS library](https://emscripten.org/docs/api_reference/Filesystem-API.html).

The API exposed to you via the PHP class is succinct and abstracts await certain unintuitive parts of low-level FS interactions.
The API exposed to you via the PHP class is succinct and abstracts
await certain unintuitive parts of low-level FS interactions.

Here's how to use it:

Expand All @@ -181,11 +190,19 @@ For more details consult the PHP class directly.

**Data dependencies:**

Using existing PHP packages by manually recreating them file-by-file would be quite inconvenient. Fortunately, Emscripten provides a "data dependencies" feature.
Using existing PHP packages by manually recreating them file-by-file would
be quite inconvenient. Fortunately, Emscripten provides a "data dependencies"
feature.

Data dependencies consist of a `dependency.data` file and a `dependency.js` loader and can be packaged with the [file_packager.py tool](api/php-wasm.startphp.md). This project requires wrapping the Emscripten-generated `dependency.js` file in an ES module as follows:
Data dependencies consist of a `dependency.data` file and a `dependency.js` loader and
can be packaged with the [file_packager.py tool](api/php-wasm.startphp.md).
This project requires wrapping the Emscripten-generated `dependency.js` file in an ES
module as follows:

1. Prepend `export default function(emscriptenPHPModule) {'; ` 2. Prepend `export const dependencyFilename = '<DATA FILE NAME>'; ` 3. Prepend `export const dependenciesTotalSize = <DATA FILE SIZE>;` 4. Append `}`
1. Prepend `export default function(emscriptenPHPModule) {'; `
2. Prepend `export const dependencyFilename = '<DATA FILE NAME>'; `
3. Prepend `export const dependenciesTotalSize = <DATA FILE SIZE>;`
4. Append `}`

Be sure to use the `--export-name="emscriptenPHPModule"` file_packager.py option.

Expand All @@ -200,7 +217,8 @@ export default function(emscriptenPHPModule) {
// ... the rest of it ...
}
```
Such a constructions enables loading the `dependency.js` as an ES Module using `import("/dependency.js")`<!-- -->.
Such a constructions enables loading the `dependency.js` as an ES Module using
`import("/dependency.js")`<!-- -->.

Once it's ready, you can load PHP and your data dependencies as follows:

Expand All @@ -223,7 +241,8 @@ Once it's ready, you can load PHP and your data dependencies as follows:
```typescript
class PHPServer
```
A fake PHP server that handles HTTP requests but does not bind to any port.
A fake PHP server that handles HTTP requests but does not
bind to any port.

## Constructors

Expand All @@ -247,7 +266,10 @@ Constructs a new instance of the `PHPServer` class
* Returns: The response.


Serves the requesteither by serving a static file, or by dispatching it to the PHP runtime.
Serves the requesteither by serving a static file, or by
dispatching it to the PHP runtime.



## Example

Expand Down
15 changes: 12 additions & 3 deletions docs/using-php-in-the-browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,21 @@ the heavy lifting. Here's its documentation:

initializeWorkerThread<!-- -->(<!-- -->config<!-- -->: [WorkerThreadConfiguration](api/php-wasm-browser.initializeworkerthread.md)<!-- -->)<!-- -->: [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<!-- -->&lt;[any](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any)<!-- -->&gt;

* `config` – The worker thread configuration. The backend object to communicate with the parent thread.
* `config` – The worker thread configuration.
The backend object to communicate with the parent thread.


Call this in a worker thread script to set the stage for offloading the PHP processing. This function:

* Initializes the PHP runtime * Starts PHPServer and PHPBrowser * Lets the main app know when its ready * Listens for messages from the main app * Runs the requested operations (like `run_php`<!-- -->) * Replies to the main app with the results using the [request/reply protocol](api/php-wasm-browser.initializeworkerthread.md)

Call this in a worker thread script to set the stage for
offloading the PHP processing. This function:

* Initializes the PHP runtime
* Starts PHPServer and PHPBrowser
* Lets the main app know when its ready
* Listens for messages from the main app
* Runs the requested operations (like `run_php`<!-- -->)
* Replies to the main app with the results using the [request/reply protocol](api/php-wasm-browser.initializeworkerthread.md)

Remember: The worker thread code must live in a separate JavaScript file.

Expand Down

0 comments on commit 44289e6

Please sign in to comment.