From f88aa8db01c402eda6e8382e7c4e8960bf6f0681 Mon Sep 17 00:00:00 2001 From: Lo Date: Wed, 6 Mar 2024 09:32:57 +0800 Subject: [PATCH] feat: add window.__VITEPRESS__ --- src/client/app/index.ts | 4 ++++ src/shared/shared.ts | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/client/app/index.ts b/src/client/app/index.ts index 57cfe15a..5bc8087c 100644 --- a/src/client/app/index.ts +++ b/src/client/app/index.ts @@ -17,6 +17,7 @@ import { usePrefetch } from './composables/preFetch' import { dataSymbol, initData, siteDataRef, useData } from './data' import { RouterSymbol, createRouter, scrollTo, type Router } from './router' import { inBrowser, pathToFile } from './utils' +import { getGlobalThis } from '../shared' function resolveThemeExtends(theme: typeof RawTheme): typeof RawTheme { if (theme.extends) { @@ -64,6 +65,9 @@ const VitePressApp = defineComponent({ }) export async function createApp() { + const target = getGlobalThis() + target.__VITEPRESS__ = true + const router = newRouter() const app = newApp() diff --git a/src/shared/shared.ts b/src/shared/shared.ts index 1308840e..d24b98b3 100644 --- a/src/shared/shared.ts +++ b/src/shared/shared.ts @@ -211,3 +211,20 @@ export function treatAsHtml(filename: string): boolean { export function escapeRegExp(str: string) { return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d') } + +let _globalThis: any +export const getGlobalThis = (): any => { + return ( + _globalThis || + (_globalThis = + typeof globalThis !== 'undefined' + ? globalThis + : typeof self !== 'undefined' + ? self + : typeof window !== 'undefined' + ? window + : typeof global !== 'undefined' + ? global + : {}) + ) +}