diff --git a/src/core/declaration.ts b/src/core/declaration.ts index 9922d6d4..a66a3fc4 100644 --- a/src/core/declaration.ts +++ b/src/core/declaration.ts @@ -12,7 +12,7 @@ const multilineCommentsRE = /\/\*.*?\*\//gms const singlelineCommentsRE = /\/\/.*$/gm function extractImports(code: string) { - return Object.fromEntries(Array.from(code.matchAll(/['"]?([\S]+?)['"]?\s*:\s*(.+?)[,;\n]/g)).map(i => [i[1], i[2]])) + return Object.fromEntries(Array.from(code.matchAll(/['"]?([^\s'"]+)['"]?\s*:\s*(.+?)[,;\n]/g)).map(i => [i[1], i[2]])) } export function parseDeclaration(code: string): DeclarationImports | undefined { diff --git a/test/__snapshots__/dts.test.ts.snap b/test/__snapshots__/dts.test.ts.snap index f00f87fd..44267f2d 100644 --- a/test/__snapshots__/dts.test.ts.snap +++ b/test/__snapshots__/dts.test.ts.snap @@ -22,6 +22,43 @@ declare module '@vue/runtime-core' { " `; +exports[`dts > parseDeclaration - has icon component like 1`] = ` +{ + "component": { + "ComponentA": "typeof import('./src/components/ComponentA.vue')['default']", + "IMdi:diceD12": "typeof import('~icons/mdi/dice-d12')['default']", + "IMdiLightAlarm": "typeof import('~icons/mdi-light/alarm')['default']", + }, + "directive": {}, +} +`; + +exports[`dts > parseDeclaration - with directives 1`] = ` +{ + "component": { + "ComponentA": "typeof import('./src/components/ComponentA.vue')['default']", + "IMdi:diceD12": "typeof import('~icons/mdi/dice-d12')['default']", + "IMdiLightAlarm": "typeof import('~icons/mdi-light/alarm')['default']", + }, + "directive": { + "vDirective": "typeof import('foo')", + "vLoading": "typeof import('test/directive/Loading')['default']", + "vSome": "typeof import('test/directive/Some')['default']", + }, +} +`; + +exports[`dts > parseDeclaration 1`] = ` +{ + "component": { + "ComponentA": "typeof import('./src/components/ComponentA.vue')['default']", + "ComponentB": "typeof import('./src/components/ComponentB.vue')['default']", + "ComponentC": "typeof import('./src/components/component-c.vue')['default']", + }, + "directive": {}, +} +`; + exports[`dts > writeDeclaration - keep unused 1`] = ` "// generated by unplugin-vue-components // We suggest you to commit this file into source control diff --git a/test/dts.test.ts b/test/dts.test.ts index ccad2ce3..ec0de04b 100644 --- a/test/dts.test.ts +++ b/test/dts.test.ts @@ -3,7 +3,7 @@ import path from 'path' import { describe, expect, test } from 'vitest' import type { ComponentResolver } from '../src' import { Context } from '../src/core/context' -import { getDeclaration } from '../src/core/declaration' +import { getDeclaration, parseDeclaration } from '../src/core/declaration' const resolver: ComponentResolver[] = [ { @@ -82,4 +82,73 @@ const _directive_loading = _resolveDirective("loading")` expect(contents).not.toContain('comment') expect(contents).toContain('vSome') }) + + test('parseDeclaration', async () => { + const code = ` +// generated by unplugin-vue-components +// We suggest you to commit this file into source control +// Read more: https://github.com/vuejs/core/pull/3399 +import '@vue/runtime-core' + +export {} + +declare module '@vue/runtime-core' { + export interface GlobalComponents { + ComponentA: typeof import('./src/components/ComponentA.vue')['default'] + ComponentB: typeof import('./src/components/ComponentB.vue')['default'] + ComponentC: typeof import('./src/components/component-c.vue')['default'] + } +}` + + const imports = parseDeclaration(code) + expect(imports).matchSnapshot() + }) + + test('parseDeclaration - has icon component like ', async () => { + const code = ` +// generated by unplugin-vue-components +// We suggest you to commit this file into source control +// Read more: https://github.com/vuejs/core/pull/3399 +import '@vue/runtime-core' + +export {} + +declare module '@vue/runtime-core' { + export interface GlobalComponents { + ComponentA: typeof import('./src/components/ComponentA.vue')['default'] + 'IMdi:diceD12': typeof import('~icons/mdi/dice-d12')['default'] + IMdiLightAlarm: typeof import('~icons/mdi-light/alarm')['default'] + } +}` + + const imports = parseDeclaration(code) + expect(imports).matchSnapshot() + }) + + test('parseDeclaration - with directives', async () => { + const code = ` +// generated by unplugin-vue-components +// We suggest you to commit this file into source control +// Read more: https://github.com/vuejs/core/pull/3399 +import '@vue/runtime-core' + +export {} + +declare module '@vue/runtime-core' { + export interface GlobalComponents { + ComponentA: typeof import('./src/components/ComponentA.vue')['default'] + 'IMdi:diceD12': typeof import('~icons/mdi/dice-d12')['default'] + IMdiLightAlarm: typeof import('~icons/mdi-light/alarm')['default'] + } + + export interface ComponentCustomProperties { + vDirective: typeof import('foo') + vLoading: typeof import('test/directive/Loading')['default'] + vSome: typeof import('test/directive/Some')['default'] + } +}` + + const imports = parseDeclaration(code) + expect(imports).matchSnapshot() + }) }) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy