From 6c763e1ea509404465e79e0556a0d02b7921b8e3 Mon Sep 17 00:00:00 2001 From: anderskiaer Date: Sat, 11 Mar 2023 21:03:18 +0100 Subject: [PATCH] Add possibility for disabling inlining `wasm` in `opencv.js` --- doc/js_tutorials/js_setup/js_setup/js_setup.markdown | 3 +++ modules/js/CMakeLists.txt | 2 +- platforms/js/build_js.py | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/doc/js_tutorials/js_setup/js_setup/js_setup.markdown b/doc/js_tutorials/js_setup/js_setup/js_setup.markdown index 26a4e419bd..5b0e65b250 100644 --- a/doc/js_tutorials/js_setup/js_setup/js_setup.markdown +++ b/doc/js_tutorials/js_setup/js_setup/js_setup.markdown @@ -83,6 +83,9 @@ Building OpenCV.js from Source It requires `python` and `cmake` installed in your development environment. -# The build script builds asm.js version by default. To build WebAssembly version, append `--build_wasm` switch. + By default everything is bundled into one JavaScript file by `base64` encoding the WebAssembly code. For production + builds you can add `--disable_single_file` which will reduce total size by writing the WebAssembly code + to a dedicated `.wasm` file which the generated JavaScript file will automatically load. For example, to build wasm version in `build_wasm` directory: @code{.bash} diff --git a/modules/js/CMakeLists.txt b/modules/js/CMakeLists.txt index 5996e419dd..19f0b19790 100644 --- a/modules/js/CMakeLists.txt +++ b/modules/js/CMakeLists.txt @@ -70,7 +70,7 @@ if(COMPILE_FLAGS) endif() set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} --memory-init-file 0 -s TOTAL_MEMORY=128MB -s WASM_MEM_MAX=1GB -s ALLOW_MEMORY_GROWTH=1") -set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} -s MODULARIZE=1 -s SINGLE_FILE=1") +set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} -s MODULARIZE=1") set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} -s EXPORT_NAME=\"'cv'\" -s DEMANGLE_SUPPORT=1") set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} -s FORCE_FILESYSTEM=1 --use-preload-plugins --bind --post-js ${JS_HELPER} ${COMPILE_FLAGS}") set_target_properties(${the_module} PROPERTIES LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS}") diff --git a/platforms/js/build_js.py b/platforms/js/build_js.py index 64dc1a6c67..3e8edfe4ad 100644 --- a/platforms/js/build_js.py +++ b/platforms/js/build_js.py @@ -180,6 +180,8 @@ class Builder: flags += "-s WASM=1 " elif self.options.disable_wasm: flags += "-s WASM=0 " + if not self.options.disable_single_file: + flags += "-s SINGLE_FILE=1 " if self.options.threads: flags += "-s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=4 " else: @@ -233,6 +235,7 @@ if __name__ == "__main__": parser.add_argument('--emscripten_dir', default=emscripten_dir, help="Path to Emscripten to use for build (deprecated in favor of 'emcmake' launcher)") parser.add_argument('--build_wasm', action="store_true", help="Build OpenCV.js in WebAssembly format") parser.add_argument('--disable_wasm', action="store_true", help="Build OpenCV.js in Asm.js format") + parser.add_argument('--disable_single_file', action="store_true", help="Do not merge JavaScript and WebAssembly into one single file") parser.add_argument('--threads', action="store_true", help="Build OpenCV.js with threads optimization") parser.add_argument('--simd', action="store_true", help="Build OpenCV.js with SIMD optimization") parser.add_argument('--build_test', action="store_true", help="Build tests")