Skip to content

Commit

Permalink
fix: skip parsing non-sfcs (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Aug 3, 2022
1 parent d664dd8 commit 6f4f4cd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"@nuxt/kit": "^3.0.0-rc.3",
"@vue/compiler-sfc": "^3.2.33",
"pathe": "^0.3.3",
"scule": "^0.2.1"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { readFile } from 'fs/promises'
import { basename } from 'pathe'
import { defineNuxtModule, resolveModule, createResolver, addServerHandler } from '@nuxt/kit'
import { parseComponent } from './utils/parseComponent'
import type { ComponentProp, ComponentSlot, HookData } from './types'
Expand Down Expand Up @@ -35,7 +36,7 @@ export default defineNuxtModule<ModuleOptions>({
source
}

const { props, slots } = parseComponent(data.meta.name, source)
const { props, slots } = parseComponent(data.meta.name, source, { filename: basename(path) })
data.meta.props = props
data.meta.slots = slots

Expand Down
12 changes: 9 additions & 3 deletions src/utils/parseComponent.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { parse } from '@vue/compiler-sfc'
import { extname } from 'pathe'
import type { ComponentProp } from '../types'
import { parseSetupScript } from './parseSetupScript'
import { parseScript } from './parseScript'
import { parseTemplate } from './parseTemplate'

export function parseComponent (name: string, source: string) {
// Parse component source
const { descriptor } = parse(source)
export function parseComponent (name: string, source: string, options?: { filename?: string }) {
// TODO: parse non-SFC components
if (options?.filename && extname(options.filename) !== '.vue') {
return { name, props: [], slots: [] }
}

// Parse SFC source
const { descriptor } = parse(source, { filename: options?.filename })
let props: ComponentProp[] = []
let slots

Expand Down
2 changes: 1 addition & 1 deletion test/basic-component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Basic Component', async () => {
const path = fileURLToPath(new URL('./fixtures/basic/components/BasicComponent.vue', import.meta.url))
const source = await fsp.readFile(path, { encoding: 'utf-8' })
// Parse component source
const { props, slots } = parseComponent('BasicComponent', source)
const { props, slots } = parseComponent('BasicComponent', source, { filename: 'BasicComponent.vue' })

test('Slots', () => {
expect(slots).toEqual([
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@
resolved "https://registry.yarnpkg.com/@hutson/parse-repository-url/-/parse-repository-url-3.0.2.tgz#98c23c950a3d9b6c8f0daed06da6c3af06981340"
integrity sha512-H9XAx3hc0BQHY6l+IFSWHDySypcXsvsuLhgYLUGywmJ5pswRVQJUHpOsobnLYp2ZUaUlKiKDrgWWhosOwAEM8Q==

"@iconify/vue@^3.2.1":
version "3.2.1"
resolved "https://registry.yarnpkg.com/@iconify/vue/-/vue-3.2.1.tgz#bd8a2f1213ba8775d4b7a7a8ba895e53ae2f4dfc"
integrity sha512-c4R6ZgFo1JrJ8aPMMgOPgfU7lBswihMGR+yWe/P4ZukC3kTkeT4+lkt9Pc/itVFMkwva/S/7u9YofmYv57fnNQ==

"@ioredis/commands@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@ioredis/commands/-/commands-1.1.1.tgz#2ba4299ea624a6bfac15b35f6df90b0015691ec3"
Expand Down Expand Up @@ -4951,6 +4956,11 @@ pathe@^0.3.0:
resolved "https://registry.yarnpkg.com/pathe/-/pathe-0.3.0.tgz#fd95bc16208263fa6dc1c78c07b3907a528de6eb"
integrity sha512-3vUjp552BJzCw9vqKsO5sttHkbYqqsZtH0x1PNtItgqx8BXEXzoY1SYRKcL6BTyVh4lGJGLj0tM42elUDMvcYA==

pathe@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/pathe/-/pathe-0.3.3.tgz#8d6d70a25d4db6024ed4d59e59c1bf80fcf18753"
integrity sha512-x3nrPvG0HDSDzUiJ0WqtzhN4MD+h5B+dFJ3/qyxVuARlr4Y3aJv8gri2cZzp9Z8sGs2a+aG9gNbKngh3gme57A==

pathval@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.1.tgz#8534e77a77ce7ac5a2512ea21e0fdb8fcf6c3d8d"
Expand Down

0 comments on commit 6f4f4cd

Please sign in to comment.