compileStyle.spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. import {
  2. type SFCStyleCompileOptions,
  3. compileStyle,
  4. compileStyleAsync,
  5. } from '../src/compileStyle'
  6. import path from 'node:path'
  7. export function compileScoped(
  8. source: string,
  9. options?: Partial<SFCStyleCompileOptions>,
  10. ): string {
  11. const res = compileStyle({
  12. source,
  13. filename: 'test.css',
  14. id: 'data-v-test',
  15. scoped: true,
  16. ...options,
  17. })
  18. if (res.errors.length) {
  19. res.errors.forEach(err => {
  20. console.error(err)
  21. })
  22. expect(res.errors.length).toBe(0)
  23. }
  24. return res.code
  25. }
  26. describe('SFC scoped CSS', () => {
  27. test('simple selectors', () => {
  28. expect(compileScoped(`h1 { color: red; }`)).toMatch(
  29. `h1[data-v-test] { color: red;`,
  30. )
  31. expect(compileScoped(`.foo { color: red; }`)).toMatch(
  32. `.foo[data-v-test] { color: red;`,
  33. )
  34. })
  35. test('descendent selector', () => {
  36. expect(compileScoped(`h1 .foo { color: red; }`)).toMatch(
  37. `h1 .foo[data-v-test] { color: red;`,
  38. )
  39. })
  40. test('nesting selector', () => {
  41. expect(compileScoped(`h1 { color: red; .foo { color: red; } }`)).toMatch(
  42. `h1 {\n&[data-v-test] { color: red;\n}\n.foo[data-v-test] { color: red;`,
  43. )
  44. })
  45. test('nesting selector with atrule and comment', () => {
  46. expect(
  47. compileScoped(
  48. `h1 {
  49. color: red;
  50. /*background-color: pink;*/
  51. @media only screen and (max-width: 800px) {
  52. background-color: green;
  53. .bar { color: white }
  54. }
  55. .foo { color: red; }
  56. }`,
  57. ),
  58. ).toMatch(
  59. `h1 {
  60. &[data-v-test] {
  61. color: red
  62. /*background-color: pink;*/
  63. }
  64. @media only screen and (max-width: 800px) {
  65. &[data-v-test] {
  66. background-color: green
  67. }
  68. .bar[data-v-test] { color: white
  69. }
  70. }
  71. .foo[data-v-test] { color: red;
  72. }
  73. }`,
  74. )
  75. })
  76. test('multiple selectors', () => {
  77. expect(compileScoped(`h1 .foo, .bar, .baz { color: red; }`)).toMatch(
  78. `h1 .foo[data-v-test], .bar[data-v-test], .baz[data-v-test] { color: red;`,
  79. )
  80. })
  81. test('pseudo class', () => {
  82. expect(compileScoped(`.foo:after { color: red; }`)).toMatch(
  83. `.foo[data-v-test]:after { color: red;`,
  84. )
  85. })
  86. test('pseudo element', () => {
  87. expect(compileScoped(`::selection { display: none; }`)).toMatch(
  88. '[data-v-test]::selection {',
  89. )
  90. })
  91. test('spaces before pseudo element', () => {
  92. const code = compileScoped(`.abc, ::selection { color: red; }`)
  93. expect(code).toMatch('.abc[data-v-test],')
  94. expect(code).toMatch('[data-v-test]::selection {')
  95. })
  96. test('::v-deep', () => {
  97. expect(compileScoped(`:deep(.foo) { color: red; }`)).toMatchInlineSnapshot(`
  98. "[data-v-test] .foo { color: red;
  99. }"
  100. `)
  101. expect(compileScoped(`::v-deep(.foo) { color: red; }`))
  102. .toMatchInlineSnapshot(`
  103. "[data-v-test] .foo { color: red;
  104. }"
  105. `)
  106. expect(compileScoped(`::v-deep(.foo .bar) { color: red; }`))
  107. .toMatchInlineSnapshot(`
  108. "[data-v-test] .foo .bar { color: red;
  109. }"
  110. `)
  111. expect(compileScoped(`.baz .qux ::v-deep(.foo .bar) { color: red; }`))
  112. .toMatchInlineSnapshot(`
  113. ".baz .qux[data-v-test] .foo .bar { color: red;
  114. }"
  115. `)
  116. expect(compileScoped(`:is(.foo :deep(.bar)) { color: red; }`))
  117. .toMatchInlineSnapshot(`
  118. ":is(.foo[data-v-test] .bar) { color: red;
  119. }"
  120. `)
  121. expect(compileScoped(`:where(.foo :deep(.bar)) { color: red; }`))
  122. .toMatchInlineSnapshot(`
  123. ":where(.foo[data-v-test] .bar) { color: red;
  124. }"
  125. `)
  126. expect(compileScoped(`:deep(.foo) { color: red; .bar { color: red; } }`))
  127. .toMatchInlineSnapshot(`
  128. "[data-v-test] .foo { color: red;
  129. .bar { color: red;
  130. }
  131. }"
  132. `)
  133. })
  134. test('::v-slotted', () => {
  135. expect(compileScoped(`:slotted(.foo) { color: red; }`))
  136. .toMatchInlineSnapshot(`
  137. ".foo[data-v-test-s] { color: red;
  138. }"
  139. `)
  140. expect(compileScoped(`::v-slotted(.foo) { color: red; }`))
  141. .toMatchInlineSnapshot(`
  142. ".foo[data-v-test-s] { color: red;
  143. }"
  144. `)
  145. expect(compileScoped(`::v-slotted(.foo .bar) { color: red; }`))
  146. .toMatchInlineSnapshot(`
  147. ".foo .bar[data-v-test-s] { color: red;
  148. }"
  149. `)
  150. expect(compileScoped(`.baz .qux ::v-slotted(.foo .bar) { color: red; }`))
  151. .toMatchInlineSnapshot(`
  152. ".baz .qux .foo .bar[data-v-test-s] { color: red;
  153. }"
  154. `)
  155. })
  156. test('::v-global', () => {
  157. expect(compileScoped(`:global(.foo) { color: red; }`))
  158. .toMatchInlineSnapshot(`
  159. ".foo { color: red;
  160. }"
  161. `)
  162. expect(compileScoped(`::v-global(.foo) { color: red; }`))
  163. .toMatchInlineSnapshot(`
  164. ".foo { color: red;
  165. }"
  166. `)
  167. expect(compileScoped(`::v-global(.foo .bar) { color: red; }`))
  168. .toMatchInlineSnapshot(`
  169. ".foo .bar { color: red;
  170. }"
  171. `)
  172. // global ignores anything before it
  173. expect(compileScoped(`.baz .qux ::v-global(.foo .bar) { color: red; }`))
  174. .toMatchInlineSnapshot(`
  175. ".foo .bar { color: red;
  176. }"
  177. `)
  178. })
  179. test(':is() and :where() with multiple selectors', () => {
  180. expect(compileScoped(`:is(.foo) { color: red; }`)).toMatchInlineSnapshot(`
  181. ":is(.foo[data-v-test]) { color: red;
  182. }"
  183. `)
  184. expect(compileScoped(`:where(.foo, .bar) { color: red; }`))
  185. .toMatchInlineSnapshot(`
  186. ":where(.foo[data-v-test], .bar[data-v-test]) { color: red;
  187. }"
  188. `)
  189. expect(compileScoped(`:is(.foo, .bar) div { color: red; }`))
  190. .toMatchInlineSnapshot(`
  191. ":is(.foo, .bar) div[data-v-test] { color: red;
  192. }"
  193. `)
  194. })
  195. // #10511
  196. test(':is() and :where() in compound selectors', () => {
  197. expect(
  198. compileScoped(`.div { color: red; } .div:where(:hover) { color: blue; }`),
  199. ).toMatchInlineSnapshot(`
  200. ".div[data-v-test] { color: red;
  201. }
  202. .div[data-v-test]:where(:hover) { color: blue;
  203. }"`)
  204. expect(
  205. compileScoped(`.div { color: red; } .div:is(:hover) { color: blue; }`),
  206. ).toMatchInlineSnapshot(`
  207. ".div[data-v-test] { color: red;
  208. }
  209. .div[data-v-test]:is(:hover) { color: blue;
  210. }"`)
  211. expect(
  212. compileScoped(
  213. `.div { color: red; } .div:where(.foo:hover) { color: blue; }`,
  214. ),
  215. ).toMatchInlineSnapshot(`
  216. ".div[data-v-test] { color: red;
  217. }
  218. .div[data-v-test]:where(.foo:hover) { color: blue;
  219. }"`)
  220. expect(
  221. compileScoped(
  222. `.div { color: red; } .div:is(.foo:hover) { color: blue; }`,
  223. ),
  224. ).toMatchInlineSnapshot(`
  225. ".div[data-v-test] { color: red;
  226. }
  227. .div[data-v-test]:is(.foo:hover) { color: blue;
  228. }"`)
  229. })
  230. test('media query', () => {
  231. expect(compileScoped(`@media print { .foo { color: red }}`))
  232. .toMatchInlineSnapshot(`
  233. "@media print {
  234. .foo[data-v-test] { color: red
  235. }}"
  236. `)
  237. })
  238. test('supports query', () => {
  239. expect(compileScoped(`@supports(display: grid) { .foo { display: grid }}`))
  240. .toMatchInlineSnapshot(`
  241. "@supports(display: grid) {
  242. .foo[data-v-test] { display: grid
  243. }}"
  244. `)
  245. })
  246. test('scoped keyframes', () => {
  247. const style = compileScoped(
  248. `
  249. .anim {
  250. animation: color 5s infinite, other 5s;
  251. }
  252. .anim-2 {
  253. animation-name: color;
  254. animation-duration: 5s;
  255. }
  256. .anim-3 {
  257. animation: 5s color infinite, 5s other;
  258. }
  259. .anim-multiple {
  260. animation: color 5s infinite, opacity 2s;
  261. }
  262. .anim-multiple-2 {
  263. animation-name: color, opacity;
  264. animation-duration: 5s, 2s;
  265. }
  266. @keyframes color {
  267. from { color: red; }
  268. to { color: green; }
  269. }
  270. @-webkit-keyframes color {
  271. from { color: red; }
  272. to { color: green; }
  273. }
  274. @keyframes opacity {
  275. from { opacity: 0; }
  276. to { opacity: 1; }
  277. }
  278. @-webkit-keyframes opacity {
  279. from { opacity: 0; }
  280. to { opacity: 1; }
  281. }
  282. `,
  283. { id: 'data-v-test' },
  284. )
  285. expect(style).toContain(
  286. `.anim[data-v-test] {\n animation: color-test 5s infinite, other 5s;`,
  287. )
  288. expect(style).toContain(
  289. `.anim-2[data-v-test] {\n animation-name: color-test`,
  290. )
  291. expect(style).toContain(
  292. `.anim-3[data-v-test] {\n animation: 5s color-test infinite, 5s other;`,
  293. )
  294. expect(style).toContain(`@keyframes color-test {`)
  295. expect(style).toContain(`@-webkit-keyframes color-test {`)
  296. expect(style).toContain(
  297. `.anim-multiple[data-v-test] {\n animation: color-test 5s infinite,opacity-test 2s;`,
  298. )
  299. expect(style).toContain(
  300. `.anim-multiple-2[data-v-test] {\n animation-name: color-test,opacity-test;`,
  301. )
  302. expect(style).toContain(`@keyframes opacity-test {\nfrom { opacity: 0;`)
  303. expect(style).toContain(
  304. `@-webkit-keyframes opacity-test {\nfrom { opacity: 0;`,
  305. )
  306. })
  307. // vue-loader/#1370
  308. test('spaces after selector', () => {
  309. expect(compileScoped(`.foo , .bar { color: red; }`)).toMatchInlineSnapshot(`
  310. ".foo[data-v-test], .bar[data-v-test] { color: red;
  311. }"
  312. `)
  313. })
  314. describe('deprecated syntax', () => {
  315. test('::v-deep as combinator', () => {
  316. expect(compileScoped(`::v-deep .foo { color: red; }`))
  317. .toMatchInlineSnapshot(`
  318. "[data-v-test] .foo { color: red;
  319. }"
  320. `)
  321. expect(compileScoped(`.bar ::v-deep .foo { color: red; }`))
  322. .toMatchInlineSnapshot(`
  323. ".bar[data-v-test] .foo { color: red;
  324. }"
  325. `)
  326. expect(
  327. `::v-deep usage as a combinator has been deprecated.`,
  328. ).toHaveBeenWarned()
  329. })
  330. test('>>> (deprecated syntax)', () => {
  331. const code = compileScoped(`>>> .foo { color: red; }`)
  332. expect(code).toMatchInlineSnapshot(`
  333. "[data-v-test] .foo { color: red;
  334. }"
  335. `)
  336. expect(
  337. `the >>> and /deep/ combinators have been deprecated.`,
  338. ).toHaveBeenWarned()
  339. })
  340. test('/deep/ (deprecated syntax)', () => {
  341. const code = compileScoped(`/deep/ .foo { color: red; }`)
  342. expect(code).toMatchInlineSnapshot(`
  343. "[data-v-test] .foo { color: red;
  344. }"
  345. `)
  346. expect(
  347. `the >>> and /deep/ combinators have been deprecated.`,
  348. ).toHaveBeenWarned()
  349. })
  350. })
  351. })
  352. describe('SFC CSS modules', () => {
  353. test('should include resulting classes object in result', async () => {
  354. const result = await compileStyleAsync({
  355. source: `.red { color: red }\n.green { color: green }\n:global(.blue) { color: blue }`,
  356. filename: `test.css`,
  357. id: 'test',
  358. modules: true,
  359. })
  360. expect(result.modules).toBeDefined()
  361. expect(result.modules!.red).toMatch('_red_')
  362. expect(result.modules!.green).toMatch('_green_')
  363. expect(result.modules!.blue).toBeUndefined()
  364. })
  365. test('postcss-modules options', async () => {
  366. const result = await compileStyleAsync({
  367. source: `:local(.foo-bar) { color: red }\n.baz-qux { color: green }`,
  368. filename: `test.css`,
  369. id: 'test',
  370. modules: true,
  371. modulesOptions: {
  372. scopeBehaviour: 'global',
  373. generateScopedName: `[name]__[local]__[hash:base64:5]`,
  374. localsConvention: 'camelCaseOnly',
  375. },
  376. })
  377. expect(result.modules).toBeDefined()
  378. expect(result.modules!.fooBar).toMatch('__foo-bar__')
  379. expect(result.modules!.bazQux).toBeUndefined()
  380. })
  381. })
  382. describe('SFC style preprocessors', () => {
  383. test('scss @import', () => {
  384. const res = compileStyle({
  385. source: `
  386. @import "./import.scss";
  387. `,
  388. filename: path.resolve(__dirname, './fixture/test.scss'),
  389. id: '',
  390. preprocessLang: 'scss',
  391. })
  392. expect([...res.dependencies]).toStrictEqual([
  393. path.join(__dirname, './fixture/import.scss'),
  394. ])
  395. })
  396. test('scss respect user-defined string options.additionalData', () => {
  397. const res = compileStyle({
  398. preprocessOptions: {
  399. additionalData: `
  400. @mixin square($size) {
  401. width: $size;
  402. height: $size;
  403. }`,
  404. },
  405. source: `
  406. .square {
  407. @include square(100px);
  408. }
  409. `,
  410. filename: path.resolve(__dirname, './fixture/test.scss'),
  411. id: '',
  412. preprocessLang: 'scss',
  413. })
  414. expect(res.errors.length).toBe(0)
  415. })
  416. test('scss respect user-defined function options.additionalData', () => {
  417. const source = `
  418. .square {
  419. @include square(100px);
  420. }
  421. `
  422. const filename = path.resolve(__dirname, './fixture/test.scss')
  423. const res = compileStyle({
  424. preprocessOptions: {
  425. additionalData: (s: string, f: string) => {
  426. expect(s).toBe(source)
  427. expect(f).toBe(filename)
  428. return `
  429. @mixin square($size) {
  430. width: $size;
  431. height: $size;
  432. }`
  433. },
  434. },
  435. source,
  436. filename,
  437. id: '',
  438. preprocessLang: 'scss',
  439. })
  440. expect(res.errors.length).toBe(0)
  441. })
  442. test('should mount scope on correct selector when have universal selector', () => {
  443. expect(compileScoped(`* { color: red; }`)).toMatchInlineSnapshot(`
  444. "[data-v-test] { color: red;
  445. }"
  446. `)
  447. expect(compileScoped('* .foo { color: red; }')).toMatchInlineSnapshot(`
  448. ".foo[data-v-test] { color: red;
  449. }"
  450. `)
  451. expect(compileScoped(`*.foo { color: red; }`)).toMatchInlineSnapshot(`
  452. ".foo[data-v-test] { color: red;
  453. }"
  454. `)
  455. expect(compileScoped(`.foo * { color: red; }`)).toMatchInlineSnapshot(`
  456. ".foo[data-v-test] * { color: red;
  457. }"
  458. `)
  459. })
  460. })