Skip to content

Commit e1f11c9

Browse files
committed
Fix #10 and #11: provide ESM/CJS modules for Node/browsers
Test various bundling configurations with Webpack, Rollup and esbuild and test the resulting bundles.
1 parent bbddd7b commit e1f11c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+14387
-4992
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help me improve barcode-detector-polyfill
4+
title: "[BUG]"
5+
labels: bug
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
12+
A clear and concise description of what the bug is.
13+
14+
**To Reproduce**
15+
16+
A minimal working example that reproduces the behavior would be much appreciated.
17+
18+
Or else the environment in which the behavior occurs needs to be specified:
19+
- Which target environment (Node/browser/other)?
20+
- Which type of modules (ESM/CommonJS/other)?
21+
- Which framework(s)?
22+
- Which devtools, including their configuration files?
23+
- `package.json` content
24+
- Any other context about the problem
25+
26+
**Expected behavior**
27+
A clear and concise description of what you expected to happen.
28+
29+
**Screenshots**
30+
If applicable, add screenshots to help explain your problem.

.testcaferc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"src": "tests/bundling-browser.test.js",
3+
"browsers": [ "chromium:headless" ],
4+
"hostname": "localhost"
5+
}

Makefile

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ ZBAR_SRC = zbar-$(ZBAR_VERSION)
44
SRC = src
55
BUILD = build
66
DIST = dist
7-
TEST = test
8-
TEST_TS = $(wildcard ./$(TEST)/*.ts)
9-
TEST_JS = $(patsubst ./$(TEST)/%.ts,./$(BUILD)/%.js,$(TEST_TS))
10-
TEST_COVERAGE = ./coverage
7+
INLINED = $(DIST)/inlined
8+
TESTS = tests
9+
TESTS_SRC = $(wildcard ./$(TESTS)/[0123456789]*.test.ts)
10+
TESTS_BUILT = $(patsubst ./$(TESTS)/%.ts,./$(BUILD)/%.js,$(TESTS_SRC))
11+
TESTS_COVERAGE = ./coverage
1112

12-
EM_VERSION = 3.1.30
13+
EM_VERSION = 3.1.44
1314
EM_OPTS = --rm -w /$(SRC) -v $$PWD:/$(SRC) emscripten/emsdk:$(EM_VERSION)
1415
EM_DOCKER = docker run -u $(shell id -u):$(shell id -g) $(EM_OPTS)
1516
EM_PODMAN = podman run $(EM_OPTS)
@@ -29,7 +30,10 @@ EMCC_FLAGS = -Oz -Wall -Werror -s ALLOW_MEMORY_GROWTH=1 \
2930
-s EXPORTED_FUNCTIONS="['_malloc','_free']" \
3031
-s MODULARIZE=1 -s EXPORT_NAME=zbarWasm
3132

32-
BUNDLES = $(DIST)/main.js $(DIST)/main.cjs $(DIST)/index.js
33+
LOADERS = $(BUILD)/zbar.js $(BUILD)/zbar.mjs $(BUILD)/zbar-inlined.js $(BUILD)/zbar-inlined.mjs
34+
35+
BUNDLES = $(DIST)/main.mjs $(DIST)/main.cjs $(DIST)/index.js $(DIST)/index.mjs \
36+
$(INLINED)/main.mjs $(INLINED)/main.cjs $(INLINED)/index.js $(INLINED)/index.mjs
3337

3438
TSC = npx tsc
3539
TSC_FLAGS = -p ./tsconfig.test.json
@@ -41,35 +45,39 @@ ROLLUP_FLAGS = -c
4145

4246
all: dist test
4347

44-
test: dist $(TEST_JS)
45-
jest --config ./jest.config.cjs --coverage
48+
test: dist $(TESTS_BUILT)
49+
node --experimental-vm-modules node_modules/jest/bin/jest.js --config ./jest.config.cjs --runInBand --coverage
4650

4751
dist: $(BUNDLES) $(DIST)/zbar.wasm
4852

4953
clean-build:
50-
-rm -rf $(DIST) $(BUILD)
54+
-rm -rf $(DIST) $(BUILD) undecaf-zbar-wasm-*.tgz $(TESTS)/node_modules $(TESTS)/build $(TESTS)/package-lock.json
5155

5256
clean: clean-build
5357
-rm $(ZBAR_SRC).tar.gz
54-
-rm -rf $(ZBAR_SRC) $(TEST_COVERAGE)
58+
-rm -rf $(ZBAR_SRC) $(TESTS_COVERAGE)
5559

56-
$(TEST_JS): $(TEST_TS) $(BUNDLES) tsconfig.json tsconfig.test.json
60+
$(TESTS_BUILT): $(TESTS)/* $(BUNDLES) tsconfig.test.json jest.config.cjs .testcaferc.json
5761
$(TSC) $(TSC_FLAGS)
5862

59-
$(BUNDLES): $(BUILD)/zbar.js $(BUILD)/zbar.mjs $(SRC)/*.ts tsconfig.json rollup.config.js package.json
63+
$(BUNDLES): $(LOADERS) $(DIST)/zbar.wasm $(SRC)/*.ts tsconfig.json rollup.config.js package.json
64+
mkdir -p $(DIST)/
6065
$(ROLLUP) $(ROLLUP_FLAGS)
66+
npm pack
6167

6268
$(DIST)/zbar.wasm: $(BUILD)/zbar.wasm
6369
mkdir -p $(DIST)/
6470
cp $(BUILD)/zbar.wasm $(DIST)/
6571

66-
$(BUILD)/zbar.wasm $(BUILD)/zbar.js $(BUILD)/zbar.mjs: $(ZBAR_DEPS) $(SRC)/module.c $(BUILD)/symbol.test.o
72+
$(BUILD)/zbar.wasm $(LOADERS): $(ZBAR_DEPS) $(SRC)/module.c $(BUILD)/symbol.test.o
6773
$(EMCC) $(EMCC_FLAGS) -o $(BUILD)/zbar.js $(SRC)/module.c $(ZBAR_INC) $(ZBAR_OBJS)
68-
$(EMCC) $(EMCC_FLAGS) -o $(BUILD)/zbar.mjs $(SRC)/module.c $(ZBAR_INC) $(ZBAR_OBJS)
74+
$(EMCC) $(EMCC_FLAGS) -o $(BUILD)/zbar.mjs -sEXPORT_ES6 $(SRC)/module.c $(ZBAR_INC) $(ZBAR_OBJS)
75+
$(EMCC) $(EMCC_FLAGS) -o $(BUILD)/zbar-inlined.js -sSINGLE_FILE $(SRC)/module.c $(ZBAR_INC) $(ZBAR_OBJS)
76+
$(EMCC) $(EMCC_FLAGS) -o $(BUILD)/zbar-inlined.mjs -sEXPORT_ES6 -sSINGLE_FILE $(SRC)/module.c $(ZBAR_INC) $(ZBAR_OBJS)
6977

70-
$(BUILD)/symbol.test.o: $(ZBAR_DEPS) $(TEST)/symbol.test.c
78+
$(BUILD)/symbol.test.o: $(ZBAR_DEPS) $(TESTS)/symbol.test.c
7179
mkdir -p $(BUILD)/
72-
$(EMCC) -Wall -Werror -g2 -c $(TEST)/symbol.test.c -o $@ $(ZBAR_INC)
80+
$(EMCC) -Wall -Werror -g2 -c $(TESTS)/symbol.test.c -o $@ $(ZBAR_INC)
7381

7482
$(ZBAR_DEPS): $(ZBAR_SRC)/Makefile
7583
cd $(ZBAR_SRC) && $(EMMAKE) make CFLAGS=-Os CXXFLAGS=-Os \

README.md

Lines changed: 90 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# A WebAssembly build of the ZBar Bar Code Reader
22

3-
![Install size](https://badgen.net/packagephobia/install/@undecaf/zbar-wasm?color=42cc24)
43
![Open issues](https://badgen.net/github/open-issues/undecaf/zbar-wasm)
54
![Vulnerabilities](https://snyk.io/test/npm/@undecaf/zbar-wasm/badge.svg)
65
![Total downloads](https://badgen.net/npm/dt/@undecaf/zbar-wasm)
@@ -15,6 +14,7 @@ of the [ZBar Bar Code Reader](https://github.com/mchehab/zbar) written in C/C++.
1514

1615
+ Provided as minified ES module, CommonJS module and plain script
1716
+ Runs in modern browsers, in Node.js and also in workers
17+
+ Deployment size approx. 330 kByte
1818
+ Supports Code-39, Code-93, Code-128, Codabar, Databar/Expanded,
1919
EAN/GTIN-5/8/13, ISBN-10/13, ISBN-13+2, ISBN-13+5, ITF (Interleaved 2 of 5), QR Code, UPC-A/E.
2020
+ Detects multiple barcodes per frame, also with different types
@@ -23,6 +23,8 @@ of the [ZBar Bar Code Reader](https://github.com/mchehab/zbar) written in C/C++.
2323
RGB/grayscale [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) objects
2424
+ Outperforms pure JavaScript barcode scanners
2525

26+
:warning: v0.10.0 contains breaking changes regarding bundling, please refer to section [Bundling/deploying zbar-wasm](#bundling-deploying-zbar-wasm).
27+
2628

2729
## Examples based on zbar-wasm
2830

@@ -48,11 +50,11 @@ An example that scans a static image file:
4850
<!DOCTYPE html>
4951
<html>
5052
<body>
51-
<img id="img" crossorigin="anonymous" src="https://raw.githubusercontent.com/undecaf/zbar-wasm/master/test/img/qr_code.png">
53+
<img id="img" crossorigin="anonymous" src="https://raw.githubusercontent.com/undecaf/zbar-wasm/master/tests/img/qr_code.png">
5254
<pre id="result"></pre>
5355

5456
<script type="module">
55-
import * as zbarWasm from 'https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.9.16/dist/main.js'
57+
import * as zbarWasm from 'https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.10.0/dist/main.js'
5658
5759
(async () => {
5860
const
@@ -86,28 +88,28 @@ Almost identical to the snippet above, just replace the lines
8688
```html
8789
8890
<script type="module">
89-
import * as zbarWasm from 'https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.9.16/dist/main.js'
91+
import * as zbarWasm from 'https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.10.0/dist/main.js'
9092
9193
```
9294
9395
with
9496
9597
```html
9698
97-
<script src="https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.9.16/dist/index.js"></script>
99+
<script src="https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.10.0/dist/index.js"></script>
98100
<script>
99101
100102
```
101103
102104
103-
### Including zbar-wasm as ESM or as CommonJS module
105+
### Using zbar-wasm as an ESM or CommonJS module in Node.js
104106
105107
Installing:
106108
107109
```shell script
108-
$ npm install @undecaf/zbar-wasm@0.9.16
110+
$ npm install @undecaf/zbar-wasm@0.10.0
109111
or
110-
$ yarn add @undecaf/zbar-wasm@0.9.16
112+
$ yarn add @undecaf/zbar-wasm@0.10.0
111113
```
112114
113115
Using:
@@ -117,42 +119,100 @@ Using:
117119
118120
Please refer to the [API documentation](#api-documentation) for what can be imported/required.
119121
120-
A simple Node.js example that scans a static image file:
122+
A simple ES module that scans a static image file:
121123
122124
```javascript
123-
const { createCanvas, loadImage } = require('canvas');
124-
const { scanImageData } = require('@undecaf/zbar-wasm');
125+
import { createCanvas, loadImage } from 'canvas';
126+
import { scanImageData } from '@undecaf/zbar-wasm';
125127
126128
(async (url) => {
127129
const
128-
img = await loadImage(url),
129-
canvas = createCanvas(img.width, img.height),
130-
ctx = canvas.getContext('2d');
130+
img = await loadImage(url),
131+
canvas = createCanvas(img.width, img.height),
132+
ctx = canvas.getContext('2d');
131133
132-
ctx.drawImage(img, 0, 0);
134+
ctx.drawImage(img, 0, 0)
133135
134136
const
135-
imageData = ctx.getImageData(0, 0, img.width, img.height),
136-
symbols = await scanImageData(imageData);
137+
imageData = ctx.getImageData(0, 0, img.width, img.height),
138+
// @ts-ignore
139+
symbols = await scanImageData(imageData);
137140
138-
console.log(symbols[0].typeName, symbols[0].decode());
139-
}) ('https://raw.githubusercontent.com/undecaf/zbar-wasm/master/test/img/qr_code.png');
141+
console.log(ssymbols[0]?.typeName, ymbols[0]?.decode())
142+
})('https://raw.githubusercontent.com/undecaf/zbar-wasm/master/tests/img/qr_code.png')
140143
```
141144
142-
### Bundling/deploying zbar-wasm
145+
For a CommonJS module, just replace the first lines with
143146
144-
zbar-wasm loads the WebAssembly file `zbar.wasm` at runtime. `zbar.wasm` must be located in the same directory
145-
as the zbar-wasm `<script>` or module, be it on a file system or at a remote endpoint.
147+
```javascript
148+
const { createCanvas, loadImage } = require('canvas');
149+
const { scanImageData } = require('@undecaf/zbar-wasm');
150+
```
146151
147-
This must be observed when bundling zbar-wasm or deploying it to a server:
148152
149-
+ `@undecaf/zbar-wasm/dist/zbar.wasm` must be copied as-is (e.g. using [`copy-webpack-plugin`](https://www.npmjs.com/package/copy-webpack-plugin),
150-
[`rollup-plugin-copy`](https://www.npmjs.com/package/rollup-plugin-copy), [`esbuild-plugin-copy`](https://www.npmjs.com/package/esbuild-plugin-copy)
151-
or similar).
152-
+ `zbar.wasm` must be copied to the directory where the zbar-wasm module/the bundle containing that module is located.
153-
+ It should be served as `application/wasm` so that it can be compiled in parallel with being received
154-
by the browser.
153+
### Bundling/deploying zbar-wasm
155154
155+
zbar-wasm delegates barcode scanning to the WebAssembly code in file `zbar.wasm`.
156+
This file is part of the package and can be provided at runtime in different ways:
157+
158+
+ It can be loaded from a CDN by browsers.
159+
+ It can be bundled as an asset. That asset should be served to browsers as `application/wasm`
160+
+ so that it can be compiled in parallel with being received.
161+
+ zbar-wasm also provides modules that contain `zbar.wasm` as inline data.
162+
163+
The [package entry points](https://nodejs.org/docs/latest-v16.x/api/packages.html#package-entry-points)
164+
of zbar-wasm have been chosen so that bundlers will select the
165+
appropriate module by default in most cases. `zbar.wasm` as inline data requires an
166+
[export condition](https://nodejs.org/docs/latest-v16.x/api/packages.html#conditional-exports)
167+
in the bundler configuration.
168+
169+
The build process of zbar-wasm tests bundling with
170+
[Webpack](https://webpack.js.org/), [Rollup](https://rollupjs.org/) and
171+
[esbuild](https://esbuild.github.io/) and also tests the resulting bundles.
172+
The bundler configuration files
173+
[`tests/{webpack,rollup,esbuild}.config.js`](https://github.com/undecaf/zbar-wasm/tree/master/tests)
174+
may be used as a reference of how to achieve a particular bundling result. They cover the following
175+
combinations of platform, module type and `zbar.wasm` provisioning:
176+
177+
<table>
178+
<thead>
179+
<tr>
180+
<th style='text-align: left;'>Platform &rarr;</th>
181+
<th colspan='2'>Node.js</th>
182+
<th colspan='2'>Browser</th>
183+
</tr>
184+
<tr>
185+
<th style='text-align: left;'><code>zbar.wasm</code>&darr;</th>
186+
<th>ESM</th>
187+
<th>CommonJS</th>
188+
<th>ESM</th>
189+
<th>Script</th>
190+
</tr>
191+
</thead>
192+
<tbody>
193+
<tr>
194+
<th style='text-align: left;'>from CDN</th>
195+
<td></td>
196+
<td></td>
197+
<td>:white_check_mark:</td>
198+
<td>:white_check_mark:</td>
199+
</tr>
200+
<tr>
201+
<th style='text-align: left;'>bundled</th>
202+
<td>:white_check_mark:</td>
203+
<td>:white_check_mark:</td>
204+
<td>:white_check_mark:</td>
205+
<td></td>
206+
</tr>
207+
<tr>
208+
<th style='text-align: left;'>inlined</th>
209+
<td>:white_check_mark:</td>
210+
<td>:white_check_mark:</td>
211+
<td>:white_check_mark:</td>
212+
<td>:white_check_mark:</td>
213+
</tr>
214+
</tbody>
215+
</table>
156216
157217
## API documentation
158218
@@ -183,7 +243,7 @@ Prerequisites:
183243
+ A Linux platform
184244
+ GNU `make`, `tar` and `curl`
185245
+ [Docker](https://www.docker.com/) or [Podman](https://podman.io/)
186-
+ [Node.js](https://nodejs.org/) v12+
246+
+ [Node.js](https://nodejs.org/) v16+
187247
188248
To build:
189249

docs/example/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ <h5>Result</h5>
6363
</div>
6464
</div>
6565

66-
<script src="https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.9.16/dist/index.js"></script>
66+
<script src="https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.10.0/dist/index.js"></script>
6767
<script src="js/main.js"></script>
6868

6969
</body>

jest.config.cjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ module.exports = {
44
'node_modules', 'dist'
55
],
66
transform: {
7-
'\\.wasm$': './test/jestFileTransformer.cjs',
7+
'\\.wasm$': './tests/jestFileTransformer.cjs',
88
},
99
testEnvironment: 'node',
10-
testTimeout: 15000,
11-
}
10+
testTimeout: 5000,
11+
testSequencer: './tests/testSequencer.cjs',
12+
}

0 commit comments

Comments
 (0)