seanmorris / php-wasm

PHP in Browser, powered by WebAssembly.
https://php-wasm.seanmorr.is/
Apache License 2.0
800 stars 42 forks source link

Unable to do a custom Builds #70

Open kambo-1st opened 3 weeks ago

kambo-1st commented 3 weeks ago

What I am trying to achieve: I want to bundle an existing PHP CLI application with a multiple files and run it with php-wasm by building my own php-wasm with included files through the configuration in .php-wasm-rc.

Platform:

Steps to Reproduce:

  1. Followed the instructions at: https://github.com/seanmorris/php-wasm/tree/master?tab=readme-ov-file#%EF%B8%8F-custom-builds
  2. Run: npm install -g php-wasm-builder
  3. Tried to run:
    • php-wasm-builder image (Command not found)
    • php-wasm-build image (This worked)
  4. Tried to run: php-wasm-build build

Result:

PHP_DIST_DIR=/home/xxx/Public/temp docker-compose --progress auto -p phpwasm run  --rm -e PKG_CONFIG_PATH=/src/lib/lib/pkgconfig -e PRELOAD_ASSETS='lib/share/icu/72.1/icudt72l.dat' -e INITIAL_MEMORY=64MB -e ENVIRONMENT=web -e PHP_BRANCH=php-8.2.11 -e EMCC_CORES=`nproc` emscripten-builder cp -rf /src/third_party/libicu-72-1 /src/third_party/libicu_alt
PHP_DIST_DIR=/home/xxx/Public/temp docker-compose --progress auto -p phpwasm run  --rm -e PKG_CONFIG_PATH=/src/lib/lib/pkgconfig -e PRELOAD_ASSETS='lib/share/icu/72.1/icudt72l.dat' -e INITIAL_MEMORY=64MB -e ENVIRONMENT=web -e PHP_BRANCH=php-8.2.11 -e EMCC_CORES=`nproc` emscripten-builder mv /src/third_party/libicu_alt /src/third_party/libicu-72-1
PHP_DIST_DIR=/home/xxx/Public/temp docker-compose --progress auto -p phpwasm run  --rm -e PKG_CONFIG_PATH=/src/lib/lib/pkgconfig -e PRELOAD_ASSETS='lib/share/icu/72.1/icudt72l.dat' -e INITIAL_MEMORY=64MB -e ENVIRONMENT=web -e PHP_BRANCH=php-8.2.11 -e EMCC_CORES=`nproc` -w /src/third_party/libicu-72-1/icu4c/source emscripten-builder git apply --no-index ../../../../patch/libicu.patch
error: can't open patch 'icu4c/source/../../../../patch/libicu.patch': No such file or directory
make: *** [/home/xxx/.nvm/versions/node/v23.1.0/lib/node_modules/php-wasm-builder/node_modules/php-wasm-libicu/static.mak:56: third_party/libicu-72-1/.gitignore] Error 128

What I tried next:

Result:

npm warn deprecated core-js@1.2.7: core-js@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js.
ReferenceError: [BABEL] source/PhpBase.js: Unknown option: /home/xxx/.nvm/versions/node/v23.1.0/lib/node_modules/php-wasm-builder/.babelrc.presets
    at Logger.error (/home/xxx/.npm/_npx/02ef052036b6e199/node_modules/babel-core/lib/transformation/file/logger.js:58:11)
    at OptionManager.mergeOptions (/home/xxx/.npm/_npx/02ef052036b6e199/node_modules/babel-core/lib/transformation/file/options/option-manager.js:126:29)
    at OptionManager.addConfig (/home/xxx/.npm/_npx/02ef052036b6e199/node_modules/babel-core/lib/transformation/file/options/option-manager.js:107:10)
    at OptionManager.findConfigs (/home/xxx/.npm/_npx/02ef052036b6e199/node_modules/babel-core/lib/transformation/file/options/option-manager.js:168:35)
    at OptionManager.init (/home/xxx/.npm/_npx/02ef052036b6e199/node_modules/babel-core/lib/transformation/file/options/option-manager.js:229:12)
    at File.initOptions (/home/xxx/.npm/_npx/02ef052036b6e199/node_modules/babel-core/lib/transformation/file/index.js:147:75)
    at new File (/home/xxx/.npm/_npx/02ef052036b6e199/node_modules/babel-core/lib/transformation/file/index.js:137:22)
    at Pipeline.transform (/home/xxx/.npm/_npx/02ef052036b6e199/node_modules/babel-core/lib/transformation/pipeline.js:164:16)
    at exports.transform (/home/xxx/.npm/_npx/02ef052036b6e199/node_modules/babel/lib/babel/util.js:40:22)
    at exports.compile (/home/xxx/.npm/_npx/02ef052036b6e199/node_modules/babel/lib/babel/util.js:49:20)
