Skip to content

Commit 4b57d58

Browse files
committed
feat: build browser version
1 parent 685b42d commit 4b57d58

File tree

10 files changed

+73
-9
lines changed

10 files changed

+73
-9
lines changed

.github/workflows/publish.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,32 @@ name: Publish
33
on:
44
push:
55
branches:
6-
- "!*"
6+
- '!*'
77
tags:
8-
- "v*"
8+
- 'v*'
9+
10+
permissions:
11+
id-token: write
912

1013
jobs:
1114
publish:
1215
runs-on: ubuntu-latest
1316
steps:
1417
- name: Checkout
15-
uses: actions/checkout@v3
18+
uses: actions/checkout@v4
1619
- name: Setup wasm-pack
1720
uses: jetli/[email protected]
1821
- name: Build
19-
run: ./build.sh
22+
run: |
23+
yarn install --frozen-lockfile
24+
./scripts/build.sh
2025
- name: Publish
21-
uses: JS-DevTools/npm-publish@v2
26+
uses: JS-DevTools/npm-publish@v3
2227
with:
2328
token: ${{ secrets.NPM_TOKEN }}
2429
package: ./pkg
2530
access: public
31+
provenance: true
2632
- name: Release
2733
uses: marvinpinto/action-automatic-releases@latest
2834
with:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
22
/Cargo.lock
3+
/node_modules

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "unity-js-tools"
3-
version = "2.0.0"
3+
version = "3.0.0"
44
edition = "2021"
55
authors = ["神代綺凛 <[email protected]>"]
66
license = "MIT"

build.bat

Lines changed: 0 additions & 1 deletion
This file was deleted.

build.sh

Lines changed: 0 additions & 2 deletions
This file was deleted.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"esm-to-cjs": "^1.2.1"
4+
}
5+
}

scripts/build.bat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
wasm-pack build --release --target nodejs --scope arkntools --out-name index
2+
wasm-pack build --release --target web --out-name index --out-dir pkg/web
3+
node scripts/process.js

scripts/build.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
wasm-pack build --release --target nodejs --scope arkntools --out-name index
3+
wasm-pack build --release --target web --out-name index --out-dir pkg/web
4+
node scripts/process.js

scripts/process.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const { readFileSync, writeFileSync } = require('fs');
2+
const { resolve, join } = require('path');
3+
const { runTransform } = require('esm-to-cjs');
4+
5+
const pkgPath = resolve(__dirname, '../pkg');
6+
const webPkgPath = resolve(__dirname, '../pkg/web');
7+
8+
const wasmBase64 = readFileSync(join(webPkgPath, 'index_bg.wasm')).toString(
9+
'base64'
10+
);
11+
12+
function base64Decode(input) {
13+
return Uint8Array.from(atob(input), (m) => m.codePointAt(0)).buffer;
14+
}
15+
16+
const content =
17+
readFileSync(join(webPkgPath, 'index.js'))
18+
.toString()
19+
.replace(/\nasync function __wbg_load\([\s\S]+?\n}/, '')
20+
.replace(/\nasync function __wbg_init\([\s\S]+?\n}/, '')
21+
.replace(/__wbg_init\.[^;]+?;\s+/, '')
22+
.replace('export { initSync }\n', '')
23+
.replace('export default __wbg_init;\n', '') +
24+
`${base64Decode.toString()}
25+
26+
initSync(base64Decode('${wasmBase64}'));
27+
`;
28+
29+
writeFileSync(
30+
join(pkgPath, 'index.browser.js'),
31+
runTransform(content, { quote: 'single', lenIdentifier: 30 })
32+
);
33+
34+
const packageJsonPath = join(pkgPath, 'package.json');
35+
const packageJson = JSON.parse(readFileSync(packageJsonPath));
36+
37+
packageJson.files.push('index.browser.js');
38+
packageJson.browser = 'index.browser.js';
39+
40+
writeFileSync(packageJsonPath, JSON.stringify(packageJson, undefined, 2));

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
esm-to-cjs@^1.2.1:
6+
version "1.2.1"
7+
resolved "https://registry.yarnpkg.com/esm-to-cjs/-/esm-to-cjs-1.2.1.tgz#4f5a0c347ab73e9330131c4a5d34172386d1c41f"
8+
integrity sha512-5zzepH0eV7F3LOz+p3Hn8iTaszfqVu9ueGcLltcuCz2kNe7Dbu9p2hePzi3xN+0S/m/d0WCC1IjIEP2UoHbmuQ==

0 commit comments

Comments
 (0)