diff --git a/.github/workflows/publish-core.yml b/.github/workflows/publish-core.yml index 9c76004a3085..0c4798a46551 100644 --- a/.github/workflows/publish-core.yml +++ b/.github/workflows/publish-core.yml @@ -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: @@ -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 }} diff --git a/bindings/binding_core_wasm/scripts/build.sh b/bindings/binding_core_wasm/scripts/build.sh deleted file mode 100755 index 2e9676dd568d..000000000000 --- a/bindings/binding_core_wasm/scripts/build.sh +++ /dev/null @@ -1 +0,0 @@ -wasm-pack build --debug --scope swc -t nodejs --features plugin --features getrandom/js $@ diff --git a/bindings/binding_core_wasm/scripts/test.sh b/bindings/binding_core_wasm/scripts/test.sh index 38d3522d0252..dcc3fb682ba1 100755 --- a/bindings/binding_core_wasm/scripts/test.sh +++ b/bindings/binding_core_wasm/scripts/test.sh @@ -2,5 +2,5 @@ set -eu -./scripts/build.sh +wasm-pack build --out-name wasm --release --scope=swc --target nodejs npx jest $@ \ No newline at end of file diff --git a/bindings/binding_typescript_wasm/Cargo.toml b/bindings/binding_typescript_wasm/Cargo.toml index af04a79923d4..9f577e363de1 100644 --- a/bindings/binding_typescript_wasm/Cargo.toml +++ b/bindings/binding_typescript_wasm/Cargo.toml @@ -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 diff --git a/bindings/binding_typescript_wasm/scripts/build.sh b/bindings/binding_typescript_wasm/scripts/build.sh index 84e9df918c60..0a24219504d7 100755 --- a/bindings/binding_typescript_wasm/scripts/build.sh +++ b/bindings/binding_typescript_wasm/scripts/build.sh @@ -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 \ No newline at end of file diff --git a/bindings/binding_typescript_wasm/scripts/patch.mjs b/bindings/binding_typescript_wasm/scripts/patch.mjs new file mode 100755 index 000000000000..7ce3b7fcafa7 --- /dev/null +++ b/bindings/binding_typescript_wasm/scripts/patch.mjs @@ -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)); \ No newline at end of file