From 14458422cc7ad81f3d74b0733611ea959982e593 Mon Sep 17 00:00:00 2001 From: broven Date: Wed, 1 Apr 2020 16:05:48 +0800 Subject: [PATCH 01/14] feat: asyncstorage for bytedance --- packages/asyncstorage/src/index.ts | 8 +- .../src/miniapp/bytedance/index.ts | 84 +++++++++++++++++++ 2 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 packages/asyncstorage/src/miniapp/bytedance/index.ts diff --git a/packages/asyncstorage/src/index.ts b/packages/asyncstorage/src/index.ts index b3acc9e0..77065514 100644 --- a/packages/asyncstorage/src/index.ts +++ b/packages/asyncstorage/src/index.ts @@ -1,8 +1,9 @@ -import { isWeb, isWeex, isMiniApp, isWeChatMiniprogram } from 'universal-env'; +import { isWeb, isWeex, isMiniApp, isWeChatMiniProgram, isByteDanceMicroApp } from 'universal-env'; import webModule from './web/index'; import weexModule from './weex/index'; import miniAppModule from './miniapp/ali/index'; import weChatModule from './miniapp/wechat/index'; +import bytedanceModule from './miniapp/bytedance/index'; import { AsyncStorage } from './types'; let AsyncStorage: AsyncStorage; @@ -19,8 +20,11 @@ if (isMiniApp) { AsyncStorage = miniAppModule; } -if (isWeChatMiniprogram) { +if (isWeChatMiniProgram) { AsyncStorage = weChatModule; } +if (isByteDanceMicroApp) { + AsyncStorage = bytedanceModule; +} export default AsyncStorage; diff --git a/packages/asyncstorage/src/miniapp/bytedance/index.ts b/packages/asyncstorage/src/miniapp/bytedance/index.ts new file mode 100644 index 00000000..896da678 --- /dev/null +++ b/packages/asyncstorage/src/miniapp/bytedance/index.ts @@ -0,0 +1,84 @@ +import { AsyncStorage } from '../../types'; + +declare const tt: any; + +const AsyncStorage: AsyncStorage = { + getItem: (key: string): Promise => { + return new Promise((resolve, reject): void => { + tt.getStorage({ + key, + success: (res): void => { + resolve(res.data); + }, + fail: (e): void => { + reject(e); + } + }); + }); + }, + setItem: (key: string, value: object | string): Promise => { + return new Promise((resolve, reject): void => { + tt.setStorage({ + key, + data: value, + success: (): void => { + resolve(null); + }, + fail: (e): void => { + reject(e); + } + }); + }); + }, + removeItem: (key: string): Promise => { + return new Promise((resolve, reject): void => { + tt.removeStorage({ + key, + success: (): void => { + resolve(null); + }, + fail: (e): void => { + reject(e); + } + }); + }); + }, + getAllKeys: (): Promise => { + return new Promise((resolve, reject): void => { + tt.getStorageInfo({ + success: (res): void => { + resolve(res.keys); + }, + fail: (e): void => { + reject(e); + } + }); + }); + }, + clear: (): Promise => { + return new Promise((resolve, reject): void => { + tt.clearStorage({ + success: (): void => { + resolve(null); + }, + fail: (e): void => { + reject(e); + } + }); + }); + }, + length: (): Promise => { + return new Promise((resolve, reject): void => { + tt.getStorageInfo({ + success: (res): void => { + resolve(res.keys.length); + }, + fail: (e): void => { + reject(e); + } + }); + }); + } +}; + +export default AsyncStorage; From 3c76eb5005b46098d49cafe430ddc938bdb08eb4 Mon Sep 17 00:00:00 2001 From: broven Date: Wed, 1 Apr 2020 16:56:59 +0800 Subject: [PATCH 02/14] feat: request for bytedance --- packages/asyncstorage/package.json | 3 ++ packages/request/src/index.ts | 11 ++++- .../request/src/miniapp-bytedance/index.ts | 45 +++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 packages/request/src/miniapp-bytedance/index.ts diff --git a/packages/asyncstorage/package.json b/packages/asyncstorage/package.json index a2550887..7854d4a7 100644 --- a/packages/asyncstorage/package.json +++ b/packages/asyncstorage/package.json @@ -43,6 +43,9 @@ "rax-scripts": "^2.0.0", "rax-test-renderer": "^1.0.0" }, + "publishConfig": { + "registry": "https://registry.npm.alibaba-inc.com" + }, "pre-commit": [ "lint", "test" diff --git a/packages/request/src/index.ts b/packages/request/src/index.ts index 87a54ec7..633e4b0b 100644 --- a/packages/request/src/index.ts +++ b/packages/request/src/index.ts @@ -1,4 +1,4 @@ -import { isWeex, isMiniApp, isWeChatMiniProgram, isWeb } from 'universal-env'; +import { isWeex, isMiniApp, isWeChatMiniProgram, isWeb, isByteDanceMicroApp } from 'universal-env'; import { RequestOptions, DEFAULT_REQUEST_OPTIONS @@ -8,6 +8,7 @@ import webModule from './web/index'; import weexModule from './weex/index'; import miniAppModule from './miniapp/index'; import weChatModule from './wechat-miniprogram/index'; +import bytedanceModule from './miniapp-bytedance/index'; function dutyChain(...fns) { for (let i = 0; i < fns.length; i++) { @@ -49,7 +50,12 @@ function handleWeChatMiniprogram(afterOptions) { } return null; } - +function handleBytedanceMiniprogram(afterOptions) { + if (isByteDanceMicroApp) { + const request = bytedanceModule; + return request(afterOptions); + } +} export default function(options: RequestOptions) { let afterOptions: RequestOptions = Object.assign({}, DEFAULT_REQUEST_OPTIONS, @@ -63,5 +69,6 @@ export default function(options: RequestOptions) { handleWeex.bind(null, afterOptions), handleWeb.bind(null, afterOptions), handleMiniApp.bind(null, afterOptions), + handleBytedanceMiniprogram.bind(null, afterOptions), handleWeChatMiniprogram.bind(null, afterOptions)); } diff --git a/packages/request/src/miniapp-bytedance/index.ts b/packages/request/src/miniapp-bytedance/index.ts new file mode 100644 index 00000000..d273fbda --- /dev/null +++ b/packages/request/src/miniapp-bytedance/index.ts @@ -0,0 +1,45 @@ +import { + RequestOptions, + ResponseData, + ERROR_REQUEST_TIMEOUT, + ERROR_REQUEST_ABORT +} from '../types'; +import { + object2json +} from '../utils'; +declare const tt: any; + +export default function(options: RequestOptions): Promise { + return new Promise((resolve, reject) => { + let { url, method, data, dataType, headers, timeout } = options; + let timeoutTimer; + let requestTask = tt.request({ + url, + headers, + method, + data, + timeout, + dataType, + success: function(res: ResponseData) { + resolve(res); + }, + fail: function(res) { + reject({ + code: ERROR_REQUEST_ABORT.code, + message: object2json(res) + }); + }, + complete() { + if (timeoutTimer) { + clearTimeout(timeoutTimer); + } + } + }); + if (timeout) { + timeoutTimer = setTimeout(() => { + requestTask.abort(); + reject(ERROR_REQUEST_TIMEOUT); + }, timeout); + } + }); +} From fe7118258434d075af6df2a32927d428ff63aae0 Mon Sep 17 00:00:00 2001 From: broven Date: Wed, 1 Apr 2020 17:14:45 +0800 Subject: [PATCH 03/14] chore: clean pkgjson --- packages/asyncstorage/package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/asyncstorage/package.json b/packages/asyncstorage/package.json index 7854d4a7..a2550887 100644 --- a/packages/asyncstorage/package.json +++ b/packages/asyncstorage/package.json @@ -43,9 +43,6 @@ "rax-scripts": "^2.0.0", "rax-test-renderer": "^1.0.0" }, - "publishConfig": { - "registry": "https://registry.npm.alibaba-inc.com" - }, "pre-commit": [ "lint", "test" From a54bf12c55c0925e9bac00433b83dcc154fd0540 Mon Sep 17 00:00:00 2001 From: broven Date: Tue, 7 Apr 2020 16:02:33 +0800 Subject: [PATCH 04/14] chore: bump version --- packages/asyncstorage/package.json | 2 +- packages/request/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/asyncstorage/package.json b/packages/asyncstorage/package.json index a2550887..d83c7f48 100644 --- a/packages/asyncstorage/package.json +++ b/packages/asyncstorage/package.json @@ -1,6 +1,6 @@ { "name": "universal-asyncstorage", - "version": "1.0.2", + "version": "1.1.0", "description": "A universal AsyncStorage API.", "license": "BSD-3-Clause", "main": "lib/index.js", diff --git a/packages/request/package.json b/packages/request/package.json index 5de409a8..d321f93e 100644 --- a/packages/request/package.json +++ b/packages/request/package.json @@ -1,6 +1,6 @@ { "name": "universal-request", - "version": "2.0.0", + "version": "2.1.0", "description": "", "main": "lib/index.js", "files": [ From e61493817a247d3f5a50651beb405a90ffdd8c19 Mon Sep 17 00:00:00 2001 From: broven Date: Tue, 7 Apr 2020 16:31:54 +0800 Subject: [PATCH 05/14] update: docs --- packages/asyncstorage/docs-template/README-zh_CN.md | 3 +-- packages/asyncstorage/docs-template/README.md | 2 +- packages/request/docs-template/README-zh_CN.md | 2 +- packages/request/docs-template/README.md | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/asyncstorage/docs-template/README-zh_CN.md b/packages/asyncstorage/docs-template/README-zh_CN.md index dc199644..934f1b1c 100644 --- a/packages/asyncstorage/docs-template/README-zh_CN.md +++ b/packages/asyncstorage/docs-template/README-zh_CN.md @@ -3,8 +3,7 @@ > AsyncStorage ## 支持 -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ - +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_miniapp_bytedance__ ## 安装 ```bash diff --git a/packages/asyncstorage/docs-template/README.md b/packages/asyncstorage/docs-template/README.md index 0461cdbf..8ca40421 100644 --- a/packages/asyncstorage/docs-template/README.md +++ b/packages/asyncstorage/docs-template/README.md @@ -3,7 +3,7 @@ > AsyncStorage ## Support -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_miniapp_bytedance__ ## Install diff --git a/packages/request/docs-template/README-zh_CN.md b/packages/request/docs-template/README-zh_CN.md index ae6d3835..fcde4749 100644 --- a/packages/request/docs-template/README-zh_CN.md +++ b/packages/request/docs-template/README-zh_CN.md @@ -31,7 +31,7 @@ request({ #### 支持 -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ __icon_miniapp_bytedance__ #### 参数 diff --git a/packages/request/docs-template/README.md b/packages/request/docs-template/README.md index e267bfd6..52114b43 100644 --- a/packages/request/docs-template/README.md +++ b/packages/request/docs-template/README.md @@ -31,7 +31,7 @@ request({ #### Supported -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ __icon_miniapp_bytedance__ #### Arguments From aa950bc207b1aaa650a6901b80b07af16b040e3a Mon Sep 17 00:00:00 2001 From: broven Date: Fri, 10 Apr 2020 17:49:55 +0800 Subject: [PATCH 06/14] update: docs --- packages/asyncstorage/README-zh_CN.md | 3 +-- packages/asyncstorage/README.md | 2 +- packages/asyncstorage/docs-template/README-zh_CN.md | 2 +- packages/asyncstorage/docs-template/README.md | 2 +- packages/env/README.md | 1 - packages/request/README-zh_CN.md | 2 +- packages/request/README.md | 2 +- packages/request/docs-template/README-zh_CN.md | 2 +- packages/request/docs-template/README.md | 2 +- 9 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/asyncstorage/README-zh_CN.md b/packages/asyncstorage/README-zh_CN.md index f750ae71..81dc9057 100644 --- a/packages/asyncstorage/README-zh_CN.md +++ b/packages/asyncstorage/README-zh_CN.md @@ -3,8 +3,7 @@ > AsyncStorage ## 支持 -browser weex miniApp wechatMiniprogram - +browser weex miniApp wechatMiniprogram bytedanceMicroApp ## 安装 ```bash diff --git a/packages/asyncstorage/README.md b/packages/asyncstorage/README.md index 9bdcf2bc..c171549c 100644 --- a/packages/asyncstorage/README.md +++ b/packages/asyncstorage/README.md @@ -3,7 +3,7 @@ > AsyncStorage ## Support -browser weex miniApp wechatMiniprogram +browser weex miniApp wechatMiniprogram bytedanceMicroApp ## Install diff --git a/packages/asyncstorage/docs-template/README-zh_CN.md b/packages/asyncstorage/docs-template/README-zh_CN.md index 934f1b1c..41f582bf 100644 --- a/packages/asyncstorage/docs-template/README-zh_CN.md +++ b/packages/asyncstorage/docs-template/README-zh_CN.md @@ -3,7 +3,7 @@ > AsyncStorage ## 支持 -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_miniapp_bytedance__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_microapp_tt__ ## 安装 ```bash diff --git a/packages/asyncstorage/docs-template/README.md b/packages/asyncstorage/docs-template/README.md index 8ca40421..3c1fff38 100644 --- a/packages/asyncstorage/docs-template/README.md +++ b/packages/asyncstorage/docs-template/README.md @@ -3,7 +3,7 @@ > AsyncStorage ## Support -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_miniapp_bytedance__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_microapp_tt__ ## Install diff --git a/packages/env/README.md b/packages/env/README.md index 78cd28a5..6fb989d5 100644 --- a/packages/env/README.md +++ b/packages/env/README.md @@ -3,7 +3,6 @@ Judge runtime environment ## Support - browser weex miniApp wechatMiniprogram quickApp ## Install diff --git a/packages/request/README-zh_CN.md b/packages/request/README-zh_CN.md index ec27ca6c..b1423d1c 100644 --- a/packages/request/README-zh_CN.md +++ b/packages/request/README-zh_CN.md @@ -31,7 +31,7 @@ request({ #### 支持 -browser weex miniApp wechatMiniprogram quickApp +browser weex miniApp wechatMiniprogram quickApp bytedanceMicroApp #### 参数 diff --git a/packages/request/README.md b/packages/request/README.md index 817fbd58..259ff0de 100644 --- a/packages/request/README.md +++ b/packages/request/README.md @@ -31,7 +31,7 @@ request({ #### Supported -browser weex miniApp wechatMiniprogram quickApp +browser weex miniApp wechatMiniprogram quickApp bytedanceMicroApp #### Arguments diff --git a/packages/request/docs-template/README-zh_CN.md b/packages/request/docs-template/README-zh_CN.md index fcde4749..39d9b687 100644 --- a/packages/request/docs-template/README-zh_CN.md +++ b/packages/request/docs-template/README-zh_CN.md @@ -31,7 +31,7 @@ request({ #### 支持 -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ __icon_miniapp_bytedance__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ __icon_microapp_tt__ #### 参数 diff --git a/packages/request/docs-template/README.md b/packages/request/docs-template/README.md index 52114b43..73eb7e39 100644 --- a/packages/request/docs-template/README.md +++ b/packages/request/docs-template/README.md @@ -31,7 +31,7 @@ request({ #### Supported -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ __icon_miniapp_bytedance__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ __icon_microapp_tt__ #### Arguments From 79c6a76619f0717b1c11765de1c21302390b7282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8B=92=E7=8B=92=E7=A5=9E?= <350740249@qq.com> Date: Wed, 1 Apr 2020 16:11:51 +0800 Subject: [PATCH 07/14] feat: support bytedance microapp in universal-env (#61) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: support isByteDance (#60) * feat: support isByteDance * fix: beta version * fix: isByteDanceMicroApp * Fix: exclude tt in judgement of wechat mini program (#65) * feat: support isByteDance * fix: beta version * fix: isByteDanceMicroApp * feat: exclude tt in isWeChatMiniProgram * feat: exclude tt in isWeChatMiniProgram * fix: version * chore: bump version Co-authored-by: 小杰 --- packages/env/README-zh_CN.md | 33 +++++++++++++--------- packages/env/README.md | 33 +++++++++++++--------- packages/env/docs-template/README-zh_CN.md | 33 +++++++++++++--------- packages/env/docs-template/README.md | 33 +++++++++++++--------- packages/env/package.json | 2 +- packages/env/src/index.ts | 5 +++- scripts/compile-readme.js | 3 +- 7 files changed, 87 insertions(+), 55 deletions(-) diff --git a/packages/env/README-zh_CN.md b/packages/env/README-zh_CN.md index 03f168a9..b9ed4f81 100644 --- a/packages/env/README-zh_CN.md +++ b/packages/env/README-zh_CN.md @@ -3,7 +3,7 @@ 判断和获取运行时环境 ## 支持 -browser weex miniApp wechatMiniprogram quickApp +browser weex miniApp wechatMiniprogram bytedanceMicroApp quickApp ## 安装 ```bash @@ -12,7 +12,7 @@ $ npm install universal-env --save ## 示例 ```javascript -import { isWeex, isWeb, isMiniApp, isNode, isWeChatMiniProgram, isQuickApp } from 'universal-env'; +import { isWeex, isWeb, isMiniApp, isNode, isWeChatMiniProgram, isByteDanceMicroApp, isQuickApp } from 'universal-env'; ``` @@ -29,6 +29,9 @@ import { isWeex, isWeb, isMiniApp, isNode, isWeChatMiniProgram, isQuickApp } fro ### `isWeChatMiniProgram: boolean` 校验微信小程序环境 +### `isByteDanceMicroApp: boolean` +校验字节跳动小程序环境 + ### `isQuickApp: boolean` 校验快应用环境 @@ -37,17 +40,21 @@ import { isWeex, isWeb, isMiniApp, isNode, isWeChatMiniProgram, isQuickApp } fro ## 各容器内返回结果 -| | isMiniApp | isWeChatMiniProgram | isQuickApp | isWeb | isWeex | isNode | -| ------------------ | --------- | ------------------- | ---------- | ----- | ------ | ------ | -| 阿里小程序 | ✔️ | ✘ | ✘ | ✘ | ✘ | ✘ | -| 阿里小程序 WebView | ✔️ | ✘ | ✘ | ✔️ | ✘ | ✘ | -| 微信小程序 | ✘ | ✔️ | ✘ | ✘ | ✘ | ✘ | -| 微信小程序 WebView | ✘ | ✔️ | ✘ | ✔️ | ✘ | ✘ | -| 快应用 | ✘ | ✘ | ✔️ | ✘ | ✘ | ✘ | -| 快应用 WebView | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | -| Weex | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | -| Node | ✘ | ✘ | ✘ | ✘ | ✘ | ✔️ | -| Web | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | +注:以下各小程序 WebView 内容器判断依赖 WebView 中注入容器的JS SDK。 + +| | isMiniApp | isWeChatMiniProgram | isByteDanceMicroApp | isQuickApp | isWeb | isWeex | isNode | +| ------------------- | --------- | ------------------- | ------------------ | ---------- | ----- | ------ | ------ | +| 阿里小程序 | ✔️ | ✘ | ✘ | ✘ | ✘ | ✘ | ✘ | +| 阿里小程序 WebView | ✔️ | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | +| 微信小程序 | ✘ | ✔️ | ✘ | ✘ | ✘ | ✘ | ✘ | +| 微信小程序 WebView | ✘ | ✔️ | ✘ | ✘ | ✔️ | ✘ | ✘ | +| 字节跳动小程序 | ✘ | ✘ | ✔️ | ✘ | ✘ | ✘ | ✘ | +| 字节跳动小程序 WebView | ✘ | ✘ | ✔️ | ✘ | ✔️ | ✘ | ✘ | +| 快应用 | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | ✘ | +| 快应用 WebView | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | +| Weex | ✘ | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | +| Node | ✘ | ✘ | ✘ | ✘ | ✘ | ✘ | ✔️ | +| Web | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | diff --git a/packages/env/README.md b/packages/env/README.md index 6fb989d5..75b3303c 100644 --- a/packages/env/README.md +++ b/packages/env/README.md @@ -3,7 +3,7 @@ Judge runtime environment ## Support -browser weex miniApp wechatMiniprogram quickApp +browser weex miniApp wechatMiniprogram bytedanceMicroApp quickApp ## Install ```bash @@ -12,7 +12,7 @@ $ npm install universal-env --save ## Usage ```javascript -import { isWeex, isWeb, isMiniApp, isNode, isWeChatMiniProgram, isQuickApp } from 'universal-env'; +import { isWeex, isWeb, isMiniApp, isNode, isWeChatMiniProgram, isByteDanceMicroApp, isQuickApp } from 'universal-env'; ``` @@ -29,6 +29,9 @@ Checks if environment is an alibaba miniprogram environment. ### `isWeChatMiniProgram: boolean` Checks if environment is a wechat miniprogram environment. +### `isByteDanceMicroApp: boolean` +Checks if environment is a bytedance microapp environment. + ### `isQuickApp: boolean` Checks if environment is a quickapp environment. @@ -37,16 +40,20 @@ Checks if environment is a Node.js environment. ## Results in each container -| | isMiniApp | isWeChatMiniProgram | isQuickApp | isWeb | isWeex | isNode | -| ---------------------------- | --------- | ------------------- | ---------- | ----- | ------ | ------ | -| Alibaba MiniApp | ✔️ | ✘ | ✘ | ✘ | ✘ | ✘ | -| Alibaba MiniApp WebView | ✔️ | ✘ | ✘ | ✔️ | ✘ | ✘ | -| Wechat MiniProgram | ✘ | ✔️ | ✘ | ✘ | ✘ | ✘ | -| Wechat MiniProgram WebView | ✘ | ✔️ | ✘ | ✔️ | ✘ | ✘ | -| QuickApp | ✘ | ✘ | ✔️ | ✘ | ✘ | ✘ | -| QuickApp WebView | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | -| Weex | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | -| Node | ✘ | ✘ | ✘ | ✘ | ✘ | ✔️ | -| Web | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | +Note:To judge MiniProgram env in WebView, please insert JS SDK accordingly. + +| | isMiniApp | isWeChatMiniProgram | isByteDanceMicroApp | isQuickApp | isWeb | isWeex | isNode | +| ----------------------------- | --------- | ------------------- | ------------------- | ---------- | ----- | ------ | ------ | +| Alibaba MiniApp | ✔️ | ✘ | ✘ | ✘ | ✘ | ✘ | ✘ | +| Alibaba MiniApp WebView | ✔️ | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | +| Wechat MiniProgram | ✘ | ✔️ | ✘ | ✘ | ✘ | ✘ | ✘ | +| Wechat MiniProgram WebView | ✘ | ✔️ | ✘ | ✘ | ✔️ | ✘ | ✘ | +| ByteDance MicroApp | ✘ | ✘ | ✔️ | ✘ | ✘ | ✘ | ✘ | +| ByteDance MicroApp WebView | ✘ | ✘ | ✔️ | ✘ | ✔️ | ✘ | ✘ | +| QuickApp | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | ✘ | +| QuickApp WebView | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | +| Weex | ✘ | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | +| Node | ✘ | ✘ | ✘ | ✘ | ✘ | ✘ | ✔️ | +| Web | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | diff --git a/packages/env/docs-template/README-zh_CN.md b/packages/env/docs-template/README-zh_CN.md index 60358ffe..7957d48c 100644 --- a/packages/env/docs-template/README-zh_CN.md +++ b/packages/env/docs-template/README-zh_CN.md @@ -3,7 +3,7 @@ 判断和获取运行时环境 ## 支持 -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_microapp_tt__ __icon_quick_app__ ## 安装 ```bash @@ -12,7 +12,7 @@ $ npm install universal-env --save ## 示例 ```javascript -import { isWeex, isWeb, isMiniApp, isNode, isWeChatMiniProgram, isQuickApp } from 'universal-env'; +import { isWeex, isWeb, isMiniApp, isNode, isWeChatMiniProgram, isByteDanceMicroApp, isQuickApp } from 'universal-env'; ``` @@ -29,6 +29,9 @@ import { isWeex, isWeb, isMiniApp, isNode, isWeChatMiniProgram, isQuickApp } fro ### `isWeChatMiniProgram: boolean` 校验微信小程序环境 +### `isByteDanceMicroApp: boolean` +校验字节跳动小程序环境 + ### `isQuickApp: boolean` 校验快应用环境 @@ -37,17 +40,21 @@ import { isWeex, isWeb, isMiniApp, isNode, isWeChatMiniProgram, isQuickApp } fro ## 各容器内返回结果 -| | isMiniApp | isWeChatMiniProgram | isQuickApp | isWeb | isWeex | isNode | -| ------------------ | --------- | ------------------- | ---------- | ----- | ------ | ------ | -| 阿里小程序 | ✔️ | ✘ | ✘ | ✘ | ✘ | ✘ | -| 阿里小程序 WebView | ✔️ | ✘ | ✘ | ✔️ | ✘ | ✘ | -| 微信小程序 | ✘ | ✔️ | ✘ | ✘ | ✘ | ✘ | -| 微信小程序 WebView | ✘ | ✔️ | ✘ | ✔️ | ✘ | ✘ | -| 快应用 | ✘ | ✘ | ✔️ | ✘ | ✘ | ✘ | -| 快应用 WebView | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | -| Weex | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | -| Node | ✘ | ✘ | ✘ | ✘ | ✘ | ✔️ | -| Web | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | +注:以下各小程序 WebView 内容器判断依赖 WebView 中注入容器的JS SDK。 + +| | isMiniApp | isWeChatMiniProgram | isByteDanceMicroApp | isQuickApp | isWeb | isWeex | isNode | +| ------------------- | --------- | ------------------- | ------------------ | ---------- | ----- | ------ | ------ | +| 阿里小程序 | ✔️ | ✘ | ✘ | ✘ | ✘ | ✘ | ✘ | +| 阿里小程序 WebView | ✔️ | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | +| 微信小程序 | ✘ | ✔️ | ✘ | ✘ | ✘ | ✘ | ✘ | +| 微信小程序 WebView | ✘ | ✔️ | ✘ | ✘ | ✔️ | ✘ | ✘ | +| 字节跳动小程序 | ✘ | ✘ | ✔️ | ✘ | ✘ | ✘ | ✘ | +| 字节跳动小程序 WebView | ✘ | ✘ | ✔️ | ✘ | ✔️ | ✘ | ✘ | +| 快应用 | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | ✘ | +| 快应用 WebView | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | +| Weex | ✘ | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | +| Node | ✘ | ✘ | ✘ | ✘ | ✘ | ✘ | ✔️ | +| Web | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | diff --git a/packages/env/docs-template/README.md b/packages/env/docs-template/README.md index 81a82239..2e2871e3 100644 --- a/packages/env/docs-template/README.md +++ b/packages/env/docs-template/README.md @@ -3,7 +3,7 @@ Judge runtime environment ## Support -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_microapp_tt__ __icon_quick_app__ ## Install ```bash @@ -12,7 +12,7 @@ $ npm install universal-env --save ## Usage ```javascript -import { isWeex, isWeb, isMiniApp, isNode, isWeChatMiniProgram, isQuickApp } from 'universal-env'; +import { isWeex, isWeb, isMiniApp, isNode, isWeChatMiniProgram, isByteDanceMicroApp, isQuickApp } from 'universal-env'; ``` @@ -29,6 +29,9 @@ Checks if environment is an alibaba miniprogram environment. ### `isWeChatMiniProgram: boolean` Checks if environment is a wechat miniprogram environment. +### `isByteDanceMicroApp: boolean` +Checks if environment is a bytedance microapp environment. + ### `isQuickApp: boolean` Checks if environment is a quickapp environment. @@ -37,16 +40,20 @@ Checks if environment is a Node.js environment. ## Results in each container -| | isMiniApp | isWeChatMiniProgram | isQuickApp | isWeb | isWeex | isNode | -| ---------------------------- | --------- | ------------------- | ---------- | ----- | ------ | ------ | -| Alibaba MiniApp | ✔️ | ✘ | ✘ | ✘ | ✘ | ✘ | -| Alibaba MiniApp WebView | ✔️ | ✘ | ✘ | ✔️ | ✘ | ✘ | -| Wechat MiniProgram | ✘ | ✔️ | ✘ | ✘ | ✘ | ✘ | -| Wechat MiniProgram WebView | ✘ | ✔️ | ✘ | ✔️ | ✘ | ✘ | -| QuickApp | ✘ | ✘ | ✔️ | ✘ | ✘ | ✘ | -| QuickApp WebView | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | -| Weex | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | -| Node | ✘ | ✘ | ✘ | ✘ | ✘ | ✔️ | -| Web | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | +Note:To judge MiniProgram env in WebView, please insert JS SDK accordingly. + +| | isMiniApp | isWeChatMiniProgram | isByteDanceMicroApp | isQuickApp | isWeb | isWeex | isNode | +| ----------------------------- | --------- | ------------------- | ------------------- | ---------- | ----- | ------ | ------ | +| Alibaba MiniApp | ✔️ | ✘ | ✘ | ✘ | ✘ | ✘ | ✘ | +| Alibaba MiniApp WebView | ✔️ | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | +| Wechat MiniProgram | ✘ | ✔️ | ✘ | ✘ | ✘ | ✘ | ✘ | +| Wechat MiniProgram WebView | ✘ | ✔️ | ✘ | ✘ | ✔️ | ✘ | ✘ | +| ByteDance MicroApp | ✘ | ✘ | ✔️ | ✘ | ✘ | ✘ | ✘ | +| ByteDance MicroApp WebView | ✘ | ✘ | ✔️ | ✘ | ✔️ | ✘ | ✘ | +| QuickApp | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | ✘ | +| QuickApp WebView | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | +| Weex | ✘ | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | +| Node | ✘ | ✘ | ✘ | ✘ | ✘ | ✘ | ✔️ | +| Web | ✘ | ✘ | ✘ | ✘ | ✔️ | ✘ | ✘ | diff --git a/packages/env/package.json b/packages/env/package.json index 756792cf..3fb35ec2 100644 --- a/packages/env/package.json +++ b/packages/env/package.json @@ -1,7 +1,7 @@ { "name": "universal-env", "author": "rax", - "version": "3.1.0", + "version": "3.2.0", "description": "", "main": "lib/index.js", "files": [ diff --git a/packages/env/src/index.ts b/packages/env/src/index.ts index 788e3ec8..cecd7b34 100644 --- a/packages/env/src/index.ts +++ b/packages/env/src/index.ts @@ -2,6 +2,7 @@ declare const WXEnvironment: any; declare const __kraken__: any; declare const my: any; declare const wx: any; +declare const tt: any; declare const global: any; function isUndef(type): boolean { @@ -13,7 +14,9 @@ export const isNode = !isUndef(typeof process) && !!(process.versions && process export const isWeex = !isUndef(typeof WXEnvironment) && WXEnvironment.platform !== 'Web'; export const isKraken = !isUndef(typeof __kraken__); export const isMiniApp = !isUndef(typeof my) && my !== null && !isUndef(typeof my.alert); +export const isByteDanceMicroApp = !isUndef(typeof tt) && tt !== null && !isUndef(typeof tt) && !isUndef(tt.showToast); // In wechat mini program, wx.login is a function // In wechat mini propgram webview, there is no wx.login, but exist wx.miniProgram -export const isWeChatMiniProgram = !isUndef(typeof wx) && wx !== null && (!isUndef(typeof wx.login) || !isUndef(typeof wx.miniProgram)); +// In bytedance maicro app, there is wx variable. +export const isWeChatMiniProgram = !isByteDanceMicroApp && !isUndef(typeof wx) && wx !== null && (!isUndef(typeof wx.login) || !isUndef(typeof wx.miniProgram)); export const isQuickApp = !isUndef(typeof global) && global !== null && !isUndef(typeof global.callNative) && !isWeex; diff --git a/scripts/compile-readme.js b/scripts/compile-readme.js index cb438123..16d5c6a2 100644 --- a/scripts/compile-readme.js +++ b/scripts/compile-readme.js @@ -25,12 +25,13 @@ function buildPackage(packagesDir, packageDir) { try { mdFileNames = fs.readdirSync(path.resolve(packageDir, DOCS_TEMP_DIR)); } catch (e) {}; - const iconArr = ['__icon_web__', '__icon_weex__', '__icon_miniapp_mp__', '__icon_miniapp_wx__', '__icon_quick_app__']; + const iconArr = ['__icon_web__', '__icon_weex__', '__icon_miniapp_mp__', '__icon_miniapp_wx__', '__icon_microapp_tt__', '__icon_quick_app__']; const iconMap = { __icon_web__: 'browser', __icon_weex__: 'weex', __icon_miniapp_mp__: 'miniApp', __icon_miniapp_wx__: 'wechatMiniprogram', + __icon_microapp_tt__: 'bytedanceMicroApp', __icon_quick_app__: 'quickApp' }; mdFileNames.forEach(mdFileName => { From 6c9c68a79467ba0ef348db0c6336b856fa3ff102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8B=92=E7=8B=92=E7=A5=9E?= <350740249@qq.com> Date: Fri, 3 Apr 2020 10:45:02 +0800 Subject: [PATCH 08/14] feat(device): support pixelRatio (#67) * feat: support pixelRatio * feat: support weex --- packages/device/.gitignore | 13 ----- packages/device/README-zh_CN.md | 13 +++-- packages/device/README.md | 13 +++-- packages/device/build.json | 11 ++++ packages/device/demo/index.js | 19 +++++++ packages/device/demo/index.tsx | 23 -------- packages/device/docs-template/README-zh_CN.md | 13 +++-- packages/device/docs-template/README.md | 13 +++-- packages/device/package.json | 54 ++++++------------- .../{miniapp/ali.ts => ali-miniapp/index.ts} | 3 ++ packages/device/src/index.ts | 6 ++- packages/device/src/types.ts | 14 ----- packages/device/src/web/index.ts | 3 ++ .../wechat.ts => wechat-miniprogram/index.ts} | 5 +- packages/device/src/weex/index.ts | 3 ++ packages/device/tsconfig.json | 27 ++++------ 16 files changed, 106 insertions(+), 127 deletions(-) delete mode 100755 packages/device/.gitignore create mode 100644 packages/device/build.json create mode 100644 packages/device/demo/index.js delete mode 100755 packages/device/demo/index.tsx mode change 100755 => 100644 packages/device/package.json rename packages/device/src/{miniapp/ali.ts => ali-miniapp/index.ts} (89%) delete mode 100755 packages/device/src/types.ts rename packages/device/src/{miniapp/wechat.ts => wechat-miniprogram/index.ts} (85%) mode change 100755 => 100644 packages/device/tsconfig.json diff --git a/packages/device/.gitignore b/packages/device/.gitignore deleted file mode 100755 index 3de44ff9..00000000 --- a/packages/device/.gitignore +++ /dev/null @@ -1,13 +0,0 @@ -*.swp -*.log - -.DS_Store -.idea/ -package-lock.json -yarn.lock -coverage/ -node_modules/ -build/ -dist/ -lib/ -_miniapp/ diff --git a/packages/device/README-zh_CN.md b/packages/device/README-zh_CN.md index f68c3aa6..87aeb7a7 100644 --- a/packages/device/README-zh_CN.md +++ b/packages/device/README-zh_CN.md @@ -18,17 +18,20 @@ import { platform, appName, screenWidth, screenHeight, appVersion } from 'univer ## APIS -### `appName: String` +### `appName: string` app 名称或浏览器名称。 -### `appVersion: String` +### `appVersion: string` app 版本号或浏览器版本信息。 -### `platform: String` +### `platform: string` 平台: 'Android'、'iOS'、'MacIntel' 等。 -### `screenWidth: Number` +### `screenWidth: number` 屏幕宽度,单位为px。 -### `screenHeight: Number` +### `screenHeight: number` 屏幕高度,单位为px。 + +### `devicePixelRatio: number` +当前显示设备的物理像素分辨率与CSS像素分辨率的比值。 diff --git a/packages/device/README.md b/packages/device/README.md index 3ca52011..92a1a61c 100644 --- a/packages/device/README.md +++ b/packages/device/README.md @@ -19,17 +19,20 @@ import { platform, appName, screenWidth, screenHeight, appVersion } from 'univer ## APIS -### `appName: String` +### `appName: string` app name or browser name -### `appVersion: String` +### `appVersion: string` app version or browser version info -### `platform: String` +### `platform: string` platform: 'Android'、'iOS'、'MacIntel', etc. -### `screenWidth: Number` +### `screenWidth: number` Screen width in px -### `screenHeight: Number` +### `screenHeight: number` Screen height in px + +### `devicePixelRatio: number` + The ratio of the resolution in physical pixels to the resolution in CSS pixels for the current display device. diff --git a/packages/device/build.json b/packages/device/build.json new file mode 100644 index 00000000..3edf1438 --- /dev/null +++ b/packages/device/build.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "build-plugin-rax-component", + { + "type": "rax", + "targets": ["web"] + } + ] + ] +} diff --git a/packages/device/demo/index.js b/packages/device/demo/index.js new file mode 100644 index 00000000..07863e18 --- /dev/null +++ b/packages/device/demo/index.js @@ -0,0 +1,19 @@ +// @ts-ignore +import { createElement, render } from 'rax'; +import DriverUniversal from 'driver-universal'; +import View from 'rax-view'; +import Text from 'rax-text'; +import { platform, appName, screenWidth, screenHeight } from '../src'; + +const App = () => { + return ( + + {platform} + {appName} + {screenWidth} + {screenHeight} + + ); +}; + +render(, document.body, { driver: DriverUniversal }); diff --git a/packages/device/demo/index.tsx b/packages/device/demo/index.tsx deleted file mode 100755 index ecaeb979..00000000 --- a/packages/device/demo/index.tsx +++ /dev/null @@ -1,23 +0,0 @@ -// @ts-ignore -import { createElement, render } from 'rax'; -import * as DriverDOM from 'driver-dom'; -import * as WeexDriver from 'driver-weex'; -import { isWeb } from 'universal-env'; -import View from 'rax-view'; -import Text from 'rax-text'; -import { platform, appName, screenWidth, screenHeight, px2rpx } from '../src/'; - -const App = () => { - console.log(px2rpx(1440)); - return ( - - {platform} - {appName} - {screenWidth} - {screenHeight} - {px2rpx(1440)} - - ); -}; - -render(, document.body, { driver: isWeb ? DriverDOM : WeexDriver }); \ No newline at end of file diff --git a/packages/device/docs-template/README-zh_CN.md b/packages/device/docs-template/README-zh_CN.md index b4e34b4c..336be7ea 100755 --- a/packages/device/docs-template/README-zh_CN.md +++ b/packages/device/docs-template/README-zh_CN.md @@ -18,17 +18,20 @@ import { platform, appName, screenWidth, screenHeight, appVersion } from 'univer ## APIS -### `appName: String` +### `appName: string` app 名称或浏览器名称。 -### `appVersion: String` +### `appVersion: string` app 版本号或浏览器版本信息。 -### `platform: String` +### `platform: string` 平台: 'Android'、'iOS'、'MacIntel' 等。 -### `screenWidth: Number` +### `screenWidth: number` 屏幕宽度,单位为px。 -### `screenHeight: Number` +### `screenHeight: number` 屏幕高度,单位为px。 + +### `devicePixelRatio: number` +当前显示设备的物理像素分辨率与CSS像素分辨率的比值。 diff --git a/packages/device/docs-template/README.md b/packages/device/docs-template/README.md index 31e1bae0..d89d6be3 100755 --- a/packages/device/docs-template/README.md +++ b/packages/device/docs-template/README.md @@ -19,17 +19,20 @@ import { platform, appName, screenWidth, screenHeight, appVersion } from 'univer ## APIS -### `appName: String` +### `appName: string` app name or browser name -### `appVersion: String` +### `appVersion: string` app version or browser version info -### `platform: String` +### `platform: string` platform: 'Android'、'iOS'、'MacIntel', etc. -### `screenWidth: Number` +### `screenWidth: number` Screen width in px -### `screenHeight: Number` +### `screenHeight: number` Screen height in px + +### `devicePixelRatio: number` + The ratio of the resolution in physical pixels to the resolution in CSS pixels for the current display device. diff --git a/packages/device/package.json b/packages/device/package.json old mode 100755 new mode 100644 index 515932fb..ccf95561 --- a/packages/device/package.json +++ b/packages/device/package.json @@ -1,19 +1,19 @@ { "name": "universal-device", "author": "rax", - "version": "2.0.0", + "version": "2.1.0", "license": "BSD-3-Clause", "main": "lib/index.js", - "types": "lib/index.d.ts", + "files": [ + "src", + "lib", + "dist" + ], "scripts": { - "start": "rax-scripts start --type 'component' -p 8000", - "start:miniapp": "rax-scripts start --type 'component-miniapp'", - "build": "rax-scripts build --type 'component'", - "test": "rax-scripts test", - "lint": "eslint --ext .js --ext .jsx --ext .ts --ext .tsx ./src", - "prebuild": "npm run lint && npm run test", - "prepublish": "npm run build" + "start": "build-scripts start", + "build": "build-scripts build" }, + "pre-commit": ["lint"], "keywords": [ "Rax" ], @@ -24,32 +24,12 @@ "universal-env": "^3.0.0" }, "devDependencies": { - "@types/jest": "^24.0.12", - "@typescript-eslint/eslint-plugin": "^1.7.0", - "@typescript-eslint/parser": "^1.7.0", - "babel-eslint": "^10.0.1", - "driver-dom": "^1.0.0", - "driver-weex": "^1.0.0", - "eslint": "^5.15.1", - "eslint-config-rax": "^0.0.0", - "eslint-plugin-import": "^2.17.2", - "eslint-plugin-react": "^7.12.4", - "rax": "^1.0.4", - "rax-scripts": "1.2.1-beta.2", - "rax-text": "^1.0.0", - "rax-view": "^1.0.0", - "typescript": "^3.4.5" - }, - "repository": { - "type": "git", - "url": "https://github.com/raxjs/universal-api.git" - }, - "bugs": { - "url": "https://github.com/raxjs/universal-api/issues" - }, - "homepage": "https://github.com/raxjs/universal-api#readme", - "pre-commit": [ - "lint", - "test" - ] + "typescript": "^3.7.5", + "driver-universal": "^3.0.0", + "rax": "^1.1.0", + "rax-text": "^1.0.1", + "rax-view": "^1.0.2", + "build-plugin-rax-component": "^0.2.0", + "@alib/build-scripts": "^0.1.0" + } } diff --git a/packages/device/src/miniapp/ali.ts b/packages/device/src/ali-miniapp/index.ts similarity index 89% rename from packages/device/src/miniapp/ali.ts rename to packages/device/src/ali-miniapp/index.ts index 5dc3949c..9b6a848e 100644 --- a/packages/device/src/miniapp/ali.ts +++ b/packages/device/src/ali-miniapp/index.ts @@ -23,6 +23,9 @@ const module = {}; }, { key: 'appVersion', getFn: () => getSystemInfo().version +}, { + key: 'devicePixelRatio', + getFn: () => getSystemInfo().pixelRatio }].forEach(({key, getFn}) => { Object.defineProperty(module, key, { get: getFn diff --git a/packages/device/src/index.ts b/packages/device/src/index.ts index bd31e73f..dc041d2b 100755 --- a/packages/device/src/index.ts +++ b/packages/device/src/index.ts @@ -1,8 +1,8 @@ import { isWeb, isWeex, isMiniApp, isWeChatMiniProgram } from 'universal-env'; import webModule from './web'; import weexModule from './weex'; -import miniappModule from './miniapp/ali'; -import wechatModule from './miniapp/wechat'; +import miniappModule from './ali-miniapp'; +import wechatModule from './wechat-miniprogram'; function dutyChain(...fns) { for (let i = 0; i < fns.length; i++) { @@ -53,6 +53,7 @@ const appVersion = deviceInfo.appVersion; const platform = deviceInfo.platform; const screenWidth = deviceInfo.screenWidth; const screenHeight = deviceInfo.screenHeight; +const devicePixelRatio = deviceInfo.devicePixelRatio; export { appName, @@ -60,4 +61,5 @@ export { platform, screenWidth, screenHeight, + devicePixelRatio }; diff --git a/packages/device/src/types.ts b/packages/device/src/types.ts deleted file mode 100755 index 33ab5511..00000000 --- a/packages/device/src/types.ts +++ /dev/null @@ -1,14 +0,0 @@ -export interface Device { - platform: string; - appName: string; - screenWidth: number; - screenHeight: number; -} - -export interface MiniAppSystem { - app: string; - platform: string; - screenWidth: number; - screenHeight: number; - [propName: string]: any; -} diff --git a/packages/device/src/web/index.ts b/packages/device/src/web/index.ts index f494546b..ec5db32f 100644 --- a/packages/device/src/web/index.ts +++ b/packages/device/src/web/index.ts @@ -31,6 +31,9 @@ let platform; }, { key: 'appVersion', getFn: () => navigator.appVersion +}, { + key: 'devicePixelRatio', + getFn: () => window.devicePixelRatio }].forEach(({key, getFn}) => { Object.defineProperty(module, key, { get: getFn diff --git a/packages/device/src/miniapp/wechat.ts b/packages/device/src/wechat-miniprogram/index.ts similarity index 85% rename from packages/device/src/miniapp/wechat.ts rename to packages/device/src/wechat-miniprogram/index.ts index 3136bd06..45f3abe8 100644 --- a/packages/device/src/miniapp/wechat.ts +++ b/packages/device/src/wechat-miniprogram/index.ts @@ -9,7 +9,7 @@ const module = {}; [{ key: 'appName', - getFn: () => getSystemInfo().app + getFn: () => 'wechat' }, { key: 'platform', getFn: () => getSystemInfo().platform @@ -22,6 +22,9 @@ const module = {}; }, { key: 'appVersion', getFn: () => getSystemInfo().version +}, { + key: 'devicePixelRatio', + getFn: () => getSystemInfo().pixelRatio }].forEach(({key, getFn}) => { Object.defineProperty(module, key, { get: getFn diff --git a/packages/device/src/weex/index.ts b/packages/device/src/weex/index.ts index ae3a52d3..a4ded1ac 100644 --- a/packages/device/src/weex/index.ts +++ b/packages/device/src/weex/index.ts @@ -15,6 +15,9 @@ const module = {}; }, { key: 'appVersion', getFn: () => navigator.appVersion +}, { + key: 'devicePixelRatio', + getFn: () => window.devicePixelRatio }].forEach(({key, getFn}) => { Object.defineProperty(module, key, { get: getFn diff --git a/packages/device/tsconfig.json b/packages/device/tsconfig.json old mode 100755 new mode 100644 index 3cbc844a..b69dff42 --- a/packages/device/tsconfig.json +++ b/packages/device/tsconfig.json @@ -2,24 +2,17 @@ "compilerOptions": { "module": "esNext", "target": "es2015", - "jsx": "preserve", - "jsxFactory": "createElement", - + "outDir": "build", "moduleResolution": "node", - "baseUrl": ".", - - "rootDir": "src", - "downlevelIteration": true, + "sourceMap": true, "alwaysStrict": true, - "outDir": "lib", - "strict": true, - "preserveConstEnums": true, - "experimentalDecorators": true, - "noImplicitReturns": true, - "noUnusedLocals": true, - "noImplicitAny": false, - "noImplicitThis": false + "baseUrl": "." }, - "include": ["src/**/*"], - "exclude": ["**/__tests__"] + "include": [ + "./src/**/*" + ], + "exclude": [ + "demo", + "**/__tests__" + ] } From 0093fdbdf432c9e1b06cc879de4948de80a56f6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8B=92=E7=8B=92=E7=A5=9E?= <350740249@qq.com> Date: Tue, 7 Apr 2020 14:17:52 +0800 Subject: [PATCH 09/14] fix: compaty node (#68) --- packages/device/package.json | 2 +- packages/device/src/index.ts | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/device/package.json b/packages/device/package.json index ccf95561..71ba8655 100644 --- a/packages/device/package.json +++ b/packages/device/package.json @@ -1,7 +1,7 @@ { "name": "universal-device", "author": "rax", - "version": "2.1.0", + "version": "2.1.1", "license": "BSD-3-Clause", "main": "lib/index.js", "files": [ diff --git a/packages/device/src/index.ts b/packages/device/src/index.ts index dc041d2b..ba42aa5e 100755 --- a/packages/device/src/index.ts +++ b/packages/device/src/index.ts @@ -41,11 +41,16 @@ function handleWeChat() { return null; } +function handleDefault() { + return {}; +} + const deviceInfo = dutyChain( handleWeb, handleWeex, handleMiniApp, - handleWeChat + handleWeChat, + handleDefault ); const appName = deviceInfo.appName; From e1e7d2eaac0ac05e4005358059230151311ff94a Mon Sep 17 00:00:00 2001 From: metaJS Date: Fri, 10 Apr 2020 17:44:40 +0800 Subject: [PATCH 10/14] universal-unit-tool pkg (#70) * feat: init commit for unit-tool * update: remove test script * chore: clean dep * chore: update build script * update: move doc to folder * update: doc generate --- packages/unit-tool/.npmignore | 10 +++++ packages/unit-tool/README-zh_CN.md | 30 +++++++++++++ packages/unit-tool/README.md | 30 +++++++++++++ packages/unit-tool/build.json | 11 +++++ .../unit-tool/docs-template/README-zh_CN.md | 30 +++++++++++++ packages/unit-tool/docs-template/README.md | 30 +++++++++++++ packages/unit-tool/package.json | 44 +++++++++++++++++++ packages/unit-tool/src/index.ts | 11 +++++ packages/unit-tool/tsconfig.json | 25 +++++++++++ 9 files changed, 221 insertions(+) create mode 100644 packages/unit-tool/.npmignore create mode 100644 packages/unit-tool/README-zh_CN.md create mode 100644 packages/unit-tool/README.md create mode 100644 packages/unit-tool/build.json create mode 100644 packages/unit-tool/docs-template/README-zh_CN.md create mode 100644 packages/unit-tool/docs-template/README.md create mode 100644 packages/unit-tool/package.json create mode 100644 packages/unit-tool/src/index.ts create mode 100644 packages/unit-tool/tsconfig.json diff --git a/packages/unit-tool/.npmignore b/packages/unit-tool/.npmignore new file mode 100644 index 00000000..0f00844e --- /dev/null +++ b/packages/unit-tool/.npmignore @@ -0,0 +1,10 @@ +*.swp +*.log + +.DS_Store +.idea/ +package-lock.json +coverage/ +node_modules/ +build/ +demo/ diff --git a/packages/unit-tool/README-zh_CN.md b/packages/unit-tool/README-zh_CN.md new file mode 100644 index 00000000..f8876e53 --- /dev/null +++ b/packages/unit-tool/README-zh_CN.md @@ -0,0 +1,30 @@ +# universal-unit-tool [![npm](https://img.shields.io/npm/v/universal-unit-tool.svg)](https://www.npmjs.com/package/universal-unit-tool) + +工具库 + +## 支持 + +browser weex miniApp wechatMiniprogram bytedanceMicroApp quickApp + +## 安装 + +```bash +$ npm install universal-unit-tool --save +``` +## 示例 + +```js +import { px2rpx, rpx2px } from 'universal-unit-tool'; +``` + +## 方法 +### `px2rpx(val)` +px 转 rpx 方法。 + +### `rpx2px(val)` +rpx 转 px 方法。 + +#### 入参 +| 成员 | 类型 | 描述 | 默认值 | 支持 | +| ---- | -------- | ----- | :----: | :---: | +| val | `number` | px 值 | - | | diff --git a/packages/unit-tool/README.md b/packages/unit-tool/README.md new file mode 100644 index 00000000..4cde560d --- /dev/null +++ b/packages/unit-tool/README.md @@ -0,0 +1,30 @@ +# universal-unit-tool [![npm](https://img.shields.io/npm/v/universal-unit-tool.svg)](https://www.npmjs.com/package/universal-unit-tool) + +Unit tool + +## Supported + +browser weex miniApp wechatMiniprogram bytedanceMicroApp quickApp + +## Install + +```bash +$ npm install universal-unit-tool --save +``` +## Usage + +```js +import { px2rpx, rpx2px } from 'universal-unit-tool'; +``` + +## APIS +### `px2rpx(val)` +px to rpx method + +### `rpx2px(val)` +rpx to px method + +#### Arguments +| Property | Type | Describes | Default | Supports | +| -------- | -------- | --------- | :-----: | :------: | +| val | `number` | px value | - | | diff --git a/packages/unit-tool/build.json b/packages/unit-tool/build.json new file mode 100644 index 00000000..3edf1438 --- /dev/null +++ b/packages/unit-tool/build.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "build-plugin-rax-component", + { + "type": "rax", + "targets": ["web"] + } + ] + ] +} diff --git a/packages/unit-tool/docs-template/README-zh_CN.md b/packages/unit-tool/docs-template/README-zh_CN.md new file mode 100644 index 00000000..9a171ee6 --- /dev/null +++ b/packages/unit-tool/docs-template/README-zh_CN.md @@ -0,0 +1,30 @@ +# universal-unit-tool [![npm](https://img.shields.io/npm/v/universal-unit-tool.svg)](https://www.npmjs.com/package/universal-unit-tool) + +工具库 + +## 支持 + +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_microapp_tt__ __icon_quick_app__ + +## 安装 + +```bash +$ npm install universal-unit-tool --save +``` +## 示例 + +```js +import { px2rpx, rpx2px } from 'universal-unit-tool'; +``` + +## 方法 +### `px2rpx(val)` +px 转 rpx 方法。 + +### `rpx2px(val)` +rpx 转 px 方法。 + +#### 入参 +| 成员 | 类型 | 描述 | 默认值 | 支持 | +| ---- | -------- | ----- | :----: | :---: | +| val | `number` | px 值 | - | | diff --git a/packages/unit-tool/docs-template/README.md b/packages/unit-tool/docs-template/README.md new file mode 100644 index 00000000..f784107a --- /dev/null +++ b/packages/unit-tool/docs-template/README.md @@ -0,0 +1,30 @@ +# universal-unit-tool [![npm](https://img.shields.io/npm/v/universal-unit-tool.svg)](https://www.npmjs.com/package/universal-unit-tool) + +Unit tool + +## Supported + +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_microapp_tt__ __icon_quick_app__ + +## Install + +```bash +$ npm install universal-unit-tool --save +``` +## Usage + +```js +import { px2rpx, rpx2px } from 'universal-unit-tool'; +``` + +## APIS +### `px2rpx(val)` +px to rpx method + +### `rpx2px(val)` +rpx to px method + +#### Arguments +| Property | Type | Describes | Default | Supports | +| -------- | -------- | --------- | :-----: | :------: | +| val | `number` | px value | - | | diff --git a/packages/unit-tool/package.json b/packages/unit-tool/package.json new file mode 100644 index 00000000..1911c8fb --- /dev/null +++ b/packages/unit-tool/package.json @@ -0,0 +1,44 @@ +{ + "name": "universal-unit-tool", + "version": "1.0.0", + "license": "BSD-3-Clause", + "description": "A universal tool API.", + "main": "lib/index.js", + "scripts": { + "start": "build-scripts start", + "build": "build-scripts build", + "lint": "eslint --ext .js --ext .jsx --ext .ts --ext .tsx ./src", + "prebuild": "npm run lint", + "prepublish": "npm run build" + }, + "keywords": [ + "universal" + ], + "repository": "https://github.com/raxjs/universal-api.git", + "bugs": { + "url": "https://github.com/raxjs/universal-api/issues" + }, + "homepage": "https://github.com/raxjs/universal-api#readme", + "dependencies": { + "universal-device": "^2.1.1" + }, + "devDependencies": { + "@types/jest": "^24.0.18", + "@typescript-eslint/eslint-plugin": "^1.7.0", + "@typescript-eslint/parser": "^1.7.0", + "babel-eslint": "^10.0.2", + "eslint": "5.15.1", + "eslint-config-rax": "^0.0.0", + "eslint-plugin-import": "^2.18.2", + "eslint-plugin-react": "^7.12.4", + "pre-commit": "^1.2.2", + "rax": "^1.0.8", + "build-plugin-rax-component": "^0.2.0", + "@alib/build-scripts": "^0.1.0" + + }, + "pre-commit": [ + "lint", + "test" + ] +} diff --git a/packages/unit-tool/src/index.ts b/packages/unit-tool/src/index.ts new file mode 100644 index 00000000..83141438 --- /dev/null +++ b/packages/unit-tool/src/index.ts @@ -0,0 +1,11 @@ +import { screenWidth } from 'universal-device'; + +const CALCULATION_ACCURACY = 8; + +export const px2rpx = (value: number) => { + return Number((750 * value / screenWidth).toFixed(CALCULATION_ACCURACY)); +}; + +export const rpx2px = (value: number) => { + return Number((screenWidth / 750 * value).toFixed(CALCULATION_ACCURACY)); +}; diff --git a/packages/unit-tool/tsconfig.json b/packages/unit-tool/tsconfig.json new file mode 100644 index 00000000..70b1d932 --- /dev/null +++ b/packages/unit-tool/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "module": "esNext", + "target": "es5", + "jsx": "preserve", + "jsxFactory": "createElement", + + "moduleResolution": "node", + "baseUrl": ".", + + "rootDir": "src", + "downlevelIteration": true, + "alwaysStrict": true, + "outDir": "lib", + "strict": true, + "preserveConstEnums": true, + "experimentalDecorators": true, + "noImplicitReturns": true, + "noUnusedLocals": true, + "noImplicitAny": false, + "noImplicitThis": false + }, + "include": ["src/**/*"], + "exclude": ["**/__tests__"] +} From b70ef514361c68af2e66ba77953ae7954fe0a81c Mon Sep 17 00:00:00 2001 From: broven Date: Wed, 15 Apr 2020 16:00:33 +0800 Subject: [PATCH 11/14] chore: bump async version --- packages/asyncstorage/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/asyncstorage/package.json b/packages/asyncstorage/package.json index d83c7f48..781f983b 100644 --- a/packages/asyncstorage/package.json +++ b/packages/asyncstorage/package.json @@ -1,6 +1,6 @@ { "name": "universal-asyncstorage", - "version": "1.1.0", + "version": "1.2.0", "description": "A universal AsyncStorage API.", "license": "BSD-3-Clause", "main": "lib/index.js", From da6f879f335f95d635bcef7ef6501609e14e0172 Mon Sep 17 00:00:00 2001 From: broven Date: Wed, 15 Apr 2020 16:23:13 +0800 Subject: [PATCH 12/14] chore: remove quickapp --- packages/asyncstorage/src/index.ts | 7 +-- packages/asyncstorage/src/quickapp/index.js | 70 --------------------- 2 files changed, 2 insertions(+), 75 deletions(-) delete mode 100644 packages/asyncstorage/src/quickapp/index.js diff --git a/packages/asyncstorage/src/index.ts b/packages/asyncstorage/src/index.ts index 16269427..c51c87bd 100644 --- a/packages/asyncstorage/src/index.ts +++ b/packages/asyncstorage/src/index.ts @@ -1,18 +1,15 @@ -import { isWeb, isWeex, isMiniApp, isWeChatMiniProgram, isByteDanceMicroApp, isQuickApp } from 'universal-env'; +import { isWeb, isWeex, isMiniApp, isWeChatMiniProgram, isByteDanceMicroApp } from 'universal-env'; import webModule from './web/index'; import weexModule from './weex/index'; import miniAppModule from './miniapp/ali/index'; import weChatModule from './miniapp/wechat/index'; import bytedanceModule from './miniapp/bytedance/index'; -import quickModule from './quickapp/index'; import { AsyncStorage } from './types'; let AsyncStorage: AsyncStorage; -if (isQuickApp) { - AsyncStorage = quickModule; -} else if (isWeb) { +if (isWeb) { AsyncStorage = webModule; } else if (isWeex) { AsyncStorage = weexModule; diff --git a/packages/asyncstorage/src/quickapp/index.js b/packages/asyncstorage/src/quickapp/index.js deleted file mode 100644 index d4d18fff..00000000 --- a/packages/asyncstorage/src/quickapp/index.js +++ /dev/null @@ -1,70 +0,0 @@ -function setItem(key, value) { - return new Promise(function(resolve, reject) { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const storage = require('@system.storage'); - storage.set({ - key, - value, - success: function(data) { - resolve(data); - }, - fail: function(data, code) { - reject(`handling fail, code = ${code}`); - } - }); - }); -} - -function getItem(key) { - return new Promise(function(resolve, reject) { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const storage = require('@system.storage'); - storage.get({ - key, - success: function(data) { - resolve(data); - }, - fail: function(data, code) { - reject(`handling fail, code = ${code}`); - } - }); - }); -} - -function removeItem(key) { - return new Promise(function(resolve, reject) { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const storage = require('@system.storage'); - storage.delete({ - key, - success: function(data) { - resolve(data); - }, - fail: function(data, code) { - reject(`handling fail, code = ${code}`); - } - }); - }); -} - -function clear() { - return new Promise(function(resolve, reject) { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const storage = require('@system.storage'); - storage.clear({ - success: function(data) { - resolve(data); - }, - fail: function(data, code) { - reject(`handling fail, code = ${code}`); - } - }); - }); -} - -module.exports = { - setItem, - getItem, - removeItem, - clear -}; From 732fa583040d854b2bfcc9c850486d91af11cad7 Mon Sep 17 00:00:00 2001 From: broven Date: Wed, 15 Apr 2020 16:26:39 +0800 Subject: [PATCH 13/14] update: doc --- packages/asyncstorage/README-zh_CN.md | 2 +- packages/asyncstorage/README.md | 2 +- packages/asyncstorage/docs-template/README-zh_CN.md | 2 +- packages/asyncstorage/docs-template/README.md | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/asyncstorage/README-zh_CN.md b/packages/asyncstorage/README-zh_CN.md index c65337df..1cf0e393 100644 --- a/packages/asyncstorage/README-zh_CN.md +++ b/packages/asyncstorage/README-zh_CN.md @@ -3,7 +3,7 @@ > AsyncStorage ## 支持 -browser weex miniApp wechatMiniprogram quickApp bytedanceMicroApp +browser weex miniApp wechatMiniprogram bytedanceMicroApp ## 安装 diff --git a/packages/asyncstorage/README.md b/packages/asyncstorage/README.md index 1c9b919e..c171549c 100644 --- a/packages/asyncstorage/README.md +++ b/packages/asyncstorage/README.md @@ -3,7 +3,7 @@ > AsyncStorage ## Support -browser weex miniApp wechatMiniprogram quickApp bytedanceMicroApp +browser weex miniApp wechatMiniprogram bytedanceMicroApp ## Install diff --git a/packages/asyncstorage/docs-template/README-zh_CN.md b/packages/asyncstorage/docs-template/README-zh_CN.md index c97b5869..e8ce65a9 100644 --- a/packages/asyncstorage/docs-template/README-zh_CN.md +++ b/packages/asyncstorage/docs-template/README-zh_CN.md @@ -3,7 +3,7 @@ > AsyncStorage ## 支持 -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ __icon_microapp_tt__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_microapp_tt__ ## 安装 diff --git a/packages/asyncstorage/docs-template/README.md b/packages/asyncstorage/docs-template/README.md index 0cbd1242..3c1fff38 100644 --- a/packages/asyncstorage/docs-template/README.md +++ b/packages/asyncstorage/docs-template/README.md @@ -3,7 +3,7 @@ > AsyncStorage ## Support -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ __icon_microapp_tt__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_microapp_tt__ ## Install From 01d8123878f13feb58ac0327fd889ba467a71b18 Mon Sep 17 00:00:00 2001 From: broven Date: Wed, 15 Apr 2020 17:20:16 +0800 Subject: [PATCH 14/14] doc: remove quickapp --- packages/request/README-zh_CN.md | 2 +- packages/request/README.md | 2 +- packages/request/docs-template/README-zh_CN.md | 2 +- packages/request/docs-template/README.md | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/request/README-zh_CN.md b/packages/request/README-zh_CN.md index b1423d1c..051df8fa 100644 --- a/packages/request/README-zh_CN.md +++ b/packages/request/README-zh_CN.md @@ -31,7 +31,7 @@ request({ #### 支持 -browser weex miniApp wechatMiniprogram quickApp bytedanceMicroApp +browser weex miniApp wechatMiniprogram bytedanceMicroApp #### 参数 diff --git a/packages/request/README.md b/packages/request/README.md index 259ff0de..e241ec63 100644 --- a/packages/request/README.md +++ b/packages/request/README.md @@ -31,7 +31,7 @@ request({ #### Supported -browser weex miniApp wechatMiniprogram quickApp bytedanceMicroApp +browser weex miniApp wechatMiniprogram bytedanceMicroApp #### Arguments diff --git a/packages/request/docs-template/README-zh_CN.md b/packages/request/docs-template/README-zh_CN.md index 39d9b687..5fb1098b 100644 --- a/packages/request/docs-template/README-zh_CN.md +++ b/packages/request/docs-template/README-zh_CN.md @@ -31,7 +31,7 @@ request({ #### 支持 -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ __icon_microapp_tt__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_microapp_tt__ #### 参数 diff --git a/packages/request/docs-template/README.md b/packages/request/docs-template/README.md index 73eb7e39..5415735d 100644 --- a/packages/request/docs-template/README.md +++ b/packages/request/docs-template/README.md @@ -31,7 +31,7 @@ request({ #### Supported -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ __icon_microapp_tt__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_microapp_tt__ #### Arguments