@hylid/env 2.12.0-alpha.41 → 2.12.0-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/getClientName/getName4Mp.js +1 -3
- package/lib/getClientName/getName4MpWeb.d.ts +0 -5
- package/lib/getClientName/getName4MpWeb.js +2 -17
- package/lib/getClientName/getName4Web.d.ts +1 -1
- package/lib/getClientName/getName4Web.js +2 -12
- package/lib/index.d.ts +5 -2
- package/lib/index.js +30 -25
- package/lib/utils.d.ts +2 -3
- package/lib/utils.js +16 -9
- package/package.json +3 -4
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { mpChecker } from "../checker";
|
|
2
2
|
import { CLIENT } from "../constants";
|
|
3
|
-
import { getMatchName
|
|
3
|
+
import { getMatchName } from "../utils";
|
|
4
4
|
export function getName4MP() {
|
|
5
5
|
// 微信小程序
|
|
6
6
|
// @ts-ignore
|
|
7
7
|
if (typeof wx !== 'undefined') return CLIENT.WECHAT;
|
|
8
|
-
// 支付宝系小程序
|
|
9
|
-
if (isNotMP()) return;
|
|
10
8
|
return getMatchName(mpChecker, my.getSystemInfoSync().app);
|
|
11
9
|
}
|
|
@@ -1,20 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { getMatchName, getUrlClientName, getStorageClientName } from "../utils";
|
|
4
|
-
/**
|
|
5
|
-
* 1. 优先从 URL 解析 __app__
|
|
6
|
-
* 2. 其次从 Storage 解析 __app__
|
|
7
|
-
* 3. 再之解析 userAgent
|
|
8
|
-
*/
|
|
1
|
+
import { mpWebChecker } from "../checker";
|
|
2
|
+
import { getMatchName } from "../utils";
|
|
9
3
|
export function getName4MpWeb() {
|
|
10
|
-
var appName = getUrlClientName() || getStorageClientName();
|
|
11
|
-
if (appName) {
|
|
12
|
-
// 如果 hylid-bridge 的 client 枚举值找到了 app,则优先用 hylid-bridge 的
|
|
13
|
-
if (Object.keys(CLIENT).find(function (i) {
|
|
14
|
-
return CLIENT[i] === appName;
|
|
15
|
-
})) return appName;
|
|
16
|
-
// 套壳的 __app__ 参数,一般是壳子拼的,值从 getSystemInfoSync 取的,所以使用 mpChecker
|
|
17
|
-
return getMatchName(mpChecker, appName);
|
|
18
|
-
}
|
|
19
4
|
return getMatchName(mpWebChecker, window.navigator.userAgent);
|
|
20
5
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function getName4Web(): string
|
|
1
|
+
export declare function getName4Web(): string;
|
|
@@ -1,16 +1,6 @@
|
|
|
1
|
-
import { webChecker
|
|
1
|
+
import { webChecker } from "../checker";
|
|
2
2
|
import { CLIENT } from "../constants";
|
|
3
|
-
import { getMatchName
|
|
3
|
+
import { getMatchName } from "../utils";
|
|
4
4
|
export function getName4Web() {
|
|
5
|
-
var appName = getUrlClientName() || getStorageClientName();
|
|
6
|
-
// 无法识别的,可以手动指定 __app__ 参数
|
|
7
|
-
if (appName) {
|
|
8
|
-
// 如果 hylid-bridge 的 client 枚举值找到了 app,则优先用 hylid-bridge 的
|
|
9
|
-
if (Object.keys(CLIENT).find(function (i) {
|
|
10
|
-
return CLIENT[i] === appName;
|
|
11
|
-
})) return appName;
|
|
12
|
-
// 指定的 __app__ 和小程序返回值对齐
|
|
13
|
-
return getMatchName(mpChecker, appName);
|
|
14
|
-
}
|
|
15
5
|
return getMatchName(webChecker, window.navigator.userAgent) || CLIENT.H5;
|
|
16
6
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { ClientEnv, PlatformEnv } from './types';
|
|
2
|
-
declare
|
|
3
|
-
|
|
2
|
+
export declare function getDefaultDiscernEnv(): {
|
|
3
|
+
client: string | undefined;
|
|
4
|
+
platform: string | undefined;
|
|
5
|
+
};
|
|
6
|
+
declare let client: string | undefined, platform: string | undefined;
|
|
4
7
|
interface CustomEnv {
|
|
5
8
|
client?: string;
|
|
6
9
|
platform?: string;
|
package/lib/index.js
CHANGED
|
@@ -1,34 +1,39 @@
|
|
|
1
|
-
import { createCommonEnv, notice } from "./utils";
|
|
1
|
+
import { createCommonEnv, getSettingName, isMP, notice } from "./utils";
|
|
2
2
|
import { getName4MP, getName4MpWeb, getName4Web } from "./getClientName";
|
|
3
3
|
import { CLIENT, PLATFORM } from "./constants";
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* 不满足上面的条件时,判断为端外 H5
|
|
11
|
-
*/
|
|
12
|
-
// 小程序
|
|
13
|
-
client = getName4MP();
|
|
14
|
-
if (client) {
|
|
15
|
-
platform = PLATFORM.MP;
|
|
16
|
-
} else {
|
|
17
|
-
// 套壳 H5
|
|
18
|
-
client = getName4MpWeb();
|
|
19
|
-
if (client) {
|
|
20
|
-
platform = PLATFORM.MPWEB;
|
|
4
|
+
export function getDefaultDiscernEnv() {
|
|
5
|
+
var client;
|
|
6
|
+
var platform;
|
|
7
|
+
if (isMP()) {
|
|
8
|
+
client = getName4MP();
|
|
9
|
+
platform = PLATFORM.MP;
|
|
21
10
|
} else {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
11
|
+
var setting = getSettingName();
|
|
12
|
+
if (setting.platform) {
|
|
13
|
+
platform = setting.platform;
|
|
14
|
+
client = setting.client || (platform === 'mpweb' ? getName4MpWeb() : getName4Web());
|
|
15
|
+
} else {
|
|
16
|
+
// 如果只设置了 client,默认是套壳场景
|
|
17
|
+
client = setting.client || getName4MpWeb();
|
|
18
|
+
if (client) {
|
|
19
|
+
platform = PLATFORM.MPWEB;
|
|
20
|
+
} else {
|
|
21
|
+
client = getName4Web();
|
|
22
|
+
platform = PLATFORM.WEB;
|
|
23
|
+
}
|
|
26
24
|
}
|
|
27
25
|
}
|
|
26
|
+
if (!platform || !client) {
|
|
27
|
+
notice('Cannot identify your client.');
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
client: client,
|
|
31
|
+
platform: platform
|
|
32
|
+
};
|
|
28
33
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
var _a = getDefaultDiscernEnv(),
|
|
35
|
+
client = _a.client,
|
|
36
|
+
platform = _a.platform;
|
|
32
37
|
export function customEnv(config) {
|
|
33
38
|
var custom = typeof config === 'function' ? config({
|
|
34
39
|
client: client,
|
package/lib/utils.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Checker } from './types';
|
|
2
|
-
export declare function
|
|
2
|
+
export declare function isMP(): boolean;
|
|
3
3
|
export declare function getMatchName(checks: Checker[], pattern: string): string | undefined;
|
|
4
4
|
export declare function createCommonEnv<T>(source: Record<string, string>, current?: string): T;
|
|
5
5
|
export declare function notice(text: string): void;
|
|
6
|
-
export declare function
|
|
7
|
-
export declare function getStorageClientName(): string | null;
|
|
6
|
+
export declare function getSettingName(): any;
|
package/lib/utils.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
export function
|
|
1
|
+
export function isMP() {
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
if (typeof wx !== 'undefined') return true;
|
|
2
4
|
// 小程序一定有 my
|
|
3
|
-
if (typeof my
|
|
5
|
+
if (typeof my === 'undefined') return false;
|
|
4
6
|
// SDKVersion 只在小程序有
|
|
5
|
-
if (typeof my.SDKVersion
|
|
7
|
+
if (typeof my.SDKVersion === 'string') return true;
|
|
6
8
|
return false;
|
|
7
9
|
}
|
|
8
10
|
export function getMatchName(checks, pattern) {
|
|
@@ -30,10 +32,15 @@ export function createCommonEnv(source, current) {
|
|
|
30
32
|
export function notice(text) {
|
|
31
33
|
console.info("[hylid-env]: ".concat(text));
|
|
32
34
|
}
|
|
33
|
-
export function
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
export function getSettingName() {
|
|
36
|
+
var client = (window.location.search.match(/[?&]__app__=([^?&=]+)/) || [])[1];
|
|
37
|
+
var platform = (window.location.search.match(/[?&]__platform__=([^?&=]+)/) || [])[1];
|
|
38
|
+
if (client || platform) {
|
|
39
|
+
localStorage.setItem('__hy_env__', JSON.stringify({
|
|
40
|
+
client: client,
|
|
41
|
+
platform: platform
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
var cached = localStorage.getItem('__hy_env__');
|
|
45
|
+
return cached ? JSON.parse(cached) : {};
|
|
39
46
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hylid/env",
|
|
3
|
-
"version": "2.12.0-alpha.
|
|
3
|
+
"version": "2.12.0-alpha.9",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"files": [
|
|
6
6
|
"lib"
|
|
@@ -10,6 +10,5 @@
|
|
|
10
10
|
},
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"sideEffects": false,
|
|
13
|
-
"registry": "https://registry.npmjs.org/"
|
|
14
|
-
|
|
15
|
-
}
|
|
13
|
+
"registry": "https://registry.npmjs.org/"
|
|
14
|
+
}
|