Skip to content

Commit

Permalink
examples: add preinstall script
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Jul 25, 2024
1 parent b036020 commit e407461
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
3 changes: 3 additions & 0 deletions examples/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"type": "module",
"main": "index.js",
"scripts": {
"preinstall": "cargo run --bin bundle_pubky_npm"
},
"dependencies": {
"@synonymdev/pubky":"file:../../pubky/pkg"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/web/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"build": "vite build",
"start": "vite serve",
"preview": "vite preview",
"wasm": "wasm-pack build ../../../pubky/ --target web"
"preinstall": "cargo run --bin bundle_pubky_npm"
},
"dependencies": {
"@synonymdev/pubky": "file:../../../pubky/pkg",
Expand Down
3 changes: 2 additions & 1 deletion examples/web/no-bundler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"scripts": {
"start": "vite serve"
"start": "vite serve",
"preinstall": "cargo run --bin bundle_pubky_npm"
},
"dependencies": {
"@synonymdev/pubky": "file:../../../pubky/pkg",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::env;
use std::io;
use std::process::{Command, ExitStatus};

Expand All @@ -11,9 +12,12 @@ fn main() {
}

fn build_wasm(target: &str) -> io::Result<ExitStatus> {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");

let output = Command::new("wasm-pack")
.args([
"build",
&manifest_dir,
"--release",
"--target",
target,
Expand All @@ -38,8 +42,11 @@ fn build_wasm(target: &str) -> io::Result<ExitStatus> {
}

fn patch() -> io::Result<ExitStatus> {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");

println!("{manifest_dir}/src/bin/patch.mjs");
let output = Command::new("node")
.args(["./src/bin/patch.mjs"])
.args([format!("{manifest_dir}/src/bin/patch.mjs")])
.output()?;

println!(
Expand All @@ -49,7 +56,7 @@ fn patch() -> io::Result<ExitStatus> {

if !output.status.success() {
eprintln!(
"wasm-pack failed: {}",
"patch.mjs failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}
Expand Down
13 changes: 9 additions & 4 deletions pubky/src/bin/patch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
// Based on hacks from [this issue](https://github.com/rustwasm/wasm-pack/issues/1334)

import { readFile, writeFile } from "node:fs/promises";
import { fileURLToPath } from 'node:url';
import path, { dirname } from 'node:path';

const cargoTomlContent = await readFile("./Cargo.toml", "utf8");
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

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

const content = await readFile(`./pkg/nodejs/${name}.js`, "utf8");
const content = await readFile(path.join(__dirname, `../../pkg/nodejs/${name}.js`), "utf8");

const patched = content
// use global TextDecoder TextEncoder
Expand Down Expand Up @@ -46,9 +51,9 @@ var __toBinary = /* @__PURE__ */ (() => {
};
})();
const bytes = __toBinary(${JSON.stringify(await readFile(`./pkg/nodejs/${name}_bg.wasm`, "base64"))
const bytes = __toBinary(${JSON.stringify(await readFile(path.join(__dirname, `../../pkg/nodejs/${name}_bg.wasm`), "base64"))
});
`,
);

await writeFile(`./pkg/${name}.mjs`, patched);
await writeFile(path.join(__dirname, `../../pkg/${name}.mjs`), patched);

0 comments on commit e407461

Please sign in to comment.