Skip to content

Commit 70d9af2

Browse files
authored
feat: add mini-app support (#8)
1 parent b9133db commit 70d9af2

File tree

12 files changed

+88
-8
lines changed

12 files changed

+88
-8
lines changed

.changeset/yellow-toys-add.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ab64": patch
3+
---
4+
5+
feat: add `mini-app` support

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ coverage
22
dist
33
lib
44
CHANGELOG.md
5+
/src/mini-app.ts
56
/pnpm-lock.yaml
67
!/.github
78
!/.*.cjs

.github/workflows/size-limit.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ jobs:
2525
- name: Install Dependencies
2626
run: pnpm i
2727

28+
- name: Build MiniApp entry
29+
run: pnpm mini-app
30+
2831
- uses: andresz1/size-limit-action@v1
2932
with:
3033
github_token: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ coverage
55
dist
66
lib
77
node_modules
8+
/src/mini-app.ts

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
coverage
22
dist
3+
/src/mini-app.ts
34
/pnpm-lock.yaml

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The smallest and fastest Base64 implementation in JavaScript based on `atob` and
2323
- [Basic](#basic)
2424
- [Polyfill](#polyfill)
2525
- [Ponyfill](#ponyfill)
26+
- [MiniApp (WeChat)](#miniapp-wechat)
2627
- [Sponsors](#sponsors)
2728
- [Backers](#backers)
2829
- [Changelog](#changelog)
@@ -87,6 +88,16 @@ import { atob, btoa } from 'ab64/ponyfill'
8788
// same as browser native
8889
```
8990

91+
#### MiniApp (WeChat)
92+
93+
Since mini app (from wechat) does not support global polyfill, so you have to use the separate `mini-app` entry instead which uses the above ponyfill inside
94+
95+
```js
96+
import { decode, decodeUrl, encode, encodeUrl } from 'ab64/mini-app'
97+
```
98+
99+
Or you should add an alias mapping `ab64` to `ab64/mini-app` in your `rollup`/`vite`/`webpack` configuration
100+
90101
## Sponsors
91102

92103
| 1stG | RxTS | UnTS |

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@
7272
"lint:es": "eslint . --cache -f friendly --max-warnings 10",
7373
"lint:style": "stylelint . --cache",
7474
"lint:tsc": "tsc --noEmit",
75+
"mini-app": "tsx scripts/mini-app",
7576
"postversion": "pnpm i --no-frozen-lockfile",
7677
"prepare": "simple-git-hooks",
77-
"prerelease": "pnpm build",
78+
"prerelease": "pnpm mini-app && pnpm build",
7879
"release": "changeset publish",
7980
"serve": "sirv dist -s",
8081
"test": "run-p test:*",
@@ -109,6 +110,7 @@
109110
"react-router-dom": "^6.3.0",
110111
"sirv-cli": "^2.0.2",
111112
"size-limit": "^8.0.0",
113+
"tsx": "^3.8.0",
112114
"type-coverage": "^2.22.0",
113115
"typescript": "4.7.4",
114116
"unplugin-auto-import": "^0.10.3",
@@ -124,6 +126,7 @@
124126
"import": "./lib/index.js",
125127
"require": "./lib/index.cjs"
126128
},
129+
"./mini-app": "./lib/mini-app.js",
127130
"./polyfill": "./lib/polyfill.js",
128131
"./ponyfill": "./lib/ponyfill.js"
129132
},
@@ -132,11 +135,15 @@
132135
"size-limit": [
133136
{
134137
"path": "lib/browser.js",
135-
"limit": "280B"
138+
"limit": "310B"
139+
},
140+
{
141+
"path": "lib/mini-app.js",
142+
"limit": "730B"
136143
},
137144
{
138145
"path": "lib/ponyfill.js",
139-
"limit": "520B"
146+
"limit": "530B"
140147
}
141148
],
142149
"typeCoverage": {

pnpm-lock.yaml

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/mini-app.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import fs from 'node:fs/promises'
2+
3+
await fs.writeFile(
4+
'src/mini-app.ts',
5+
`import { atob, btoa } from './ponyfill.js'
6+
7+
${await fs.readFile('src/browser.ts', 'utf8')}
8+
`,
9+
)

src/browser.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/* eslint-disable unicorn/prefer-code-point -- for smaller bundler and compatibility */
22

3+
import { slice } from './helper.js'
4+
35
const HEX = 16
46
const CHUNK = 4
57

68
export const decode = (val: string) =>
79
decodeURIComponent(
8-
[
10+
slice(
911
// eslint-disable-next-line sonar/deprecation -- it's fine on browser
10-
...atob(val),
11-
]
12+
atob(val),
13+
)
1214
.map(
1315
char => '%' + ('00' + char.charCodeAt(0)!.toString(HEX)).slice(-1 * 2),
1416
)

0 commit comments

Comments
 (0)