forked from matrix-org/matrix-rich-text-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load wasm via fetch rather than base64 embedding
Requires splitting into two packages to make use of vite's externals until something like vitejs/vite#9734 lands Gets rid of hack_myfetch.js Replaces hack_exports.js with vite plugin options Stops copying generated wasm-pack files around Switches the packages to ESM Simplifies scripts and updates some dependencies for necessary features Fixes types and ditches a lot of ts disablements
- Loading branch information
Showing
27 changed files
with
591 additions
and
575 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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
Copyright 2024 New Vector Ltd. | ||
SPDX-License-Identifier: AGPL-3.0-only | ||
Please see LICENSE in the repository root for full details. | ||
*/ | ||
|
||
export * from './pkg/wysiwyg.d'; | ||
|
||
/** | ||
* Load the WebAssembly module in the background, if it has not already been loaded. | ||
* | ||
* Returns a promise which will resolve once the other methods are ready. | ||
* | ||
* @returns {Promise<void>} | ||
*/ | ||
export default function initAsync(): Promise<void>; |
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
Copyright 2024 New Vector Ltd. | ||
SPDX-License-Identifier: AGPL-3.0-only | ||
Please see LICENSE in the repository root for full details. | ||
*/ | ||
|
||
// @ts-check | ||
|
||
/** | ||
* This is the entrypoint on non-node ESM environments (such as Element Web). | ||
* `asyncLoad` will load the WASM module using a `fetch` call. | ||
*/ | ||
|
||
import * as bindings from './pkg/wysiwyg_bg.js'; | ||
|
||
const moduleUrl = new URL( | ||
'./pkg/wysiwyg_bg.wasm?url', | ||
import.meta.url, | ||
); | ||
|
||
// We want to throw an error if the user tries to use the bindings before | ||
// calling `initAsync`. | ||
bindings.__wbg_set_wasm( | ||
new Proxy( | ||
{}, | ||
{ | ||
get() { | ||
throw new Error( | ||
'@element-hq/matrix-wysiwyg was used before it was initialized. Call `initAsync` first.', | ||
); | ||
}, | ||
}, | ||
), | ||
); | ||
|
||
/** | ||
* Stores a promise of the `loadModule` call | ||
* @type {Promise<void> | null} | ||
*/ | ||
let modPromise = null; | ||
|
||
/** | ||
* Loads the WASM module asynchronously | ||
* | ||
* @returns {Promise<void>} | ||
*/ | ||
async function loadModule() { | ||
let mod; | ||
if (typeof WebAssembly.compileStreaming === 'function') { | ||
mod = await WebAssembly.compileStreaming(fetch(moduleUrl)); | ||
} else { | ||
// Fallback to fetch and compile | ||
const response = await fetch(moduleUrl); | ||
if (!response.ok) { | ||
throw new Error(`Failed to fetch wasm module: ${moduleUrl}`); | ||
} | ||
const bytes = await response.arrayBuffer(); | ||
mod = await WebAssembly.compile(bytes); | ||
} | ||
|
||
const instance = await WebAssembly.instantiate(mod, { | ||
'./wysiwyg_bg.js': bindings, | ||
}); | ||
|
||
bindings.__wbg_set_wasm(instance.exports); | ||
instance.exports.__wbindgen_start(); | ||
} | ||
|
||
/** | ||
* Load the WebAssembly module in the background, if it has not already been loaded. | ||
* | ||
* Returns a promise which will resolve once the other methods are ready. | ||
* | ||
* @returns {Promise<void>} | ||
*/ | ||
export default async function initAsync() { | ||
if (!modPromise) modPromise = loadModule(); | ||
await modPromise; | ||
} | ||
|
||
// Re-export everything from the generated javascript wrappers | ||
export * from './pkg/wysiwyg_bg.js'; |
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,9 +1,10 @@ | ||
{ | ||
"name": "wysiwyg-wasm", | ||
"name": "@vector-im/matrix-wysiwyg-wasm", | ||
"version": "2.37.14", | ||
"homepage": "https://gitlab.com/andybalaam/wysiwyg-rust", | ||
"description": "WASM bindings for wysiwyg-rust", | ||
"license": "AGPL-3.0", | ||
"type": "module", | ||
"collaborators": [ | ||
"Andy Balaam <[email protected]>" | ||
], | ||
|
@@ -17,13 +18,21 @@ | |
"messaging", | ||
"wysiwyg" | ||
], | ||
"main": "wysiwyg.js", | ||
"types": "pkg/wysiwyg.d.ts", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"exports": { | ||
"." : { | ||
"import": "./index.js", | ||
"types": "./index.d.ts" | ||
} | ||
}, | ||
"files": [ | ||
"pkg/wysiwyg_bg.wasm", | ||
"pkg/wysiwyg_bg.wasm.d.ts", | ||
"pkg/wysiwyg.js", | ||
"pkg/wysiwyg.d.ts" | ||
"pkg/wysiwyg.d.ts", | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"devDependencies": { | ||
"wasm-pack": "^0.13.0", | ||
|
@@ -34,8 +43,8 @@ | |
"node": ">= 10" | ||
}, | ||
"scripts": { | ||
"dev-build": "WASM_BINDGEN_WEAKREF=1 wasm-pack build --profiling --target web --out-name wysiwyg --out-dir ./pkg", | ||
"build": "RUSTFLAGS='-C opt-level=s' WASM_BINDGEN_WEAKREF=1 wasm-pack build --release --target web --out-name wysiwyg --out-dir ./pkg", | ||
"dev-build": "WASM_BINDGEN_WEAKREF=1 wasm-pack build --profiling --target bundler --out-name wysiwyg --out-dir ./pkg", | ||
"build": "RUSTFLAGS='-C opt-level=s' WASM_BINDGEN_WEAKREF=1 wasm-pack build --release --target bundler --out-name wysiwyg --out-dir ./pkg", | ||
"test": "jest --verbose", | ||
"doc": "typedoc --tsconfig ." | ||
} | ||
|
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
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
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
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
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
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
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
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
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
Oops, something went wrong.