From 70d9af21940b81edbe5fc150ba2b85d870522e87 Mon Sep 17 00:00:00 2001 From: JounQin Date: Sun, 31 Jul 2022 18:56:07 +0800 Subject: [PATCH] feat: add `mini-app` support (#8) --- .changeset/yellow-toys-add.md | 5 +++++ .eslintignore | 1 + .github/workflows/size-limit.yml | 3 +++ .gitignore | 1 + .prettierignore | 1 + README.md | 11 +++++++++++ package.json | 13 +++++++++--- pnpm-lock.yaml | 34 ++++++++++++++++++++++++++++++++ scripts/mini-app.ts | 9 +++++++++ src/browser.ts | 8 +++++--- src/helper.ts | 5 +++++ src/ponyfill.ts | 5 +++-- 12 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 .changeset/yellow-toys-add.md create mode 100644 scripts/mini-app.ts create mode 100644 src/helper.ts diff --git a/.changeset/yellow-toys-add.md b/.changeset/yellow-toys-add.md new file mode 100644 index 0000000..bd22664 --- /dev/null +++ b/.changeset/yellow-toys-add.md @@ -0,0 +1,5 @@ +--- +"ab64": patch +--- + +feat: add `mini-app` support diff --git a/.eslintignore b/.eslintignore index 8a7b586..f74ac07 100644 --- a/.eslintignore +++ b/.eslintignore @@ -2,6 +2,7 @@ coverage dist lib CHANGELOG.md +/src/mini-app.ts /pnpm-lock.yaml !/.github !/.*.cjs diff --git a/.github/workflows/size-limit.yml b/.github/workflows/size-limit.yml index e14908a..4be373e 100644 --- a/.github/workflows/size-limit.yml +++ b/.github/workflows/size-limit.yml @@ -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 }} diff --git a/.gitignore b/.gitignore index 021af0c..0b70c8f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ coverage dist lib node_modules +/src/mini-app.ts diff --git a/.prettierignore b/.prettierignore index d5b88cf..ee688e0 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,4 @@ coverage dist +/src/mini-app.ts /pnpm-lock.yaml diff --git a/README.md b/README.md index 017b947..4598c72 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 | diff --git a/package.json b/package.json index fa12f25..505ce43 100644 --- a/package.json +++ b/package.json @@ -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:*", @@ -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", @@ -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" }, @@ -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": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0bd25c8..9fd45c3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,6 +23,7 @@ specifiers: sirv-cli: ^2.0.2 size-limit: ^8.0.0 tslib: ^2.4.0 + tsx: ^3.8.0 type-coverage: ^2.22.0 typescript: 4.7.4 unplugin-auto-import: ^0.10.3 @@ -53,6 +54,7 @@ devDependencies: react-router-dom: 6.3.0_biqbaboplfbrettd7655fr4n2y sirv-cli: 2.0.2 size-limit: 8.0.0 + tsx: 3.8.0 type-coverage: 2.22.0_typescript@4.7.4 typescript: 4.7.4 unplugin-auto-import: 0.10.3_hwlw6yqdlmgvyf5pwan5vjfl2a @@ -2527,6 +2529,27 @@ packages: jsdoc-type-pratt-parser: 3.1.0 dev: true + /@esbuild-kit/cjs-loader/2.3.2: + resolution: {integrity: sha512-3UIFKrGfq2d2R2A/SpJaeHTP5z3nrOnVMxE+cNpOuuW+Lotm0Sfbc9lVHCjcxaxgcx0MKI7g2FvxvWlylzDRKg==} + dependencies: + '@esbuild-kit/core-utils': 2.1.0 + get-tsconfig: 4.2.0 + dev: true + + /@esbuild-kit/core-utils/2.1.0: + resolution: {integrity: sha512-fZirrc2KjeTumVjE4bpleWOk2gD83b7WuGeQqOceKFQL+heNKKkNB5G5pekOUTLzfSBc0hP7hCSBoD9TuR0hLw==} + dependencies: + esbuild: 0.14.51 + source-map-support: 0.5.21 + dev: true + + /@esbuild-kit/esm-loader/2.4.2: + resolution: {integrity: sha512-N9dPKAj8WOx6djVnStgILWXip4fjDcBk9L7azO0/uQDpu8Ee0eaL78mkN4Acid9BzvNAKWwdYXFJZnsVahNEew==} + dependencies: + '@esbuild-kit/core-utils': 2.1.0 + get-tsconfig: 4.2.0 + dev: true + /@eslint/eslintrc/1.3.0: resolution: {integrity: sha512-UWW0TMTmk2d7hLcWD1/e2g5HDM/HQ3csaLSqXCfqwh4uNDuNqlaKWXmEsL4Cs41Z0KnILNvwbHAah3C2yt06kw==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} @@ -16070,6 +16093,17 @@ packages: typescript: 4.7.4 dev: true + /tsx/3.8.0: + resolution: {integrity: sha512-PcvTwRXTm6hDWfPihA4n5WW/9SmgFNxKaDKqvLLG+FKNEPA4crsipChzC7PVozPtdOaMfR5QctDlkC/hKoIsxw==} + hasBin: true + dependencies: + '@esbuild-kit/cjs-loader': 2.3.2 + '@esbuild-kit/core-utils': 2.1.0 + '@esbuild-kit/esm-loader': 2.4.2 + optionalDependencies: + fsevents: 2.3.2 + dev: true + /tty-table/4.1.6: resolution: {integrity: sha512-kRj5CBzOrakV4VRRY5kUWbNYvo/FpOsz65DzI5op9P+cHov3+IqPbo1JE1ZnQGkHdZgNFDsrEjrfqqy/Ply9fw==} engines: {node: '>=8.0.0'} diff --git a/scripts/mini-app.ts b/scripts/mini-app.ts new file mode 100644 index 0000000..7210888 --- /dev/null +++ b/scripts/mini-app.ts @@ -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')} +`, +) diff --git a/src/browser.ts b/src/browser.ts index f28529b..2661309 100644 --- a/src/browser.ts +++ b/src/browser.ts @@ -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), ) diff --git a/src/helper.ts b/src/helper.ts new file mode 100644 index 0000000..4e0df32 --- /dev/null +++ b/src/helper.ts @@ -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 = (items: { [index: number]: T; length: number }) => + _slice.call(items) as T[] diff --git a/src/ponyfill.ts b/src/ponyfill.ts index 3e2f8fd..a543aad 100644 --- a/src/ponyfill.ts +++ b/src/ponyfill.ts @@ -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>((acc, char, index) => { acc[char] = index