make: *** [Makefile:648: /home/xxx/Public/temp/PhpBase.js] Error 1

Expected behavior: Successful build of a PHP application that can run with php-wasm.

Actual behavior:

Extra question: I wonder if building PHP is really necessary. Would it be possible to just bundle a couple of PHP files without this extra step?

seanmorris commented 3 weeks ago

@kambo-1st Usually building a custom PHP version isn't necessary, unless you need to change the list of statically compiled extensions. There are a few different ways you can bring files in, however. What is your use-case?

kambo-1st commented 3 weeks ago

@seanmorris Thanks for asking! My goal is to bundle a PHP CLI application (involving multiple files and using composer for autoloading) to run it within a PHP-WASM environment. I'm mainly aiming to get the CLI application running in the browser without requiring server-side PHP.

If building PHP isn’t necessary, I'd love to know more about the alternative ways you mentioned to bring files in, especially if it means avoiding the build process.

seanmorris commented 3 weeks ago

@kambo-1st You can load files into the FS via constructor params. You just need to use an array of objects with the following form:

const files = [
    {
        name: 'icudt72l.dat',
        parent: '/preload/',
        url: 'https://unpkg.com/php-wasm-intl/icudt72l.dat'
    }
];

https://php-wasm.seanmorr.is/filesystem/loading-files.html

Those constructor params are also accessible from static HTML:

https://codepen.io/SeanMorris227/pen/KKLeePJ?editors=1000

kambo-1st commented 3 weeks ago

@seanmorris Thanks for the heads-up! I know about loading files via constructor params. Honestly, though, I’m not sure if this approach would be ideal for a PHP application that has over 10,325 files (some of them are composer depedencies). Do you think this is the only viable approach in this case?

seanmorris commented 3 weeks ago

@kambo-1st Ah, yea its somewhat annoying that we need to do custom builds to create emscripten preloaded FS, so I try to avoid that. The demo-web application actually uses the php-zip extension and a small script to set up the drupal, laravel, cakephp & lamias demos.

I could probably add some functionality to download and unzip a file right to the FS as well. Let me know if that sounds useful.

kambo-1st commented 2 weeks ago

Thanks for explaining that to me, now it make a sense. I will try to use approach with zip file, but I am afraid that it will be too slow. Nevertheless I will try it and will see.

On Mon, 11 Nov 2024, 02:34 Sean Morris, @.***> wrote:

@kambo-1st https://github.com/kambo-1st Ah, yea its somewhat annoying that we need to do custom builds to create emscripten preloaded FS, so I try to avoid that. The demo-web application actually uses the php-zip extension and a small script to set up the drupal, laravel, cakephp & lamias demos.

I could probably add some functionality to download and unzip a file right to the FS as well. Let me know if that sounds useful.

— Reply to this email directly, view it on GitHub https://github.com/seanmorris/php-wasm/issues/70#issuecomment-2467063784, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABRRG6BPZSZKFXWBO4VJ5ZT2AACQXAVCNFSM6AAAAABRE6B4QKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINRXGA3DGNZYGQ . You are receiving this because you were mentioned.Message ID: @.***>