compileScript.spec.ts 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. import { BindingTypes } from '@vue/compiler-core'
  2. import {
  3. assertCode,
  4. compileSFCScript as compile,
  5. getPositionInCode,
  6. mockId,
  7. } from './utils'
  8. import { type RawSourceMap, SourceMapConsumer } from 'source-map-js'
  9. describe('SFC compile <script setup>', () => {
  10. test('should compile JS syntax', () => {
  11. const { content } = compile(`
  12. <script setup lang='js'>
  13. const a = 1
  14. const b = 2
  15. </script>
  16. `)
  17. expect(content).toMatch(`return { a, b }`)
  18. assertCode(content)
  19. })
  20. test('should expose top level declarations', () => {
  21. const { content, bindings } = compile(`
  22. <script setup>
  23. import { x } from './x'
  24. let a = 1
  25. const b = 2
  26. function c() {}
  27. class d {}
  28. </script>
  29. <script>
  30. import { xx } from './x'
  31. let aa = 1
  32. const bb = 2
  33. function cc() {}
  34. class dd {}
  35. </script>
  36. `)
  37. expect(content).toMatch(
  38. `return { get aa() { return aa }, set aa(v) { aa = v }, ` +
  39. `bb, cc, dd, get a() { return a }, set a(v) { a = v }, b, c, d, ` +
  40. `get xx() { return xx }, get x() { return x } }`,
  41. )
  42. expect(bindings).toStrictEqual({
  43. x: BindingTypes.SETUP_MAYBE_REF,
  44. a: BindingTypes.SETUP_LET,
  45. b: BindingTypes.SETUP_CONST,
  46. c: BindingTypes.SETUP_CONST,
  47. d: BindingTypes.SETUP_CONST,
  48. xx: BindingTypes.SETUP_MAYBE_REF,
  49. aa: BindingTypes.SETUP_LET,
  50. bb: BindingTypes.LITERAL_CONST,
  51. cc: BindingTypes.SETUP_CONST,
  52. dd: BindingTypes.SETUP_CONST,
  53. })
  54. assertCode(content)
  55. })
  56. test('binding analysis for destructure', () => {
  57. const { content, bindings } = compile(`
  58. <script setup>
  59. const { foo, b: bar, ['x' + 'y']: baz, x: { y, zz: { z }}} = {}
  60. </script>
  61. `)
  62. expect(content).toMatch('return { foo, bar, baz, y, z }')
  63. expect(bindings).toStrictEqual({
  64. foo: BindingTypes.SETUP_MAYBE_REF,
  65. bar: BindingTypes.SETUP_MAYBE_REF,
  66. baz: BindingTypes.SETUP_MAYBE_REF,
  67. y: BindingTypes.SETUP_MAYBE_REF,
  68. z: BindingTypes.SETUP_MAYBE_REF,
  69. })
  70. assertCode(content)
  71. })
  72. describe('<script> and <script setup> co-usage', () => {
  73. test('script first', () => {
  74. const { content } = compile(`
  75. <script>
  76. export const n = 1
  77. export default {}
  78. </script>
  79. <script setup>
  80. import { x } from './x'
  81. x()
  82. </script>
  83. `)
  84. assertCode(content)
  85. })
  86. test('script setup first', () => {
  87. const { content } = compile(`
  88. <script setup>
  89. import { x } from './x'
  90. x()
  91. </script>
  92. <script>
  93. export const n = 1
  94. export default {}
  95. </script>
  96. `)
  97. assertCode(content)
  98. })
  99. // #7805
  100. test('keep original semi style', () => {
  101. const { content } = compile(`
  102. <script setup>
  103. console.log('test')
  104. const props = defineProps(['item']);
  105. const emit = defineEmits(['change']);
  106. (function () {})()
  107. </script>
  108. `)
  109. assertCode(content)
  110. expect(content).toMatch(`console.log('test')`)
  111. expect(content).toMatch(`const props = __props;`)
  112. expect(content).toMatch(`const emit = __emit;`)
  113. expect(content).toMatch(`(function () {})()`)
  114. })
  115. test('script setup first, named default export', () => {
  116. const { content } = compile(`
  117. <script setup>
  118. import { x } from './x'
  119. x()
  120. </script>
  121. <script>
  122. export const n = 1
  123. const def = {}
  124. export { def as default }
  125. </script>
  126. `)
  127. assertCode(content)
  128. })
  129. // #4395
  130. test('script setup first, lang="ts", script block content export default', () => {
  131. const { content } = compile(`
  132. <script setup lang="ts">
  133. import { x } from './x'
  134. x()
  135. </script>
  136. <script lang="ts">
  137. export default {
  138. name: "test"
  139. }
  140. </script>
  141. `)
  142. // ensure __default__ is declared before used
  143. expect(content).toMatch(/const __default__[\S\s]*\.\.\.__default__/m)
  144. assertCode(content)
  145. })
  146. describe('spaces in ExportDefaultDeclaration node', () => {
  147. // #4371
  148. test('with many spaces and newline', () => {
  149. // #4371
  150. const { content } = compile(`
  151. <script>
  152. export const n = 1
  153. export default
  154. {
  155. some:'option'
  156. }
  157. </script>
  158. <script setup>
  159. import { x } from './x'
  160. x()
  161. </script>
  162. `)
  163. assertCode(content)
  164. })
  165. test('with minimal spaces', () => {
  166. const { content } = compile(`
  167. <script>
  168. export const n = 1
  169. export default{
  170. some:'option'
  171. }
  172. </script>
  173. <script setup>
  174. import { x } from './x'
  175. x()
  176. </script>
  177. `)
  178. assertCode(content)
  179. })
  180. })
  181. test('export call expression as default', () => {
  182. const { content } = compile(`
  183. <script>
  184. function fn() {
  185. return "hello, world";
  186. }
  187. export default fn();
  188. </script>
  189. <script setup>
  190. console.log('foo')
  191. </script>
  192. `)
  193. assertCode(content)
  194. })
  195. })
  196. describe('imports', () => {
  197. test('should hoist and expose imports', () => {
  198. assertCode(
  199. compile(`<script setup>
  200. import { ref } from 'vue'
  201. import 'foo/css'
  202. </script>`).content,
  203. )
  204. })
  205. test('should extract comment for import or type declarations', () => {
  206. assertCode(
  207. compile(`
  208. <script setup>
  209. import a from 'a' // comment
  210. import b from 'b'
  211. </script>
  212. `).content,
  213. )
  214. })
  215. // #2740
  216. test('should allow defineProps/Emit at the start of imports', () => {
  217. assertCode(
  218. compile(`<script setup>
  219. import { ref } from 'vue'
  220. defineProps(['foo'])
  221. defineEmits(['bar'])
  222. const r = ref(0)
  223. </script>`).content,
  224. )
  225. })
  226. test('dedupe between user & helper', () => {
  227. const { content } = compile(
  228. `
  229. <script setup>
  230. import { useCssVars, ref } from 'vue'
  231. const msg = ref()
  232. </script>
  233. <style>
  234. .foo {
  235. color: v-bind(msg)
  236. }
  237. </style>
  238. `,
  239. )
  240. assertCode(content)
  241. expect(content).toMatch(
  242. `import { useCssVars as _useCssVars, unref as _unref } from 'vue'`,
  243. )
  244. expect(content).toMatch(`import { useCssVars, ref } from 'vue'`)
  245. })
  246. test('import dedupe between <script> and <script setup>', () => {
  247. const { content } = compile(`
  248. <script>
  249. import { x } from './x'
  250. </script>
  251. <script setup>
  252. import { x } from './x'
  253. x()
  254. </script>
  255. `)
  256. assertCode(content)
  257. expect(content.indexOf(`import { x }`)).toEqual(
  258. content.lastIndexOf(`import { x }`),
  259. )
  260. })
  261. describe('import ref/reactive function from other place', () => {
  262. test('import directly', () => {
  263. const { bindings } = compile(`
  264. <script setup>
  265. import { ref, reactive } from './foo'
  266. const foo = ref(1)
  267. const bar = reactive(1)
  268. </script>
  269. `)
  270. expect(bindings).toStrictEqual({
  271. ref: BindingTypes.SETUP_MAYBE_REF,
  272. reactive: BindingTypes.SETUP_MAYBE_REF,
  273. foo: BindingTypes.SETUP_MAYBE_REF,
  274. bar: BindingTypes.SETUP_MAYBE_REF,
  275. })
  276. })
  277. test('import w/ alias', () => {
  278. const { bindings } = compile(`
  279. <script setup>
  280. import { ref as _ref, reactive as _reactive } from './foo'
  281. const foo = ref(1)
  282. const bar = reactive(1)
  283. </script>
  284. `)
  285. expect(bindings).toStrictEqual({
  286. _reactive: BindingTypes.SETUP_MAYBE_REF,
  287. _ref: BindingTypes.SETUP_MAYBE_REF,
  288. foo: BindingTypes.SETUP_MAYBE_REF,
  289. bar: BindingTypes.SETUP_MAYBE_REF,
  290. })
  291. })
  292. test('aliased usage before import site', () => {
  293. const { bindings } = compile(`
  294. <script setup>
  295. const bar = x(1)
  296. import { reactive as x } from 'vue'
  297. </script>
  298. `)
  299. expect(bindings).toStrictEqual({
  300. bar: BindingTypes.SETUP_REACTIVE_CONST,
  301. x: BindingTypes.SETUP_CONST,
  302. })
  303. })
  304. })
  305. test('should support module string names syntax', () => {
  306. const { content, bindings } = compile(`
  307. <script>
  308. import { "😏" as foo } from './foo'
  309. </script>
  310. <script setup>
  311. import { "😏" as foo } from './foo'
  312. </script>
  313. `)
  314. assertCode(content)
  315. expect(bindings).toStrictEqual({
  316. foo: BindingTypes.SETUP_MAYBE_REF,
  317. })
  318. })
  319. })
  320. describe('inlineTemplate mode', () => {
  321. test('should work', () => {
  322. const { content } = compile(
  323. `
  324. <script setup>
  325. import { ref } from 'vue'
  326. const count = ref(0)
  327. </script>
  328. <template>
  329. <div>{{ count }}</div>
  330. <div>static</div>
  331. </template>
  332. `,
  333. { inlineTemplate: true },
  334. )
  335. // check snapshot and make sure helper imports and
  336. // hoists are placed correctly.
  337. assertCode(content)
  338. // in inline mode, no need to call expose() since nothing is exposed
  339. // anyway!
  340. expect(content).not.toMatch(`expose()`)
  341. })
  342. test('with defineExpose()', () => {
  343. const { content } = compile(
  344. `
  345. <script setup>
  346. const count = ref(0)
  347. defineExpose({ count })
  348. </script>
  349. `,
  350. { inlineTemplate: true },
  351. )
  352. assertCode(content)
  353. expect(content).toMatch(`setup(__props, { expose: __expose })`)
  354. expect(content).toMatch(`expose({ count })`)
  355. })
  356. test('referencing scope components and directives', () => {
  357. const { content } = compile(
  358. `
  359. <script setup>
  360. import ChildComp from './Child.vue'
  361. import SomeOtherComp from './Other.vue'
  362. import vMyDir from './my-dir'
  363. </script>
  364. <template>
  365. <div v-my-dir></div>
  366. <ChildComp/>
  367. <some-other-comp/>
  368. </template>
  369. `,
  370. { inlineTemplate: true },
  371. )
  372. expect(content).toMatch('[_unref(vMyDir)]')
  373. expect(content).toMatch('_createVNode(ChildComp)')
  374. // kebab-case component support
  375. expect(content).toMatch('_createVNode(SomeOtherComp)')
  376. assertCode(content)
  377. })
  378. test('avoid unref() when necessary', () => {
  379. // function, const, component import
  380. const { content } = compile(
  381. `<script setup>
  382. import { ref } from 'vue'
  383. import Foo, { bar } from './Foo.vue'
  384. import other from './util'
  385. import * as tree from './tree'
  386. const count = ref(0)
  387. const constant = {}
  388. const maybe = foo()
  389. let lett = 1
  390. function fn() {}
  391. </script>
  392. <template>
  393. <Foo>{{ bar }}</Foo>
  394. <div @click="fn">{{ count }} {{ constant }} {{ maybe }} {{ lett }} {{ other }}</div>
  395. {{ tree.foo() }}
  396. </template>
  397. `,
  398. { inlineTemplate: true },
  399. )
  400. // no need to unref vue component import
  401. expect(content).toMatch(`createVNode(Foo,`)
  402. // #2699 should unref named imports from .vue
  403. expect(content).toMatch(`unref(bar)`)
  404. // should unref other imports
  405. expect(content).toMatch(`unref(other)`)
  406. // no need to unref constant literals
  407. expect(content).not.toMatch(`unref(constant)`)
  408. // should directly use .value for known refs
  409. expect(content).toMatch(`count.value`)
  410. // should unref() on const bindings that may be refs
  411. expect(content).toMatch(`unref(maybe)`)
  412. // should unref() on let bindings
  413. expect(content).toMatch(`unref(lett)`)
  414. // no need to unref namespace import (this also preserves tree-shaking)
  415. expect(content).toMatch(`tree.foo()`)
  416. // no need to unref function declarations
  417. expect(content).toMatch(`{ onClick: fn }`)
  418. // no need to mark constant fns in patch flag
  419. expect(content).not.toMatch(`PROPS`)
  420. assertCode(content)
  421. })
  422. test('v-model codegen', () => {
  423. const { content } = compile(
  424. `<script setup>
  425. import { ref } from 'vue'
  426. const count = ref(0)
  427. const maybe = foo()
  428. let lett = 1
  429. </script>
  430. <template>
  431. <input v-model="count">
  432. <input v-model="maybe">
  433. <input v-model="lett">
  434. </template>
  435. `,
  436. { inlineTemplate: true },
  437. )
  438. // known const ref: set value
  439. expect(content).toMatch(`(count).value = $event`)
  440. // const but maybe ref: assign if ref, otherwise do nothing
  441. expect(content).toMatch(`_isRef(maybe) ? (maybe).value = $event : null`)
  442. // let: handle both cases
  443. expect(content).toMatch(
  444. `_isRef(lett) ? (lett).value = $event : lett = $event`,
  445. )
  446. assertCode(content)
  447. })
  448. test('v-model w/ newlines codegen', () => {
  449. const { content } = compile(
  450. `<script setup>
  451. const count = ref(0)
  452. </script>
  453. <template>
  454. <input v-model="
  455. count
  456. ">
  457. </template>
  458. `,
  459. { inlineTemplate: true },
  460. )
  461. expect(content).toMatch(`_isRef(count) ? (count).value = $event : null`)
  462. assertCode(content)
  463. })
  464. test('v-model should not generate ref assignment code for non-setup bindings', () => {
  465. const { content } = compile(
  466. `<script setup>
  467. import { ref } from 'vue'
  468. const count = ref(0)
  469. </script>
  470. <script>
  471. export default {
  472. data() { return { foo: 123 } }
  473. }
  474. </script>
  475. <template>
  476. <input v-model="foo">
  477. </template>
  478. `,
  479. { inlineTemplate: true },
  480. )
  481. expect(content).not.toMatch(`_isRef(foo)`)
  482. })
  483. test('template assignment expression codegen', () => {
  484. const { content } = compile(
  485. `<script setup>
  486. import { ref } from 'vue'
  487. const count = ref(0)
  488. const maybe = foo()
  489. let lett = 1
  490. let v = ref(1)
  491. </script>
  492. <template>
  493. <div @click="count = 1"/>
  494. <div @click="maybe = count"/>
  495. <div @click="lett = count"/>
  496. <div @click="v += 1"/>
  497. <div @click="v -= 1"/>
  498. <div @click="() => {
  499. let a = '' + lett
  500. v = a
  501. }"/>
  502. <div @click="() => {
  503. // nested scopes
  504. (()=>{
  505. let x = a
  506. (()=>{
  507. let z = x
  508. let z2 = z
  509. })
  510. let lz = z
  511. })
  512. v = a
  513. }"/>
  514. </template>
  515. `,
  516. { inlineTemplate: true },
  517. )
  518. // known const ref: set value
  519. expect(content).toMatch(`count.value = 1`)
  520. // const but maybe ref: only assign after check
  521. expect(content).toMatch(`maybe.value = count.value`)
  522. // let: handle both cases
  523. expect(content).toMatch(
  524. `_isRef(lett) ? lett.value = count.value : lett = count.value`,
  525. )
  526. expect(content).toMatch(`_isRef(v) ? v.value += 1 : v += 1`)
  527. expect(content).toMatch(`_isRef(v) ? v.value -= 1 : v -= 1`)
  528. expect(content).toMatch(`_isRef(v) ? v.value = a : v = a`)
  529. expect(content).toMatch(`_isRef(v) ? v.value = _ctx.a : v = _ctx.a`)
  530. assertCode(content)
  531. })
  532. test('template update expression codegen', () => {
  533. const { content } = compile(
  534. `<script setup>
  535. import { ref } from 'vue'
  536. const count = ref(0)
  537. const maybe = foo()
  538. let lett = 1
  539. </script>
  540. <template>
  541. <div @click="count++"/>
  542. <div @click="--count"/>
  543. <div @click="maybe++"/>
  544. <div @click="--maybe"/>
  545. <div @click="lett++"/>
  546. <div @click="--lett"/>
  547. </template>
  548. `,
  549. { inlineTemplate: true },
  550. )
  551. // known const ref: set value
  552. expect(content).toMatch(`count.value++`)
  553. expect(content).toMatch(`--count.value`)
  554. // const but maybe ref (non-ref case ignored)
  555. expect(content).toMatch(`maybe.value++`)
  556. expect(content).toMatch(`--maybe.value`)
  557. // let: handle both cases
  558. expect(content).toMatch(`_isRef(lett) ? lett.value++ : lett++`)
  559. expect(content).toMatch(`_isRef(lett) ? --lett.value : --lett`)
  560. assertCode(content)
  561. })
  562. test('template destructure assignment codegen', () => {
  563. const { content } = compile(
  564. `<script setup>
  565. import { ref } from 'vue'
  566. const val = {}
  567. const count = ref(0)
  568. const maybe = foo()
  569. let lett = 1
  570. </script>
  571. <template>
  572. <div @click="({ count } = val)"/>
  573. <div @click="[maybe] = val"/>
  574. <div @click="({ lett } = val)"/>
  575. </template>
  576. `,
  577. { inlineTemplate: true },
  578. )
  579. // known const ref: set value
  580. expect(content).toMatch(`({ count: count.value } = val)`)
  581. // const but maybe ref (non-ref case ignored)
  582. expect(content).toMatch(`[maybe.value] = val`)
  583. // let: assumes non-ref
  584. expect(content).toMatch(`{ lett: lett } = val`)
  585. assertCode(content)
  586. })
  587. test('ssr codegen', () => {
  588. const { content } = compile(
  589. `
  590. <script setup>
  591. import { ref } from 'vue'
  592. const count = ref(0)
  593. const style = { color: 'red' }
  594. const height = ref(0)
  595. </script>
  596. <template>
  597. <div>{{ count }}</div>
  598. <div>static</div>
  599. </template>
  600. <style>
  601. div { color: v-bind(count) }
  602. span { color: v-bind(style.color) }
  603. span { color: v-bind(height + "px") }
  604. </style>
  605. `,
  606. {
  607. inlineTemplate: true,
  608. templateOptions: {
  609. ssr: true,
  610. },
  611. },
  612. )
  613. expect(content).toMatch(`\n __ssrInlineRender: true,\n`)
  614. expect(content).toMatch(`return (_ctx, _push`)
  615. expect(content).toMatch(`ssrInterpolate`)
  616. expect(content).not.toMatch(`useCssVars`)
  617. expect(content).toMatch(`":--${mockId}-count": (count.value)`)
  618. expect(content).toMatch(`":--${mockId}-style\\\\.color": (style.color)`)
  619. expect(content).toMatch(
  620. `":--${mockId}-height\\\\ \\\\+\\\\ \\\\\\"px\\\\\\"": (height.value + "px")`,
  621. )
  622. assertCode(content)
  623. })
  624. test('the v-for wrapped in parentheses can be correctly parsed & inline is false', () => {
  625. expect(() =>
  626. compile(
  627. `
  628. <script setup lang="ts">
  629. import { ref } from 'vue'
  630. const stacks = ref([])
  631. </script>
  632. <template>
  633. <div v-for="({ file: efile }) of stacks"></div>
  634. </template>
  635. `,
  636. {
  637. inlineTemplate: false,
  638. },
  639. ),
  640. ).not.toThrowError()
  641. })
  642. test('unref + new expression', () => {
  643. const { content } = compile(
  644. `
  645. <script setup>
  646. import Foo from './foo'
  647. </script>
  648. <template>
  649. <div>{{ new Foo() }}</div>
  650. <div>{{ new Foo.Bar() }}</div>
  651. </template>
  652. `,
  653. { inlineTemplate: true },
  654. )
  655. expect(content).toMatch(`new (_unref(Foo))()`)
  656. expect(content).toMatch(`new (_unref(Foo)).Bar()`)
  657. assertCode(content)
  658. })
  659. // #12682
  660. test('source map', () => {
  661. const source = `
  662. <script setup>
  663. const count = ref(0)
  664. </script>
  665. <template>
  666. <button @click="throw new Error(\`msg\`);"></button>
  667. </template>
  668. `
  669. const { content, map } = compile(source, { inlineTemplate: true })
  670. expect(map).not.toBeUndefined()
  671. const consumer = new SourceMapConsumer(map as RawSourceMap)
  672. expect(
  673. consumer.originalPositionFor(getPositionInCode(content, 'count')),
  674. ).toMatchObject(getPositionInCode(source, `count`))
  675. expect(
  676. consumer.originalPositionFor(getPositionInCode(content, 'Error')),
  677. ).toMatchObject(getPositionInCode(source, `Error`))
  678. })
  679. describe('destructure setup context for built-in properties', () => {
  680. const theCompile = (template: string, setup = '/* ... */') =>
  681. compile(
  682. `<script setup>${setup}</script>\n<template>${template}</template>`,
  683. { inlineTemplate: true },
  684. )
  685. test('should extract attrs when $attrs is used', () => {
  686. let { content } = theCompile('<div v-bind="$attrs"></div>')
  687. expect(content).toMatch('setup(__props, { attrs: $attrs })')
  688. expect(content).not.toMatch('slots: $slots')
  689. expect(content).not.toMatch('emit: $emit')
  690. expect(content).not.toMatch('const $props = __props')
  691. assertCode(content)
  692. })
  693. test('should extract slots when $slots is used', () => {
  694. let { content } = theCompile('<Comp :foo="$slots.foo"></Comp>')
  695. expect(content).toMatch('setup(__props, { slots: $slots })')
  696. assertCode(content)
  697. })
  698. test('should alias __props to $props when $props is used', () => {
  699. let { content } = theCompile('<div>{{ $props }}</div>')
  700. expect(content).toMatch('setup(__props)')
  701. expect(content).toMatch('const $props = __props')
  702. assertCode(content)
  703. })
  704. test('should extract emit when $emit is used', () => {
  705. let { content } = theCompile(`<div @click="$emit('click')"></div>`)
  706. expect(content).toMatch('setup(__props, { emit: $emit })')
  707. expect(content).not.toMatch('const $emit = __emit')
  708. assertCode(content)
  709. })
  710. test('should alias __emit to $emit when defineEmits is used', () => {
  711. let { content } = compile(
  712. `
  713. <script setup>
  714. const emit = defineEmits(['click'])
  715. </script>
  716. <template>
  717. <div @click="$emit('click')"></div>
  718. </template>
  719. `,
  720. { inlineTemplate: true },
  721. )
  722. expect(content).toMatch('setup(__props, { emit: __emit })')
  723. expect(content).toMatch('const $emit = __emit')
  724. expect(content).toMatch('const emit = __emit')
  725. assertCode(content)
  726. })
  727. test('should extract all built-in properties when they are used', () => {
  728. let { content } = theCompile(
  729. '<div>{{ $props }}{{ $slots }}{{ $emit }}{{ $attrs }}</div>',
  730. )
  731. expect(content).toMatch(
  732. 'setup(__props, { emit: $emit, attrs: $attrs, slots: $slots })',
  733. )
  734. expect(content).toMatch('const $props = __props')
  735. assertCode(content)
  736. })
  737. test('should not extract built-in properties when neither is used', () => {
  738. let { content } = theCompile('<div>{{ msg }}</div>')
  739. expect(content).toMatch('setup(__props)')
  740. expect(content).not.toMatch('attrs: $attrs')
  741. expect(content).not.toMatch('slots: $slots')
  742. expect(content).not.toMatch('emit: $emit')
  743. expect(content).not.toMatch('props: $props')
  744. assertCode(content)
  745. })
  746. describe('user-defined properties override', () => {
  747. test('should not extract $attrs when user defines it', () => {
  748. let { content } = theCompile(
  749. '<div v-bind="$attrs"></div>',
  750. 'let $attrs',
  751. )
  752. expect(content).toMatch('setup(__props)')
  753. expect(content).not.toMatch('attrs: $attrs')
  754. assertCode(content)
  755. })
  756. test('should not extract $slots when user defines it', () => {
  757. let { content } = theCompile(
  758. '<Comp :foo="$slots.foo"></Comp>',
  759. 'let $slots',
  760. )
  761. expect(content).toMatch('setup(__props)')
  762. expect(content).not.toMatch('slots: $slots')
  763. assertCode(content)
  764. })
  765. test('should not extract $emit when user defines it', () => {
  766. let { content } = theCompile(
  767. `<div @click="$emit('click')">click</div>`,
  768. 'let $emit',
  769. )
  770. expect(content).toMatch('setup(__props)')
  771. expect(content).not.toMatch('emit: $emit')
  772. assertCode(content)
  773. })
  774. test('should not generate $props alias when user defines it', () => {
  775. let { content } = theCompile(
  776. '<div>{{ $props.msg }}</div>',
  777. 'let $props',
  778. )
  779. expect(content).toMatch('setup(__props)')
  780. expect(content).not.toMatch('const $props = __props')
  781. assertCode(content)
  782. })
  783. test('should only extract non-user-defined properties', () => {
  784. let { content } = theCompile(
  785. '<div>{{ $attrs }}{{ $slots }}{{ $emit }}{{ $props }}</div>',
  786. 'let $attrs',
  787. )
  788. expect(content).toMatch(
  789. 'setup(__props, { emit: $emit, slots: $slots })',
  790. )
  791. expect(content).not.toMatch('attrs: $attrs')
  792. expect(content).toMatch('const $props = __props')
  793. assertCode(content)
  794. })
  795. test('should handle mixed defineEmits and user-defined $emit', () => {
  796. let { content } = theCompile(
  797. `<div @click="$emit('click')">click</div>`,
  798. `
  799. const emit = defineEmits(['click'])
  800. let $emit
  801. `,
  802. )
  803. expect(content).toMatch('setup(__props, { emit: __emit })')
  804. expect(content).toMatch('const emit = __emit')
  805. expect(content).not.toMatch('const $emit = __emit')
  806. assertCode(content)
  807. })
  808. })
  809. })
  810. })
  811. describe('with TypeScript', () => {
  812. test('hoist type declarations', () => {
  813. const { content } = compile(`
  814. <script setup lang="ts">
  815. export interface Foo {}
  816. type Bar = {}
  817. </script>`)
  818. assertCode(content)
  819. })
  820. test('runtime Enum', () => {
  821. const { content, bindings } = compile(
  822. `<script setup lang="ts">
  823. enum Foo { A = 123 }
  824. </script>`,
  825. )
  826. assertCode(content)
  827. expect(bindings).toStrictEqual({
  828. Foo: BindingTypes.LITERAL_CONST,
  829. })
  830. })
  831. test('runtime Enum in normal script', () => {
  832. const { content, bindings } = compile(
  833. `<script lang="ts">
  834. export enum D { D = "D" }
  835. const enum C { C = "C" }
  836. enum B { B = "B" }
  837. </script>
  838. <script setup lang="ts">
  839. enum Foo { A = 123 }
  840. </script>`,
  841. )
  842. assertCode(content)
  843. expect(bindings).toStrictEqual({
  844. D: BindingTypes.LITERAL_CONST,
  845. C: BindingTypes.LITERAL_CONST,
  846. B: BindingTypes.LITERAL_CONST,
  847. Foo: BindingTypes.LITERAL_CONST,
  848. })
  849. })
  850. test('const Enum', () => {
  851. const { content, bindings } = compile(
  852. `<script setup lang="ts">
  853. const enum Foo { A = 123 }
  854. </script>`,
  855. { hoistStatic: true },
  856. )
  857. assertCode(content)
  858. expect(bindings).toStrictEqual({
  859. Foo: BindingTypes.LITERAL_CONST,
  860. })
  861. })
  862. test('import type', () => {
  863. const { content } = compile(
  864. `<script setup lang="ts">
  865. import type { Foo } from './main.ts'
  866. import { type Bar, Baz } from './main.ts'
  867. </script>`,
  868. )
  869. expect(content).toMatch(`return { get Baz() { return Baz } }`)
  870. assertCode(content)
  871. })
  872. test('with generic attribute', () => {
  873. const { content } = compile(`
  874. <script setup lang="ts" generic="T extends Record<string,string>">
  875. type Bar = {}
  876. </script>`)
  877. assertCode(content)
  878. })
  879. })
  880. describe('async/await detection', () => {
  881. function assertAwaitDetection(code: string, shouldAsync = true) {
  882. const { content } = compile(`<script setup>${code}</script>`)
  883. if (shouldAsync) {
  884. expect(content).toMatch(`let __temp, __restore`)
  885. }
  886. expect(content).toMatch(`${shouldAsync ? `async ` : ``}setup(`)
  887. assertCode(content)
  888. return content
  889. }
  890. test('expression statement', () => {
  891. assertAwaitDetection(`await foo`)
  892. })
  893. test('variable', () => {
  894. assertAwaitDetection(`const a = 1 + (await foo)`)
  895. })
  896. test('ref', () => {
  897. assertAwaitDetection(`let a = ref(1 + (await foo))`)
  898. })
  899. // #4448
  900. test('nested await', () => {
  901. assertAwaitDetection(`await (await foo)`)
  902. assertAwaitDetection(`await ((await foo))`)
  903. assertAwaitDetection(`await (await (await foo))`)
  904. })
  905. // should prepend semicolon
  906. test('nested leading await in expression statement', () => {
  907. const code = assertAwaitDetection(`foo()\nawait 1 + await 2`)
  908. expect(code).toMatch(`foo()\n;(`)
  909. })
  910. // #4596 should NOT prepend semicolon
  911. test('single line conditions', () => {
  912. const code = assertAwaitDetection(`if (false) await foo()`)
  913. expect(code).not.toMatch(`if (false) ;(`)
  914. })
  915. test('nested statements', () => {
  916. assertAwaitDetection(`if (ok) { await foo } else { await bar }`)
  917. })
  918. test('multiple `if` nested statements', () => {
  919. assertAwaitDetection(`if (ok) {
  920. let a = 'foo'
  921. await 0 + await 1
  922. await 2
  923. } else if (a) {
  924. await 10
  925. if (b) {
  926. await 0 + await 1
  927. } else {
  928. let a = 'foo'
  929. await 2
  930. }
  931. if (b) {
  932. await 3
  933. await 4
  934. }
  935. } else {
  936. await 5
  937. }`)
  938. })
  939. test('multiple `if while` nested statements', () => {
  940. assertAwaitDetection(`if (ok) {
  941. while (d) {
  942. await 5
  943. }
  944. while (d) {
  945. await 5
  946. await 6
  947. if (c) {
  948. let f = 10
  949. 10 + await 7
  950. } else {
  951. await 8
  952. await 9
  953. }
  954. }
  955. }`)
  956. })
  957. test('multiple `if for` nested statements', () => {
  958. assertAwaitDetection(`if (ok) {
  959. for (let a of [1,2,3]) {
  960. await a
  961. }
  962. for (let a of [1,2,3]) {
  963. await a
  964. await a
  965. }
  966. }`)
  967. })
  968. test('should ignore await inside functions', () => {
  969. // function declaration
  970. assertAwaitDetection(`async function foo() { await bar }`, false)
  971. // function expression
  972. assertAwaitDetection(`const foo = async () => { await bar }`, false)
  973. // object method
  974. assertAwaitDetection(`const obj = { async method() { await bar }}`, false)
  975. // class method
  976. assertAwaitDetection(
  977. `const cls = class Foo { async method() { await bar }}`,
  978. false,
  979. )
  980. })
  981. })
  982. describe('errors', () => {
  983. test('<script> and <script setup> must have same lang', () => {
  984. expect(() =>
  985. compile(`<script>foo()</script><script setup lang="ts">bar()</script>`),
  986. ).toThrow(`<script> and <script setup> must have the same language type`)
  987. // #13193 must check lang before parsing with babel
  988. expect(() =>
  989. compile(
  990. `<script lang="ts">const a = 1</script><script setup lang="tsx">const Comp = () => <p>test</p></script>`,
  991. ),
  992. ).toThrow(`<script> and <script setup> must have the same language type`)
  993. })
  994. const moduleErrorMsg = `cannot contain ES module exports`
  995. test('non-type named exports', () => {
  996. expect(() =>
  997. compile(`<script setup>
  998. export const a = 1
  999. </script>`),
  1000. ).toThrow(moduleErrorMsg)
  1001. expect(() =>
  1002. compile(`<script setup>
  1003. export * from './foo'
  1004. </script>`),
  1005. ).toThrow(moduleErrorMsg)
  1006. expect(() =>
  1007. compile(`<script setup>
  1008. const bar = 1
  1009. export { bar as default }
  1010. </script>`),
  1011. ).toThrow(moduleErrorMsg)
  1012. })
  1013. test('defineProps/Emit() referencing local var', () => {
  1014. expect(() =>
  1015. compile(`<script setup>
  1016. let bar = 1
  1017. defineProps({
  1018. foo: {
  1019. default: () => bar
  1020. }
  1021. })
  1022. </script>`),
  1023. ).toThrow(`cannot reference locally declared variables`)
  1024. expect(() =>
  1025. compile(`<script setup>
  1026. let bar = 'hello'
  1027. defineEmits([bar])
  1028. </script>`),
  1029. ).toThrow(`cannot reference locally declared variables`)
  1030. // #4644
  1031. expect(() =>
  1032. compile(`
  1033. <script>const bar = 1</script>
  1034. <script setup>
  1035. defineProps({
  1036. foo: {
  1037. default: () => bar
  1038. }
  1039. })
  1040. </script>`),
  1041. ).not.toThrow(`cannot reference locally declared variables`)
  1042. })
  1043. test('should allow defineProps/Emit() referencing scope var', () => {
  1044. assertCode(
  1045. compile(`<script setup>
  1046. const bar = 1
  1047. defineProps({
  1048. foo: {
  1049. default: bar => bar + 1
  1050. }
  1051. })
  1052. defineEmits({
  1053. foo: bar => bar > 1
  1054. })
  1055. </script>`).content,
  1056. )
  1057. })
  1058. test('should allow defineProps/Emit() referencing imported binding', () => {
  1059. assertCode(
  1060. compile(`<script setup>
  1061. import { bar } from './bar'
  1062. defineProps({
  1063. foo: {
  1064. default: () => bar
  1065. }
  1066. })
  1067. defineEmits({
  1068. foo: () => bar > 1
  1069. })
  1070. </script>`).content,
  1071. )
  1072. })
  1073. test('defineModel() referencing local var', () => {
  1074. expect(() =>
  1075. compile(`<script setup>
  1076. let bar = 1
  1077. defineModel({
  1078. default: () => bar
  1079. })
  1080. </script>`),
  1081. ).toThrow(`cannot reference locally declared variables`)
  1082. // allow const
  1083. expect(() =>
  1084. compile(`<script setup>
  1085. const bar = 1
  1086. defineModel({
  1087. default: () => bar
  1088. })
  1089. </script>`),
  1090. ).not.toThrow(`cannot reference locally declared variables`)
  1091. // allow in get/set
  1092. expect(() =>
  1093. compile(`<script setup>
  1094. let bar = 1
  1095. defineModel({
  1096. get: () => bar,
  1097. set: () => bar
  1098. })
  1099. </script>`),
  1100. ).not.toThrow(`cannot reference locally declared variables`)
  1101. })
  1102. })
  1103. })
  1104. describe('SFC analyze <script> bindings', () => {
  1105. it('can parse decorators syntax in typescript block', () => {
  1106. const { scriptAst } = compile(`
  1107. <script lang="ts">
  1108. import { Options, Vue } from 'vue-class-component';
  1109. @Options({
  1110. components: {
  1111. HelloWorld,
  1112. },
  1113. props: ['foo', 'bar']
  1114. })
  1115. export default class Home extends Vue {}
  1116. </script>
  1117. `)
  1118. expect(scriptAst).toBeDefined()
  1119. })
  1120. it('recognizes props array declaration', () => {
  1121. const { bindings } = compile(`
  1122. <script>
  1123. export default {
  1124. props: ['foo', 'bar']
  1125. }
  1126. </script>
  1127. `)
  1128. expect(bindings).toStrictEqual({
  1129. foo: BindingTypes.PROPS,
  1130. bar: BindingTypes.PROPS,
  1131. })
  1132. expect(bindings!.__isScriptSetup).toBe(false)
  1133. })
  1134. it('recognizes props object declaration', () => {
  1135. const { bindings } = compile(`
  1136. <script>
  1137. export default {
  1138. props: {
  1139. foo: String,
  1140. bar: {
  1141. type: String,
  1142. },
  1143. baz: null,
  1144. qux: [String, Number]
  1145. }
  1146. }
  1147. </script>
  1148. `)
  1149. expect(bindings).toStrictEqual({
  1150. foo: BindingTypes.PROPS,
  1151. bar: BindingTypes.PROPS,
  1152. baz: BindingTypes.PROPS,
  1153. qux: BindingTypes.PROPS,
  1154. })
  1155. expect(bindings!.__isScriptSetup).toBe(false)
  1156. })
  1157. it('recognizes setup return', () => {
  1158. const { bindings } = compile(`
  1159. <script>
  1160. const bar = 2
  1161. export default {
  1162. setup() {
  1163. return {
  1164. foo: 1,
  1165. bar
  1166. }
  1167. }
  1168. }
  1169. </script>
  1170. `)
  1171. expect(bindings).toStrictEqual({
  1172. foo: BindingTypes.SETUP_MAYBE_REF,
  1173. bar: BindingTypes.SETUP_MAYBE_REF,
  1174. })
  1175. expect(bindings!.__isScriptSetup).toBe(false)
  1176. })
  1177. it('recognizes exported vars', () => {
  1178. const { bindings } = compile(`
  1179. <script>
  1180. export const foo = 2
  1181. </script>
  1182. <script setup>
  1183. console.log(foo)
  1184. </script>
  1185. `)
  1186. expect(bindings).toStrictEqual({
  1187. foo: BindingTypes.LITERAL_CONST,
  1188. })
  1189. })
  1190. it('recognizes async setup return', () => {
  1191. const { bindings } = compile(`
  1192. <script>
  1193. const bar = 2
  1194. export default {
  1195. async setup() {
  1196. return {
  1197. foo: 1,
  1198. bar
  1199. }
  1200. }
  1201. }
  1202. </script>
  1203. `)
  1204. expect(bindings).toStrictEqual({
  1205. foo: BindingTypes.SETUP_MAYBE_REF,
  1206. bar: BindingTypes.SETUP_MAYBE_REF,
  1207. })
  1208. expect(bindings!.__isScriptSetup).toBe(false)
  1209. })
  1210. it('recognizes data return', () => {
  1211. const { bindings } = compile(`
  1212. <script>
  1213. const bar = 2
  1214. export default {
  1215. data() {
  1216. return {
  1217. foo: null,
  1218. bar
  1219. }
  1220. }
  1221. }
  1222. </script>
  1223. `)
  1224. expect(bindings).toStrictEqual({
  1225. foo: BindingTypes.DATA,
  1226. bar: BindingTypes.DATA,
  1227. })
  1228. })
  1229. it('recognizes methods', () => {
  1230. const { bindings } = compile(`
  1231. <script>
  1232. export default {
  1233. methods: {
  1234. foo() {}
  1235. }
  1236. }
  1237. </script>
  1238. `)
  1239. expect(bindings).toStrictEqual({ foo: BindingTypes.OPTIONS })
  1240. })
  1241. it('recognizes computeds', () => {
  1242. const { bindings } = compile(`
  1243. <script>
  1244. export default {
  1245. computed: {
  1246. foo() {},
  1247. bar: {
  1248. get() {},
  1249. set() {},
  1250. }
  1251. }
  1252. }
  1253. </script>
  1254. `)
  1255. expect(bindings).toStrictEqual({
  1256. foo: BindingTypes.OPTIONS,
  1257. bar: BindingTypes.OPTIONS,
  1258. })
  1259. })
  1260. it('recognizes injections array declaration', () => {
  1261. const { bindings } = compile(`
  1262. <script>
  1263. export default {
  1264. inject: ['foo', 'bar']
  1265. }
  1266. </script>
  1267. `)
  1268. expect(bindings).toStrictEqual({
  1269. foo: BindingTypes.OPTIONS,
  1270. bar: BindingTypes.OPTIONS,
  1271. })
  1272. })
  1273. it('recognizes injections object declaration', () => {
  1274. const { bindings } = compile(`
  1275. <script>
  1276. export default {
  1277. inject: {
  1278. foo: {},
  1279. bar: {},
  1280. }
  1281. }
  1282. </script>
  1283. `)
  1284. expect(bindings).toStrictEqual({
  1285. foo: BindingTypes.OPTIONS,
  1286. bar: BindingTypes.OPTIONS,
  1287. })
  1288. })
  1289. it('works for mixed bindings', () => {
  1290. const { bindings } = compile(`
  1291. <script>
  1292. export default {
  1293. inject: ['foo'],
  1294. props: {
  1295. bar: String,
  1296. },
  1297. setup() {
  1298. return {
  1299. baz: null,
  1300. }
  1301. },
  1302. data() {
  1303. return {
  1304. qux: null
  1305. }
  1306. },
  1307. methods: {
  1308. quux() {}
  1309. },
  1310. computed: {
  1311. quuz() {}
  1312. }
  1313. }
  1314. </script>
  1315. `)
  1316. expect(bindings).toStrictEqual({
  1317. foo: BindingTypes.OPTIONS,
  1318. bar: BindingTypes.PROPS,
  1319. baz: BindingTypes.SETUP_MAYBE_REF,
  1320. qux: BindingTypes.DATA,
  1321. quux: BindingTypes.OPTIONS,
  1322. quuz: BindingTypes.OPTIONS,
  1323. })
  1324. })
  1325. it('works for script setup', () => {
  1326. const { bindings } = compile(`
  1327. <script setup>
  1328. import { ref as r } from 'vue'
  1329. defineProps({
  1330. foo: String
  1331. })
  1332. const a = r(1)
  1333. let b = 2
  1334. const c = 3
  1335. const { d } = someFoo()
  1336. let { e } = someBar()
  1337. </script>
  1338. `)
  1339. expect(bindings).toStrictEqual({
  1340. r: BindingTypes.SETUP_CONST,
  1341. a: BindingTypes.SETUP_REF,
  1342. b: BindingTypes.SETUP_LET,
  1343. c: BindingTypes.LITERAL_CONST,
  1344. d: BindingTypes.SETUP_MAYBE_REF,
  1345. e: BindingTypes.SETUP_LET,
  1346. foo: BindingTypes.PROPS,
  1347. })
  1348. })
  1349. describe('auto name inference', () => {
  1350. test('basic', () => {
  1351. const { content } = compile(
  1352. `<script setup>const a = 1</script>
  1353. <template>{{ a }}</template>`,
  1354. undefined,
  1355. {
  1356. filename: 'FooBar.vue',
  1357. },
  1358. )
  1359. expect(content).toMatch(`export default {
  1360. __name: 'FooBar'`)
  1361. assertCode(content)
  1362. })
  1363. test('do not overwrite manual name (object)', () => {
  1364. const { content } = compile(
  1365. `<script>
  1366. export default {
  1367. name: 'Baz'
  1368. }
  1369. </script>
  1370. <script setup>const a = 1</script>
  1371. <template>{{ a }}</template>`,
  1372. undefined,
  1373. {
  1374. filename: 'FooBar.vue',
  1375. },
  1376. )
  1377. expect(content).not.toMatch(`name: 'FooBar'`)
  1378. expect(content).toMatch(`name: 'Baz'`)
  1379. assertCode(content)
  1380. })
  1381. test('do not overwrite manual name (call)', () => {
  1382. const { content } = compile(
  1383. `<script>
  1384. import { defineComponent } from 'vue'
  1385. export default defineComponent({
  1386. name: 'Baz'
  1387. })
  1388. </script>
  1389. <script setup>const a = 1</script>
  1390. <template>{{ a }}</template>`,
  1391. undefined,
  1392. {
  1393. filename: 'FooBar.vue',
  1394. },
  1395. )
  1396. expect(content).not.toMatch(`name: 'FooBar'`)
  1397. expect(content).toMatch(`name: 'Baz'`)
  1398. assertCode(content)
  1399. })
  1400. })
  1401. })
  1402. describe('SFC genDefaultAs', () => {
  1403. test('normal <script> only', () => {
  1404. const { content } = compile(
  1405. `<script>
  1406. export default {}
  1407. </script>`,
  1408. {
  1409. genDefaultAs: '_sfc_',
  1410. },
  1411. )
  1412. expect(content).not.toMatch('export default')
  1413. expect(content).toMatch(`const _sfc_ = {}`)
  1414. assertCode(content)
  1415. })
  1416. test('normal <script> w/ cssVars', () => {
  1417. const { content } = compile(
  1418. `<script>
  1419. export default {}
  1420. </script>
  1421. <style>
  1422. .foo { color: v-bind(x) }
  1423. </style>`,
  1424. {
  1425. genDefaultAs: '_sfc_',
  1426. },
  1427. )
  1428. expect(content).not.toMatch('export default')
  1429. expect(content).not.toMatch('__default__')
  1430. expect(content).toMatch(`const _sfc_ = {}`)
  1431. assertCode(content)
  1432. })
  1433. test('<script> + <script setup>', () => {
  1434. const { content } = compile(
  1435. `<script>
  1436. export default {}
  1437. </script>
  1438. <script setup>
  1439. const a = 1
  1440. </script>`,
  1441. {
  1442. genDefaultAs: '_sfc_',
  1443. },
  1444. )
  1445. expect(content).not.toMatch('export default')
  1446. expect(content).toMatch(
  1447. `const _sfc_ = /*@__PURE__*/Object.assign(__default__`,
  1448. )
  1449. assertCode(content)
  1450. })
  1451. test('<script> + <script setup>', () => {
  1452. const { content } = compile(
  1453. `<script>
  1454. export default {}
  1455. </script>
  1456. <script setup>
  1457. const a = 1
  1458. </script>`,
  1459. {
  1460. genDefaultAs: '_sfc_',
  1461. },
  1462. )
  1463. expect(content).not.toMatch('export default')
  1464. expect(content).toMatch(
  1465. `const _sfc_ = /*@__PURE__*/Object.assign(__default__`,
  1466. )
  1467. assertCode(content)
  1468. })
  1469. test('<script setup> only', () => {
  1470. const { content } = compile(
  1471. `<script setup>
  1472. const a = 1
  1473. </script>`,
  1474. {
  1475. genDefaultAs: '_sfc_',
  1476. },
  1477. )
  1478. expect(content).not.toMatch('export default')
  1479. expect(content).toMatch(`const _sfc_ = {\n setup`)
  1480. assertCode(content)
  1481. })
  1482. test('<script setup> only w/ ts', () => {
  1483. const { content } = compile(
  1484. `<script setup lang="ts">
  1485. const a = 1
  1486. </script>`,
  1487. {
  1488. genDefaultAs: '_sfc_',
  1489. },
  1490. )
  1491. expect(content).not.toMatch('export default')
  1492. expect(content).toMatch(`const _sfc_ = /*@__PURE__*/_defineComponent(`)
  1493. assertCode(content)
  1494. })
  1495. test('<script> + <script setup> w/ ts', () => {
  1496. const { content } = compile(
  1497. `<script lang="ts">
  1498. export default {}
  1499. </script>
  1500. <script setup lang="ts">
  1501. const a = 1
  1502. </script>`,
  1503. {
  1504. genDefaultAs: '_sfc_',
  1505. },
  1506. )
  1507. expect(content).not.toMatch('export default')
  1508. expect(content).toMatch(
  1509. `const _sfc_ = /*@__PURE__*/_defineComponent({\n ...__default__`,
  1510. )
  1511. assertCode(content)
  1512. })
  1513. test('binding type for edge cases', () => {
  1514. const { bindings } = compile(
  1515. `<script setup lang="ts">
  1516. import { toRef } from 'vue'
  1517. const props = defineProps<{foo: string}>()
  1518. const foo = toRef(() => props.foo)
  1519. </script>`,
  1520. )
  1521. expect(bindings).toStrictEqual({
  1522. toRef: BindingTypes.SETUP_CONST,
  1523. props: BindingTypes.SETUP_REACTIVE_CONST,
  1524. foo: BindingTypes.SETUP_REF,
  1525. })
  1526. })
  1527. describe('parser plugins', () => {
  1528. test('import attributes', () => {
  1529. const { content } = compile(`
  1530. <script setup>
  1531. import { foo } from './foo.js' with { type: 'foobar' }
  1532. </script>
  1533. `)
  1534. assertCode(content)
  1535. expect(() =>
  1536. compile(`
  1537. <script setup>
  1538. import { foo } from './foo.js' assert { type: 'foobar' }
  1539. </script>`),
  1540. ).toThrow()
  1541. })
  1542. test('import attributes (user override for deprecated syntax)', () => {
  1543. const { content } = compile(
  1544. `
  1545. <script setup>
  1546. import { foo } from './foo.js' assert { type: 'foobar' }
  1547. </script>
  1548. `,
  1549. {
  1550. babelParserPlugins: [
  1551. ['importAttributes', { deprecatedAssertSyntax: true }],
  1552. ],
  1553. },
  1554. )
  1555. assertCode(content)
  1556. })
  1557. })
  1558. })
  1559. describe('compileScript', () => {
  1560. test('should care about runtimeModuleName', () => {
  1561. const { content } = compile(
  1562. `
  1563. <script setup>
  1564. await Promise.resolve(1)
  1565. </script>
  1566. `,
  1567. {
  1568. templateOptions: {
  1569. compilerOptions: {
  1570. runtimeModuleName: 'npm:vue',
  1571. },
  1572. },
  1573. },
  1574. )
  1575. expect(content).toMatch(
  1576. `import { withAsyncContext as _withAsyncContext } from "npm:vue"\n`,
  1577. )
  1578. assertCode(content)
  1579. })
  1580. test('should not compile unrecognized language', () => {
  1581. const { content, lang, scriptAst } = compile(
  1582. `<script lang="coffee">
  1583. export default
  1584. data: ->
  1585. myVal: 0
  1586. </script>`,
  1587. )
  1588. expect(content).toMatch(`export default
  1589. data: ->
  1590. myVal: 0`)
  1591. expect(lang).toBe('coffee')
  1592. expect(scriptAst).not.toBeDefined()
  1593. })
  1594. })
  1595. describe('vapor mode + ssr', () => {
  1596. test('rewrite defineVaporAsyncComponent import', () => {
  1597. const { content } = compile(
  1598. `
  1599. <script setup vapor>
  1600. import { defineVaporAsyncComponent } from 'vue'
  1601. </script>
  1602. `,
  1603. {
  1604. templateOptions: {
  1605. ssr: true,
  1606. },
  1607. },
  1608. )
  1609. expect(content).toContain(
  1610. `import { defineAsyncComponent as defineVaporAsyncComponent } from 'vue'`,
  1611. )
  1612. })
  1613. test('rewrite defineVaporAsyncComponent import with local name', () => {
  1614. const { content } = compile(
  1615. `
  1616. <script setup vapor>
  1617. import { defineVaporAsyncComponent as def } from 'vue'
  1618. </script>
  1619. `,
  1620. {
  1621. templateOptions: {
  1622. ssr: true,
  1623. },
  1624. },
  1625. )
  1626. expect(content).toContain(
  1627. `import { defineAsyncComponent as def } from 'vue'`,
  1628. )
  1629. })
  1630. })