feat(es/typescript): Add esm build for fast ts strip (#9286)

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/9283
This commit is contained in:
magic-akari 2024-07-20 20:51:51 +08:00 committed by GitHub
parent 76fe139334
commit d10cb9ffa2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 45 additions and 9 deletions

View File

@ -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 }}

View File

@ -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)),
]);

View File

@ -0,0 +1,2 @@
wasm-pack build --out-name wasm --out-dir esm --release --scope=swc --target web
node ./scripts/esm.mjs

View File

@ -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));

View File

@ -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";