).toHaveBeenWarned()
})
+ describe('duplicate definePage()', () => {
+ const duplicateCode = vue`
+<script setup>
+definePage({
+ name: 'first',
+})
+definePage({
+ name: 'second',
+})
+</script>
+`
+
+ it('does not throw and keeps the first call when extracting', async () => {
+ const result = (await definePageTransform({
+ code: duplicateCode,
+ id: 'src/pages/dup.vue?definePage&vue',
+ })) as Exclude<TransformResult, string>
+
+ expect(result).toHaveProperty('code')
+ expect(result?.code).toContain('first')
+ expect(result?.code).not.toContain('second')
+ expect('duplicate definePage() call').toHaveBeenWarned()
+ })
+
+ it('removes every call from the component and does not throw', async () => {
+ const result = (await definePageTransform({
+ code: duplicateCode,
+ id: 'src/pages/dup.vue',
+ })) as Exclude<TransformResult, string>
+
+ expect(result).toHaveProperty('code')
+ expect(result?.code).not.toContain('definePage')
+ expect('duplicate definePage() call').toHaveBeenWarned()
+ })
+
+ it('extracts info from the first call only', () => {
+ expect(extractDefinePageInfo(duplicateCode, 'src/pages/dup.vue')).toEqual(
+ {
+ name: 'first',
+ hasRemainingProperties: false,
+ }
+ )
+ expect('duplicate definePage() call').toHaveBeenWarned()
+ })
+ })
+
it('extracts name and path', () => {
expect(extractDefinePageInfo(sampleCode, 'src/pages/basic.vue')).toEqual({
name: 'custom',
: // e.g. index.vue that contains a commented `definePage()
null
} else if (definePageNodes.length > 1) {
- throw new SyntaxError(`duplicate definePage() call`)
+ // ignore the extra calls and keep only the first one instead of crashing
+ diagnostics.VUE_ROUTER_B0020({ filename: id })
}
const definePageNode = definePageNodes[0]!
const s = new MagicString(code)
- // s.removeNode(definePageNode, { offset })
- s.remove(offset + definePageNode.start!, offset + definePageNode.end!)
+ // remove every definePage() call so duplicates don't leak into the component
+ for (const node of definePageNodes) {
+ s.remove(offset + node.start!, offset + node.end!)
+ }
return generateTransform(s, id)
}
if (!definePageNodes.length) {
return
} else if (definePageNodes.length > 1) {
- throw new SyntaxError(`duplicate definePage() call`)
+ // ignore the extra calls and keep only the first one instead of crashing
+ diagnostics.VUE_ROUTER_B0020({ filename: id })
}
const definePageNode = definePageNodes[0]!
`route alias array must only contain string literals. Found "${p.found}" in file "${p.filename}".`,
fix: 'Only use string literals inside the route `alias` array.',
},
+ VUE_ROUTER_B0020: {
+ why: (p: { filename: string }) =>
+ `duplicate definePage() call in file "${p.filename}". Only the first one is used, the rest are ignored.`,
+ fix: 'A file can only have one definePage() call. Merge them into a single definePage() call.',
+ },
// --- options.ts ---
VUE_ROUTER_B0009: {