You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example demonstrates how to use *itk-wasm*in a web browser application built with [Vite](https://vitejs.dev/). Find the code in [itk-wasm/examples/Vite](https://github.com/InsightSoftwareConsortium/ITK-Wasm/tree/main/examples/Vite).
3
+
This example demonstrates how to use an ITK-wasm package in a web browser application bundled with [Vite](https://vitejs.dev/).
4
4
5
-
*itk-wasm***asynchronously** downloads web worker JavaScript and WebAssembly Emscripten modules **on demand**. For *itk-wasm* to work:
5
+
Find the full example in the `ITK-Wasm/examples/vite`[directory of the GitHub repository](https://github.com/InsightSoftwareConsortium/ITK-Wasm/tree/main/examples/vite).
6
6
7
-
* Copy *itk-wasm* Javascript and WebAssembly files to a public directory
8
-
* Tell *itk-wasm* the location to download the Javascript and WebAssembly files in the public directory
7
+
ITK-Wasm **asynchronously** downloads web worker JavaScript and WebAssembly Emscripten modules **on demand**. This allows the main application to load quickly, while the ITK-Wasm modules are loaded in the background when needed. It also allows the application to use only the ITK-Wasm modules that it needs, rather than loading all of them at once. Finally, computation-intensive tasks can be offloaded to web workers, which run in a separate thread from the main application, preventing the UI from freezing during long-running tasks.
9
8
10
-
## Copy *itk-wasm* Javascript and WebAssembly files to a public directory
9
+
A few steps are required to configure Vite to work with ITK-Wasm packages:
11
10
12
-
In the Vite example, `vite.config.js` uses `vite-plugin-static-copy` to move prebuilt *itk-wasm* files to the `/dist` directory.
11
+
1. Copy ITK-Wasm Javascript and WebAssembly assets to a public directory.
12
+
2. Tell ITK-Wasm the location of the assets.
13
+
3. Prevent Vite from pre-bundling ITK-Wasm packages.
14
+
15
+
## Copy Javascript and WebAssembly assets to a public directory
16
+
17
+
In the Vite example, `vite.config.js` uses `vite-plugin-static-copy` to copy prebuilt *ITK-Wasm* pipeline assets to the `/dist` directory.
18
+
19
+
```sh
20
+
npm install @itk-wasm/image-io
21
+
npm install -D vite-plugin-static-copy
22
+
```
13
23
14
24
```js
15
25
import { defineConfig } from'vite'
@@ -20,84 +30,99 @@ export default defineConfig({
20
30
// put lazy loaded JavaScript and Wasm bundles in dist directory
The Vite config copies *web-workers* directory, which asynchronously perform IO or runs processing pipelines in a background thread.
44
+
## Tell ITK-Wasm the location of the WebAssembly assets
45
+
46
+
Call `setPipelinesBaseUrl` to tell the ITK-Wasm package where to find the WebAssembly assets. This is typically done in the main JavaScript file of your application, after importing the ITK-Wasm package.
41
47
42
-
The config copies the complete *image-io* and *mesh-io* directories. You may want to copy a subset of *image-io* or *mesh-io* files, based on what features you use of *itk-wasm*.
// Use app-vendored WebAssembly module assets copied by viteStaticCopy
53
+
constviteBaseUrl=import.meta.env.BASE_URL || "/";
54
+
constpipelinesBaseUrl=newURL(
55
+
`${viteBaseUrl}pipelines`,
56
+
document.location.origin
57
+
).href;
58
+
setPipelinesBaseUrl(pipelinesBaseUrl);
59
+
60
+
[...]
61
+
// Call the readImage function from the package
62
+
const { image } =awaitreadImage(files[0]);
63
+
[...]
64
+
```
43
65
44
-
## Tell *itk-wasm* the location to download the Javascript and WebAssembly files
66
+
## Prevent Vite from pre-bundling ITK-Wasm packages
45
67
46
-
To change the location of the *itk-wasm*web worker and Emscripten modules, configure Vite's `resolve.alias` setting.
68
+
Ensure that Vite does try to pre-bundle the ITK-Wasm packages, which can break lazy loading of the web worker and Emscripten modules. This is done by adding associated packages the `optimizeDeps.exclude` array in `vite.config.js`. This is more important when an ITK-Wasm package is a transitive dependency.
0 commit comments