Skip to content

Commit 590844c

Browse files
authored
Merge pull request #1385 from thewtex/esm-docs
doc: add esm module documentation
2 parents a3a66b6 + a52d632 commit 590844c

9 files changed

Lines changed: 74 additions & 126 deletions

File tree

-176 KB
Binary file not shown.

docs/_static/umd/umd_example.gif

-239 KB
Binary file not shown.

docs/_static/umd/umd_run_tests.png

-38.9 KB
Binary file not shown.
-26.6 KB
Binary file not shown.
-80.3 KB
Binary file not shown.

docs/_static/videos/esm.webm

592 KB
Binary file not shown.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# ITK-Wasm ESM module from an HTML script tag
2+
3+
This example demonstrates how to use an ITK-Wasm package in a web browser application via its pre-built [ECMAScript Module (ESM)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules).
4+
5+
This is an alternative to bundling the modules with a bundler like Vite or ESBuild or using the package directly in a runtime like Node.js or Deno. In this example, we use an ITK-Wasm IO modules published on [NPM](https://npmjs.com), which is indexed by the [jsdelivr.com](https://jsdelivr.com) Content Delivery Network (CDN).
6+
7+
Find the full example in the `ITK-Wasm/examples/esm` [directory of the GitHub repository](https://github.com/InsightSoftwareConsortium/ITK-Wasm/tree/main/examples/esm).
8+
9+
----
10+
11+
Inside a JavaScript ESM `<script type="module">` tag, load the desired function from the ITK-Wasm package's ESM:
12+
13+
```html
14+
<script type="module">
15+
[...]
16+
import { readImage } from "https://cdn.jsdelivr.net/npm/@itk-wasm/image-io@1.6.0/dist/bundle/index-worker-embedded.min.js";
17+
```
18+
19+
The `dist/bundle/index-worker-embedded.min.js` file is a pre-built, minified ESM module that contains the ITK-Wasm package's JavaScript code with the associated Web Worker script embedded. The `@itk-wasm/image-io` package is an example of an ITK-Wasm IO module that can read images.
20+
21+
Inside body JavaScript code, use the imported function to read an image from a file input element:
22+
23+
```js
24+
[...]
25+
const { image } = await readImage(files[0]);
26+
```
27+
28+
These JavaScript ITK-Wasm components are used by the HTML, CSS, and JavaScript of [the full example](https://github.com/InsightSoftwareConsortium/ITK-Wasm/blob/main/examples/esm/dist/index.html).
29+
30+
## Simple HTTP server
31+
32+
Optionally, add an npm script that will start a local web server for development.
33+
34+
```shell
35+
npm init -y
36+
npm install --save-dev http-server
37+
```
38+
39+
Next, define a `start` command to start a local development web server in the *scripts* section of the `package.json` file,
40+
41+
```js
42+
"scripts": {
43+
"start": "http-server -c-1 ./dist/"
44+
},
45+
```
46+
47+
To start the development web server hosting the `./dist/` directory contents, run
48+
49+
```sh
50+
npm run start
51+
```
52+
53+
<video width="480" autoplay muted loop>
54+
<source src="../../_static/videos/esm.webm" type="video/webm">
55+
Sorry, your browser doesn't support embedded videos.
56+
</video>
57+
58+
## Testing with Playwright
59+
60+
The full example is configured for browser-based testing with the [Playwright](https://playwright.dev/) library.
61+
62+
To run the tests, first install Playwright,
63+
64+
```sh
65+
npm install
66+
npx playwright install --with-deps
67+
```
68+
69+
Then run the tests:
70+
71+
```sh
72+
npm run test
73+
```

docs/typescript/distribution/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ for deployment that rely on [ITK-Wasm packages](../../introduction/packages.md).
77
:maxdepth: 1
88
:caption: 📦 Distribution
99
10-
umd.md
10+
esm.md
1111
vite.md
1212
webpack.md
1313
node.md

docs/typescript/distribution/umd.md

Lines changed: 0 additions & 125 deletions
This file was deleted.

0 commit comments

Comments
 (0)