|
1 | 1 | import { HELPER_PREFIX } from '@vue-macros/common'
|
2 |
| -import type { TransformOptions } from '.' |
| 2 | +import type { RootMap, TransformOptions } from '.' |
| 3 | + |
| 4 | +export function getGlobalTypes( |
| 5 | + rootMap: RootMap, |
| 6 | + options: TransformOptions, |
| 7 | +): string { |
| 8 | + let defineStyle = '' |
| 9 | + if (options.defineSlots.alias) { |
| 10 | + defineStyle = options.defineStyle.alias |
| 11 | + .map( |
| 12 | + (alias) => |
| 13 | + `declare const ${alias}: { <T>(...args: ${HELPER_PREFIX}StyleArgs): T; scss: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; sass: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; stylus: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; less: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; postcss: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T };`, |
| 14 | + ) |
| 15 | + .join('\n') |
| 16 | + defineStyle += `type ${HELPER_PREFIX}StyleArgs = [style: string, options?: { scoped?: boolean }];` |
| 17 | + } |
| 18 | + if (!rootMap.size) { |
| 19 | + return defineStyle |
| 20 | + } |
3 | 21 |
|
4 |
| -export function getGlobalTypes(options: TransformOptions): string { |
5 | 22 | const defineSlots = options.defineSlots.alias
|
6 | 23 | .flatMap((alias) => [
|
7 | 24 | `declare function ${alias}<T extends Record<string, any>>(): Partial<T>;`,
|
8 |
| - `declare function ${alias}<T extends Record<string, any>>(slots: T): T;\n`, |
| 25 | + `declare function ${alias}<T extends Record<string, any>>(slots: T): T;`, |
9 | 26 | ])
|
10 |
| - .join('') |
| 27 | + .join('\n') |
11 | 28 | const defineExpose = options.defineExpose.alias
|
12 | 29 | .map(
|
13 | 30 | (alias) =>
|
14 | 31 | `declare function ${alias}<Exposed extends Record<string, any> = Record<string, any>>(exposed?: Exposed): Exposed;`,
|
15 | 32 | )
|
16 |
| - .join('') |
17 |
| - const defineStyle = options.defineStyle.alias |
18 |
| - .map( |
19 |
| - (alias) => |
20 |
| - `declare const ${alias}: { <T>(...args: ${HELPER_PREFIX}StyleArgs): T; scss: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; sass: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; stylus: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; less: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T; postcss: <T>(...args: ${HELPER_PREFIX}StyleArgs)=> T };\n`, |
21 |
| - ) |
22 |
| - .join('') |
23 |
| - const defineModel = options.defineModel.alias |
24 |
| - .map((alias) => |
25 |
| - alias === 'defineModel' ? 'defineModel' : `defineModel: ${alias}`, |
26 |
| - ) |
27 |
| - .join(', ') |
28 |
| - const defineComponent = options.defineComponent.alias |
29 |
| - .map((alias) => |
30 |
| - ['defineComponent', 'defineVaporComponent'].includes(alias) |
31 |
| - ? '' |
32 |
| - : `defineComponent: ${alias}`, |
33 |
| - ) |
| 33 | + .join('\n') |
| 34 | + const defineModel = options.defineModel.alias.map((alias) => |
| 35 | + alias === 'defineModel' ? 'defineModel' : `defineModel: ${alias}`, |
| 36 | + ) |
| 37 | + const defineComponent = options.defineComponent.alias.map((alias) => |
| 38 | + ['defineComponent', 'defineVaporComponent'].includes(alias) |
| 39 | + ? '' |
| 40 | + : `defineComponent: ${alias}`, |
| 41 | + ) |
| 42 | + const VueMacros = [...defineModel, ...defineComponent] |
34 | 43 | .filter(Boolean)
|
35 |
| - .join(', ') |
| 44 | + .join(',') |
36 | 45 | return `
|
37 |
| -declare const { ${defineModel}, ${defineComponent} }: typeof import('vue') |
| 46 | +${VueMacros ? `declare const { ${VueMacros} }: typeof import('vue');` : ''} |
38 | 47 | ${defineSlots}
|
39 | 48 | ${defineExpose}
|
40 | 49 | ${defineStyle}
|
41 |
| -type ${HELPER_PREFIX}StyleArgs = [style: string, options?: { scoped?: boolean }]; |
42 | 50 | // @ts-ignore
|
43 | 51 | type __VLS_IsAny<T> = 0 extends 1 & T ? true : false; type __VLS_PickNotAny<A, B> = __VLS_IsAny<A> extends true ? B : A;
|
44 | 52 | `
|
|
0 commit comments