From 4b57d58d7659280d033ca030333e0313f9556177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=9E=E4=BB=A3=E7=B6=BA=E5=87=9B?= Date: Wed, 29 May 2024 16:00:38 +0800 Subject: [PATCH] feat: build browser version --- .github/workflows/publish.yml | 16 +++++++++----- .gitignore | 1 + Cargo.toml | 2 +- build.bat | 1 - build.sh | 2 -- package.json | 5 +++++ scripts/build.bat | 3 +++ scripts/build.sh | 4 ++++ scripts/process.js | 40 +++++++++++++++++++++++++++++++++++ yarn.lock | 8 +++++++ 10 files changed, 73 insertions(+), 9 deletions(-) delete mode 100644 build.bat delete mode 100755 build.sh create mode 100644 package.json create mode 100644 scripts/build.bat create mode 100755 scripts/build.sh create mode 100644 scripts/process.js create mode 100644 yarn.lock diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 23ac598..608489b 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -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/wasm-pack-action@v0.4.0 - 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: diff --git a/.gitignore b/.gitignore index 4fffb2f..17a4f2f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /Cargo.lock +/node_modules diff --git a/Cargo.toml b/Cargo.toml index b9948b4..3c0526b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "unity-js-tools" -version = "2.0.0" +version = "3.0.0" edition = "2021" authors = ["神代綺凛 "] license = "MIT" diff --git a/build.bat b/build.bat deleted file mode 100644 index 6a193d5..0000000 --- a/build.bat +++ /dev/null @@ -1 +0,0 @@ -wasm-pack build --release --target nodejs --scope arkntools --out-name index diff --git a/build.sh b/build.sh deleted file mode 100755 index 1d5e784..0000000 --- a/build.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -wasm-pack build --release --target nodejs --scope arkntools --out-name index diff --git a/package.json b/package.json new file mode 100644 index 0000000..2f65a26 --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "esm-to-cjs": "^1.2.1" + } +} diff --git a/scripts/build.bat b/scripts/build.bat new file mode 100644 index 0000000..1324b1e --- /dev/null +++ b/scripts/build.bat @@ -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 diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..797f173 --- /dev/null +++ b/scripts/build.sh @@ -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 diff --git a/scripts/process.js b/scripts/process.js new file mode 100644 index 0000000..f5e3b55 --- /dev/null +++ b/scripts/process.js @@ -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)); diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..15d297a --- /dev/null +++ b/yarn.lock @@ -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==