Skip to content

Commit

Permalink
fix: update to use common wasm builder
Browse files Browse the repository at this point in the history
- second attempt

Signed-off-by: Michael Dawson <mdawson@devrus.com>
  • Loading branch information
mhdawson committed Nov 13, 2024
1 parent 638ed4a commit 767595f
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 151 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
node_modules
dist
package-lock.json
deps/swc/bindings/target/
166 changes: 83 additions & 83 deletions lib/wasm.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"prepack": "npm run build",
"postpack": "npm run clean",
"build": "node esbuild.config.mjs",
"build:wasm": "node tools/build-wasm.js",
"typecheck": "tsc --noEmit",
"test": "node --test --experimental-test-snapshots \"**/*.test.js\"",
"test:regenerate": "node --test --experimental-test-snapshots --test-update-snapshots \"**/*.test.js\""
Expand Down
47 changes: 0 additions & 47 deletions tools/Dockerfile

This file was deleted.

84 changes: 63 additions & 21 deletions tools/build-wasm.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,71 @@
const { execFileSync } = require("node:child_process");
const WASM_BUILDER_CONTAINER =
"ghcr.io/nodejs/wasm-builder@sha256:975f391d907e42a75b8c72eb77c782181e941608687d4d8694c3e9df415a0970"; // v0.0.9

const { execSync, execFileSync } = require("node:child_process");
const { resolve } = require("node:path");
const os = require("node:os");
const fs = require("node:fs");
const path = require("node:path");

const ROOT = resolve(__dirname, "../");
const DOCKERFILE = resolve(__dirname, "./Dockerfile");

const name = `swc_build_wasm-${Date.now()}`;

const buildArgs = [
"build",
"-t",
"swc_wasm_typescript",
"-f",
DOCKERFILE,
ROOT,
];
execFileSync("docker", buildArgs, { stdio: "inherit" });
let platform = process.env.WASM_PLATFORM;
if (!platform && !process.argv[2]) {
platform = execFileSync("docker", [
"info",
"-f",
"{{.OSType}}/{{.Architecture}}",
])
.toString()
.trim();
}

const runArgs = ["run", "-d", "--name", name, "swc_wasm_typescript"];
execFileSync("docker", runArgs, { stdio: "inherit" });
if (process.argv[2] !== "--in-container") {
// we must map in a home directory since we might not run as a user that
// is defined in the container. Create it here.
const workdir = fs.mkdtempSync(path.join(os.tmpdir(), "amaro-build"));

// Copies the new directory inside the Docker image to the host.
const copyArgs = ["cp", `${name}:/usr/src/amaro/swc/.`, `${ROOT}/lib/`];
execFileSync("docker", copyArgs, { stdio: "inherit" });
// buile and execute the docker command to run the build
const args = [];
args.push("run");
args.push("--rm");
args.push("--platform");
args.push(`${platform.toString().trim()}`);
if (process.platform === "linux") {
args.push("--user");
args.push(`${process.getuid()}:${process.getegid()}`);
}
args.push("--mount");
args.push(
`type=bind,source=${ROOT}/deps/swc/bindings,target=/home/node/build/bindings`,
);
args.push("--mount");
args.push(`type=bind,source=${ROOT}/lib,target=/home/node/build/lib`);
args.push("--mount");
args.push(`type=bind,source=${ROOT}/tools,target=/home/node/build/tools`);
args.push("--mount");
args.push(`type=bind,source=${ROOT}/deps,target=/home/node/build/deps`);
args.push("--mount");
args.push(`type=bind,source=${workdir},target=/home/node/home`);
args.push("-t");
args.push(`${WASM_BUILDER_CONTAINER}`);
args.push("node");
args.push("./tools/build-wasm.js");
args.push("--in-container");
console.log(`> docker ${args}\n\n`);
execFileSync("docker", args, { stdio: "inherit" });

// Removes the Docker image.
execFileSync("docker", ["rm", name], { stdio: "inherit" });
// clean up the temporary working directory and then exit
fs.rmSync(workdir, { recursive: true });
process.exit(0);
}

process.exit(0);
execSync(
`HOME=/home/node/home && \
cd bindings/binding_typescript_wasm && \
cargo install --locked wasm-pack && \
PATH=/home/node/home/.cargo/bin:$PATH && \
./scripts/build.sh && \
cp -r pkg/* ../../lib`,
{ stdio: "inherit" },
);

0 comments on commit 767595f

Please sign in to comment.