Skip to content

Commit

Permalink
feat(es/typescript): Add esm build for fast ts strip
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Jul 20, 2024
1 parent 76fe139 commit 8c8a0fe
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
26 changes: 17 additions & 9 deletions .github/workflows/publish-npm-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 }}
15 changes: 15 additions & 0 deletions bindings/binding_typescript_wasm/scripts/esm.mjs
Original file line number Diff line number Diff line change
@@ -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)),
]);
2 changes: 2 additions & 0 deletions bindings/binding_typescript_wasm/scripts/esm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
wasm-pack build --out-name wasm --out-dir esm --release --scope=swc --target web
node ./scripts/esm.mjs
1 change: 1 addition & 0 deletions bindings/binding_typescript_wasm/scripts/patch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
10 changes: 10 additions & 0 deletions bindings/binding_typescript_wasm/src/wasm-node.js
Original file line number Diff line number Diff line change
@@ -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";

0 comments on commit 8c8a0fe

Please sign in to comment.