Skip to content

Commit

Permalink
fix: compile wasm package for browser (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
lsndr committed Sep 30, 2024
1 parent 939b657 commit b9e5822
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
4 changes: 2 additions & 2 deletions browser.replacer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports.default = ({ orig, file }) => {
if (!file.includes('dist/browser') || orig !== 'require("@lib")') {
if (!file.includes('dist/browser') || orig !== "from '@lib'") {
return orig;
}

return 'require("@rrule-rust/lib-wasm32-wasi")';
return "from '@rrule-rust/lib-wasm32-wasi'";
};
9 changes: 5 additions & 4 deletions npm/wasm32-wasi/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"name": "@rrule-rust/lib-wasm32-wasi",
"version": "0.0.0",
"cpu": [
"wasm32"
],
"main": "rrule-rust.wasi.cjs",
"files": [
"rrule-rust.wasm32-wasi.wasm",
Expand All @@ -24,9 +21,13 @@
"repository": "lsndr/rrule-rust",
"browser": "rrule-rust.wasi-browser.js",
"dependencies": {
"@napi-rs/wasm-runtime": "^0.2.4"
"@napi-rs/wasm-runtime": "=0.2.4",
"patch-package": "^8.0.0"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"postinstall": "patch-package"
}
}
28 changes: 28 additions & 0 deletions npm/wasm32-wasi/patches/@napi-rs+wasm-runtime+0.2.4.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
diff --git a/node_modules/@napi-rs/wasm-runtime/dist/runtime.js b/node_modules/@napi-rs/wasm-runtime/dist/runtime.js
index e06c158..a23882c 100644
--- a/node_modules/@napi-rs/wasm-runtime/dist/runtime.js
+++ b/node_modules/@napi-rs/wasm-runtime/dist/runtime.js
@@ -2532,7 +2532,22 @@ function createNapiModule(options) {
}
return str;
}
- return emnapiString.utf8Decoder.decode(((typeof SharedArrayBuffer === "function" && HEAPU8.buffer instanceof SharedArrayBuffer) || (Object.prototype.toString.call(HEAPU8.buffer.constructor) === "[object SharedArrayBuffer]")) ? HEAPU8.slice(ptr, end) : HEAPU8.subarray(ptr, end));
+ var decodeFromSharedBuffer = (sharedBuffer) => {
+ const decoder = new TextDecoder()
+ const copyLength = sharedBuffer.byteLength
+
+ const tempBuffer = new ArrayBuffer(copyLength)
+ const tempView = new Uint8Array(tempBuffer)
+
+ let sharedView = new Uint8Array(sharedBuffer)
+ if (sharedBuffer.byteLength != copyLength) {
+ sharedView = sharedView.subarray(0, copyLength)
+ }
+ tempView.set(sharedView)
+
+ return decoder.decode(tempBuffer)
+ }
+ return decodeFromSharedBuffer(((typeof SharedArrayBuffer === "function" && HEAPU8.buffer instanceof SharedArrayBuffer) || (Object.prototype.toString.call(HEAPU8.buffer.constructor) === "[object SharedArrayBuffer]")) ? HEAPU8.slice(ptr, end) : HEAPU8.subarray(ptr, end));
},
stringToUTF8: function (str, outPtr, maxBytesToWrite) {
var HEAPU8 = new Uint8Array(wasmMemory.buffer);
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"name": "rrule-rust",
"version": "0.0.0",
"main": "dist/node/index.js",
"types": "dist/node/index.d.ts",
"browser": "dist/node/browser.js",
"browser": "dist/browser/index.js",
"keywords": [
"rrule",
"icalendar",
Expand Down Expand Up @@ -83,7 +82,7 @@
"build:node": "tsc -p ./tsconfig.build.json --outDir dist/node",
"postbuild:node": "cpy ./src/lib/* ./dist/node/lib --flat && tsc-alias -p tsconfig.replacer.json --replacer ./node.replacer.js",
"prebuild:browser": "rimraf ./dist/browser",
"build:browser": "tsc -p ./tsconfig.build.json --outDir dist/browser",
"build:browser": "tsc -p ./tsconfig.browser.json --outDir dist/browser",
"postbuild:browser": "cpy ./src/lib/* ./dist/browser/lib --flat && tsc-alias -p tsconfig.replacer.json --replacer ./browser.replacer.js",
"build": "npm run build:node && npm run build:browser",
"prepublishOnly": "napi prepublish -t npm",
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.browser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler"
},
"extends": "./tsconfig.build.json",
"exclude": ["./src/lib"]
}

0 comments on commit b9e5822

Please sign in to comment.