From 6d2380d493004aadb5b76e5478f39cda5a2cf814 Mon Sep 17 00:00:00 2001 From: yuntian001 Date: Mon, 3 Apr 2023 10:22:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20meKeepAlive=E5=B8=B8=E9=87=8F=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=8A=A8=E6=80=81=E8=8E=B7=E5=8F=96(=E5=9B=A0?= =?UTF-8?q?=E4=B8=BA@vue/share=E5=9C=A8=E9=87=8C=E9=9D=A2=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E4=BA=86)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/meKeepAlive/core/renderer.ts | 5 +---- src/components/meKeepAlive/core/warning.ts | 8 -------- src/components/meKeepAlive/index.ts | 17 ++++++++--------- types/global.d.ts | 10 ++++++++++ vite.config.ts | 9 +++++++++ 5 files changed, 28 insertions(+), 21 deletions(-) delete mode 100644 src/components/meKeepAlive/core/warning.ts diff --git a/src/components/meKeepAlive/core/renderer.ts b/src/components/meKeepAlive/core/renderer.ts index fdae4b8c..142e411b 100644 --- a/src/components/meKeepAlive/core/renderer.ts +++ b/src/components/meKeepAlive/core/renderer.ts @@ -9,9 +9,6 @@ import { queuePostFlushCb, } from 'vue'; import { queueEffectWithSuspense } from './Suspense'; -import {getGlobalThis} from '@vue/shared'; -const globalThis = getGlobalThis(); - // An object exposing the internals of a renderer, passed to tree-shakeable // features so that they can be decoupled from this file. Keys are shortened @@ -114,7 +111,7 @@ type PatchBlockChildrenFn = ( type NextFn = (vnode: VNode) => RendererNode | null; -export const queuePostRenderEffect = globalThis.__FEATURE_SUSPENSE__ ? queueEffectWithSuspense : queuePostFlushCb; +export const queuePostRenderEffect = __FEATURE_SUSPENSE__ ? queueEffectWithSuspense : queuePostFlushCb; export type SetupRenderEffectFn = ( instance: ComponentInternalInstance, diff --git a/src/components/meKeepAlive/core/warning.ts b/src/components/meKeepAlive/core/warning.ts deleted file mode 100644 index 12c7e60d..00000000 --- a/src/components/meKeepAlive/core/warning.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { pauseTracking, resetTracking } from '@vue/reactivity'; - -export function warn(msg: string, ...args: any[]) { - pauseTracking(); - // TODO::因为拿不到stack所以只做一个简单的提示 - console.warn(`[Vue warn]: ${msg}`, ...args); - resetTracking(); -} diff --git a/src/components/meKeepAlive/index.ts b/src/components/meKeepAlive/index.ts index ba350653..c0415279 100644 --- a/src/components/meKeepAlive/index.ts +++ b/src/components/meKeepAlive/index.ts @@ -14,11 +14,11 @@ import { RendererElement, RendererNode, setTransitionHooks, + warn, } from 'vue'; import { getComponentName } from './core/component'; import { invokeVNodeHook } from './core/vnode'; -import { warn } from './core/warning'; -import { isString, isArray, invokeArrayFns,getGlobalThis } from '@vue/shared'; +import { isString, isArray, invokeArrayFns } from '@vue/shared'; import { ShapeFlags } from './core/shapeFlags'; import { RendererInternals, queuePostRenderEffect, MoveType } from './core/renderer'; import { ComponentRenderContext } from './core/componentPublicInstance'; @@ -26,7 +26,6 @@ import { devtoolsComponentAdded } from './core/devtools'; import { isAsyncWrapper } from './core/apiAsyncComponent'; import { isSuspense } from './core/Suspense'; type MatchPattern = string | RegExp | Array; -const globalThis = getGlobalThis(); export interface MeKeepAliveProps { include?: MatchPattern; exclude?: MatchPattern; @@ -77,7 +76,7 @@ const KeepAliveImpl: ComponentOptions = { // if the internal renderer is not registered, it indicates that this is server-side rendering, // for KeepAlive, we just need to render its children - if (globalThis.__SSR__ && !sharedContext.renderer) { + if (__SSR__ && !sharedContext.renderer) { return () => { const children = slots.default && slots.default(); return children && children.length === 1 ? children[0] : children; @@ -88,7 +87,7 @@ const KeepAliveImpl: ComponentOptions = { const keys: Keys = new Set(); let current: VNode | null = null; - if (globalThis.__DEV__ || globalThis.__FEATURE_PROD_DEVTOOLS__) { + if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) { (instance as any).__v_cache = cache; } @@ -120,7 +119,7 @@ const KeepAliveImpl: ComponentOptions = { } }, parentSuspense); - if (globalThis.__DEV__ || globalThis.__FEATURE_PROD_DEVTOOLS__) { + if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) { // Update components tree devtoolsComponentAdded(instance); } @@ -140,7 +139,7 @@ const KeepAliveImpl: ComponentOptions = { instance.isDeactivated = true; }, parentSuspense); - if (globalThis.__DEV__ || globalThis.__FEATURE_PROD_DEVTOOLS__) { + if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) { // Update components tree devtoolsComponentAdded(instance); } @@ -241,7 +240,7 @@ const KeepAliveImpl: ComponentOptions = { const children = slots.default(); const rawVNode = children[0]; if (children.length > 1) { - if (globalThis.__DEV__) { + if (__DEV__) { warn(`KeepAlive should contain exactly one component child.`, current); } current = null; @@ -318,7 +317,7 @@ const KeepAliveImpl: ComponentOptions = { }, }; -if (globalThis.__COMPAT__) { +if (__COMPAT__) { KeepAliveImpl.__isBuildIn = true; } diff --git a/types/global.d.ts b/types/global.d.ts index 7aa4e2cb..2841ff88 100644 --- a/types/global.d.ts +++ b/types/global.d.ts @@ -11,6 +11,16 @@ declare global { color: StringConstructor; }>; + /* eslint-disable */ + declare const __SSR__: boolean; + declare const __DEV__: boolean; + declare const __COMPAT__: boolean; + + // Feature flags + declare const __FEATURE_PROD_DEVTOOLS__: boolean; + declare const __FEATURE_SUSPENSE__: boolean; + /* eslint-disable */ + type ComponentProps = { -readonly [K in keyof Omit< InstanceType['$props'], diff --git a/vite.config.ts b/vite.config.ts index 6efb27fd..f53e5f23 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -47,6 +47,15 @@ export default async (configEnv: ConfigEnv): Promise => { ], extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'], }, + /* eslint-disable */ + define: { + __SSR__: `true`, + __DEV__: configEnv.mode === 'development' ? `true` : `false`, + __COMPAT__: `false`, + __FEATURE_SUSPENSE__: `true`, + __FEATURE_PROD_DEVTOOLS__: `false`, + }, + /* eslint-disable */ build: { rollupOptions: { output: {