Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(bindings/ts): Inline Wasm file into wasm.js #9139

Merged
merged 9 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/publish-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,15 @@ jobs:
wasm-pack build --out-name wasm --release --scope=swc --target ${{ matrix.settings.target }}
sed -i -e 's/"name": "@swc\/${{ matrix.settings.crate }}"/"name": "${{ matrix.settings.npm }}"/g' pkg/package.json

# If scripts/patch.mjs exists, apply it
if [ -f scripts/patch.mjs ]; then
node scripts/patch.mjs
fi

- name: Publish
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
run: |
(cd bindings/${{ matrix.settings.crate }}/pkg && yarn npm publish --access public --tag $NPM_TAG)
(cd bindings/${{ matrix.settings.crate }}/pkg && npm publish --access public --tag $NPM_TAG)
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 0 additions & 2 deletions bindings/binding_typescript_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,3 @@ tracing = { version = "0.1.37", features = ["max_level_off"] }
wasm-bindgen = { version = "0.2.82", features = ["enable-interning"] }
wasm-bindgen-futures = { version = "0.4.41" }

[package.metadata.wasm-pack.profile.release]
wasm-opt = false
6 changes: 5 additions & 1 deletion bindings/binding_typescript_wasm/scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
wasm-pack build --debug --scope swc -t nodejs --features getrandom/js $@
#!/usr/bin/env bash
set -eux

wasm-pack build --out-name wasm --release --scope=swc --target nodejs
node ./scripts/patch.mjs
24 changes: 24 additions & 0 deletions bindings/binding_typescript_wasm/scripts/patch.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import fs from 'node:fs/promises';


const rawWasmFile = await fs.readFile('pkg/wasm_bg.wasm');
const origJsFile = await fs.readFile('pkg/wasm.js', 'utf8');

const base64 = rawWasmFile.toString('base64');

const patchedJsFile = origJsFile
.replace(`const path = require('path').join(__dirname, 'wasm_bg.wasm');`, '')
.replace(`const bytes = require('fs').readFileSync(path);`, `
const { Buffer } = require('node:buffer');
const bytes = Buffer.from('${base64}', 'base64');`)

await fs.writeFile('pkg/wasm.js', patchedJsFile);

// Remove wasm file
await fs.unlink('pkg/wasm_bg.wasm');

// Remove wasm from .files section of package.json
const pkgJsonFile = await fs.readFile('pkg/package.json', 'utf8');
const pkgJson = JSON.parse(pkgJsonFile);
pkgJson.files = pkgJson.files.filter(file => file !== 'wasm_bg.wasm');
await fs.writeFile('pkg/package.json', JSON.stringify(pkgJson, null, 2));
Loading