compileStyle.spec.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import {
  2. compileStyle,
  3. compileStyleAsync,
  4. SFCStyleCompileOptions
  5. } from '../src/compileStyle'
  6. import path from '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('media query', () => {
  137. expect(compileScoped(`@media print { .foo { color: red }}`))
  138. .toMatchInlineSnapshot(`
  139. "@media print {
  140. .foo[data-v-test] { color: red
  141. }}"
  142. `)
  143. })
  144. test('supports query', () => {
  145. expect(compileScoped(`@supports(display: grid) { .foo { display: grid }}`))
  146. .toMatchInlineSnapshot(`
  147. "@supports(display: grid) {
  148. .foo[data-v-test] { display: grid
  149. }}"
  150. `)
  151. })
  152. test('scoped keyframes', () => {
  153. const style = compileScoped(
  154. `
  155. .anim {
  156. animation: color 5s infinite, other 5s;
  157. }
  158. .anim-2 {
  159. animation-name: color;
  160. animation-duration: 5s;
  161. }
  162. .anim-3 {
  163. animation: 5s color infinite, 5s other;
  164. }
  165. .anim-multiple {
  166. animation: color 5s infinite, opacity 2s;
  167. }
  168. .anim-multiple-2 {
  169. animation-name: color, opacity;
  170. animation-duration: 5s, 2s;
  171. }
  172. @keyframes color {
  173. from { color: red; }
  174. to { color: green; }
  175. }
  176. @-webkit-keyframes color {
  177. from { color: red; }
  178. to { color: green; }
  179. }
  180. @keyframes opacity {
  181. from { opacity: 0; }
  182. to { opacity: 1; }
  183. }
  184. @-webkit-keyframes opacity {
  185. from { opacity: 0; }
  186. to { opacity: 1; }
  187. }
  188. `,
  189. { id: 'data-v-test' }
  190. )
  191. expect(style).toContain(
  192. `.anim[data-v-test] {\n animation: color-test 5s infinite, other 5s;`
  193. )
  194. expect(style).toContain(
  195. `.anim-2[data-v-test] {\n animation-name: color-test`
  196. )
  197. expect(style).toContain(
  198. `.anim-3[data-v-test] {\n animation: 5s color-test infinite, 5s other;`
  199. )
  200. expect(style).toContain(`@keyframes color-test {`)
  201. expect(style).toContain(`@-webkit-keyframes color-test {`)
  202. expect(style).toContain(
  203. `.anim-multiple[data-v-test] {\n animation: color-test 5s infinite,opacity-test 2s;`
  204. )
  205. expect(style).toContain(
  206. `.anim-multiple-2[data-v-test] {\n animation-name: color-test,opacity-test;`
  207. )
  208. expect(style).toContain(`@keyframes opacity-test {\nfrom { opacity: 0;`)
  209. expect(style).toContain(
  210. `@-webkit-keyframes opacity-test {\nfrom { opacity: 0;`
  211. )
  212. })
  213. // vue-loader/#1370
  214. test('spaces after selector', () => {
  215. expect(compileScoped(`.foo , .bar { color: red; }`)).toMatchInlineSnapshot(`
  216. ".foo[data-v-test], .bar[data-v-test] { color: red;
  217. }"
  218. `)
  219. })
  220. describe('deprecated syntax', () => {
  221. test('::v-deep as combinator', () => {
  222. expect(compileScoped(`::v-deep .foo { color: red; }`))
  223. .toMatchInlineSnapshot(`
  224. "[data-v-test] .foo { color: red;
  225. }"
  226. `)
  227. expect(compileScoped(`.bar ::v-deep .foo { color: red; }`))
  228. .toMatchInlineSnapshot(`
  229. ".bar[data-v-test] .foo { color: red;
  230. }"
  231. `)
  232. expect(
  233. `::v-deep usage as a combinator has been deprecated.`
  234. ).toHaveBeenWarned()
  235. })
  236. test('>>> (deprecated syntax)', () => {
  237. const code = compileScoped(`>>> .foo { color: red; }`)
  238. expect(code).toMatchInlineSnapshot(`
  239. "[data-v-test] .foo { color: red;
  240. }"
  241. `)
  242. expect(
  243. `the >>> and /deep/ combinators have been deprecated.`
  244. ).toHaveBeenWarned()
  245. })
  246. test('/deep/ (deprecated syntax)', () => {
  247. const code = compileScoped(`/deep/ .foo { color: red; }`)
  248. expect(code).toMatchInlineSnapshot(`
  249. "[data-v-test] .foo { color: red;
  250. }"
  251. `)
  252. expect(
  253. `the >>> and /deep/ combinators have been deprecated.`
  254. ).toHaveBeenWarned()
  255. })
  256. })
  257. })
  258. describe('SFC CSS modules', () => {
  259. test('should include resulting classes object in result', async () => {
  260. const result = await compileStyleAsync({
  261. source: `.red { color: red }\n.green { color: green }\n:global(.blue) { color: blue }`,
  262. filename: `test.css`,
  263. id: 'test',
  264. modules: true
  265. })
  266. expect(result.modules).toBeDefined()
  267. expect(result.modules!.red).toMatch('_red_')
  268. expect(result.modules!.green).toMatch('_green_')
  269. expect(result.modules!.blue).toBeUndefined()
  270. })
  271. test('postcss-modules options', async () => {
  272. const result = await compileStyleAsync({
  273. source: `:local(.foo-bar) { color: red }\n.baz-qux { color: green }`,
  274. filename: `test.css`,
  275. id: 'test',
  276. modules: true,
  277. modulesOptions: {
  278. scopeBehaviour: 'global',
  279. generateScopedName: `[name]__[local]__[hash:base64:5]`,
  280. localsConvention: 'camelCaseOnly'
  281. }
  282. })
  283. expect(result.modules).toBeDefined()
  284. expect(result.modules!.fooBar).toMatch('__foo-bar__')
  285. expect(result.modules!.bazQux).toBeUndefined()
  286. })
  287. })
  288. describe('SFC style preprocessors', () => {
  289. test('scss @import', () => {
  290. const res = compileStyle({
  291. source: `
  292. @import "./import.scss";
  293. `,
  294. filename: path.resolve(__dirname, './fixture/test.scss'),
  295. id: '',
  296. preprocessLang: 'scss'
  297. })
  298. expect([...res.dependencies]).toStrictEqual([
  299. path.join(__dirname, './fixture/import.scss')
  300. ])
  301. })
  302. test('scss respect user-defined string options.additionalData', () => {
  303. const res = compileStyle({
  304. preprocessOptions: {
  305. additionalData: `
  306. @mixin square($size) {
  307. width: $size;
  308. height: $size;
  309. }`
  310. },
  311. source: `
  312. .square {
  313. @include square(100px);
  314. }
  315. `,
  316. filename: path.resolve(__dirname, './fixture/test.scss'),
  317. id: '',
  318. preprocessLang: 'scss'
  319. })
  320. expect(res.errors.length).toBe(0)
  321. })
  322. test('scss respect user-defined function options.additionalData', () => {
  323. const source = `
  324. .square {
  325. @include square(100px);
  326. }
  327. `
  328. const filename = path.resolve(__dirname, './fixture/test.scss')
  329. const res = compileStyle({
  330. preprocessOptions: {
  331. additionalData: (s: string, f: string) => {
  332. expect(s).toBe(source)
  333. expect(f).toBe(filename)
  334. return `
  335. @mixin square($size) {
  336. width: $size;
  337. height: $size;
  338. }`
  339. }
  340. },
  341. source,
  342. filename,
  343. id: '',
  344. preprocessLang: 'scss'
  345. })
  346. expect(res.errors.length).toBe(0)
  347. })
  348. })