Skip to content

Commit 6f4f4cd

Browse files
authored
fix: skip parsing non-sfcs (#36)
1 parent d664dd8 commit 6f4f4cd

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"dependencies": {
2828
"@nuxt/kit": "^3.0.0-rc.3",
2929
"@vue/compiler-sfc": "^3.2.33",
30+
"pathe": "^0.3.3",
3031
"scule": "^0.2.1"
3132
},
3233
"devDependencies": {

src/module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { readFile } from 'fs/promises'
2+
import { basename } from 'pathe'
23
import { defineNuxtModule, resolveModule, createResolver, addServerHandler } from '@nuxt/kit'
34
import { parseComponent } from './utils/parseComponent'
45
import type { ComponentProp, ComponentSlot, HookData } from './types'
@@ -35,7 +36,7 @@ export default defineNuxtModule<ModuleOptions>({
3536
source
3637
}
3738

38-
const { props, slots } = parseComponent(data.meta.name, source)
39+
const { props, slots } = parseComponent(data.meta.name, source, { filename: basename(path) })
3940
data.meta.props = props
4041
data.meta.slots = slots
4142

src/utils/parseComponent.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { parse } from '@vue/compiler-sfc'
2+
import { extname } from 'pathe'
23
import type { ComponentProp } from '../types'
34
import { parseSetupScript } from './parseSetupScript'
45
import { parseScript } from './parseScript'
56
import { parseTemplate } from './parseTemplate'
67

7-
export function parseComponent (name: string, source: string) {
8-
// Parse component source
9-
const { descriptor } = parse(source)
8+
export function parseComponent (name: string, source: string, options?: { filename?: string }) {
9+
// TODO: parse non-SFC components
10+
if (options?.filename && extname(options.filename) !== '.vue') {
11+
return { name, props: [], slots: [] }
12+
}
13+
14+
// Parse SFC source
15+
const { descriptor } = parse(source, { filename: options?.filename })
1016
let props: ComponentProp[] = []
1117
let slots
1218

test/basic-component.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('Basic Component', async () => {
88
const path = fileURLToPath(new URL('./fixtures/basic/components/BasicComponent.vue', import.meta.url))
99
const source = await fsp.readFile(path, { encoding: 'utf-8' })
1010
// Parse component source
11-
const { props, slots } = parseComponent('BasicComponent', source)
11+
const { props, slots } = parseComponent('BasicComponent', source, { filename: 'BasicComponent.vue' })
1212

1313
test('Slots', () => {
1414
expect(slots).toEqual([

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@
311311
resolved "https://registry.yarnpkg.com/@hutson/parse-repository-url/-/parse-repository-url-3.0.2.tgz#98c23c950a3d9b6c8f0daed06da6c3af06981340"
312312
integrity sha512-H9XAx3hc0BQHY6l+IFSWHDySypcXsvsuLhgYLUGywmJ5pswRVQJUHpOsobnLYp2ZUaUlKiKDrgWWhosOwAEM8Q==
313313

314+
"@iconify/vue@^3.2.1":
315+
version "3.2.1"
316+
resolved "https://registry.yarnpkg.com/@iconify/vue/-/vue-3.2.1.tgz#bd8a2f1213ba8775d4b7a7a8ba895e53ae2f4dfc"
317+
integrity sha512-c4R6ZgFo1JrJ8aPMMgOPgfU7lBswihMGR+yWe/P4ZukC3kTkeT4+lkt9Pc/itVFMkwva/S/7u9YofmYv57fnNQ==
318+
314319
"@ioredis/commands@^1.1.1":
315320
version "1.1.1"
316321
resolved "https://registry.yarnpkg.com/@ioredis/commands/-/commands-1.1.1.tgz#2ba4299ea624a6bfac15b35f6df90b0015691ec3"
@@ -4951,6 +4956,11 @@ pathe@^0.3.0:
49514956
resolved "https://registry.yarnpkg.com/pathe/-/pathe-0.3.0.tgz#fd95bc16208263fa6dc1c78c07b3907a528de6eb"
49524957
integrity sha512-3vUjp552BJzCw9vqKsO5sttHkbYqqsZtH0x1PNtItgqx8BXEXzoY1SYRKcL6BTyVh4lGJGLj0tM42elUDMvcYA==
49534958

4959+
pathe@^0.3.3:
4960+
version "0.3.3"
4961+
resolved "https://registry.yarnpkg.com/pathe/-/pathe-0.3.3.tgz#8d6d70a25d4db6024ed4d59e59c1bf80fcf18753"
4962+
integrity sha512-x3nrPvG0HDSDzUiJ0WqtzhN4MD+h5B+dFJ3/qyxVuARlr4Y3aJv8gri2cZzp9Z8sGs2a+aG9gNbKngh3gme57A==
4963+
49544964
pathval@^1.1.1:
49554965
version "1.1.1"
49564966
resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.1.tgz#8534e77a77ce7ac5a2512ea21e0fdb8fcf6c3d8d"

0 commit comments

Comments
 (0)