From e60af1b5e80c8e73de90dd6eb7577f5ddd6b75d8 Mon Sep 17 00:00:00 2001 From: Fan Pei Date: Thu, 13 Jun 2024 23:54:56 +0900 Subject: [PATCH 1/3] feat: expose selected store as --- packages/pinia/src/devtools/plugin.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/pinia/src/devtools/plugin.ts b/packages/pinia/src/devtools/plugin.ts index b9388485b7..1eefbc0be8 100644 --- a/packages/pinia/src/devtools/plugin.ts +++ b/packages/pinia/src/devtools/plugin.ts @@ -230,6 +230,12 @@ export function registerPiniaDevtools(app: DevtoolsApp, pinia: Pinia) { } if (inspectedStore) { + const win = window as any + if (win) { + win.$p = payload.nodeId === PINIA_ROOT_ID + ? inspectedStore + : toRaw(inspectedStore) + } payload.state = formatStoreForInspectorState(inspectedStore) } } From 36a56a88f9b1f461e0d746b711404c810bc73c8c Mon Sep 17 00:00:00 2001 From: Fan Pei Date: Sat, 15 Jun 2024 02:06:04 +0900 Subject: [PATCH 2/3] fix: Expose and to window --- packages/pinia/src/devtools/plugin.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/pinia/src/devtools/plugin.ts b/packages/pinia/src/devtools/plugin.ts index 1eefbc0be8..7c3db0174b 100644 --- a/packages/pinia/src/devtools/plugin.ts +++ b/packages/pinia/src/devtools/plugin.ts @@ -216,6 +216,10 @@ export function registerPiniaDevtools(app: DevtoolsApp, pinia: Pinia) { } }) + // Expose pinia instance as $pinia to window + const win = window as any + if (win) win.$pinia = pinia + api.on.getInspectorState((payload) => { if (payload.app === app && payload.inspectorId === INSPECTOR_ID) { const inspectedStore = @@ -230,12 +234,9 @@ export function registerPiniaDevtools(app: DevtoolsApp, pinia: Pinia) { } if (inspectedStore) { - const win = window as any - if (win) { - win.$p = payload.nodeId === PINIA_ROOT_ID - ? inspectedStore - : toRaw(inspectedStore) - } + // Expose selected store as $store to window + if (win && payload.nodeId !== PINIA_ROOT_ID) + win.$store = toRaw(inspectedStore) payload.state = formatStoreForInspectorState(inspectedStore) } } From 1373b805cb6852cd0e95c27e8af513759d6b6ca8 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 26 Jun 2024 11:23:35 +0200 Subject: [PATCH 3/3] refactor: add to globalThis --- packages/pinia/src/devtools/plugin.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/pinia/src/devtools/plugin.ts b/packages/pinia/src/devtools/plugin.ts index 7c3db0174b..c4d4008667 100644 --- a/packages/pinia/src/devtools/plugin.ts +++ b/packages/pinia/src/devtools/plugin.ts @@ -217,8 +217,7 @@ export function registerPiniaDevtools(app: DevtoolsApp, pinia: Pinia) { }) // Expose pinia instance as $pinia to window - const win = window as any - if (win) win.$pinia = pinia + globalThis.$pinia = pinia api.on.getInspectorState((payload) => { if (payload.app === app && payload.inspectorId === INSPECTOR_ID) { @@ -235,8 +234,8 @@ export function registerPiniaDevtools(app: DevtoolsApp, pinia: Pinia) { if (inspectedStore) { // Expose selected store as $store to window - if (win && payload.nodeId !== PINIA_ROOT_ID) - win.$store = toRaw(inspectedStore) + if (payload.nodeId !== PINIA_ROOT_ID) + globalThis.$store = toRaw(inspectedStore as StoreGeneric) payload.state = formatStoreForInspectorState(inspectedStore) } } @@ -594,3 +593,14 @@ export function devtoolsPlugin< store as StoreGeneric ) } + +declare global { + /** + * Exposes the `pinia` instance when Devtools are opened. + */ + var $pinia: Pinia | undefined + /** + * Exposes the current store when Devtools are opened. + */ + var $store: StoreGeneric | undefined +}