Skip to content

Commit

Permalink
feat: build browser version
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsuk1ko committed May 29, 2024
1 parent 685b42d commit 4b57d58
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 9 deletions.
16 changes: 11 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,32 @@ name: Publish
on:
push:
branches:
- "!*"
- '!*'
tags:
- "v*"
- 'v*'

permissions:
id-token: write

jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Setup wasm-pack
uses: jetli/[email protected]
- name: Build
run: ./build.sh
run: |
yarn install --frozen-lockfile
./scripts/build.sh
- name: Publish
uses: JS-DevTools/npm-publish@v2
uses: JS-DevTools/npm-publish@v3
with:
token: ${{ secrets.NPM_TOKEN }}
package: ./pkg
access: public
provenance: true
- name: Release
uses: marvinpinto/action-automatic-releases@latest
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/Cargo.lock
/node_modules
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "unity-js-tools"
version = "2.0.0"
version = "3.0.0"
edition = "2021"
authors = ["神代綺凛 <[email protected]>"]
license = "MIT"
Expand Down
1 change: 0 additions & 1 deletion build.bat

This file was deleted.

2 changes: 0 additions & 2 deletions build.sh

This file was deleted.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"esm-to-cjs": "^1.2.1"
}
}
3 changes: 3 additions & 0 deletions scripts/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
wasm-pack build --release --target nodejs --scope arkntools --out-name index
wasm-pack build --release --target web --out-name index --out-dir pkg/web
node scripts/process.js
4 changes: 4 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
wasm-pack build --release --target nodejs --scope arkntools --out-name index
wasm-pack build --release --target web --out-name index --out-dir pkg/web
node scripts/process.js
40 changes: 40 additions & 0 deletions scripts/process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { readFileSync, writeFileSync } = require('fs');
const { resolve, join } = require('path');
const { runTransform } = require('esm-to-cjs');

const pkgPath = resolve(__dirname, '../pkg');
const webPkgPath = resolve(__dirname, '../pkg/web');

const wasmBase64 = readFileSync(join(webPkgPath, 'index_bg.wasm')).toString(
'base64'
);

function base64Decode(input) {
return Uint8Array.from(atob(input), (m) => m.codePointAt(0)).buffer;
}

const content =
readFileSync(join(webPkgPath, 'index.js'))
.toString()
.replace(/\nasync function __wbg_load\([\s\S]+?\n}/, '')
.replace(/\nasync function __wbg_init\([\s\S]+?\n}/, '')
.replace(/__wbg_init\.[^;]+?;\s+/, '')
.replace('export { initSync }\n', '')
.replace('export default __wbg_init;\n', '') +
`${base64Decode.toString()}
initSync(base64Decode('${wasmBase64}'));
`;

writeFileSync(
join(pkgPath, 'index.browser.js'),
runTransform(content, { quote: 'single', lenIdentifier: 30 })
);

const packageJsonPath = join(pkgPath, 'package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath));

packageJson.files.push('index.browser.js');
packageJson.browser = 'index.browser.js';

writeFileSync(packageJsonPath, JSON.stringify(packageJson, undefined, 2));
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


esm-to-cjs@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/esm-to-cjs/-/esm-to-cjs-1.2.1.tgz#4f5a0c347ab73e9330131c4a5d34172386d1c41f"
integrity sha512-5zzepH0eV7F3LOz+p3Hn8iTaszfqVu9ueGcLltcuCz2kNe7Dbu9p2hePzi3xN+0S/m/d0WCC1IjIEP2UoHbmuQ==

0 comments on commit 4b57d58

Please sign in to comment.