Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: miniprogram #107

Merged
merged 13 commits into from
Feb 23, 2025
Merged
5 changes: 5 additions & 0 deletions .changeset/pink-tips-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"zxing-wasm": minor
---

Support WeChat Mini Program.
5 changes: 5 additions & 0 deletions .changeset/pretty-ravens-send.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"zxing-wasm": minor
---

Accept `ArrayBuffer` and `Uint8Array` as input types in `readBarcodes`.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Apart from ES and CJS modules, this package also ships IIFE scripts. The registe

### [`readBarcodes`](https://zxing-wasm.deno.dev/functions/full.readBarcodes.html)

[`readBarcodes`](https://zxing-wasm.deno.dev/functions/full.readBarcodes.html) accepts an image [`Blob`](https://developer.mozilla.org/docs/Web/API/Blob), image [`File`](https://developer.mozilla.org/docs/Web/API/File), or an [`ImageData`](https://developer.mozilla.org/docs/Web/API/ImageData) as its first argument, and various options are supported in [`ReaderOptions`](https://zxing-wasm.deno.dev/interfaces/full.ReaderOptions.html) as an optional second argument.
[`readBarcodes`](https://zxing-wasm.deno.dev/functions/full.readBarcodes.html) accepts an image [`Blob`](https://developer.mozilla.org/docs/Web/API/Blob), image [`File`](https://developer.mozilla.org/docs/Web/API/File), [`ArrayBuffer`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer), [`Uint8Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array), or an [`ImageData`](https://developer.mozilla.org/docs/Web/API/ImageData) as its first argument, and various options are supported in [`ReaderOptions`](https://zxing-wasm.deno.dev/interfaces/full.ReaderOptions.html) as an optional second argument.

The return result of this function is a `Promise` of an array of [`ReadResult`](https://zxing-wasm.deno.dev/interfaces/full.ReadResult.html)s.

Expand Down Expand Up @@ -336,6 +336,30 @@ If you want to use this library in non-web runtimes (such as Node.js, Bun, Deno,
});
```

> [!NOTE]
> To use this library in a WeChat mini program <img src="https://github.com/user-attachments/assets/7d8f3337-dd9c-43ec-aab4-8d4e72d32867" width="16" height="16">, there are several things to keep in mind:
>
> 1. Only the `zxing-wasm` import path is supported; `zxing-wasm/reader` or `zxing-wasm/writer` is not supported.
> 2. Before using the library, you need to copy/move the `node_modules/zxing-wasm/dist/full/zxing_full.wasm` file into your project directory.
> 3. You must use `prepareZXingModule` to configure how the `.wasm` file will be fetched, loaded, and compiled before calling `readBarcodes` or `writeBarcode`. This is mandatory, and you can do so with the following code:
>
> ```typescript
> prepareZXingModule({
> overrides: {
> instantiateWasm(imports, successCallback) {
> WXWebAssembly.instantiate("path/to/zxing_full.wasm", imports).then(
> ({ instance }) => successCallback(instance),
> );
> return {};
> },
> },
> });
> ```
>
> Note that WeChat mini programs use `WXWebAssembly` instead of the standard `WebAssembly`, and the first argument in `WXWebAssembly.instantiate` should point to the location where the `zxing_full.wasm` file was moved earlier.
>
> 4. This library uses a bare minimum `Blob` polyfill in the mini program environment so that no errors will be thrown if you call `writeBarcode`. However, it's recommended to use a full-fledged `Blob` polyfill for not breaking other parts of your program.

> [!IMPORTANT]
>
> Each version of this library has a unique corresponding `.wasm` file. If you choose to serve it yourself, please ensure that the `.wasm` file matches the version of the `zxing-wasm` library you are using. Otherwise, you may encounter unexpected errors.
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
],
"main": "./dist/cjs/full/index.js",
"module": "./dist/es/full/index.js",
"miniprogram": "./dist/miniprogram",
"exports": {
".": {
"import": "./dist/es/full/index.js",
Expand Down Expand Up @@ -171,7 +172,8 @@
"build:es": "vite build",
"build:cjs": "tsx ./scripts/build-cjs.ts",
"build:iife": "tsx ./scripts/build-iife.ts",
"build": "conc \"pnpm:build:es\" \"pnpm:build:cjs\" \"pnpm:build:iife\"",
"build:miniprogram": "tsx ./scripts/build-miniprogram.ts",
"build": "conc \"pnpm:build:es\" \"pnpm:build:cjs\" \"pnpm:build:iife\" \"pnpm:build:miniprogram\"",
"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\"",
Expand All @@ -189,6 +191,7 @@
"@babel/types": "^7.26.9",
"@biomejs/biome": "1.9.4",
"@changesets/cli": "^2.28.1",
"@napi-rs/canvas": "^0.1.67",
"@types/babel__core": "^7.20.5",
"@types/node": "^22.13.5",
"@vitest/ui": "^3.0.6",
Expand Down
110 changes: 110 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions scripts/build-cjs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { writeFile } from "node:fs/promises";
import { rimraf } from "rimraf";
import { type LibraryOptions, build } from "vite";
import viteConfig from "../vite.config.js";

async function buildCjs() {
await rimraf("dist/cjs");
await build({
...viteConfig,
build: {
Expand Down
42 changes: 42 additions & 0 deletions scripts/build-miniprogram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { writeFile } from "node:fs/promises";
import { rimraf } from "rimraf";
import { type LibraryOptions, build } from "vite";
import viteConfig from "../vite.config.js";

async function buildMiniprogram() {
await rimraf("dist/miniprogram");
await build({
...viteConfig,
mode: "miniprogram",
build: {
...viteConfig.build,
target: ["es2018"],
lib: {
...(viteConfig.build?.lib as LibraryOptions),
entry: {
index: "./src/full/index.ts",
},
formats: ["cjs"],
},
outDir: "dist/miniprogram",
rollupOptions: {
...viteConfig.build?.rollupOptions,
output: {
...viteConfig.build?.rollupOptions?.output,
manualChunks: {},
},
},
},
configFile: false,
define: {
...viteConfig.define,
WebAssembly: "WXWebAssembly",
},
});
await writeFile(
"dist/miniprogram/package.json",
`${JSON.stringify({ type: "commonjs" }, undefined, 2)}\n`,
);
}

buildMiniprogram();
2 changes: 1 addition & 1 deletion src/full/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function setZXingModuleOverrides(
}

export async function readBarcodes(
input: Blob | ImageData,
input: Blob | ArrayBuffer | Uint8Array | ImageData,
readerOptions?: ReaderOptions,
) {
return readBarcodesWithFactory(zxingModuleFactory, input, readerOptions);
Expand Down
2 changes: 1 addition & 1 deletion src/reader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function setZXingModuleOverrides(
}

export async function readBarcodes(
input: Blob | ImageData,
input: Blob | ArrayBuffer | Uint8Array | ImageData,
readerOptions?: ReaderOptions,
) {
return readBarcodesWithFactory(zxingModuleFactory, input, readerOptions);
Expand Down
Loading