-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ESM export for Node * Add changeset
- Loading branch information
Showing
3 changed files
with
45 additions
and
4 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
user_preferences_bindings_wasm/.changeset/red-beans-vanish.md
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,5 @@ | ||
--- | ||
"@xmtp/user-preferences-bindings-wasm": patch | ||
--- | ||
|
||
Add ESM export for Node |
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,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); |