File size: 14,794 Bytes
4d70170 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
import type { DevtoolsApi } from '@vue-devtools/app-backend-api'
import type { App, ComponentState, CustomInspectorNode, CustomInspectorState } from '@vue/devtools-api'
import { setupDevtoolsPlugin } from '@vue/devtools-api'
import { isEmptyObject, target } from '@vue-devtools/shared-utils'
import copy from 'clone-deep'
let actionId = 0
const VUEX_ROOT_PATH = '__vdt_root'
const VUEX_MODULE_PATH_SEPARATOR = '[vdt]'
const VUEX_MODULE_PATH_SEPARATOR_REG = /\[vdt\]/g
/**
* Extracted from tailwind palette
*/
const BLUE_600 = 0x2563EB
const LIME_500 = 0x84CC16
const CYAN_400 = 0x22D3EE
const ORANGE_400 = 0xFB923C
const WHITE = 0xFFFFFF
const DARK = 0x666666
export function setupPlugin(api: DevtoolsApi, app: App, Vue) {
const ROUTER_INSPECTOR_ID = 'vue2-router-inspector'
const ROUTER_CHANGES_LAYER_ID = 'vue2-router-changes'
const VUEX_INSPECTOR_ID = 'vue2-vuex-inspector'
const VUEX_MUTATIONS_ID = 'vue2-vuex-mutations'
const VUEX_ACTIONS_ID = 'vue2-vuex-actions'
setupDevtoolsPlugin({
app,
id: 'org.vuejs.vue2-internal',
label: 'Vue 2',
homepage: 'https://vuejs.org/',
logo: 'https://v2.vuejs.org/images/icons/favicon-96x96.png',
settings: {
legacyActions: {
label: 'Legacy Actions',
description: 'Enable this for Vuex < 3.1.0',
type: 'boolean',
defaultValue: false,
},
},
}, (api) => {
const hook = target.__VUE_DEVTOOLS_GLOBAL_HOOK__
// Vue Router
if (app.$router) {
const router = app.$router
// Inspector
api.addInspector({
id: ROUTER_INSPECTOR_ID,
label: 'Routes',
icon: 'book',
treeFilterPlaceholder: 'Search routes',
})
api.on.getInspectorTree((payload) => {
if (payload.inspectorId === ROUTER_INSPECTOR_ID) {
if (router.options.routes) {
payload.rootNodes = router.options.routes.map(route => formatRouteNode(router, route, '', payload.filter)).filter(Boolean)
}
else {
console.warn(`[Vue Devtools] No routes found in router`, router.options)
}
}
})
api.on.getInspectorState((payload) => {
if (payload.inspectorId === ROUTER_INSPECTOR_ID) {
const route = router.matcher.getRoutes().find(r => getPathId(r) === payload.nodeId)
if (route) {
payload.state = {
options: formatRouteData(route),
}
}
}
})
// Timeline
api.addTimelineLayer({
id: ROUTER_CHANGES_LAYER_ID,
label: 'Router Navigations',
color: 0x40A8C4,
})
router.afterEach((to, from) => {
api.addTimelineEvent({
layerId: ROUTER_CHANGES_LAYER_ID,
event: {
time: api.now(),
title: to.path,
data: {
from,
to,
},
},
})
api.sendInspectorTree(ROUTER_INSPECTOR_ID)
})
}
// Vuex
if (app.$store) {
const store = app.$store
api.addInspector({
id: VUEX_INSPECTOR_ID,
label: 'Vuex',
icon: 'storage',
treeFilterPlaceholder: 'Filter stores...',
})
api.on.getInspectorTree((payload) => {
if (payload.inspectorId === VUEX_INSPECTOR_ID) {
if (payload.filter) {
const nodes = []
flattenStoreForInspectorTree(nodes, store._modules.root, payload.filter, '')
payload.rootNodes = nodes
}
else {
payload.rootNodes = [
formatStoreForInspectorTree(store._modules.root, 'Root', ''),
]
}
}
})
api.on.getInspectorState((payload) => {
if (payload.inspectorId === VUEX_INSPECTOR_ID) {
const modulePath = payload.nodeId
const { module, getterPath } = getStoreModule(store._modules, modulePath)
if (!module) {
return
}
// Access the getters prop to init getters cache (which is lazy)
// eslint-disable-next-line no-unused-expressions
module.context.getters
payload.state = formatStoreForInspectorState(
module,
store._makeLocalGettersCache,
getterPath,
)
}
})
api.on.editInspectorState((payload) => {
if (payload.inspectorId === VUEX_INSPECTOR_ID) {
let path = payload.path
if (payload.nodeId !== VUEX_ROOT_PATH) {
path = [
...payload.nodeId.split(VUEX_MODULE_PATH_SEPARATOR).slice(0, -1),
...path,
]
}
store._committing = true
payload.set(store._vm.$data.$$state, path)
store._committing = false
}
})
api.addTimelineLayer({
id: VUEX_MUTATIONS_ID,
label: 'Vuex Mutations',
color: LIME_500,
})
api.addTimelineLayer({
id: VUEX_ACTIONS_ID,
label: 'Vuex Actions',
color: LIME_500,
})
hook.on('vuex:mutation', (mutation, state) => {
api.sendInspectorState(VUEX_INSPECTOR_ID)
const data: any = {}
if (mutation.payload) {
data.payload = mutation.payload
}
data.state = copy(state)
api.addTimelineEvent({
layerId: VUEX_MUTATIONS_ID,
event: {
time: api.now(),
title: mutation.type,
data,
},
})
})
function legacySingleActionSub(action, state) {
const data: any = {}
if (action.payload) {
data.payload = action.payload
}
data.state = state
api.addTimelineEvent({
layerId: VUEX_ACTIONS_ID,
event: {
time: api.now(),
title: action.type,
data,
},
})
}
store.subscribeAction?.(api.getSettings().legacyActions
? legacySingleActionSub
: {
before: (action, state) => {
const data: any = {}
if (action.payload) {
data.payload = action.payload
}
action._id = actionId++
action._time = api.now()
data.state = state
api.addTimelineEvent({
layerId: VUEX_ACTIONS_ID,
event: {
time: action._time,
title: action.type,
groupId: action._id,
subtitle: 'start',
data,
},
})
},
after: (action, state) => {
const data: any = {}
const duration = api.now() - action._time
data.duration = {
_custom: {
type: 'duration',
display: `${duration}ms`,
tooltip: 'Action duration',
value: duration,
},
}
if (action.payload) {
data.payload = action.payload
}
data.state = state
api.addTimelineEvent({
layerId: VUEX_ACTIONS_ID,
event: {
time: api.now(),
title: action.type,
groupId: action._id,
subtitle: 'end',
data,
},
})
},
}, { prepend: true })
// Inspect getters on mutations
api.on.inspectTimelineEvent((payload) => {
if (payload.layerId === VUEX_MUTATIONS_ID) {
const getterKeys = Object.keys(store.getters)
if (getterKeys.length) {
const vm = new Vue({
data: {
$$state: payload.data.state,
},
computed: store._vm.$options.computed,
})
const originalVm = store._vm
store._vm = vm
const tree = transformPathsToObjectTree(store.getters)
payload.data.getters = copy(tree)
store._vm = originalVm
vm.$destroy()
}
}
})
}
})
}
function formatRouteNode(router, route, parentPath: string, filter: string): CustomInspectorNode {
const node: CustomInspectorNode = {
id: route.path.startsWith('/') ? route.path : `${parentPath}/${route.path}`,
label: route.path,
children: route.children?.map(child => formatRouteNode(router, child, route.path, filter)).filter(Boolean),
tags: [],
}
if (filter && !node.id.includes(filter) && !node.children?.length) {
return null
}
if (route.name != null) {
node.tags.push({
label: String(route.name),
textColor: 0,
backgroundColor: CYAN_400,
})
}
if (route.alias != null) {
node.tags.push({
label: 'alias',
textColor: 0,
backgroundColor: ORANGE_400,
})
}
if (node.id === router.currentRoute.path) {
node.tags.push({
label: 'active',
textColor: WHITE,
backgroundColor: BLUE_600,
})
}
if (route.redirect) {
node.tags.push({
label:
`redirect: ${
typeof route.redirect === 'string' ? route.redirect : 'Object'}`,
textColor: WHITE,
backgroundColor: DARK,
})
}
return node
}
function formatRouteData(route) {
const data: Omit<ComponentState, 'type'>[] = []
data.push({ key: 'path', value: route.path })
if (route.redirect) {
data.push({ key: 'redirect', value: route.redirect })
}
if (route.alias) {
data.push({ key: 'alias', value: route.alias })
}
if (route.props) {
data.push({ key: 'props', value: route.props })
}
if (route.name && route.name != null) {
data.push({ key: 'name', value: route.name })
}
if (route.component) {
const component: any = {}
// if (route.component.__file) {
// component.file = route.component.__file
// }
if (route.component.template) {
component.template = route.component.template
}
if (route.component.props) {
component.props = route.component.props
}
if (!isEmptyObject(component)) {
data.push({ key: 'component', value: component })
}
}
return data
}
function getPathId(routeMatcher) {
let path = routeMatcher.path
if (routeMatcher.parent) {
path = getPathId(routeMatcher.parent) + path
}
return path
}
const TAG_NAMESPACED = {
label: 'namespaced',
textColor: WHITE,
backgroundColor: DARK,
}
function formatStoreForInspectorTree(module, moduleName: string, path: string): CustomInspectorNode {
return {
id: path || VUEX_ROOT_PATH,
// all modules end with a `/`, we want the last segment only
// cart/ -> cart
// nested/cart/ -> cart
label: moduleName,
tags: module.namespaced ? [TAG_NAMESPACED] : [],
children: Object.keys(module._children ?? {}).map(key =>
formatStoreForInspectorTree(
module._children[key],
key,
`${path}${key}${VUEX_MODULE_PATH_SEPARATOR}`,
),
),
}
}
function flattenStoreForInspectorTree(result: CustomInspectorNode[], module, filter: string, path: string) {
if (path.includes(filter)) {
result.push({
id: path || VUEX_ROOT_PATH,
label: path.endsWith(VUEX_MODULE_PATH_SEPARATOR) ? path.slice(0, path.length - 1) : path || 'Root',
tags: module.namespaced ? [TAG_NAMESPACED] : [],
})
}
Object.keys(module._children).forEach((moduleName) => {
flattenStoreForInspectorTree(result, module._children[moduleName], filter, path + moduleName + VUEX_MODULE_PATH_SEPARATOR)
})
}
function extractNameFromPath(path: string) {
return path && path !== VUEX_ROOT_PATH ? path.split(VUEX_MODULE_PATH_SEPARATOR).slice(-2, -1)[0] : 'Root'
}
function formatStoreForInspectorState(module, getters, path): CustomInspectorState {
const storeState: CustomInspectorState = {
state: Object.keys(module.context.state ?? {}).map(key => ({
key,
editable: true,
value: module.context.state[key],
})),
}
if (getters) {
const pathWithSlashes = path.replace(VUEX_MODULE_PATH_SEPARATOR_REG, '/')
getters = !module.namespaced || path === VUEX_ROOT_PATH ? module.context.getters : getters[pathWithSlashes]
let gettersKeys = Object.keys(getters)
const shouldPickGetters = !module.namespaced && path !== VUEX_ROOT_PATH
if (shouldPickGetters) {
// Only pick the getters defined in the non-namespaced module
const definedGettersKeys = Object.keys(module._rawModule.getters ?? {})
gettersKeys = gettersKeys.filter(key => definedGettersKeys.includes(key))
}
if (gettersKeys.length) {
let moduleGetters: Record<string, any>
if (shouldPickGetters) {
// Only pick the getters defined in the non-namespaced module
moduleGetters = {}
for (const key of gettersKeys) {
moduleGetters[key] = canThrow(() => getters[key])
}
}
else {
moduleGetters = getters
}
const tree = transformPathsToObjectTree(moduleGetters)
storeState.getters = Object.keys(tree).map(key => ({
key: key.endsWith('/') ? extractNameFromPath(key) : key,
editable: false,
value: canThrow(() => tree[key]),
}))
}
}
return storeState
}
function transformPathsToObjectTree(getters) {
const result = {}
Object.keys(getters).forEach((key) => {
const path = key.split('/')
if (path.length > 1) {
let target = result
const leafKey = path.pop()
for (const p of path) {
if (!target[p]) {
target[p] = {
_custom: {
value: {},
display: p,
tooltip: 'Module',
abstract: true,
},
}
}
target = target[p]._custom.value
}
target[leafKey] = canThrow(() => getters[key])
}
else {
result[key] = canThrow(() => getters[key])
}
})
return result
}
function getStoreModule(moduleMap, path) {
const names = path.split(VUEX_MODULE_PATH_SEPARATOR).filter(n => n)
return names.reduce(
({ module, getterPath }, moduleName, i) => {
const child = module[moduleName === VUEX_ROOT_PATH ? 'root' : moduleName]
if (!child) {
return null
}
return {
module: i === names.length - 1 ? child : child._children,
getterPath: child._rawModule.namespaced
? getterPath
: getterPath.replace(`${moduleName}${VUEX_MODULE_PATH_SEPARATOR}`, ''),
}
},
{
module: path === VUEX_ROOT_PATH ? moduleMap : moduleMap.root._children,
getterPath: path,
},
)
}
function canThrow(cb: () => any) {
try {
return cb()
}
catch (e) {
return e
}
}
|