-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Let the qgis-js loader find the relatrive runtime files from…
… "assets/wasm" in order to use it from a CDN
- Loading branch information
Showing
3 changed files
with
113 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,62 @@ | ||
# Bundling | ||
|
||
<!-- FIXME: Write up on how to bundle .wasm assets with Vite and Webpack --> | ||
qgis-js consists of two parts: The runtime generated by Emscripten and the TypeScript/JavaScript API, that can be seen as a wrapper around the runtime. The wrapper can be imported, used and bundled (e.g. tree-shaked) like any other JavaScript library. But it is important that the runtime is not modified and served as is. | ||
|
||
See the [`qgis-js` Package `README.md`](../packages/qgis-js/README.md) for more information about the files involved. Everything inside `assets/wasm` is part of the runtime. | ||
|
||
To not confuse any downstream bundler, the runtime is [dynamically loaded](../packages/qgis-js/src/loader.ts) in a way that it will not be processed. Therefore **it is up to the end user to include the runtime files in the final build**. | ||
|
||
### Explicitly specifying the runtime location | ||
|
||
The runtime location can be specified with the `prefix` configuration option. This is useful when the runtime is not in the same directory as the main script or served from a different server (e.g. a CDN). | ||
|
||
```js | ||
const { api } = await qgis({ | ||
prefix: "/path/to/runtime/assets", | ||
}); | ||
``` | ||
|
||
## Examples | ||
|
||
### qgis-js with [Vite](https://vitejs.dev/) | ||
|
||
One can use the [vite-plugin-static-copy](https://github.com/sapphi-red/vite-plugin-static-copy). | ||
|
||
For an example see the [`vite.config.ts`](./examples/qgis-js-example-ol/vite.config.js) in the [qgis-js-example-ol](./examples/qgis-js-example-ol) project and note that the COOP/COEP headers have to be set after the plugin (see [`compatibility.md`](./compatibility.md) for more information). | ||
|
||
### qgis.js with [Webpack](https://webpack.js.org/) | ||
|
||
With Webpack one can use the [copy-webpack-plugin](https://www.npmjs.com/package/copy-webpack-plugin). | ||
|
||
Note that the COOP/COEP headers have to be set in the `webpack.config.js` (see [`compatibility.md`](./compatibility.md) for more information). | ||
|
||
### Using qgis-js from a CDN | ||
|
||
An example of how to use qgis-js from a CDN (e.g. [jsDelivr](https://www.jsdelivr.com/)): | ||
|
||
```html | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>qgis-js</title> | ||
</head> | ||
<body> | ||
<script type="module"> | ||
// import qgis-js from a CDN | ||
const { qgis } = await import( | ||
"https://cdn.jsdelivr.net/npm/qgis-js/dist/qgis.js" | ||
); | ||
// boot the qgis-js runtime | ||
const { api } = await qgis(); | ||
// use the qgis-js api | ||
const rect = new api.Rectangle(1, 1, 42, 42); | ||
const center = rect.center(); | ||
console.log(`Center: x: ${center.x}, y: ${center.y}`); | ||
</script> | ||
</body> | ||
</html> | ||
``` | ||
|
||
Note that the main script has to be explicitly loaded with `qgis-js/dist/qgis.js` (Or a prefix pointing to `qgis-js/dist/assets/wasm` has to be set ([see above](#explicitly-specifying-the-runtime-location))). | ||
|
||
And also ensure that the HTML document has the correct COOP/COEP headers set (see [`compatibility.md`](./compatibility.md) for more information). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters