From 8c8a0fe84b41ff979a95b1c9eb1edc2a0cb597a6 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Sat, 20 Jul 2024 20:25:04 +0800 Subject: [PATCH] feat(es/typescript): Add esm build for fast ts strip --- .github/workflows/publish-npm-package.yml | 26 ++++++++++++------- .../binding_typescript_wasm/scripts/esm.mjs | 15 +++++++++++ .../binding_typescript_wasm/scripts/esm.sh | 2 ++ .../binding_typescript_wasm/scripts/patch.mjs | 1 + .../binding_typescript_wasm/src/wasm-node.js | 10 +++++++ 5 files changed, 45 insertions(+), 9 deletions(-) create mode 100755 bindings/binding_typescript_wasm/scripts/esm.mjs create mode 100755 bindings/binding_typescript_wasm/scripts/esm.sh create mode 100644 bindings/binding_typescript_wasm/src/wasm-node.js diff --git a/.github/workflows/publish-npm-package.yml b/.github/workflows/publish-npm-package.yml index 18a9cd1e58f2..6125861a6381 100644 --- a/.github/workflows/publish-npm-package.yml +++ b/.github/workflows/publish-npm-package.yml @@ -588,12 +588,18 @@ jobs: - crate: "binding_core_wasm" npm: "@swc\\/wasm" target: nodejs + out: "pkg" - crate: "binding_core_wasm" npm: "@swc\\/wasm-web" target: web + out: "pkg" - crate: "binding_typescript_wasm" - npm: "@swc\\/wasm-typescript" - target: nodejs + build: "./scripts/build.sh" + out: "pkg" + - crate: "binding_typescript_wasm" + build: "./scripts/esm.sh" + out: "esm" + steps: - uses: actions/checkout@v4 with: @@ -631,21 +637,23 @@ jobs: - name: Build working-directory: bindings/${{ matrix.settings.crate }} + if: ${{ !matrix.settings.build }} run: | - # 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 + 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 + - name: Build + working-directory: bindings/${{ matrix.settings.crate }} + if: ${{ matrix.settings.build }} + run: | + ${{ matrix.settings.build }} + - name: Publish if: ${{ !inputs.skipBuild }} run: | npm config set provenance true echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc - (cd bindings/${{ matrix.settings.crate }}/pkg && npm publish --access public --tag $NPM_TAG) + (cd bindings/${{ matrix.settings.crate }}/${{ matrix.settings.out }} && npm publish --access public --tag $NPM_TAG) env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/bindings/binding_typescript_wasm/scripts/esm.mjs b/bindings/binding_typescript_wasm/scripts/esm.mjs new file mode 100755 index 000000000000..fef6b686e8a5 --- /dev/null +++ b/bindings/binding_typescript_wasm/scripts/esm.mjs @@ -0,0 +1,15 @@ +import fs from "node:fs/promises"; + +const pkgJsonFile = await fs.readFile("esm/package.json", "utf8"); +const pkgJson = JSON.parse(pkgJsonFile); +pkgJson.name = '@swc/wasm-typescript-esm'; +pkgJson.exports = { + types: "./wasm.d.ts", + node: "./wasm-node.js", + default: "./wasm.js", +}; + +await Promise.all([ + fs.cp("src/wasm-node.js", "esm/wasm-node.js"), + fs.writeFile("esm/package.json", JSON.stringify(pkgJson, null, 2)), +]); diff --git a/bindings/binding_typescript_wasm/scripts/esm.sh b/bindings/binding_typescript_wasm/scripts/esm.sh new file mode 100755 index 000000000000..8ab3411b071a --- /dev/null +++ b/bindings/binding_typescript_wasm/scripts/esm.sh @@ -0,0 +1,2 @@ +wasm-pack build --out-name wasm --out-dir esm --release --scope=swc --target web +node ./scripts/esm.mjs diff --git a/bindings/binding_typescript_wasm/scripts/patch.mjs b/bindings/binding_typescript_wasm/scripts/patch.mjs index 1f18112d6c7d..8d645d1f8faa 100755 --- a/bindings/binding_typescript_wasm/scripts/patch.mjs +++ b/bindings/binding_typescript_wasm/scripts/patch.mjs @@ -37,5 +37,6 @@ 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.name = '@swc/wasm-typescript'; 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 diff --git a/bindings/binding_typescript_wasm/src/wasm-node.js b/bindings/binding_typescript_wasm/src/wasm-node.js new file mode 100644 index 000000000000..98315bdc9e37 --- /dev/null +++ b/bindings/binding_typescript_wasm/src/wasm-node.js @@ -0,0 +1,10 @@ +import fs from "node:fs/promises"; +import initAsync from "./wasm.js"; + +const wasm = new URL("./wasm_bg.wasm", import.meta.url); + +export default function (init = fs.readFile(wasm)) { + return initAsync(init); +} + +export * from "./wasm.js";