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 all 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
11 changes: 9 additions & 2 deletions .github/workflows/publish-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ jobs:
if: ${{ startsWith(github.ref, 'refs/tags/v') || startsWith(github.ref, 'refs/heads/ci') }}
name: Build - wasm (${{ matrix.settings.npm }}) for node.js
runs-on: ubuntu-latest
env:
CARGO_PROFILE_RELEASE_LTO: "fat"
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -554,13 +556,18 @@ jobs:
- name: Build
working-directory: bindings/${{ matrix.settings.crate }}
run: |
wasm-pack build --out-name wasm --release --scope=swc --target ${{ matrix.settings.target }}
# If scripts/build.sh exists, apply it
if [ -f scripts/build.sh ]; then
scripts/build.sh
else
wasm-pack build --out-name wasm --release --scope=swc --target ${{ matrix.settings.target }}
fi
sed -i -e 's/"name": "@swc\/${{ matrix.settings.crate }}"/"name": "${{ matrix.settings.npm }}"/g' pkg/package.json

- 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 }}
1 change: 0 additions & 1 deletion bindings/binding_core_wasm/scripts/build.sh

This file was deleted.

2 changes: 1 addition & 1 deletion bindings/binding_core_wasm/scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

set -eu

./scripts/build.sh
wasm-pack build --out-name wasm --release --scope=swc --target nodejs
npx jest $@
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
10 changes: 9 additions & 1 deletion bindings/binding_typescript_wasm/scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
wasm-pack build --debug --scope swc -t nodejs --features getrandom/js $@
#!/usr/bin/env bash
set -eux

export CARGO_PROFILE_RELEASE_LTO="fat"
export CARGO_PROFILE_RELEASE_OPT_LEVEL="z"
wasm-pack build --out-name wasm --release --scope=swc --target nodejs
ls -al ./pkg

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