Skip to content

Commit

Permalink
Add ESM export for Node (#388)
Browse files Browse the repository at this point in the history
* Add ESM export for Node

* Add changeset
  • Loading branch information
rygine authored Dec 19, 2023
1 parent 203bfcb commit 78e5b9f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
5 changes: 5 additions & 0 deletions user_preferences_bindings_wasm/.changeset/red-beans-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xmtp/user-preferences-bindings-wasm": patch
---

Add ESM export for Node
8 changes: 4 additions & 4 deletions user_preferences_bindings_wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "npm run clean && npm run build:web && npm run build:bundler && npm run build:node && npm run clean:gitignore",
"build:web": "wasm-pack build --target web --no-pack --release --out-dir ./dist/web",
"build:bundler": "wasm-pack build --target bundler --no-pack --release --out-dir ./dist/bundler",
"build:node": "wasm-pack build --target nodejs --no-pack --release --out-dir ./dist/node",
"build:node": "wasm-pack build --target nodejs --no-pack --release --out-dir ./dist/node && node ./scripts/targetESM.js",
"prepublishOnly": "npm run build",
"clean": "rm -rf ./dist",
"clean:gitignore": "rm -f ./dist/**/.gitignore"
Expand All @@ -21,12 +21,12 @@
"./dist/**/*"
],
"main": "dist/node/user_preferences_bindings_wasm.js",
"module": "dist/bundler/user_preferences_bindings_wasm.js",
"module": "dist/node/user_preferences_bindings_wasm.mjs",
"types": "dist/node/user_preferences_bindings_wasm.d.ts",
"exports": {
".": {
"types": "./dist/bundler/user_preferences_bindings_wasm.d.ts",
"import": "./dist/bundler/user_preferences_bindings_wasm.js",
"types": "./dist/node/user_preferences_bindings_wasm.d.ts",
"import": "./dist/node/user_preferences_bindings_wasm.mjs",
"require": "./dist/node/user_preferences_bindings_wasm.js"
},
"./bundler": {
Expand Down
36 changes: 36 additions & 0 deletions user_preferences_bindings_wasm/scripts/targetESM.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { readFile, writeFile } from "node:fs/promises";

const cargoTomlContent = await readFile("./Cargo.toml", "utf8");
const cargoPackageName = /\[package\]\nname = "(.*?)"/.exec(
cargoTomlContent
)[1];
const name = cargoPackageName.replace(/-/g, "_");

const content = await readFile(`./dist/node/${name}.js`, "utf8");

const patched = content
// use global TextDecoder TextEncoder
.replace(
"let imports = {};",
`import { readFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from 'url';
let imports = {};
`
)
.replace(/const \{ TextDecoder \} = require\(`util`\);\n/, "")
// attach to `imports` instead of module.exports
.replace("= module.exports", "= imports")
.replace(/\nmodule\.exports\.(.*?)\s+/g, "\nexport const $1 = imports.$1 ")
.replace(/$/, "export default imports;")
.replace(
/\nconst path.*\nconst bytes.*\n/,
`
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const path = join(__dirname, "${name}_bg.wasm");
const bytes = await readFile(path);
`
);

await writeFile(`./dist/node/${name}.mjs`, patched);

0 comments on commit 78e5b9f

Please sign in to comment.