From b9e5822ef831142f712c89003a8515ee04e7e118 Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 1 Oct 2024 02:55:03 +0700 Subject: [PATCH] fix: compile wasm package for browser (#302) --- browser.replacer.js | 4 +-- npm/wasm32-wasi/package.json | 9 +++--- .../patches/@napi-rs+wasm-runtime+0.2.4.patch | 28 +++++++++++++++++++ package.json | 5 ++-- tsconfig.browser.json | 8 ++++++ 5 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 npm/wasm32-wasi/patches/@napi-rs+wasm-runtime+0.2.4.patch create mode 100644 tsconfig.browser.json diff --git a/browser.replacer.js b/browser.replacer.js index 5156be6..bc0b332 100644 --- a/browser.replacer.js +++ b/browser.replacer.js @@ -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'"; }; diff --git a/npm/wasm32-wasi/package.json b/npm/wasm32-wasi/package.json index 39a6de7..bbd67a9 100644 --- a/npm/wasm32-wasi/package.json +++ b/npm/wasm32-wasi/package.json @@ -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", @@ -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" } } diff --git a/npm/wasm32-wasi/patches/@napi-rs+wasm-runtime+0.2.4.patch b/npm/wasm32-wasi/patches/@napi-rs+wasm-runtime+0.2.4.patch new file mode 100644 index 0000000..5c578a2 --- /dev/null +++ b/npm/wasm32-wasi/patches/@napi-rs+wasm-runtime+0.2.4.patch @@ -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); diff --git a/package.json b/package.json index a668955..4a788cb 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/tsconfig.browser.json b/tsconfig.browser.json new file mode 100644 index 0000000..58b5daf --- /dev/null +++ b/tsconfig.browser.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "module": "esnext", + "moduleResolution": "bundler" + }, + "extends": "./tsconfig.build.json", + "exclude": ["./src/lib"] +}