Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
822eb75
feat(packages/js): add CommonJS support alongside ESM
Ankurrr27 Jul 22, 2026
5ba2f1b
feat(js)!: build separate browser, standalone, and node artifacts
Ryan-Millard Jul 25, 2026
215460f
test(Example Apps): add per-format consumer apps for cjs, esm, iife, …
Ryan-Millard Jul 25, 2026
51c8d44
Merge branch 'dev' into feat/multi-format-build
Ryan-Millard Jul 26, 2026
96025ca
Merge branch 'dev' into feat/multi-format-build
Ryan-Millard Jul 30, 2026
5597328
Merge branch 'dev' into feat/multi-format-build
Ryan-Millard Aug 5, 2026
bdf28b3
refactor(html-js apps): combine into a single project and statically …
Ryan-Millard Aug 5, 2026
3079c3e
docs(website): add example apps page to link to all example apps
Ryan-Millard Aug 5, 2026
e4e220b
chore: cleanup accidentally added generated files
Ryan-Millard Aug 5, 2026
661002d
chore(style): update .prettierignore and format files
Ryan-Millard Aug 5, 2026
df3f520
fix(web link): fix repo link for example apps page
Ryan-Millard Aug 10, 2026
040b579
applty coderabbit's suggestions
Ryan-Millard Aug 10, 2026
767065a
chore(build files): remove accidentally-comitted build files
Ryan-Millard Aug 10, 2026
92622a5
Merge branch 'dev' into feat/multi-format-build
Ryan-Millard Aug 15, 2026
81f9dbf
chore(website): refine wording on example apps page
Ryan-Millard Aug 15, 2026
6b9a1bf
fix(website): fix example app links and text
Ryan-Millard Aug 15, 2026
018ef10
chore(website): fix example apps link
Ryan-Millard Aug 15, 2026
3df89a1
chore(format): format all files
Ryan-Millard Aug 15, 2026
af2d157
docs(requirements): document new JS requirements
Ryan-Millard Aug 15, 2026
be16b23
fix(requirements): add better explanations
Ryan-Millard Aug 15, 2026
d8dfa64
fix(.gitignore): packages/py/img2num/_img2num.pyi -> /_img2num.pyi
Ryan-Millard Aug 15, 2026
0b713aa
docs: fix broken link
Ryan-Millard Aug 15, 2026
1889273
fix(website): build img2num package in prebuild stage
Ryan-Millard Aug 15, 2026
acc2d47
docs: fix requirements wording
Ryan-Millard Aug 15, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ ENV CHOKIDAR_USEPOLLING=true
ENV CHOKIDAR_INTERVAL=100

# See docker-compose.yml ports
EXPOSE 5173 4173 3000 5174 4174
EXPOSE 3000 5173 4173 5174 4174 5175 5176

# --------------------------------------------------------------------------------------------------------------
# Healthcheck (Basic if container works)
Expand Down
19 changes: 15 additions & 4 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ help:
console-cpp <input_image>: run example C++ app on input image \n \
console-c <input_image>: run example C app on input image \n \
console-py <input_image>: run example python app on input image \n \
console-js <input_image>: run example node app on input image \n \
console-js-cjs <input_image>: run example node app on input image \n \
console-js-esm <input_image>: run example node app on input image \n \
"

init:
Expand Down Expand Up @@ -104,7 +105,17 @@ console-c input:
@echo "./build-c-cpp/example-apps/console-c/console_c_app {{ input }}"
./build-c-cpp/example-apps/console-c/CImg2NumExample_console_c "{{ input }}"

console-js input:
@echo "node example-apps/console-js/index.js {{ input }}"
node example-apps/console-js/index.js "{{ input }}"
console-js-cjs input:
@echo "node example-apps/console-js-cjs/index.cjs {{ input }}"
node example-apps/console-js-cjs/index.cjs "{{ input }}"

console-js-esm input:
@echo "node example-apps/console-js-esm/index.mjs {{ input }}"
node example-apps/console-js-esm/index.mjs "{{ input }}"

html-js-iife script:
pnpm -F html-js-iife "{{script}}"

html-js-umd script:
pnpm -F html-js-umd "{{script}}"
Comment thread
Ryan-Millard marked this conversation as resolved.
Resolved

