compileStyle.spec.ts 9.5 KB

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