compileStyle.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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('multiple selectors', () => {
  41. expect(compileScoped(`h1 .foo, .bar, .baz { color: red; }`)).toMatch(
  42. `h1 .foo[data-v-test], .bar[data-v-test], .baz[data-v-test] { color: red;`,
  43. )
  44. })
  45. test('pseudo class', () => {
  46. expect(compileScoped(`.foo:after { color: red; }`)).toMatch(
  47. `.foo[data-v-test]:after { color: red;`,
  48. )
  49. })
  50. test('pseudo element', () => {
  51. expect(compileScoped(`::selection { display: none; }`)).toMatch(
  52. '[data-v-test]::selection {',
  53. )
  54. })
  55. test('spaces before pseudo element', () => {
  56. const code = compileScoped(`.abc, ::selection { color: red; }`)
  57. expect(code).toMatch('.abc[data-v-test],')
  58. expect(code).toMatch('[data-v-test]::selection {')
  59. })
  60. test('::v-deep', () => {
  61. expect(compileScoped(`:deep(.foo) { color: red; }`)).toMatchInlineSnapshot(`
  62. "[data-v-test] .foo { color: red;
  63. }"
  64. `)
  65. expect(compileScoped(`::v-deep(.foo) { color: red; }`))
  66. .toMatchInlineSnapshot(`
  67. "[data-v-test] .foo { color: red;
  68. }"
  69. `)
  70. expect(compileScoped(`::v-deep(.foo .bar) { color: red; }`))
  71. .toMatchInlineSnapshot(`
  72. "[data-v-test] .foo .bar { color: red;
  73. }"
  74. `)
  75. expect(compileScoped(`.baz .qux ::v-deep(.foo .bar) { color: red; }`))
  76. .toMatchInlineSnapshot(`
  77. ".baz .qux[data-v-test] .foo .bar { color: red;
  78. }"
  79. `)
  80. expect(compileScoped(`:is(.foo :deep(.bar)) { color: red; }`))
  81. .toMatchInlineSnapshot(`
  82. ":is(.foo[data-v-test] .bar) { color: red;
  83. }"
  84. `)
  85. expect(compileScoped(`:where(.foo :deep(.bar)) { color: red; }`))
  86. .toMatchInlineSnapshot(`
  87. ":where(.foo[data-v-test] .bar) { color: red;
  88. }"
  89. `)
  90. })
  91. test('::v-slotted', () => {
  92. expect(compileScoped(`:slotted(.foo) { color: red; }`))
  93. .toMatchInlineSnapshot(`
  94. ".foo[data-v-test-s] { color: red;
  95. }"
  96. `)
  97. expect(compileScoped(`::v-slotted(.foo) { color: red; }`))
  98. .toMatchInlineSnapshot(`
  99. ".foo[data-v-test-s] { color: red;
  100. }"
  101. `)
  102. expect(compileScoped(`::v-slotted(.foo .bar) { color: red; }`))
  103. .toMatchInlineSnapshot(`
  104. ".foo .bar[data-v-test-s] { color: red;
  105. }"
  106. `)
  107. expect(compileScoped(`.baz .qux ::v-slotted(.foo .bar) { color: red; }`))
  108. .toMatchInlineSnapshot(`
  109. ".baz .qux .foo .bar[data-v-test-s] { color: red;
  110. }"
  111. `)
  112. })
  113. test('::v-global', () => {
  114. expect(compileScoped(`:global(.foo) { color: red; }`))
  115. .toMatchInlineSnapshot(`
  116. ".foo { color: red;
  117. }"
  118. `)
  119. expect(compileScoped(`::v-global(.foo) { color: red; }`))
  120. .toMatchInlineSnapshot(`
  121. ".foo { color: red;
  122. }"
  123. `)
  124. expect(compileScoped(`::v-global(.foo .bar) { color: red; }`))
  125. .toMatchInlineSnapshot(`
  126. ".foo .bar { color: red;
  127. }"
  128. `)
  129. // global ignores anything before it
  130. expect(compileScoped(`.baz .qux ::v-global(.foo .bar) { color: red; }`))
  131. .toMatchInlineSnapshot(`
  132. ".foo .bar { color: red;
  133. }"
  134. `)
  135. })
  136. test(':is() and :where() with multiple selectors', () => {
  137. expect(compileScoped(`:is(.foo) { color: red; }`)).toMatchInlineSnapshot(`
  138. ":is(.foo[data-v-test]) { color: red;
  139. }"
  140. `)
  141. expect(compileScoped(`:where(.foo, .bar) { color: red; }`))
  142. .toMatchInlineSnapshot(`
  143. ":where(.foo[data-v-test], .bar[data-v-test]) { color: red;
  144. }"
  145. `)
  146. expect(compileScoped(`:is(.foo, .bar) div { color: red; }`))
  147. .toMatchInlineSnapshot(`
  148. ":is(.foo, .bar) div[data-v-test] { color: red;
  149. }"
  150. `)
  151. })
  152. test('media query', () => {
  153. expect(compileScoped(`@media print { .foo { color: red }}`))
  154. .toMatchInlineSnapshot(`
  155. "@media print {
  156. .foo[data-v-test] { color: red
  157. }}"
  158. `)
  159. })
  160. test('supports query', () => {
  161. expect(compileScoped(`@supports(display: grid) { .foo { display: grid }}`))
  162. .toMatchInlineSnapshot(`
  163. "@supports(display: grid) {
  164. .foo[data-v-test] { display: grid
  165. }}"
  166. `)
  167. })
  168. test('scoped keyframes', () => {
  169. const style = compileScoped(
  170. `
  171. .anim {
  172. animation: color 5s infinite, other 5s;
  173. }
  174. .anim-2 {
  175. animation-name: color;
  176. animation-duration: 5s;
  177. }
  178. .anim-3 {
  179. animation: 5s color infinite, 5s other;
  180. }
  181. .anim-multiple {
  182. animation: color 5s infinite, opacity 2s;
  183. }
  184. .anim-multiple-2 {
  185. animation-name: color, opacity;
  186. animation-duration: 5s, 2s;
  187. }
  188. @keyframes color {
  189. from { color: red; }
  190. to { color: green; }
  191. }
  192. @-webkit-keyframes color {
  193. from { color: red; }
  194. to { color: green; }
  195. }
  196. @keyframes opacity {
  197. from { opacity: 0; }
  198. to { opacity: 1; }
  199. }
  200. @-webkit-keyframes opacity {
  201. from { opacity: 0; }
  202. to { opacity: 1; }
  203. }
  204. `,
  205. { id: 'data-v-test' },
  206. )
  207. expect(style).toContain(
  208. `.anim[data-v-test] {\n animation: color-test 5s infinite, other 5s;`,
  209. )
  210. expect(style).toContain(
  211. `.anim-2[data-v-test] {\n animation-name: color-test`,
  212. )
  213. expect(style).toContain(
  214. `.anim-3[data-v-test] {\n animation: 5s color-test infinite, 5s other;`,
  215. )
  216. expect(style).toContain(`@keyframes color-test {`)
  217. expect(style).toContain(`@-webkit-keyframes color-test {`)
  218. expect(style).toContain(
  219. `.anim-multiple[data-v-test] {\n animation: color-test 5s infinite,opacity-test 2s;`,
  220. )
  221. expect(style).toContain(
  222. `.anim-multiple-2[data-v-test] {\n animation-name: color-test,opacity-test;`,
  223. )
  224. expect(style).toContain(`@keyframes opacity-test {\nfrom { opacity: 0;`)
  225. expect(style).toContain(
  226. `@-webkit-keyframes opacity-test {\nfrom { opacity: 0;`,
  227. )
  228. })
  229. // vue-loader/#1370
  230. test('spaces after selector', () => {
  231. expect(compileScoped(`.foo , .bar { color: red; }`)).toMatchInlineSnapshot(`
  232. ".foo[data-v-test], .bar[data-v-test] { color: red;
  233. }"
  234. `)
  235. })
  236. describe('deprecated syntax', () => {
  237. test('::v-deep as combinator', () => {
  238. expect(compileScoped(`::v-deep .foo { color: red; }`))
  239. .toMatchInlineSnapshot(`
  240. "[data-v-test] .foo { color: red;
  241. }"
  242. `)
  243. expect(compileScoped(`.bar ::v-deep .foo { color: red; }`))
  244. .toMatchInlineSnapshot(`
  245. ".bar[data-v-test] .foo { color: red;
  246. }"
  247. `)
  248. expect(
  249. `::v-deep usage as a combinator has been deprecated.`,
  250. ).toHaveBeenWarned()
  251. })
  252. test('>>> (deprecated syntax)', () => {
  253. const code = compileScoped(`>>> .foo { color: red; }`)
  254. expect(code).toMatchInlineSnapshot(`
  255. "[data-v-test] .foo { color: red;
  256. }"
  257. `)
  258. expect(
  259. `the >>> and /deep/ combinators have been deprecated.`,
  260. ).toHaveBeenWarned()
  261. })
  262. test('/deep/ (deprecated syntax)', () => {
  263. const code = compileScoped(`/deep/ .foo { color: red; }`)
  264. expect(code).toMatchInlineSnapshot(`
  265. "[data-v-test] .foo { color: red;
  266. }"
  267. `)
  268. expect(
  269. `the >>> and /deep/ combinators have been deprecated.`,
  270. ).toHaveBeenWarned()
  271. })
  272. })
  273. })
  274. describe('SFC CSS modules', () => {
  275. test('should include resulting classes object in result', async () => {
  276. const result = await compileStyleAsync({
  277. source: `.red { color: red }\n.green { color: green }\n:global(.blue) { color: blue }`,
  278. filename: `test.css`,
  279. id: 'test',
  280. modules: true,
  281. })
  282. expect(result.modules).toBeDefined()
  283. expect(result.modules!.red).toMatch('_red_')
  284. expect(result.modules!.green).toMatch('_green_')
  285. expect(result.modules!.blue).toBeUndefined()
  286. })
  287. test('postcss-modules options', async () => {
  288. const result = await compileStyleAsync({
  289. source: `:local(.foo-bar) { color: red }\n.baz-qux { color: green }`,
  290. filename: `test.css`,
  291. id: 'test',
  292. modules: true,
  293. modulesOptions: {
  294. scopeBehaviour: 'global',
  295. generateScopedName: `[name]__[local]__[hash:base64:5]`,
  296. localsConvention: 'camelCaseOnly',
  297. },
  298. })
  299. expect(result.modules).toBeDefined()
  300. expect(result.modules!.fooBar).toMatch('__foo-bar__')
  301. expect(result.modules!.bazQux).toBeUndefined()
  302. })
  303. })
  304. describe('SFC style preprocessors', () => {
  305. test('scss @import', () => {
  306. const res = compileStyle({
  307. source: `
  308. @import "./import.scss";
  309. `,
  310. filename: path.resolve(__dirname, './fixture/test.scss'),
  311. id: '',
  312. preprocessLang: 'scss',
  313. })
  314. expect([...res.dependencies]).toStrictEqual([
  315. path.join(__dirname, './fixture/import.scss'),
  316. ])
  317. })
  318. test('scss respect user-defined string options.additionalData', () => {
  319. const res = compileStyle({
  320. preprocessOptions: {
  321. additionalData: `
  322. @mixin square($size) {
  323. width: $size;
  324. height: $size;
  325. }`,
  326. },
  327. source: `
  328. .square {
  329. @include square(100px);
  330. }
  331. `,
  332. filename: path.resolve(__dirname, './fixture/test.scss'),
  333. id: '',
  334. preprocessLang: 'scss',
  335. })
  336. expect(res.errors.length).toBe(0)
  337. })
  338. test('scss respect user-defined function options.additionalData', () => {
  339. const source = `
  340. .square {
  341. @include square(100px);
  342. }
  343. `
  344. const filename = path.resolve(__dirname, './fixture/test.scss')
  345. const res = compileStyle({
  346. preprocessOptions: {
  347. additionalData: (s: string, f: string) => {
  348. expect(s).toBe(source)
  349. expect(f).toBe(filename)
  350. return `
  351. @mixin square($size) {
  352. width: $size;
  353. height: $size;
  354. }`
  355. },
  356. },
  357. source,
  358. filename,
  359. id: '',
  360. preprocessLang: 'scss',
  361. })
  362. expect(res.errors.length).toBe(0)
  363. })
  364. })