Skip to content

Commit

Permalink
fix: work with files named definePage
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jan 8, 2023
1 parent feb04cc commit 178107b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/core/__snapshots__/definePage.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@ const b = 1
</template>
"
`;

exports[`definePage > works if file is named definePage 1`] = `
"
<script setup>
const a = 1
const b = 1
</script>
<template>
<div>hello</div>
</template>
"
`;
31 changes: 31 additions & 0 deletions src/core/definePage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,35 @@ const b = 1

expect(result).toBe('export default {}')
})

it('works if file is named definePage', async () => {
const result = (await definePageTransform({
code: sampleCode,
id: 'src/pages/definePage.vue',
})) as Exclude<TransformResult, string>

expect(result).toHaveProperty('code')
// should be the sfc without the definePage call
expect(result?.code).toMatchSnapshot()

expect(
await definePageTransform({
code: sampleCode,
id: 'src/pages/definePage?definePage.vue',
})
).toMatchObject({
code: `\
export default {
name: 'custom',
path: '/custom',
}`,
})

expect(
await extractDefinePageNameAndPath(sampleCode, 'src/pages/definePage.vue')
).toEqual({
name: 'custom',
path: '/custom',
})
})
})
3 changes: 2 additions & 1 deletion src/core/definePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { walkAST } from 'ast-walker-scope'
import { CustomRouteBlock } from './customBlock'

const MACRO_DEFINE_PAGE = 'definePage'
const MACRO_DEFINE_PAGE_QUERY = /[?&]definePage\b/

function isStringLiteral(node: Node | null | undefined): node is StringLiteral {
return node?.type === 'StringLiteral'
Expand All @@ -35,7 +36,7 @@ export function definePageTransform({
if (!sfc.scriptSetup) return

// are we extracting only the definePage object
const isExtractingDefinePage = id.includes(MACRO_DEFINE_PAGE)
const isExtractingDefinePage = MACRO_DEFINE_PAGE_QUERY.test(id)

const { script, scriptSetup, scriptCompiled } = sfc

Expand Down

0 comments on commit 178107b

Please sign in to comment.