forked from vue-vine/vue-vine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.spec.ts
273 lines (247 loc) · 8.35 KB
/
transform.spec.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import { format } from 'prettier'
import { describe, expect, it } from 'vitest'
import { compileVineTypeScriptFile } from '../src/index'
import { createMockTransformCtx } from './shared-utils'
const testContent = `
import { ref } from 'vue'
import someDefaultExport from 'some-module-1'
import { someNamedExport } from 'some-module-2'
import * as someNamespaceExport from 'some-module-3'
import type { SomeType } from 'some-module-4'
import { someExternalFunction1, someExternalFunction2 } from 'some-module-4'
const v1: SomeType = someExternalFunction1({ a: 1, b: 2 })
const v2 = ref(0)
someExternalFunction2()
function MyProfile() {
const name = vineProp<string>()
const age = vineProp.withDefault<number>(18)
const bio = vineProp.optional<string>()
const textColor = ref('#1c1c1c')
const handleRefresh = () => {
// ...
}
const mySlots = vineSlots<{
default(props: { msg: string }): any
}>()
const defaultModelWithValue = vineModel({ default: 'test' })
const title = vineModel('title', { default: '' })
const count = vineModel<number>('count')
vineExpose({
age,
bio,
})
vineEmits<{
somethingChange: [string];
}>()
vineStyle(\`
.my-profile {
padding: 10px;
margin: 8px;
color: v-bind(textColor);
}
\`)
vineStyle.scoped(\`
.bio {
font-size: 12px;
}
\`)
vineStyle.import('../styles/test1.less').scoped()
vineStyle.import(\`../styles/test2.scss\`)
onMounted(async () => {
const data = await fetch("https://api.sampleapis.com/futurama/characters")
console.log(data)
})
return vine\`
<div class="my-profile">
<div>{{ name }}<span> - {{ age }}</span></div>
<p class="bio" v-show="bio">{{ bio }}</p>
<button @click="handleRefresh">Refresh</button>
</div>
\`
}
export default async function MyApp() {
const data = await fetch("https://api.sampleapis.com/futurama/characters")
vineStyle.scoped(\`
.my-app {
font-size: 16px;
}
\`)
return vine\`
<div class="my-app">
<h2>Hello world</h2>
<MyProfile name="Tomy" :age="24" />
</div>
\`
}`
describe('test transform', () => {
it('inline mode output result', async () => {
const { mockCompilerCtx, mockCompilerHooks } = createMockTransformCtx()
compileVineTypeScriptFile(testContent, 'testTransformInlineResult', { compilerHooks: mockCompilerHooks })
expect(mockCompilerCtx.vineCompileErrors.length).toBe(0)
const fileCtx = mockCompilerCtx.fileCtxMap.get('testTransformInlineResult')
const transformed = fileCtx?.fileMagicCode.toString() ?? ''
const formated = await format(
transformed,
{ parser: 'babel-ts' },
)
expect(formated).toMatchSnapshot()
})
it('separated mode output result', async () => {
const { mockCompilerCtx, mockCompilerHooks } = createMockTransformCtx({
inlineTemplate: false,
})
compileVineTypeScriptFile(testContent, 'testTransformSeparatedResult', { compilerHooks: mockCompilerHooks })
expect(mockCompilerCtx.vineCompileErrors.length).toBe(0)
const fileCtx = mockCompilerCtx.fileCtxMap.get('testTransformSeparatedResult')
const transformed = fileCtx?.fileMagicCode.toString() ?? ''
const formated = await format(
transformed,
{ parser: 'babel-ts' },
)
expect(formated).toMatchSnapshot()
})
it('separated mode output result by ssr', async () => {
const { mockCompilerCtx, mockCompilerHooks } = createMockTransformCtx({
inlineTemplate: false,
})
compileVineTypeScriptFile(testContent, 'testTransformSeparatedResult', { compilerHooks: mockCompilerHooks }, true)
expect(mockCompilerCtx.vineCompileErrors.length).toBe(0)
const fileCtx = mockCompilerCtx.fileCtxMap.get('testTransformSeparatedResult')
const transformed = fileCtx?.fileMagicCode.toString() ?? ''
const formated = await format(
transformed,
{ parser: 'babel-ts' },
)
expect(formated).toMatchSnapshot()
})
it('not output HMR content in non-dev mode', async () => {
const { mockCompilerCtx, mockCompilerHooks } = createMockTransformCtx({
envMode: 'production',
})
compileVineTypeScriptFile(testContent, 'testNoHMRContentOnProduction', { compilerHooks: mockCompilerHooks })
expect(mockCompilerCtx.vineCompileErrors.length).toBe(0)
const fileCtx = mockCompilerCtx.fileCtxMap.get('testNoHMRContentOnProduction')
const transformed = fileCtx?.fileMagicCode.toString() ?? ''
const formated = await format(
transformed,
{ parser: 'babel-ts' },
)
expect(formated).toMatchSnapshot()
expect(formated.includes('__hmrId')).toBe(false)
expect(formated.includes('__VUE_HMR_RUNTIME__')).toBe(false)
})
// issue#83
it('should generate hmrId If there is no style', async () => {
const { mockCompilerCtx, mockCompilerHooks } = createMockTransformCtx({
envMode: 'development',
})
const testContent = `
export function About() {
return vine\`
<div>
<h2>About page</h2>
</div>
\`
}`
compileVineTypeScriptFile(testContent, 'testHMRContentOnNoStyle', { compilerHooks: mockCompilerHooks })
expect(mockCompilerCtx.vineCompileErrors.length).toBe(0)
const fileCtx = mockCompilerCtx.fileCtxMap.get('testHMRContentOnNoStyle')
const transformed = fileCtx?.fileMagicCode.toString() ?? ''
const formated = await format(
transformed,
{ parser: 'babel-ts' },
)
expect(formated).toMatchSnapshot()
expect(formated.includes('__hmrId')).toBe(true)
expect(formated.includes('__VUE_HMR_RUNTIME__')).toBe(true)
})
it('should not add unused-in-template imports to returns in separated mode', async () => {
const specContent = `
import { ref, Ref } from 'vue'
import { foo } from './other' // Vue Vine issue #168
export function MyComp() {
const count = ref(0)
return vine\`
<div @click="foo()">{{ count }}</div>
\`
}
`
const { mockCompilerCtx, mockCompilerHooks } = createMockTransformCtx({
inlineTemplate: false,
})
compileVineTypeScriptFile(specContent, 'testUnusedImports', { compilerHooks: mockCompilerHooks })
expect(mockCompilerCtx.vineCompileErrors.length).toBe(0)
const fileCtx = mockCompilerCtx.fileCtxMap.get('testUnusedImports')
const transformed = fileCtx?.fileMagicCode.toString() ?? ''
const formated = await format(
transformed,
{ parser: 'babel-ts' },
)
expect(formated).toMatchInlineSnapshot(`
"import {
toDisplayString as _toDisplayString,
openBlock as _openBlock,
createElementBlock as _createElementBlock,
defineComponent as _defineComponent,
useCssVars as _useCssVars,
} from "vue";
import { ref, Ref } from "vue";
import { foo } from "./other"; // Vue Vine issue #168
export const MyComp = (() => {
const __vine = _defineComponent({
name: "MyComp",
/* No props */
/* No emits */
setup(__props, { expose: __expose }) {
__expose();
const props = __props;
const count = ref(0);
return {
count,
get foo() {
return foo;
},
MyComp,
};
},
});
function __sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return (
_openBlock(),
_createElementBlock(
"div",
{
onClick: _cache[0] || (_cache[0] = ($event) => $setup.foo()),
},
_toDisplayString($setup.count),
1 /* TEXT */,
)
);
}
__vine.render = __sfc_render;
__vine.__hmrId = "01d6e3a5";
return __vine;
})();
typeof __VUE_HMR_RUNTIME__ !== "undefined" &&
__VUE_HMR_RUNTIME__.createRecord(MyComp.__hmrId, MyComp);
export const _rerender_only = false;
export const _rerender_vcf_fn_name = "";
import.meta.hot?.accept((mod) => {
if (!mod) {
return;
}
const { _rerender_only, _rerender_vcf_fn_name } = mod;
if (!_rerender_vcf_fn_name) {
return;
}
const component = mod[_rerender_vcf_fn_name];
if (_rerender_only) {
__VUE_HMR_RUNTIME__.rerender(component.__hmrId, component.render);
} else {
__VUE_HMR_RUNTIME__.reload(component.__hmrId, component);
}
});
"
`)
})
})