Skip to content

Commit 3cce6f3

Browse files
committed
fix(vue2): support / in vuex module name, closes #1696
1 parent 01d8892 commit 3cce6f3

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export function setupPlugin (api: DevtoolsApi, app: App, Vue) {
103103
payload.rootNodes = nodes
104104
} else {
105105
payload.rootNodes = [
106-
formatStoreForInspectorTree(store._modules.root, ''),
106+
formatStoreForInspectorTree(store._modules.root, 'Root', ''),
107107
]
108108
}
109109
}
@@ -381,19 +381,22 @@ const TAG_NAMESPACED = {
381381
}
382382

383383
const VUEX_ROOT_PATH = '__vdt_root'
384+
const VUEX_MODULE_PATH_SEPARATOR = '[vdt]'
385+
const VUEX_MODULE_PATH_SEPARATOR_REG = /\[vdt\]/g
384386

385-
function formatStoreForInspectorTree (module, path): CustomInspectorNode {
387+
function formatStoreForInspectorTree (module, moduleName: string, path: string): CustomInspectorNode {
386388
return {
387-
id: path || VUEX_ROOT_PATH,
389+
id: path ?? VUEX_ROOT_PATH,
388390
// all modules end with a `/`, we want the last segment only
389391
// cart/ -> cart
390392
// nested/cart/ -> cart
391-
label: extractNameFromPath(path),
393+
label: moduleName,
392394
tags: module.namespaced ? [TAG_NAMESPACED] : [],
393-
children: Object.keys(module._children).map((moduleName) =>
395+
children: Object.keys(module._children).map((key) =>
394396
formatStoreForInspectorTree(
395-
module._children[moduleName],
396-
path + moduleName + '/',
397+
module._children[key],
398+
key,
399+
`${path}${key}${VUEX_MODULE_PATH_SEPARATOR}`,
397400
),
398401
),
399402
}
@@ -403,17 +406,17 @@ function flattenStoreForInspectorTree (result: CustomInspectorNode[], module, fi
403406
if (path.includes(filter)) {
404407
result.push({
405408
id: path || VUEX_ROOT_PATH,
406-
label: path.endsWith('/') ? path.slice(0, path.length - 1) : path || 'Root',
409+
label: path.endsWith(VUEX_MODULE_PATH_SEPARATOR) ? path.slice(0, path.length - 1) : path || 'Root',
407410
tags: module.namespaced ? [TAG_NAMESPACED] : [],
408411
})
409412
}
410413
Object.keys(module._children).forEach(moduleName => {
411-
flattenStoreForInspectorTree(result, module._children[moduleName], filter, path + moduleName + '/')
414+
flattenStoreForInspectorTree(result, module._children[moduleName], filter, path + moduleName + VUEX_MODULE_PATH_SEPARATOR)
412415
})
413416
}
414417

415418
function extractNameFromPath (path: string) {
416-
return path && path !== VUEX_ROOT_PATH ? path.split('/').slice(-2, -1)[0] : 'Root'
419+
return path && path !== VUEX_ROOT_PATH ? path.split(VUEX_MODULE_PATH_SEPARATOR).slice(-2, -1)[0] : 'Root'
417420
}
418421

419422
function formatStoreForInspectorState (module, getters, path): CustomInspectorState {
@@ -425,7 +428,8 @@ function formatStoreForInspectorState (module, getters, path): CustomInspectorSt
425428
})),
426429
}
427430

428-
getters = !module.namespaced || path === VUEX_ROOT_PATH ? module.context.getters : getters[path]
431+
const pathWithSlashes = path.replace(VUEX_MODULE_PATH_SEPARATOR_REG, '/')
432+
getters = !module.namespaced || path === VUEX_ROOT_PATH ? module.context.getters : getters[pathWithSlashes]
429433
let gettersKeys = Object.keys(getters)
430434
const shouldPickGetters = !module.namespaced && path !== VUEX_ROOT_PATH
431435
if (shouldPickGetters) {
@@ -484,7 +488,7 @@ function transformPathsToObjectTree (getters) {
484488
}
485489

486490
function getStoreModule (moduleMap, path) {
487-
const names = path.split('/').filter((n) => n)
491+
const names = path.split(VUEX_MODULE_PATH_SEPARATOR).filter((n) => n)
488492
return names.reduce(
489493
(module, moduleName, i) => {
490494
const child = module[moduleName === VUEX_ROOT_PATH ? 'root' : moduleName]

packages/shell-dev-vue2/src/store.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ export default new Vuex.Store({
104104
hello2: state => state.hello.repeat(2),
105105
},
106106
},
107+
'use/in/name': {
108+
state () {
109+
return {
110+
meow: 'MEOW',
111+
}
112+
},
113+
getters: {
114+
meow2: state => state.meow.repeat(2),
115+
},
116+
},
107117
},
108118
})
109119

0 commit comments

Comments
 (0)