@@ -5,132 +5,147 @@ import { resolve } from 'path';
55
66import sveltePreprocess from '../../src' ;
77import { preprocess , spyConsole } from '../utils' ;
8+ import { transformer } from '../../src/transformers/postcss' ;
89
910spyConsole ( { silent : true } ) ;
1011
11- describe ( 'transformer - postcss' , ( ) => {
12- it ( 'should not transform plain css with postcss if { postcss: falsy }' , async ( ) => {
13- const template = `<div></div><style>div{appearance:none;}</style>` ;
14- const preprocessed = await preprocess ( template , sveltePreprocess ( ) ) ;
12+ test ( 'transformer returns source map' , async ( ) => {
13+ const content = 'div{color:red}' ;
14+ const filename = '/file' ;
15+ const options = {
16+ plugins : [
17+ require ( 'autoprefixer' ) ( {
18+ overrideBrowserslist : 'Safari >= 5.1' ,
19+ } ) ,
20+ ] ,
21+ } ;
1522
16- expect ( preprocessed . toString ( ) ) . not . toMatch ( / - w e b k i t - / ) ;
17- } ) ;
23+ const { map } = await transformer ( { content, filename, options } ) ;
1824
19- it ( 'should not transform plain css with postcss if { postcss: true } and no configuration file at cwd' , async ( ) => {
20- const template = `<div></div><style>div{appearance:none;}</style>` ;
21- const preprocessed = await preprocess (
22- template ,
23- sveltePreprocess ( {
24- postcss : true ,
25- } ) ,
26- ) ;
25+ expect ( map ) . toBeTruthy ( ) ;
26+ } ) ;
2727
28- expect ( preprocessed . toString ( ) ) . not . toMatch ( / - w e b k i t - / ) ;
29- } ) ;
28+ test ( 'should not transform plain css with postcss if { postcss: falsy }' , async ( ) => {
29+ const template = `<div></div><style>div{appearance:none;}</style>` ;
30+ const preprocessed = await preprocess ( template , sveltePreprocess ( ) ) ;
3031
31- it ( 'should transform plain css with postcss if { postcss: { plugins... } }' , async ( ) => {
32- const template = `<div></div><style>div{appearance:none;}</style>` ;
33- const optsWithoutConfigFile = sveltePreprocess ( {
34- postcss : {
35- plugins : [
36- require ( 'autoprefixer' ) ( {
37- overrideBrowserslist : 'Safari >= 5.1' ,
38- } ) ,
39- ] ,
40- } ,
41- } ) ;
32+ expect ( preprocessed . toString ( ) ) . not . toMatch ( / - w e b k i t - / ) ;
33+ } ) ;
4234
43- const preprocessed = await preprocess ( template , optsWithoutConfigFile ) ;
35+ test ( 'should not transform plain css with postcss if { postcss: true } and no configuration file at cwd' , async ( ) => {
36+ const template = `<div></div><style>div{appearance:none;}</style>` ;
37+ const preprocessed = await preprocess (
38+ template ,
39+ sveltePreprocess ( {
40+ postcss : true ,
41+ } ) ,
42+ ) ;
43+
44+ expect ( preprocessed . toString ( ) ) . not . toMatch ( / - w e b k i t - / ) ;
45+ } ) ;
4446
45- expect ( preprocessed . toString ( ) ) . toMatch ( / - w e b k i t - / ) ;
47+ test ( 'should transform plain css with postcss if { postcss: { plugins... } }' , async ( ) => {
48+ const template = `<div></div><style>div{appearance:none;}</style>` ;
49+ const optsWithoutConfigFile = sveltePreprocess ( {
50+ postcss : {
51+ plugins : [
52+ require ( 'autoprefixer' ) ( {
53+ overrideBrowserslist : 'Safari >= 5.1' ,
54+ } ) ,
55+ ] ,
56+ } ,
4657 } ) ;
4758
48- it ( 'should transform async preprocessed css with postcss if { postcss: { plugins... } }' , async ( ) => {
49- const templateSass = `<div></div><style lang="scss">div{appearance:none;}</style>` ;
50- const optsWithoutConfigFile = sveltePreprocess ( {
51- postcss : {
52- plugins : [
53- require ( 'autoprefixer' ) ( {
54- overrideBrowserslist : 'Safari >= 5.1' ,
55- } ) ,
56- ] ,
57- } ,
58- } ) ;
59+ const preprocessed = await preprocess ( template , optsWithoutConfigFile ) ;
5960
60- const preprocessed = await preprocess ( templateSass , optsWithoutConfigFile ) ;
61+ expect ( preprocessed . toString ( ) ) . toMatch ( / - w e b k i t - / ) ;
62+ } ) ;
6163
62- expect ( preprocessed . toString ( ) ) . toMatch ( / - w e b k i t - / ) ;
64+ test ( 'should transform async preprocessed css with postcss if { postcss: { plugins... } }' , async ( ) => {
65+ const templateSass = `<div></div><style lang="scss">div{appearance:none;}</style>` ;
66+ const optsWithoutConfigFile = sveltePreprocess ( {
67+ postcss : {
68+ plugins : [
69+ require ( 'autoprefixer' ) ( {
70+ overrideBrowserslist : 'Safari >= 5.1' ,
71+ } ) ,
72+ ] ,
73+ } ,
6374 } ) ;
6475
65- it ( 'should transform plain css with postcss if { postcss: { configFilePath: ... } }' , async ( ) => {
66- const template = `<div></div><style>div{appearance:none;}</style>` ;
67- const preprocessed = await preprocess (
68- template ,
69- sveltePreprocess ( {
70- postcss : {
71- configFilePath : './test/fixtures/' ,
72- } ,
73- } ) ,
74- ) ;
76+ const preprocessed = await preprocess ( templateSass , optsWithoutConfigFile ) ;
7577
76- expect ( preprocessed . toString ( ) ) . toMatch ( / - w e b k i t - / ) ;
77- } ) ;
78+ expect ( preprocessed . toString ( ) ) . toMatch ( / - w e b k i t - / ) ;
79+ } ) ;
7880
79- it ( 'should return @imported files as dependencies' , async ( ) => {
80- const template = `<style>@import './fixtures/style.css';</style>` ;
81- const opts = sveltePreprocess ( {
81+ test ( 'should transform plain css with postcss if { postcss: { configFilePath: ... } }' , async ( ) => {
82+ const template = `<div></div><style>div{appearance:none;}</style>` ;
83+ const preprocessed = await preprocess (
84+ template ,
85+ sveltePreprocess ( {
8286 postcss : {
83- plugins : [ require ( 'postcss-easy-import' ) ] ,
87+ configFilePath : './test/fixtures/' ,
8488 } ,
85- } ) ;
89+ } ) ,
90+ ) ;
8691
87- const preprocessed = await preprocess ( template , opts ) ;
92+ expect ( preprocessed . toString ( ) ) . toMatch ( / - w e b k i t - / ) ;
93+ } ) ;
8894
89- expect ( preprocessed . dependencies ) . toContain (
90- resolve ( __dirname , '..' , 'fixtures' , 'style.css' ) ,
91- ) ;
95+ test ( 'should return @imported files as dependencies' , async ( ) => {
96+ const template = `<style>@import './fixtures/style.css';</style>` ;
97+ const opts = sveltePreprocess ( {
98+ postcss : {
99+ plugins : [ require ( 'postcss-easy-import' ) ] ,
100+ } ,
92101 } ) ;
93102
94- it ( 'should allow custom postcss parsers' , async ( ) => {
95- const template = `<style>
103+ const preprocessed = await preprocess ( template , opts ) ;
104+
105+ expect ( preprocessed . dependencies ) . toContain (
106+ resolve ( __dirname , '..' , 'fixtures' , 'style.css' ) ,
107+ ) ;
108+ } ) ;
109+
110+ test ( 'should allow custom postcss parsers' , async ( ) => {
111+ const template = `<style>
96112div
97113 color: red
98114</style>` ;
99115
100- const opts = sveltePreprocess ( {
101- postcss : {
102- parser : require ( 'sugarss' ) ,
103- plugins : [ require ( 'postcss-easy-import' ) ] ,
104- } ,
105- } ) ;
116+ const opts = sveltePreprocess ( {
117+ postcss : {
118+ parser : require ( 'sugarss' ) ,
119+ plugins : [ require ( 'postcss-easy-import' ) ] ,
120+ } ,
121+ } ) ;
106122
107- const preprocessed = await preprocess ( template , opts ) ;
123+ const preprocessed = await preprocess ( template , opts ) ;
108124
109- expect ( preprocessed . toString ( ) ) . toMatchInlineSnapshot ( `
125+ expect ( preprocessed . toString ( ) ) . toMatchInlineSnapshot ( `
110126 "<style>
111127 div {
112128 color: red
113129 }</style>"
114130 ` ) ;
115- } ) ;
131+ } ) ;
116132
117- it ( 'automatically removes indentation for lang=sugarss ' , async ( ) => {
118- const template = `<style lang="sugarss">
133+ test ( 'automatically removes indentation for lang=sugarss ' , async ( ) => {
134+ const template = `<style lang="sugarss">
119135 div
120136 color: red
121137 </style>` ;
122138
123- const opts = sveltePreprocess ( {
124- postcss : true ,
125- } ) ;
139+ const opts = sveltePreprocess ( {
140+ postcss : true ,
141+ } ) ;
126142
127- const preprocessed = await preprocess ( template , opts ) ;
143+ const preprocessed = await preprocess ( template , opts ) ;
128144
129- expect ( preprocessed . toString ( ) ) . toMatchInlineSnapshot ( `
145+ expect ( preprocessed . toString ( ) ) . toMatchInlineSnapshot ( `
130146 "<style lang=\\"sugarss\\">
131147 div {
132148 color: red
133149 }</style>"
134150 ` ) ;
135- } ) ;
136151} ) ;
0 commit comments