Skip to content

Commit 01d8892

Browse files
committed
fix(vue2): vuex root special id in case a module is called 'root'
1 parent a3cc310 commit 01d8892

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

packages/app-backend-vue2/src/plugin.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export function setupPlugin (api: DevtoolsApi, app: App, Vue) {
127127
api.on.editInspectorState((payload) => {
128128
if (payload.inspectorId === VUEX_INSPECTOR_ID) {
129129
let path = payload.path
130-
if (payload.nodeId !== 'root') {
130+
if (payload.nodeId !== VUEX_ROOT_PATH) {
131131
path = [
132132
...payload.nodeId.substring(0, payload.nodeId.length - 1).split('/'),
133133
...path,
@@ -380,9 +380,11 @@ const TAG_NAMESPACED = {
380380
backgroundColor: DARK,
381381
}
382382

383+
const VUEX_ROOT_PATH = '__vdt_root'
384+
383385
function formatStoreForInspectorTree (module, path): CustomInspectorNode {
384386
return {
385-
id: path || 'root',
387+
id: path || VUEX_ROOT_PATH,
386388
// all modules end with a `/`, we want the last segment only
387389
// cart/ -> cart
388390
// nested/cart/ -> cart
@@ -400,7 +402,7 @@ function formatStoreForInspectorTree (module, path): CustomInspectorNode {
400402
function flattenStoreForInspectorTree (result: CustomInspectorNode[], module, filter: string, path: string) {
401403
if (path.includes(filter)) {
402404
result.push({
403-
id: path || 'root',
405+
id: path || VUEX_ROOT_PATH,
404406
label: path.endsWith('/') ? path.slice(0, path.length - 1) : path || 'Root',
405407
tags: module.namespaced ? [TAG_NAMESPACED] : [],
406408
})
@@ -411,7 +413,7 @@ function flattenStoreForInspectorTree (result: CustomInspectorNode[], module, fi
411413
}
412414

413415
function extractNameFromPath (path: string) {
414-
return path && path !== 'root' ? path.split('/').slice(-2, -1)[0] : 'Root'
416+
return path && path !== VUEX_ROOT_PATH ? path.split('/').slice(-2, -1)[0] : 'Root'
415417
}
416418

417419
function formatStoreForInspectorState (module, getters, path): CustomInspectorState {
@@ -423,9 +425,9 @@ function formatStoreForInspectorState (module, getters, path): CustomInspectorSt
423425
})),
424426
}
425427

426-
getters = !module.namespaced || path === 'root' ? module.context.getters : getters[path]
428+
getters = !module.namespaced || path === VUEX_ROOT_PATH ? module.context.getters : getters[path]
427429
let gettersKeys = Object.keys(getters)
428-
const shouldPickGetters = !module.namespaced && path !== 'root'
430+
const shouldPickGetters = !module.namespaced && path !== VUEX_ROOT_PATH
429431
if (shouldPickGetters) {
430432
// Only pick the getters defined in the non-namespaced module
431433
const definedGettersKeys = Object.keys(module._rawModule.getters ?? {})
@@ -485,13 +487,13 @@ function getStoreModule (moduleMap, path) {
485487
const names = path.split('/').filter((n) => n)
486488
return names.reduce(
487489
(module, moduleName, i) => {
488-
const child = module[moduleName]
490+
const child = module[moduleName === VUEX_ROOT_PATH ? 'root' : moduleName]
489491
if (!child) {
490492
throw new Error(`Missing module "${moduleName}" for path "${path}".`)
491493
}
492494
return i === names.length - 1 ? child : child._children
493495
},
494-
path === 'root' ? moduleMap : moduleMap.root._children,
496+
path === VUEX_ROOT_PATH ? moduleMap : moduleMap.root._children,
495497
)
496498
}
497499

0 commit comments

Comments
 (0)