keller-mark / esbuild-py

Python bindings to the esbuild Transform API
MIT License
1 stars 0 forks source link

Support jupyter-lite / pyodide #11

Open keller-mark opened 2 months ago

keller-mark commented 2 months ago

See

keller-mark commented 2 months ago

See https://go.dev/blog/wasi - try building with GOOS=wasip1 GOARCH=wasm - use go 1.21 or higher

Update: this works up to the usage of the .so file in the Pyodide python REPL:

Following the steps at https://pyodide.org/en/stable/development/building-and-testing-packages.html#building-and-testing-packages-out-of-tree

pip install pyodide-build

git clone https://github.com/emscripten-core/emsdk.git
cd emsdk

PYODIDE_EMSCRIPTEN_VERSION=$(pyodide config get emscripten_version)
./emsdk install ${PYODIDE_EMSCRIPTEN_VERSION}
./emsdk activate ${PYODIDE_EMSCRIPTEN_VERSION}
source emsdk_env.sh

cd ..
pyodide build

pyodide venv .venv-pyodide
source .venv-pyodide/bin/activate

pip install "./dist/esbuild_py-0.1.5-cp311-cp311-emscripten_3_1_46_wasm32.whl"

python
>>> import ctypes
>>> from pathlib import Path
>>> import os
>>> lib_path = Path('build/lib.emscripten_3_1_46_wasm32-cpython-311/_esbuild.cpython-311-wasm32-emscripten.so').absolute()
>>> so = ctypes.CDLL(lib_path)
>>> so.transform
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/python311.zip/ctypes/__init__.py", line 389, in __getattr__
  File "/lib/python311.zip/ctypes/__init__.py", line 394, in __getitem__
AttributeError: Tried to lookup unknown symbol "transform" in dynamic lib: /path/to/esbuild-py/build/lib.emscripten_3_1_46_wasm32-cpython-311/_esbuild.cpython-311-wasm32-emscripten.so
>>> so.__dict__
{'_name': PosixPath('/path/to/esbuild-py/build/lib.emscripten_3_1_46_wasm32-cpython-311/_esbuild.cpython-311-wasm32-emscripten.so'), '_FuncPtr': <class 'ctypes.CDLL.__init__.<locals>._FuncPtr'>, '_handle': 23285320}
keller-mark commented 2 months ago

May not work without go:wasmexport - see https://github.com/golang/go/issues/65199 although there is discussion of using main with stdin/out

keller-mark commented 2 months ago

Alternative solution: use JS FFI to run the esbuild WASM when in pyodide contexts https://pyodide.org/en/stable/usage/api/python-api/code.html#pyodide.code.run_js - see discussion at https://github.com/pyodide/pyodide/issues/4696#issuecomment-2058915225

Can detect pyodide at runtime with https://pyodide.org/en/0.16.1/faq.html#how-to-detect-that-code-is-run-with-pyodide