@@ -5,27 +5,68 @@ const { plugins } = postcssrc.sync(ctx)
55const logger = require ( '../logger' )
66const getVueJestConfig = require ( '../get-vue-jest-config' )
77
8+ let prevCheckIsAsync = null
9+ function hasAsyncPlugin ( ) {
10+ if ( prevCheckIsAsync !== null ) {
11+ return prevCheckIsAsync
12+ }
13+ const result = postcss ( plugins )
14+ . process ( '' , {
15+ from : undefined
16+ } )
17+
18+ if ( result . processing ) {
19+ prevCheckIsAsync = true
20+ }
21+ for ( const plugin of result . processor . plugins ) {
22+ const promise = result . run ( plugin )
23+ if ( typeof promise === 'object' && typeof promise . then === 'function' ) {
24+ prevCheckIsAsync = true
25+ break
26+ }
27+ }
28+ if ( prevCheckIsAsync === null ) {
29+ prevCheckIsAsync = false
30+ }
31+
32+ return prevCheckIsAsync
33+ }
34+
35+ function catchError ( error , filePath , jestConfig ) {
36+ if ( ! getVueJestConfig ( jestConfig ) . hideStyleWarn ) {
37+ logger . warn ( `There was an error rendering the POSTCSS in ${ filePath } . ` )
38+ logger . warn ( `Error while compiling styles: ${ error } ` )
39+ }
40+ }
41+
842module . exports = ( content , filePath , jestConfig ) => {
943 let css = null
1044
11- postcss ( plugins )
45+ const res = postcss ( plugins )
1246 . process ( content , {
1347 from : undefined
1448 } )
15- . then ( result => {
16- css = result . css || ''
17- } )
18- . catch ( ( e ) => {
19- css = ''
20- if ( ! getVueJestConfig ( jestConfig ) . hideStyleWarn ) {
21- logger . warn ( `There was an error rendering the POSTCSS in ${ filePath } . ` )
22- logger . warn ( `Error while compiling styles: ${ e } ` )
23- }
24- } )
2549
26- while ( css === null ) { //eslint-disable-line
27- require ( 'deasync' ) . sleep ( 100 )
50+ if ( hasAsyncPlugin ( ) ) {
51+ res
52+ . then ( result => {
53+ css = result . css || ''
54+ } )
55+ . catch ( ( e ) => {
56+ css = ''
57+ catchError ( e , filePath , jestConfig )
58+ } )
59+
60+ while ( css === null ) { //eslint-disable-line
61+ require ( 'deasync' ) . sleep ( 100 )
62+ }
63+
64+ return css
2865 }
2966
30- return css
67+ try {
68+ return res . css
69+ } catch ( e ) {
70+ catchError ( e , filePath , jestConfig )
71+ }
3172}
0 commit comments