|
| 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 | +``` |
0 commit comments