112 changes: 80 additions & 32 deletions bindings/js/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,102 @@ if(NOT EMSCRIPTEN)
message(FATAL_ERROR "Emscripten is required to build JS bindings.")
endif()

file(GLOB_RECURSE BINDING_SRC CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c")
# Base64-inline the wasm into the UMD/IIFE glue. Costs ~33% over the wire and
# gives up instantiateStreaming, but makes the <script>-tag build a single file
# with no path resolution. Turn OFF if the binary is large (see notes below).
option(IMG2NUM_WASM_SINGLE_FILE "Inline the wasm into the standalone (UMD/IIFE) glue" ON)

add_executable(img2num_wasm ${BINDING_SRC})
file(GLOB_RECURSE BINDING_SRC CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c")

target_link_libraries(img2num_wasm PRIVATE CImg2Num)
set(WASM_OUT_DIR "${CMAKE_SOURCE_DIR}/packages/js/build-wasm")

target_include_directories(img2num_wasm PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/include"
)

# Emscripten options
set(COMMON_FLAGS
# Flags shared by every variant. Everything that differs between variants
# (ENVIRONMENT / EXPORT_ES6 / SINGLE_FILE) is applied per-target below.
set(IMG2NUM_WASM_BASE_LINK_FLAGS
"SHELL:-sMODULARIZE=1"
"SHELL:-sEXPORT_ES6=1"
"SHELL:-sENVIRONMENT=web,worker,node"
"SHELL:-sEXPORT_NAME=createImg2NumModule"
"SHELL:-sEXPORTED_FUNCTIONS=['_malloc','_free']"
"SHELL:-sEXPORTED_RUNTIME_METHODS=['ccall','cwrap','getValue','setValue','HEAPU8','HEAP32','UTF8ToString','stringToUTF8','lengthBytesUTF8']"
"SHELL:-sINITIAL_MEMORY=2GB"
"SHELL:-sMAXIMUM_MEMORY=4GB"
"SHELL:-sASYNCIFY_STACK_SIZE=131272" # in bytes
"SHELL:-sALLOW_MEMORY_GROWTH=1"
"SHELL:-sEXPORT_NAME=createImg2NumModule"
"SHELL:-sINITIAL_MEMORY=32MB"
"SHELL:-sMAXIMUM_MEMORY=4GB"
"SHELL:-sASYNCIFY=1"
"SHELL:-sSTACK_OVERFLOW_CHECK=1"
"SHELL:-sASYNCIFY_STACK_SIZE=131272" # bytes
"SHELL:-sABORTING_MALLOC=0"
"SHELL:-sNO_DISABLE_EXCEPTION_CATCHING"
"SHELL:--use-port=emdawnwebgpu"
"SHELL:-sFILESYSTEM=0"
)

target_compile_options(img2num_wasm PRIVATE -msimd128 -fexceptions)
# img2num_add_wasm_variant(<target>
# ENVIRONMENT "web,worker" | "node"
# ES6 0 | 1
# SINGLE_FILE 0 | 1
# SUBDIR web | standalone | node)
function(img2num_add_wasm_variant TARGET_NAME)
cmake_parse_arguments(V "" "ENVIRONMENT;ES6;SINGLE_FILE;SUBDIR" "" ${ARGN})

add_executable(${TARGET_NAME} ${BINDING_SRC})

target_link_options(img2num_wasm PRIVATE ${COMMON_FLAGS} -msimd128)
target_link_libraries(${TARGET_NAME} PRIVATE CImg2Num)
target_include_directories(${TARGET_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_options(img2num_wasm PRIVATE -O0 -gsource-map -fsanitize=alignment)
target_link_options(img2num_wasm PRIVATE -gsource-map -fsanitize=alignment "SHELL:-sASSERTIONS=2")
target_compile_options(${TARGET_NAME} PRIVATE -msimd128 -fexceptions)

target_link_options(${TARGET_NAME} PRIVATE
${IMG2NUM_WASM_BASE_LINK_FLAGS}
-msimd128
-fexceptions
"SHELL:-sENVIRONMENT=${V_ENVIRONMENT}"
"SHELL:-sEXPORT_ES6=${V_ES6}"
"SHELL:-sSINGLE_FILE=${V_SINGLE_FILE}"
)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_options(${TARGET_NAME} PRIVATE
-O0 -gsource-map -fsanitize=alignment)
target_link_options(${TARGET_NAME} PRIVATE
-gsource-map -fsanitize=alignment
"SHELL:-sASSERTIONS=2"
"SHELL:-sSTACK_OVERFLOW_CHECK=1")
else()
# -Oz: smallest size. Critical for the browser, where the wasm is
# fetched on nearly every page load.
target_compile_options(${TARGET_NAME} PRIVATE -Oz)
endif()

set_target_properties(${TARGET_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${WASM_OUT_DIR}/${V_SUBDIR}"
OUTPUT_NAME "img2num"
SUFFIX ".js"
)

message(STATUS
"WASM variant ${TARGET_NAME}: env=${V_ENVIRONMENT} es6=${V_ES6} "
"single_file=${V_SINGLE_FILE} -> ${WASM_OUT_DIR}/${V_SUBDIR}/img2num.js")
endfunction()

if(IMG2NUM_WASM_SINGLE_FILE)
set(_STANDALONE_SINGLE_FILE 1)
else()
# -Oz: smallest size. This is critical for websites as WASM is loaded almost every time
target_compile_options(img2num_wasm PRIVATE -Oz)
target_link_options(img2num_wasm PRIVATE "SHELL:-sSINGLE_FILE=0")
set(_STANDALONE_SINGLE_FILE 0)
endif()

set_target_properties(img2num_wasm PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/packages/js/build-wasm"
OUTPUT_NAME "index"
SUFFIX ".js"
)
# ESM consumers behind a bundler. External .wasm so it can be cached and
# relocated by the host app's asset pipeline.
img2num_add_wasm_variant(img2num_wasm_web
ENVIRONMENT "web,worker" ES6 1 SINGLE_FILE 0 SUBDIR web)

# <script>-tag consumers. Non-ES6 so the glue has no top-level await and no
# import.meta — the two things Rollup cannot place in a UMD or IIFE chunk.
img2num_add_wasm_variant(img2num_wasm_standalone
ENVIRONMENT "web,worker" ES6 0 SINGLE_FILE ${_STANDALONE_SINGLE_FILE}
SUBDIR standalone)

# Node. Separate build so the web glue carries no fs/path branches and the
# node glue carries no XHR branches.
img2num_add_wasm_variant(img2num_wasm_node
ENVIRONMENT "node" ES6 0 SINGLE_FILE 0 SUBDIR node)

message(STATUS "WASM module configured: img2num_wasm")
message(STATUS "Build output directory: ${CMAKE_SOURCE_DIR}/packages/js/build-wasm")
message(STATUS "Generated JS file: ${CMAKE_SOURCE_DIR}/packages/js/build-wasm/index.js")
add_custom_target(img2num_wasm_all
DEPENDS img2num_wasm_web img2num_wasm_standalone img2num_wasm_node)
14 changes: 10 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ services:
- /usr/src/app/.venv # PROTECTS the container's .venv from being overwritten
- pnpm-store:/pnpm
ports:
- "5173:5173" # example-apps/react-js dev
- "4173:4173" # example-apps/react-js preview
- "3000:3000" # docs/ start
- "5174:5174" # example-apps/html-js start and dev
- "4174:4174" # example-apps/html-js preview

- "5173:5173" # example-apps/react-js dev script
- "4173:4173" # example-apps/react-js preview script

- "5174:5174" # example-apps/html-js start and dev scripts
- "4174:4174" # example-apps/html-js preview script

- "5175:5175" # example-apps/html-js-iife start script

- "5176:5176" # example-apps/html-js-umd start script
stdin_open: true
tty: true

Expand Down
2 changes: 0 additions & 2 deletions docs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ node_modules
/static/docs/c/api/*
!/static/docs/c/api/.gitkeep
/static/example-apps/react-js/*
!/static/example-apps/react-js/.gitkeep
/static/example-apps/html-js/*
!/static/example-apps/html-js/.gitkeep

# Production
/build
Expand Down
1 change: 0 additions & 1 deletion docs/src/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ table td {
code {
background-color: var(--ifm-code-background);
border-radius: var(--radius-sm);
color: var(--text);
}

[data-theme="dark"] code {
Expand Down
Loading
Loading