-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.ts
48 lines (43 loc) · 2.07 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { Framework } from '../types';
import { reactWebDetect, reactSSRDetect } from './React';
import { vueWebDetect, vueSSRDetect } from './Vue';
import { svelteWebDetect, svelteSSRDetect } from './Svelte';
import { nextWebDetect, nextSSRDetect } from './Next';
import { nuxtWebDetect, nuxtSSRDetect } from './Nuxt';
import { angularWebDetect, angularSSRDetect } from './Angular';
import { reactNativeDetect } from './ReactNative';
import { expoDetect } from './Expo';
import { webDetect } from './Web';
type PlatformDetectionEntry = {
platform: Framework;
detectionMethod: () => boolean;
};
// These are in the order of detection where when both are detectable, the early Framework will be reported
const detectionMap: PlatformDetectionEntry[] = [
// First, detect mobile
{ platform: Framework.Expo, detectionMethod: expoDetect },
{ platform: Framework.ReactNative, detectionMethod: reactNativeDetect },
// Next, detect web frameworks
{ platform: Framework.NextJs, detectionMethod: nextWebDetect },
{ platform: Framework.Nuxt, detectionMethod: nuxtWebDetect },
{ platform: Framework.Angular, detectionMethod: angularWebDetect },
{ platform: Framework.React, detectionMethod: reactWebDetect },
{ platform: Framework.VueJs, detectionMethod: vueWebDetect },
{ platform: Framework.Svelte, detectionMethod: svelteWebDetect },
{ platform: Framework.WebUnknown, detectionMethod: webDetect },
// Last, detect ssr frameworks
{ platform: Framework.NextJsSSR, detectionMethod: nextSSRDetect },
{ platform: Framework.NuxtSSR, detectionMethod: nuxtSSRDetect },
{ platform: Framework.ReactSSR, detectionMethod: reactSSRDetect },
{ platform: Framework.VueJsSSR, detectionMethod: vueSSRDetect },
{ platform: Framework.AngularSSR, detectionMethod: angularSSRDetect },
{ platform: Framework.SvelteSSR, detectionMethod: svelteSSRDetect },
];
export function detect() {
return (
detectionMap.find(detectionEntry => detectionEntry.detectionMethod())
?.platform || Framework.ServerSideUnknown
);
}