WordPress / wordpress-playground

Run WordPress in the browser via WebAssembly PHP
https://w.org/playground/
GNU General Public License v2.0
1.65k stars 260 forks source link

Lazy load PHP extensions (like dom and libxml) #89

Open westonruter opened 1 year ago

westonruter commented 1 year ago

I tried uploading the Web Stories plugin, but the plugin gave an error notice:

The following PHP extensions are missing: dom, libxml. Please contact your host to finish installation.
The following PHP classes are missing: DOMAttr, DOMComment, DOMDocument, DOMElement, DOMNode, DOMNodeList, DOMText, DOMXPath. Please contact your host to finish installation.
The following PHP functions are missing: libxml_use_internal_errors, libxml_clear_errors. Please contact your host to finish installation.

The dom and libxml extensions should be enabled, as they are almost always available on WordPress sites.

westonruter commented 1 year ago

I suppose that means the WITH_LIBXML build option should rather be yes here:

https://github.com/WordPress/wordpress-playground/blob/ddebea5943045f461f48c0044a8a4ebc91f88309/src/php-wasm/wasm/gulpfile.js#L37-L38

adamziel commented 1 year ago

@westonruter It's been enabled by default, I disabled it to remove 1.21M from the bundle size (see https://github.com/WordPress/wordpress-playground/issues/2).

I do agree it can be useful, though! That's the problem of different use-cases – you don't need it for embeddable code snippets in a JavaScript-oriented WordPress course, but you do need it to use the Playground with certain WordPress plugins.

An obvious solution would be hosting two PHP builds, with and without libxml, and having a configuration switch with some smart defaults. It would work as long as that's only about a single extension. For two, three, or five PHP extensions there would need to be something smarter. Perhaps pre-packaged "environments" for specific purposes? As in:

swissspidy commented 1 year ago

This list of recommended extensions might be helpful: https://make.wordpress.org/hosting/handbook/server-environment/#php-extensions

adamziel commented 1 year ago

Another idea: Emscripten supports dynamic libraries that are loaded lazily – Playground could use them to provide a lean initial bundle while also offering all the additional features.

adamziel commented 1 year ago

Here’s v8.dev article about an unrelated topic with an example implementation of lazy-loading wasm libraries. They use the experimental JSPI api, but it should also work with the existing stable Asyncify api:

https://v8.dev/blog/jspi

adamziel commented 1 year ago

Related: https://emscripten.org/docs/optimizing/Module-Splitting.html#module-splitting

adamziel commented 1 year ago

Actually – lazy loading may not be needed at all. Building PHP extensions to .so files and loading them using zend_extension= in PHP.ini would already do the trick. Blueprints would then support declaring a list of extensions to load and voila.

I just explored that today and managed to build XDebug. The next challenge is actually loading it in PHP, dlopen seems to be disabled at the moment.

adamziel commented 1 month ago

JSPI support just got merged and lazy loading is now technically possible – as described in https://v8.dev/blog/jspi.