This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
auto-imports.test.ts
186 lines (174 loc) · 5.1 KB
/
auto-imports.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { readFileSync } from 'node:fs'
import { expect, describe, it } from 'vitest'
import { join } from 'pathe'
import { createCommonJS, findExports } from 'mlly'
import * as VueFunctions from 'vue'
import { createUnimport, Import } from 'unimport'
import { Plugin } from 'vite'
import { TransformPlugin } from '../src/imports/transform'
import { defaultPresets } from '../src/imports/presets'
describe('imports:transform', () => {
const imports: Import[] = [
{ name: 'ref', as: 'ref', from: 'vue' },
{ name: 'computed', as: 'computed', from: 'bar' },
{ name: 'foo', as: 'foo', from: 'excluded' }
]
const ctx = createUnimport({
imports
})
const transformPlugin = TransformPlugin.raw({ ctx, options: { transform: { exclude: [/node_modules/] } } }, { framework: 'rollup' }) as Plugin
const transform = async (source: string) => {
const result = await (transformPlugin.transform! as Function).call({ error: null, warn: null } as any, source, '')
return typeof result === 'string' ? result : result?.code
}
it('should correct inject', async () => {
expect(await transform('const a = ref(0)')).toMatchInlineSnapshot('"import { ref } from \'vue\';\nconst a = ref(0)"')
})
it('should ignore existing imported', async () => {
expect(await transform('import { ref } from "foo"; const a = ref(0)')).to.equal(undefined)
expect(await transform('import { computed as ref } from "foo"; const a = ref(0)')).to.equal(undefined)
expect(await transform('import ref from "foo"; const a = ref(0)')).to.equal(undefined)
expect(await transform('import { z as ref } from "foo"; const a = ref(0)')).to.equal(undefined)
expect(await transform('let ref = () => {}; const a = ref(0)')).to.equal(undefined)
expect(await transform('let { ref } = Vue; const a = ref(0)')).to.equal(undefined)
expect(await transform('let [\ncomputed,\nref\n] = Vue; const a = ref(0); const b = ref(0)')).to.equal(undefined)
})
it('should ignore comments', async () => {
const result = await transform('// import { computed } from "foo"\n;const a = computed(0)')
expect(result).toMatchInlineSnapshot(`
"import { computed } from 'bar';
// import { computed } from \\"foo\\"
;const a = computed(0)"
`)
})
it('should exclude files from transform', async () => {
expect(await transform('excluded')).toEqual(undefined)
})
})
const excludedNuxtHelpers = ['useHydration']
describe('imports:nuxt', () => {
try {
const { __dirname } = createCommonJS(import.meta.url)
const entrypointContents = readFileSync(join(__dirname, '../src/app/composables/index.ts'), 'utf8')
const names = findExports(entrypointContents).flatMap(i => i.names || i.name)
for (const name of names) {
if (excludedNuxtHelpers.includes(name)) {
continue
}
it(`should register ${name} globally`, () => {
expect(defaultPresets.flatMap(a => a.from === '#app' ? a.imports : [])).to.include(name)
})
}
} catch (e) {
it('should import composables', () => {
console.log(e)
expect(false).toBe(true)
})
}
})
const excludedVueHelpers = [
// Already globally registered
'defineEmits',
'defineExpose',
'defineProps',
'withDefaults',
'stop',
//
'__esModule',
'devtools',
'EffectScope',
'ReactiveEffect',
'stop',
'camelize',
'capitalize',
'normalizeClass',
'normalizeProps',
'normalizeStyle',
'toDisplayString',
'toHandlerKey',
'BaseTransition',
'Comment',
'Fragment',
'KeepAlive',
'Static',
'Suspense',
'Teleport',
'Text',
'callWithAsyncErrorHandling',
'callWithErrorHandling',
'cloneVNode',
'compatUtils',
'createBlock',
'createCommentVNode',
'createElementBlock',
'createElementVNode',
'createHydrationRenderer',
'createPropsRestProxy',
'createRenderer',
'createSlots',
'createStaticVNode',
'createTextVNode',
'createVNode',
'getTransitionRawChildren',
'guardReactiveProps',
'handleError',
'initCustomFormatter',
'isMemoSame',
'isRuntimeOnly',
'isVNode',
'mergeDefaults',
'mergeProps',
'openBlock',
'popScopeId',
'pushScopeId',
'queuePostFlushCb',
'registerRuntimeCompiler',
'renderList',
'renderSlot',
'resolveComponent',
'resolveDirective',
'resolveDynamicComponent',
'resolveFilter',
'resolveTransitionHooks',
'setBlockTracking',
'setDevtoolsHook',
'setTransitionHooks',
'ssrContextKey',
'ssrUtils',
'toHandlers',
'transformVNodeArgs',
'useSSRContext',
'version',
'warn',
'watchPostEffect',
'watchSyncEffect',
'withAsyncContext',
'Transition',
'TransitionGroup',
'VueElement',
'createApp',
'createSSRApp',
'defineCustomElement',
'defineSSRCustomElement',
'hydrate',
'initDirectivesForSSR',
'render',
'useCssVars',
'vModelCheckbox',
'vModelDynamic',
'vModelRadio',
'vModelSelect',
'vModelText',
'vShow',
'compile'
]
describe('imports:vue', () => {
for (const name of Object.keys(VueFunctions)) {
if (excludedVueHelpers.includes(name)) {
continue
}
it(`should register ${name} globally`, () => {
expect(defaultPresets.find(a => a.from === 'vue')!.imports).toContain(name)
})
}
})