@dianzhong/create-harness-app 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.mjs +412 -0
- package/package.json +29 -0
- package/templates/axios/.env.example +2 -0
- package/templates/axios/src/api/auth.ts +19 -0
- package/templates/axios/src/api/request.ts +61 -0
- package/templates/axios/src/types/api.ts +26 -0
- package/templates/axios/src/utils/auth.ts +5 -0
- package/templates/axios/src/utils/storage.ts +17 -0
- package/templates/harness/full/.agents/skills/find-skills/SKILL.md +143 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/LICENSE.md +21 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/SKILL.md +155 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/SYNC.md +5 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/animation-class-based-technique.md +258 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/animation-state-driven-technique.md +287 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/component-async.md +99 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/component-data-flow.md +313 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/component-fallthrough-attrs.md +179 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/component-keep-alive.md +139 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/component-slots.md +226 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/component-suspense.md +231 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/component-teleport.md +110 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/component-transition-group.md +131 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/component-transition.md +135 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/composables.md +303 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/directives.md +168 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/perf-avoid-component-abstraction-in-lists.md +177 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/perf-v-once-v-memo-directives.md +185 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/perf-virtualize-large-lists.md +182 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/plugins.md +178 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/reactivity.md +371 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/render-functions.md +227 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/sfc.md +355 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/state-management.md +138 -0
- package/templates/harness/full/.agents/skills/vue-best-practices/references/updated-hook-performance.md +193 -0
- package/templates/harness/full/.claude/agents/code-reviewer.md +109 -0
- package/templates/harness/full/.claude/agents/harness-reviewer.md +51 -0
- package/templates/harness/full/.claude/hooks/guard-tool.cjs +234 -0
- package/templates/harness/full/.claude/hooks/notify.cjs +168 -0
- package/templates/harness/full/.claude/hooks/quality-gate.cjs +135 -0
- package/templates/harness/full/.claude/rules/delivery.md +66 -0
- package/templates/harness/full/.claude/rules/formatting.md +7 -0
- package/templates/harness/full/.claude/rules/git.md +8 -0
- package/templates/harness/full/.claude/rules/skills-mcp.md +13 -0
- package/templates/harness/full/.claude/rules/vue.md +227 -0
- package/templates/harness/full/.claude/settings.json +123 -0
- package/templates/harness/full/.claude/skills/find-skills/SKILL.md +143 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/LICENSE.md +21 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/SKILL.md +155 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/SYNC.md +5 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/animation-class-based-technique.md +258 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/animation-state-driven-technique.md +287 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/component-async.md +99 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/component-data-flow.md +313 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/component-fallthrough-attrs.md +179 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/component-keep-alive.md +139 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/component-slots.md +226 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/component-suspense.md +231 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/component-teleport.md +110 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/component-transition-group.md +131 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/component-transition.md +135 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/composables.md +303 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/directives.md +168 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/perf-avoid-component-abstraction-in-lists.md +177 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/perf-v-once-v-memo-directives.md +185 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/perf-virtualize-large-lists.md +182 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/plugins.md +178 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/reactivity.md +371 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/render-functions.md +227 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/sfc.md +355 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/state-management.md +138 -0
- package/templates/harness/full/.claude/skills/vue-best-practices/references/updated-hook-performance.md +193 -0
- package/templates/harness/full/.editorconfig +8 -0
- package/templates/harness/full/.husky/commit-msg +1 -0
- package/templates/harness/full/.husky/pre-commit +1 -0
- package/templates/harness/full/.lintstagedrc.json +4 -0
- package/templates/harness/full/.nvmrc +1 -0
- package/templates/harness/full/.oxlintrc.json +11 -0
- package/templates/harness/full/.prettierrc.json +6 -0
- package/templates/harness/full/AGENTS.md +3 -0
- package/templates/harness/full/CLAUDE.md +28 -0
- package/templates/harness/full/GEMINI.md +3 -0
- package/templates/harness/full/commitlint.config.ts +3 -0
- package/templates/harness/full/docs/ai-harness.md +77 -0
- package/templates/harness/full/docs/delivery-template.md +66 -0
- package/templates/harness/full/docs/git.md +24 -0
- package/templates/harness/full/docs/harness-quick-reference.md +89 -0
- package/templates/harness/full/docs/review-checklist.md +49 -0
- package/templates/harness/full/scripts/harness-hooks.test.mjs +218 -0
- package/templates/harness/full/scripts/verify-skills.mjs +248 -0
- package/templates/harness/full/scripts/verify-skills.test.mjs +72 -0
- package/templates/harness/full/skills-lock.json +50 -0
- package/templates/harness/minimal/.claude/hooks/guard-tool.cjs +234 -0
- package/templates/harness/minimal/.claude/hooks/quality-gate.cjs +135 -0
- package/templates/harness/minimal/.claude/settings.json +27 -0
- package/templates/harness/minimal/CLAUDE.md +12 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use v-once and v-memo to Skip Unnecessary Updates
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: v-once skips all future updates for static content; v-memo conditionally memoizes subtrees
|
|
5
|
+
type: efficiency
|
|
6
|
+
tags: [vue3, performance, v-once, v-memo, optimization, directives]
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# Use v-once and v-memo to Skip Unnecessary Updates
|
|
10
|
+
|
|
11
|
+
**Impact: MEDIUM** - Vue re-evaluates templates on every reactive change. For content that never changes or changes infrequently, `v-once` and `v-memo` tell Vue to skip updates, reducing render work.
|
|
12
|
+
|
|
13
|
+
Use `v-once` for truly static content and `v-memo` for conditionally-static content in lists.
|
|
14
|
+
|
|
15
|
+
## Task List
|
|
16
|
+
|
|
17
|
+
- Apply `v-once` to elements that use runtime data but never need updating
|
|
18
|
+
- Apply `v-memo` to list items that should only update on specific condition changes
|
|
19
|
+
- Verify memoized content doesn't need to respond to other state changes
|
|
20
|
+
- Profile with Vue DevTools to confirm update skipping
|
|
21
|
+
|
|
22
|
+
## v-once: Render Once, Never Update
|
|
23
|
+
|
|
24
|
+
**BAD:**
|
|
25
|
+
|
|
26
|
+
```vue
|
|
27
|
+
<template>
|
|
28
|
+
<!-- BAD: Re-evaluated on every parent re-render -->
|
|
29
|
+
<div class="terms-content">
|
|
30
|
+
<h1>Terms of Service</h1>
|
|
31
|
+
<p>Version: {{ termsVersion }}</p>
|
|
32
|
+
<div v-html="termsContent" />
|
|
33
|
+
</div>
|
|
34
|
+
|
|
35
|
+
<!-- This content NEVER changes, but Vue checks it every render -->
|
|
36
|
+
<footer>
|
|
37
|
+
<p>Copyright {{ copyrightYear }} {{ companyName }}</p>
|
|
38
|
+
</footer>
|
|
39
|
+
</template>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**GOOD:**
|
|
43
|
+
|
|
44
|
+
```vue
|
|
45
|
+
<script setup>
|
|
46
|
+
// These values are set once at component creation
|
|
47
|
+
const termsVersion = '2.1'
|
|
48
|
+
const termsContent = fetchedTermsHTML
|
|
49
|
+
const copyrightYear = 2024
|
|
50
|
+
const companyName = 'Acme Corp'
|
|
51
|
+
</script>
|
|
52
|
+
|
|
53
|
+
<template>
|
|
54
|
+
<!-- GOOD: Rendered once, skipped on all future updates -->
|
|
55
|
+
<div v-once class="terms-content">
|
|
56
|
+
<h1>Terms of Service</h1>
|
|
57
|
+
<p>Version: {{ termsVersion }}</p>
|
|
58
|
+
<div v-html="termsContent" />
|
|
59
|
+
</div>
|
|
60
|
+
|
|
61
|
+
<!-- v-once tells Vue this never needs to update -->
|
|
62
|
+
<footer v-once>
|
|
63
|
+
<p>Copyright {{ copyrightYear }} {{ companyName }}</p>
|
|
64
|
+
</footer>
|
|
65
|
+
</template>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## v-memo: Conditional Memoization for Lists
|
|
69
|
+
|
|
70
|
+
**BAD:**
|
|
71
|
+
|
|
72
|
+
```vue
|
|
73
|
+
<template>
|
|
74
|
+
<!-- BAD: All items re-render when selectedId changes -->
|
|
75
|
+
<div v-for="item in list" :key="item.id">
|
|
76
|
+
<div :class="{ selected: item.id === selectedId }">
|
|
77
|
+
<ExpensiveComponent :data="item" />
|
|
78
|
+
</div>
|
|
79
|
+
</div>
|
|
80
|
+
</template>
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
**GOOD:**
|
|
84
|
+
|
|
85
|
+
```vue
|
|
86
|
+
<script setup>
|
|
87
|
+
import { ref } from 'vue'
|
|
88
|
+
|
|
89
|
+
const list = ref([
|
|
90
|
+
/* many items */
|
|
91
|
+
])
|
|
92
|
+
const selectedId = ref(null)
|
|
93
|
+
|
|
94
|
+
// When selectedId changes:
|
|
95
|
+
// - Only the previously-selected item re-renders (selected: true -> false)
|
|
96
|
+
// - Only the newly-selected item re-renders (selected: false -> true)
|
|
97
|
+
// - All other items are SKIPPED (v-memo values unchanged)
|
|
98
|
+
</script>
|
|
99
|
+
|
|
100
|
+
<template>
|
|
101
|
+
<!-- GOOD: Items only re-render when their selection state changes -->
|
|
102
|
+
<div v-for="item in list" :key="item.id" v-memo="[item.id === selectedId]">
|
|
103
|
+
<div :class="{ selected: item.id === selectedId }">
|
|
104
|
+
<ExpensiveComponent :data="item" />
|
|
105
|
+
</div>
|
|
106
|
+
</div>
|
|
107
|
+
</template>
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## v-memo with Multiple Dependencies
|
|
111
|
+
|
|
112
|
+
```vue
|
|
113
|
+
<script setup>
|
|
114
|
+
const selectedId = ref(null)
|
|
115
|
+
const editingId = ref(null)
|
|
116
|
+
const items = ref([
|
|
117
|
+
/* ... */
|
|
118
|
+
])
|
|
119
|
+
</script>
|
|
120
|
+
|
|
121
|
+
<template>
|
|
122
|
+
<!-- Re-render only when item's selection OR editing state changes -->
|
|
123
|
+
<div
|
|
124
|
+
v-for="item in items"
|
|
125
|
+
:key="item.id"
|
|
126
|
+
v-memo="[item.id === selectedId, item.id === editingId]"
|
|
127
|
+
>
|
|
128
|
+
<ItemCard :item="item" :selected="item.id === selectedId" :editing="item.id === editingId" />
|
|
129
|
+
</div>
|
|
130
|
+
</template>
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## v-memo with Empty Array = v-once
|
|
134
|
+
|
|
135
|
+
```vue
|
|
136
|
+
<template>
|
|
137
|
+
<!-- v-memo="[]" is equivalent to v-once -->
|
|
138
|
+
<div v-for="item in staticList" :key="item.id" v-memo="[]">
|
|
139
|
+
{{ item.name }}
|
|
140
|
+
</div>
|
|
141
|
+
</template>
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## When NOT to Use These Directives
|
|
145
|
+
|
|
146
|
+
```vue
|
|
147
|
+
<template>
|
|
148
|
+
<!-- DON'T: Content that DOES need to update -->
|
|
149
|
+
<div v-once>
|
|
150
|
+
<span>Count: {{ count }}</span>
|
|
151
|
+
<!-- count won't update! -->
|
|
152
|
+
</div>
|
|
153
|
+
|
|
154
|
+
<!-- DON'T: When child components have their own reactive state -->
|
|
155
|
+
<div v-memo="[selected]">
|
|
156
|
+
<InputField v-model="item.name" />
|
|
157
|
+
<!-- v-model won't work properly -->
|
|
158
|
+
</div>
|
|
159
|
+
|
|
160
|
+
<!-- DON'T: When the memoization benefit is minimal -->
|
|
161
|
+
<span v-once>{{ simpleText }}</span>
|
|
162
|
+
<!-- Overhead not worth it -->
|
|
163
|
+
</template>
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Performance Comparison
|
|
167
|
+
|
|
168
|
+
| Scenario | Without Directive | With v-once/v-memo |
|
|
169
|
+
| ------------------------------------- | -------------------- | ------------------- |
|
|
170
|
+
| Static header, parent re-renders 100x | Re-evaluated 100x | Evaluated 1x |
|
|
171
|
+
| 1000 items, selection changes | 1000 items re-render | 2 items re-render |
|
|
172
|
+
| Complex child component | Full re-render | Skipped if memoized |
|
|
173
|
+
|
|
174
|
+
## Debugging Memoized Components
|
|
175
|
+
|
|
176
|
+
```vue
|
|
177
|
+
<script setup>
|
|
178
|
+
import { onUpdated } from 'vue'
|
|
179
|
+
|
|
180
|
+
// This won't fire if v-memo prevents update
|
|
181
|
+
onUpdated(() => {
|
|
182
|
+
console.log('Component updated')
|
|
183
|
+
})
|
|
184
|
+
</script>
|
|
185
|
+
```
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Virtualize Large Lists to Avoid DOM Overload
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: Rendering thousands of list items creates excessive DOM nodes, causing slow renders and high memory usage
|
|
5
|
+
type: efficiency
|
|
6
|
+
tags: [vue3, performance, virtual-list, large-data, dom, optimization]
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# Virtualize Large Lists to Avoid DOM Overload
|
|
10
|
+
|
|
11
|
+
**Impact: HIGH** - Rendering all items in a large list (hundreds or thousands) creates massive amounts of DOM nodes. Each node consumes memory, slows down initial render, and makes updates expensive. List virtualization only renders visible items, dramatically improving performance.
|
|
12
|
+
|
|
13
|
+
Use a virtualization library when dealing with lists that could exceed 50-100 items, especially if items have complex content.
|
|
14
|
+
|
|
15
|
+
## Task List
|
|
16
|
+
|
|
17
|
+
- Identify lists that render more than 50-100 items
|
|
18
|
+
- Install a virtualization library (vue-virtual-scroller, @tanstack/vue-virtual)
|
|
19
|
+
- Replace standard `v-for` with virtualized component
|
|
20
|
+
- Ensure list items have consistent or estimable heights
|
|
21
|
+
- Test with realistic data volumes during development
|
|
22
|
+
|
|
23
|
+
## Recommended Libraries
|
|
24
|
+
|
|
25
|
+
| Library | Best For | Notes |
|
|
26
|
+
| ------------------------- | ------------------------- | ---------------------------- |
|
|
27
|
+
| `vue-virtual-scroller` | General use, easy setup | Most popular, good defaults |
|
|
28
|
+
| `@tanstack/vue-virtual` | Complex layouts, headless | Framework-agnostic, flexible |
|
|
29
|
+
| `vue-virtual-scroll-grid` | Grid layouts | 2D virtualization |
|
|
30
|
+
| `vueuc/VVirtualList` | Naive UI projects | Part of Naive UI ecosystem |
|
|
31
|
+
|
|
32
|
+
**BAD:**
|
|
33
|
+
|
|
34
|
+
```vue
|
|
35
|
+
<script setup>
|
|
36
|
+
import { onMounted, ref } from 'vue'
|
|
37
|
+
|
|
38
|
+
import UserCard from './UserCard.vue'
|
|
39
|
+
|
|
40
|
+
const users = ref([])
|
|
41
|
+
|
|
42
|
+
onMounted(async () => {
|
|
43
|
+
// 10,000 DOM nodes created, browser struggles
|
|
44
|
+
users.value = await fetchAllUsers()
|
|
45
|
+
})
|
|
46
|
+
</script>
|
|
47
|
+
|
|
48
|
+
<template>
|
|
49
|
+
<!-- BAD: Renders ALL 10,000 items immediately -->
|
|
50
|
+
<div class="user-list">
|
|
51
|
+
<UserCard v-for="user in users" :key="user.id" :user="user" />
|
|
52
|
+
</div>
|
|
53
|
+
</template>
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**GOOD:**
|
|
57
|
+
|
|
58
|
+
```vue
|
|
59
|
+
<script setup>
|
|
60
|
+
import { onMounted, ref } from 'vue'
|
|
61
|
+
import { RecycleScroller } from 'vue-virtual-scroller'
|
|
62
|
+
|
|
63
|
+
import UserCard from './UserCard.vue'
|
|
64
|
+
|
|
65
|
+
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'
|
|
66
|
+
|
|
67
|
+
const users = ref([])
|
|
68
|
+
|
|
69
|
+
onMounted(async () => {
|
|
70
|
+
// 10,000 items in memory, but only ~20 DOM nodes
|
|
71
|
+
users.value = await fetchAllUsers()
|
|
72
|
+
})
|
|
73
|
+
</script>
|
|
74
|
+
|
|
75
|
+
<template>
|
|
76
|
+
<!-- GOOD: Only renders ~20 visible items at a time -->
|
|
77
|
+
<RecycleScroller
|
|
78
|
+
v-slot="{ item }"
|
|
79
|
+
class="user-list"
|
|
80
|
+
:items="users"
|
|
81
|
+
:item-size="80"
|
|
82
|
+
key-field="id"
|
|
83
|
+
>
|
|
84
|
+
<UserCard :user="item" />
|
|
85
|
+
</RecycleScroller>
|
|
86
|
+
</template>
|
|
87
|
+
|
|
88
|
+
<style scoped>
|
|
89
|
+
.user-list {
|
|
90
|
+
height: 600px; /* Container must have fixed height */
|
|
91
|
+
}
|
|
92
|
+
</style>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Using @tanstack/vue-virtual
|
|
96
|
+
|
|
97
|
+
```vue
|
|
98
|
+
<script setup>
|
|
99
|
+
import { useVirtualizer } from '@tanstack/vue-virtual'
|
|
100
|
+
import { ref } from 'vue'
|
|
101
|
+
|
|
102
|
+
const users = ref([
|
|
103
|
+
/* 10,000 users */
|
|
104
|
+
])
|
|
105
|
+
const parentRef = ref(null)
|
|
106
|
+
|
|
107
|
+
const rowVirtualizer = useVirtualizer({
|
|
108
|
+
count: users.value.length,
|
|
109
|
+
getScrollElement: () => parentRef.value,
|
|
110
|
+
estimateSize: () => 80, // Estimated row height
|
|
111
|
+
overscan: 5, // Render 5 extra items above/below viewport
|
|
112
|
+
})
|
|
113
|
+
</script>
|
|
114
|
+
|
|
115
|
+
<template>
|
|
116
|
+
<div ref="parentRef" class="list-container">
|
|
117
|
+
<div
|
|
118
|
+
:style="{
|
|
119
|
+
height: `${rowVirtualizer.getTotalSize()}px`,
|
|
120
|
+
position: 'relative',
|
|
121
|
+
}"
|
|
122
|
+
>
|
|
123
|
+
<div
|
|
124
|
+
v-for="virtualRow in rowVirtualizer.getVirtualItems()"
|
|
125
|
+
:key="virtualRow.key"
|
|
126
|
+
:style="{
|
|
127
|
+
position: 'absolute',
|
|
128
|
+
top: 0,
|
|
129
|
+
left: 0,
|
|
130
|
+
width: '100%',
|
|
131
|
+
height: `${virtualRow.size}px`,
|
|
132
|
+
transform: `translateY(${virtualRow.start}px)`,
|
|
133
|
+
}"
|
|
134
|
+
>
|
|
135
|
+
<UserCard :user="users[virtualRow.index]" />
|
|
136
|
+
</div>
|
|
137
|
+
</div>
|
|
138
|
+
</div>
|
|
139
|
+
</template>
|
|
140
|
+
|
|
141
|
+
<style scoped>
|
|
142
|
+
.list-container {
|
|
143
|
+
height: 600px;
|
|
144
|
+
overflow: auto;
|
|
145
|
+
}
|
|
146
|
+
</style>
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Dynamic Heights with vue-virtual-scroller
|
|
150
|
+
|
|
151
|
+
```vue
|
|
152
|
+
<script setup>
|
|
153
|
+
import { DynamicScroller, DynamicScrollerItem } from 'vue-virtual-scroller'
|
|
154
|
+
</script>
|
|
155
|
+
|
|
156
|
+
<template>
|
|
157
|
+
<!-- For variable height items, use DynamicScroller -->
|
|
158
|
+
<DynamicScroller :items="messages" :min-item-size="54" key-field="id">
|
|
159
|
+
<template #default="{ item, index, active }">
|
|
160
|
+
<DynamicScrollerItem :item="item" :active="active" :data-index="index">
|
|
161
|
+
<ChatMessage :message="item" />
|
|
162
|
+
</DynamicScrollerItem>
|
|
163
|
+
</template>
|
|
164
|
+
</DynamicScroller>
|
|
165
|
+
</template>
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Performance Comparison
|
|
169
|
+
|
|
170
|
+
| Approach | 100 Items | 1,000 Items | 10,000 Items |
|
|
171
|
+
| ------------------ | -------------- | ---------------- | ------------------- |
|
|
172
|
+
| Regular v-for | ~100 DOM nodes | ~1,000 DOM nodes | ~10,000 DOM nodes |
|
|
173
|
+
| Virtualized | ~20 DOM nodes | ~20 DOM nodes | ~20 DOM nodes |
|
|
174
|
+
| Initial render | Fast | Slow | Very slow / crashes |
|
|
175
|
+
| Virtualized render | Fast | Fast | Fast |
|
|
176
|
+
|
|
177
|
+
## When NOT to Virtualize
|
|
178
|
+
|
|
179
|
+
- Lists under 50 items with simple content
|
|
180
|
+
- Lists where all items must be accessible to screen readers simultaneously
|
|
181
|
+
- Print layouts where all content must render
|
|
182
|
+
- SEO-critical content that must be in initial HTML
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Vue Plugin Best Practices
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: Incorrect plugin structure or injection key strategy causes install failures, collisions, and unsafe APIs
|
|
5
|
+
type: best-practice
|
|
6
|
+
tags: [vue3, plugins, provide-inject, typescript, dependency-injection]
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# Vue Plugin Best Practices
|
|
10
|
+
|
|
11
|
+
**Impact: MEDIUM** - Vue plugins should follow the `app.use()` contract, expose explicit capabilities, and use collision-safe injection keys. This keeps plugin setup predictable and composable across large apps.
|
|
12
|
+
|
|
13
|
+
## Task List
|
|
14
|
+
|
|
15
|
+
- Export plugins as an object with `install()` or as an install function
|
|
16
|
+
- Use the `app` instance in `install()` to register components/directives/provides
|
|
17
|
+
- Type plugin APIs with `Plugin` (and options tuple types when needed)
|
|
18
|
+
- Use symbol keys (prefer `InjectionKey<T>`) for `provide/inject` in plugins
|
|
19
|
+
- Add a small typed composable wrapper for required injections to fail fast
|
|
20
|
+
|
|
21
|
+
## Structure Plugins for `app.use()`
|
|
22
|
+
|
|
23
|
+
A Vue plugin must be either:
|
|
24
|
+
|
|
25
|
+
- An object with `install(app, options?)`
|
|
26
|
+
- A function with the same signature
|
|
27
|
+
|
|
28
|
+
**BAD:**
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
const notAPlugin = {
|
|
32
|
+
doSomething() {},
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
app.use(notAPlugin)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**GOOD:**
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import type { App } from 'vue'
|
|
42
|
+
|
|
43
|
+
interface PluginOptions {
|
|
44
|
+
prefix?: string
|
|
45
|
+
debug?: boolean
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const myPlugin = {
|
|
49
|
+
install(app: App, options: PluginOptions = {}) {
|
|
50
|
+
const { prefix = 'my', debug = false } = options
|
|
51
|
+
|
|
52
|
+
if (debug) {
|
|
53
|
+
console.log('Installing myPlugin with prefix:', prefix)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
app.provide('myPlugin', { prefix })
|
|
57
|
+
},
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
app.use(myPlugin, { prefix: 'custom', debug: true })
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
**GOOD:**
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import type { App } from 'vue'
|
|
67
|
+
|
|
68
|
+
function simplePlugin(app: App, options?: { message: string }) {
|
|
69
|
+
app.config.globalProperties.$greet = () => options?.message ?? 'Hello!'
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
app.use(simplePlugin, { message: 'Welcome!' })
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Register Capabilities Explicitly in `install()`
|
|
76
|
+
|
|
77
|
+
Inside `install()`, wire behavior through Vue application APIs:
|
|
78
|
+
|
|
79
|
+
- `app.component()` for global components
|
|
80
|
+
- `app.directive()` for global directives
|
|
81
|
+
- `app.provide()` for injectable services and config
|
|
82
|
+
- `app.config.globalProperties` for optional global helpers (sparingly)
|
|
83
|
+
|
|
84
|
+
**BAD:**
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const uselessPlugin = {
|
|
88
|
+
install(app, options) {
|
|
89
|
+
const service = createService(options)
|
|
90
|
+
},
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
**GOOD:**
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
const usefulPlugin = {
|
|
98
|
+
install(app, options) {
|
|
99
|
+
const service = createService(options)
|
|
100
|
+
app.provide(serviceKey, service)
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Type Plugin Contracts
|
|
106
|
+
|
|
107
|
+
Use Vue's `Plugin` type to keep install signatures and options type-safe.
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import type { App, Plugin } from 'vue'
|
|
111
|
+
|
|
112
|
+
interface MyOptions {
|
|
113
|
+
apiKey: string
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const myPlugin: Plugin<[MyOptions]> = {
|
|
117
|
+
install(app: App, options: MyOptions) {
|
|
118
|
+
app.provide(apiKeyKey, options.apiKey)
|
|
119
|
+
},
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Use Symbol Injection Keys in Plugins
|
|
124
|
+
|
|
125
|
+
String keys can collide (`'http'`, `'config'`, `'i18n'`). Use symbol keys with `InjectionKey<T>` so injections are unique and typed.
|
|
126
|
+
|
|
127
|
+
**BAD:**
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
export default {
|
|
131
|
+
install(app) {
|
|
132
|
+
app.provide('http', axios)
|
|
133
|
+
app.provide('config', appConfig)
|
|
134
|
+
},
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
**GOOD:**
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
import type { AxiosInstance } from 'axios'
|
|
142
|
+
import type { InjectionKey } from 'vue'
|
|
143
|
+
|
|
144
|
+
interface AppConfig {
|
|
145
|
+
apiUrl: string
|
|
146
|
+
timeout: number
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export const httpKey: InjectionKey<AxiosInstance> = Symbol('http')
|
|
150
|
+
export const configKey: InjectionKey<AppConfig> = Symbol('appConfig')
|
|
151
|
+
|
|
152
|
+
export default {
|
|
153
|
+
install(app) {
|
|
154
|
+
app.provide(httpKey, axios)
|
|
155
|
+
app.provide(configKey, { apiUrl: '/api', timeout: 5000 })
|
|
156
|
+
},
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Provide Required Injection Helpers
|
|
161
|
+
|
|
162
|
+
Wrap required injections in composables that throw clear setup errors.
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
import type { AuthService } from '@/injection-keys'
|
|
166
|
+
|
|
167
|
+
import { inject } from 'vue'
|
|
168
|
+
|
|
169
|
+
import { authKey } from '@/injection-keys'
|
|
170
|
+
|
|
171
|
+
export function useAuth(): AuthService {
|
|
172
|
+
const auth = inject(authKey)
|
|
173
|
+
if (!auth) {
|
|
174
|
+
throw new Error('Auth plugin not installed. Did you forget app.use(authPlugin)?')
|
|
175
|
+
}
|
|
176
|
+
return auth
|
|
177
|
+
}
|
|
178
|
+
```
|