Skip to content

Commit

Permalink
fix(wasm-build-script): zxing_writer.wasm size issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Sec-ant committed Jan 3, 2025
1 parent bffbf9d commit f8c33b2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-files-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"zxing-wasm": patch
---

Fix the `zxing_writer.wasm` size issue. See [#190](https://github.com/Sec-ant/zxing-wasm/discussions/190).
2 changes: 1 addition & 1 deletion .github/actions/test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ runs:
steps:
- name: Build WASM
shell: bash
run: pnpm -s cmake && pnpm -s build:wasm
run: pnpm -s build:wasm

- name: Run Tests
shell: bash
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ pnpm i --frozen-lockfile

# Install CMake before executing the next command:
# https://cmake.org/download/
pnpm cmake

# Install Emscripten before executing the next command:
# https://emscripten.org/docs/getting_started/downloads.html
pnpm build:wasm
Expand Down Expand Up @@ -98,15 +96,15 @@ import { readBarcodes, writeBarcode } from "zxing-wasm/full";

### `zxing-wasm/reader`

This subpath only provides a function to read barcodes. The wasm binary size is ~906 KB.
This subpath only provides a function to read barcodes. The wasm binary size is ~907 KB.

```ts
import { readBarcodes } from "zxing-wasm/reader";
```

### `zxing-wasm/writer`

This subpath only provides a function to write barcodes. The wasm binary size is ~1.17 MB.
This subpath only provides a function to write barcodes. The wasm binary size is ~594 KB.

```ts
import { writeBarcode } from "zxing-wasm/writer";
Expand Down
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,15 @@
"update-hooks": "simple-git-hooks",
"submodule:init": "git submodule update --init",
"submodule:update": "git submodule update --remote",
"cmake": "emcmake cmake -S src/cpp -B build",
"build:wasm": "cmake --build build -j$(($(nproc 2>/dev/null || sysctl -n hw.logicalcpu) - 1))",
"cmake:base": "emcmake cmake -S src/cpp -B build",
"cmake:reader": "pnpm -s cmake:base -DTARGET=READER",
"cmake:writer": "pnpm -s cmake:base -DTARGET=WRITER",
"cmake:full": "pnpm -s cmake:base -DTARGET=FULL",
"build:wasm:base": "cmake --build build -j$(($(nproc 2>/dev/null || sysctl -n hw.logicalcpu) - 1))",
"build:wasm:reader": "pnpm -s cmake:reader && pnpm -s build:wasm:base",
"build:wasm:writer": "pnpm -s cmake:writer && pnpm -s build:wasm:base",
"build:wasm:full": "pnpm -s cmake:full && pnpm -s build:wasm:base",
"build:wasm": "pnpm -s build:wasm:reader && pnpm -s build:wasm:writer && pnpm -s build:wasm:full",
"copy:wasm": "copy-files-from-to",
"docs:dev": "conc \"pnpm:docs:preview\" \"typedoc --watch --excludeInternal\"",
"docs:build": "typedoc --excludeInternal",
Expand All @@ -168,7 +175,7 @@
"postbuild:es": "tsc -p ./tsconfig.pkg.json --declarationDir ./dist/es",
"postbuild:cjs": "tsc -p ./tsconfig.pkg.json --declarationDir ./dist/cjs",
"postbuild": "conc \"pnpm:copy:wasm\" \"pnpm:docs:build\"",
"build:all": "pnpm -s submodule:init && pnpm -s cmake && pnpm -s build:wasm && pnpm -s build",
"build:all": "pnpm -s submodule:init && pnpm -s build:wasm && pnpm -s build",
"preview": "vite preview",
"prepublishOnly": "pnpm -s build:all",
"bump-biome:latest": "pnpm add -DE @biomejs/biome@latest",
Expand Down
32 changes: 18 additions & 14 deletions src/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ set(CMAKE_CXX_STANDARD 20)
set(ZXING_EXAMPLES OFF)

# Build options
set(ZXING_READERS ON)
set(ZXING_WRITERS "NEW")
if (${TARGET} MATCHES "READER")
set(ZXING_READERS ON)
set(ZXING_WRITERS OFF)
elseif (${TARGET} MATCHES "WRITER")
set(ZXING_READERS OFF)
set(ZXING_WRITERS "NEW")
else()
set(ZXING_READERS ON)
set(ZXING_WRITERS "NEW")
endif()
set(ZXING_EXPERIMENTAL_API ON)
set(ZXING_USE_BUNDLED_ZINT ON)

Expand Down Expand Up @@ -50,26 +58,22 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} \
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../zxing-cpp/core ${CMAKE_BINARY_DIR}/ZXing)

# Build targets
if(ZXING_READERS AND (ZXING_WRITERS MATCHES "NEW"))
add_executable(zxing_full ZXingWasm.cpp)
target_compile_definitions(zxing_full PRIVATE READER WRITER)
target_link_libraries(zxing_full ZXing::ZXing stb::stb)
set_target_properties(zxing_full PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../full")
endif()

if(ZXING_READERS)
if(${TARGET} MATCHES "READER")
add_executable(zxing_reader ZXingWasm.cpp)
target_compile_definitions(zxing_reader PRIVATE READER)
target_link_libraries(zxing_reader ZXing::ZXing stb::stb)
set_target_properties(zxing_reader PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../reader")
endif()

if(ZXING_WRITERS MATCHES "NEW")
elseif(${TARGET} MATCHES "WRITER")
add_executable(zxing_writer ZXingWasm.cpp)
target_compile_definitions(zxing_writer PRIVATE WRITER)
target_link_libraries(zxing_writer ZXing::ZXing stb::stb)
set_target_properties(zxing_writer PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../writer")
else()
add_executable(zxing_full ZXingWasm.cpp)
target_compile_definitions(zxing_full PRIVATE READER WRITER)
target_link_libraries(zxing_full ZXing::ZXing stb::stb)
set_target_properties(zxing_full PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../full")
endif()

0 comments on commit f8c33b2

Please sign in to comment.