261020.1 · 1d7e0126f3e601103d93586a6f872144f2789dad
chore: self-maintain Node.js features
reading declarations
Changes to tools/. All files
@@ -39,8 +39,9 @@ if (mode !== "--in-container") { args.push(`${process.getuid()}:${process.getegid()}`); } args.push("--mount");- args.push(`type=bind,source=${ROOT}/deps,target=/home/node/build/deps`);+ args.push(`type=bind,source=${ROOT}/crates,target=/home/node/build/crates`); if (mode !== "--dry-run") {+ fs.mkdirSync(path.join(ROOT, "lib"), { recursive: true }); args.push("--mount"); args.push(`type=bind,source=${ROOT}/lib,target=/home/node/build/lib`); } else {@@ -65,15 +66,24 @@ if (mode !== "--in-container") { process.exit(0); } +// wasm-bindgen-cli must match the wasm-bindgen crate in Cargo.lock exactly.+const wasmBindgenVersion = fs+ .readFileSync(resolve(ROOT, "crates/amaro/Cargo.lock"), "utf8")+ .match(/name = "wasm-bindgen"\nversion = "([^"]+)"/)?.[1];+if (!wasmBindgenVersion) {+ throw new Error("Could not find wasm-bindgen in crates/amaro/Cargo.lock");+}+ execSync( `cp -r /home/node/.rustup /home/node/home/.rustup && \ export HOME=/home/node/home && \ export PATH=/home/node/home/.cargo/bin:$PATH && \ rustc --version && \- cd deps/swc/bindings/binding_nodejs_support_wasm && \- cargo install --locked wasm-pack wasm-bindgen-cli@0.2.100 && \- ./scripts/build.sh -- --config ../../../../tools/config.toml && \- cp -r pkg/* ../../../../lib`,+ cd crates/amaro && \+ cargo install --locked wasm-pack wasm-bindgen-cli@${wasmBindgenVersion} && \+ wasm-pack build --out-name wasm --release --target nodejs -- --locked --config ../../tools/config.toml && \+ node ../../tools/patch-wasm.mjs pkg && \+ cp -r pkg/* ../../lib`, { stdio: "inherit", },
@@ -0,0 +1,44 @@+// Post-processes the wasm-pack output so the WebAssembly binary is embedded in+// wasm.js instead of being read from disk at runtime. Usage:+//+// node tools/patch-wasm.mjs <pkg-dir>+import fs from "node:fs/promises";+import path from "node:path";++const pkgDir = process.argv[2];+if (!pkgDir) {+ console.error("usage: node tools/patch-wasm.mjs <pkg-dir>");+ process.exit(1);+}++const rawWasmPath = path.join(pkgDir, "wasm_bg.wasm");+const wasmJsPath = path.join(pkgDir, "wasm.js");+const packageJsonPath = path.join(pkgDir, "package.json");++const rawWasmFile = await fs.readFile(rawWasmPath);+const origJsFile = await fs.readFile(wasmJsPath, "utf8");++const base64 = rawWasmFile.toString("base64");+const patchedJsFile = origJsFile+ .replace(`const path = require('path').join(__dirname, 'wasm_bg.wasm');`, "")+ .replace(", fatal: true", "")+ .replace(+ `const bytes = require('fs').readFileSync(path);`,+ `+const { Buffer } = require('node:buffer');+const bytes = Buffer.from('${base64}', 'base64');`,+ );++if (patchedJsFile === origJsFile) {+ console.error(+ "patch-wasm: wasm.js did not match the expected wasm-pack output",+ );+ process.exit(1);+}++await fs.writeFile(wasmJsPath, patchedJsFile);+await fs.unlink(rawWasmPath);++const pkgJson = JSON.parse(await fs.readFile(packageJsonPath, "utf8"));+pkgJson.files = pkgJson.files.filter((file) => file !== "wasm_bg.wasm");+await fs.writeFile(packageJsonPath, `${JSON.stringify(pkgJson, null, 2)}\n`);
tools/update-swc.sh
@@ -1,100 +0,0 @@-#!/bin/sh---set -ex--ROOT=$(cd "$(dirname "$0")/.." && pwd)--# Read the version from Cargo.toml of the binding_nodejs_support_wasm-CURRENT_VERSION=$(grep '^version' "$ROOT/deps/swc/bindings/binding_nodejs_support_wasm/Cargo.toml" | awk -F\" '{print $2}')--# Print the version-echo "The version installed version is: $CURRENT_VERSION"--NEW_VERSION=$(npm view @swc/nodejs-support-wasm version)--echo "Comparing $CURRENT_VERSION with $NEW_VERSION"-if [ "$NEW_VERSION" = "$CURRENT_VERSION" ]; then- echo "Skipped because @swc/nodejs-support-wasm is on the latest version."- exit 0-fi--echo "Making temporary workspace..."--WORKSPACE=$(mktemp -d 2> /dev/null || mktemp -d -t 'tmp')--cleanup () {- EXIT_CODE=$?- [ -d "$WORKSPACE" ] && rm -rf "$WORKSPACE"- exit $EXIT_CODE-}--trap cleanup INT TERM EXIT--cd "$WORKSPACE"--TARBALL=$(curl -sL "https://api.github.com/repos/swc-project/swc/releases/tags/v$NEW_VERSION" | jq -r '.tarball_url')--TARBALL_NAME="swc-$NEW_VERSION.tar.gz"--curl -sL -o "$TARBALL_NAME" "$TARBALL"--mkdir swc--tar xvfz "$TARBALL_NAME" --strip 1 -C swc--cd swc--DEPS_FOLDER="$ROOT/deps/swc"--rm -rf "$DEPS_FOLDER"--mkdir -p "$DEPS_FOLDER"--mv Cargo.toml "$DEPS_FOLDER"--mv Cargo.lock "$DEPS_FOLDER"--mv LICENSE "$DEPS_FOLDER"--mv bindings "$DEPS_FOLDER/bindings"--mv xtask "$DEPS_FOLDER/xtask"--mv .cargo "$DEPS_FOLDER/.cargo"--mv rust-toolchain "$DEPS_FOLDER/rust-toolchain"--mv crates "$DEPS_FOLDER/crates"--# Keep workspace tools (needed by Cargo workspace members like tools/generate-code)-mv tools "$DEPS_FOLDER/tools"--# Remove all unused folders (using -depth to process bottom-up)-find "$DEPS_FOLDER" -depth -type d -name "__tests__" -exec rm -rf {} + 2>/dev/null || true-find "$DEPS_FOLDER" -depth -type d -name "tests" -exec rm -rf {} + 2>/dev/null || true-find "$DEPS_FOLDER" -depth -type d -name "fixture" -exec rm -rf {} + 2>/dev/null || true-find "$DEPS_FOLDER" -depth -type d -name "fixtures" -exec rm -rf {} + 2>/dev/null || true--# Remove test files-find "$DEPS_FOLDER" -type f -name "*test*.rs" -exec rm -f {} + 2>/dev/null || true--# Remove explicit `[[test]]` targets from Cargo manifests-find "$DEPS_FOLDER" -type f -name "Cargo.toml" | while read -r MANIFEST; do- awk '- /^\[\[test\]\]/ { skip = 1; next }- /^\[/ { skip = 0 }- !skip { print }- ' "$MANIFEST" > "$MANIFEST.tmp" && mv "$MANIFEST.tmp" "$MANIFEST"-done--echo "All done!"-echo ""-echo "Please git add swc and commit the new version:"-echo ""-echo "$ git add -A deps/swc"-echo "$ git commit -m \"deps: update swc to $NEW_VERSION\""-echo ""-# The last line of the script should always print the new version,-# as we need to add it to $GITHUB_ENV variable.-echo "NEW_VERSION=$NEW_VERSION"