Skip to content

Commit

Permalink
feat: add mini-app support (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Jul 31, 2022
1 parent b9133db commit 70d9af2
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-toys-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ab64": patch
---

feat: add `mini-app` support
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ coverage
dist
lib
CHANGELOG.md
/src/mini-app.ts
/pnpm-lock.yaml
!/.github
!/.*.cjs
3 changes: 3 additions & 0 deletions .github/workflows/size-limit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
- name: Install Dependencies
run: pnpm i

- name: Build MiniApp entry
run: pnpm mini-app

- uses: andresz1/size-limit-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ coverage
dist
lib
node_modules
/src/mini-app.ts
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage
dist
/src/mini-app.ts
/pnpm-lock.yaml
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The smallest and fastest Base64 implementation in JavaScript based on `atob` and
- [Basic](#basic)
- [Polyfill](#polyfill)
- [Ponyfill](#ponyfill)
- [MiniApp (WeChat)](#miniapp-wechat)
- [Sponsors](#sponsors)
- [Backers](#backers)
- [Changelog](#changelog)
Expand Down Expand Up @@ -87,6 +88,16 @@ import { atob, btoa } from 'ab64/ponyfill'
// same as browser native
```

#### MiniApp (WeChat)

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

```js
import { decode, decodeUrl, encode, encodeUrl } from 'ab64/mini-app'
```

Or you should add an alias mapping `ab64` to `ab64/mini-app` in your `rollup`/`vite`/`webpack` configuration

## Sponsors

| 1stG | RxTS | UnTS |
Expand Down
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@
"lint:es": "eslint . --cache -f friendly --max-warnings 10",
"lint:style": "stylelint . --cache",
"lint:tsc": "tsc --noEmit",
"mini-app": "tsx scripts/mini-app",
"postversion": "pnpm i --no-frozen-lockfile",
"prepare": "simple-git-hooks",
"prerelease": "pnpm build",
"prerelease": "pnpm mini-app && pnpm build",
"release": "changeset publish",
"serve": "sirv dist -s",
"test": "run-p test:*",
Expand Down Expand Up @@ -109,6 +110,7 @@
"react-router-dom": "^6.3.0",
"sirv-cli": "^2.0.2",
"size-limit": "^8.0.0",
"tsx": "^3.8.0",
"type-coverage": "^2.22.0",
"typescript": "4.7.4",
"unplugin-auto-import": "^0.10.3",
Expand All @@ -124,6 +126,7 @@
"import": "./lib/index.js",
"require": "./lib/index.cjs"
},
"./mini-app": "./lib/mini-app.js",
"./polyfill": "./lib/polyfill.js",
"./ponyfill": "./lib/ponyfill.js"
},
Expand All @@ -132,11 +135,15 @@
"size-limit": [
{
"path": "lib/browser.js",
"limit": "280B"
"limit": "310B"
},
{
"path": "lib/mini-app.js",
"limit": "730B"
},
{
"path": "lib/ponyfill.js",
"limit": "520B"
"limit": "530B"
}
],
"typeCoverage": {
Expand Down
34 changes: 34 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions scripts/mini-app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import fs from 'node:fs/promises'

await fs.writeFile(
'src/mini-app.ts',
`import { atob, btoa } from './ponyfill.js'
${await fs.readFile('src/browser.ts', 'utf8')}
`,
)
8 changes: 5 additions & 3 deletions src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/* eslint-disable unicorn/prefer-code-point -- for smaller bundler and compatibility */

import { slice } from './helper.js'

const HEX = 16
const CHUNK = 4

export const decode = (val: string) =>
decodeURIComponent(
[
slice(
// eslint-disable-next-line sonar/deprecation -- it's fine on browser
...atob(val),
]
atob(val),
)
.map(
char => '%' + ('00' + char.charCodeAt(0)!.toString(HEX)).slice(-1 * 2),
)
Expand Down
5 changes: 5 additions & 0 deletions src/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// eslint-disable-next-line @typescript-eslint/unbound-method
const _slice = (Array.prototype as unknown[]).slice // type-coverage:ignore-line

export const slice = <T>(items: { [index: number]: T; length: number }) =>
_slice.call(items) as T[]
5 changes: 3 additions & 2 deletions src/ponyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

/* eslint-disable @typescript-eslint/no-magic-numbers, unicorn/prefer-code-point -- for smaller bundler and compatibility */

import { slice } from './helper.js'

const b64CharList =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='

const b64Chars = // type-coverage:ignore-next-line
(Array.prototype as string[]).slice.call(b64CharList)
const b64Chars = slice(b64CharList)

const b64Table = b64Chars.reduce<Record<string, number>>((acc, char, index) => {
acc[char] = index
Expand Down

0 comments on commit 70d9af2

Please sign in to comment.