File tree 8 files changed +58
-31
lines changed
8 files changed +58
-31
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ import {
16
16
Options ,
17
17
Processed ,
18
18
} from './types' ;
19
+ import { hasPostCssInstalled } from './modules/hasPostcssInstalled' ;
19
20
20
21
interface Transformers {
21
22
typescript ?: TransformerOptions < Options . Typescript > ;
@@ -261,26 +262,27 @@ export function autoPreprocess(
261
262
dependencies = concat ( dependencies , transformed . dependencies ) ;
262
263
}
263
264
264
- if ( attributes . global ) {
265
- const transformed = await runTransformer ( 'globalStyle' , null , {
265
+ if ( await hasPostCssInstalled ( ) ) {
266
+ if ( attributes . global ) {
267
+ const transformed = await runTransformer ( 'globalStyle' , null , {
268
+ content : code ,
269
+ map,
270
+ filename,
271
+ } ) ;
272
+
273
+ code = transformed . code ;
274
+ map = transformed . map ;
275
+ }
276
+
277
+ const transformed = await runTransformer ( 'globalRule' , null , {
266
278
content : code ,
267
279
map,
268
280
filename,
269
281
} ) ;
270
-
271
282
code = transformed . code ;
272
283
map = transformed . map ;
273
284
}
274
285
275
- const transformed = await runTransformer ( 'globalRule' , null , {
276
- content : code ,
277
- map,
278
- filename,
279
- } ) ;
280
-
281
- code = transformed . code ;
282
- map = transformed . map ;
283
-
284
286
return { code, map, dependencies } ;
285
287
} ,
286
288
} ;
Original file line number Diff line number Diff line change
1
+ let cachedResult : boolean ;
2
+
3
+ export async function hasPostCssInstalled ( ) {
4
+ if ( cachedResult != null ) {
5
+ return cachedResult ;
6
+ }
7
+
8
+ let result = false ;
9
+ try {
10
+ await import ( 'postcss' ) ;
11
+ result = true ;
12
+ } catch ( e ) {
13
+ result = false ;
14
+ }
15
+
16
+ return ( cachedResult = result ) ;
17
+ }
Original file line number Diff line number Diff line change
1
+ export async function importAny ( ...modules : string [ ] ) {
2
+ try {
3
+ const mod = await modules . reduce (
4
+ ( acc , moduleName ) => acc . catch ( ( ) => import ( moduleName ) ) ,
5
+ Promise . reject ( ) ,
6
+ ) ;
7
+ return mod ;
8
+ } catch ( e ) {
9
+ throw new Error ( `Cannot find any of modules: ${ modules } ` ) ;
10
+ }
11
+ }
Original file line number Diff line number Diff line change 1
1
import { Result } from 'sass' ;
2
2
3
- import { importAny , getIncludePaths } from '../utils' ;
3
+ import { getIncludePaths } from '../utils' ;
4
4
import { Transformer , Processed , Options } from '../types' ;
5
+ import { importAny } from '../modules/importAny' ;
5
6
6
7
let sass : Options . Sass [ 'implementation' ] ;
7
8
Original file line number Diff line number Diff line change @@ -158,16 +158,4 @@ export const runTransformer = async (
158
158
`Error transforming '${ name } '.\n\nMessage:\n${ e . message } \n\nStack:\n${ e . stack } ` ,
159
159
) ;
160
160
}
161
- } ;
162
-
163
- export const importAny = async ( ...modules : string [ ] ) => {
164
- try {
165
- const mod = await modules . reduce (
166
- ( acc , moduleName ) => acc . catch ( ( ) => import ( moduleName ) ) ,
167
- Promise . reject ( ) ,
168
- ) ;
169
- return mod ;
170
- } catch ( e ) {
171
- throw new Error ( `Cannot find any of modules: ${ modules } ` ) ;
172
- }
173
- } ;
161
+ } ;
Original file line number Diff line number Diff line change @@ -2,6 +2,13 @@ import autoProcess from '../../src';
2
2
import { preprocess } from '../utils' ;
3
3
4
4
describe ( 'transformer - globalRule' , ( ) => {
5
+ it ( 'does nothing if postcss is not installed' , async ( ) => {
6
+ const template = `<style>:global div{color:red}:global .test{}</style>` ;
7
+ const opts = autoProcess ( ) ;
8
+
9
+ expect ( async ( ) => await preprocess ( template , opts ) ) . not . toThrow ( ) ;
10
+ } ) ;
11
+
5
12
it ( 'wraps selector in :global(...) modifier' , async ( ) => {
6
13
const template = `<style>:global div{color:red}:global .test{}</style>` ;
7
14
const opts = autoProcess ( ) ;
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ import { Options } from '../../src/types';
7
7
const implementation : Options . Sass [ 'implementation' ] = {
8
8
render ( options , callback ) {
9
9
callback ( null , {
10
- css : Buffer . from ( 'Foo ' ) ,
10
+ css : Buffer . from ( 'div#red{color:red} ' ) ,
11
11
stats : {
12
12
entry : 'data' ,
13
13
start : 0 ,
@@ -18,7 +18,7 @@ const implementation: Options.Sass['implementation'] = {
18
18
} ) ;
19
19
} ,
20
20
renderSync : ( ) => ( {
21
- css : Buffer . from ( 'Bar ' ) ,
21
+ css : Buffer . from ( 'div#green{color:green} ' ) ,
22
22
stats : {
23
23
entry : 'data' ,
24
24
start : 0 ,
@@ -58,7 +58,7 @@ describe('transformer - scss', () => {
58
58
} ,
59
59
} ) ;
60
60
const preprocessed = await preprocess ( template , opts ) ;
61
- expect ( preprocessed . toString ( ) ) . toContain ( 'Foo ' ) ;
61
+ expect ( preprocessed . toString ( ) ) . toContain ( 'div#red{color:red} ' ) ;
62
62
} ) ;
63
63
64
64
it ( 'should prepend scss content via `data` option property - via renderSync' , async ( ) => {
@@ -95,6 +95,6 @@ describe('transformer - scss', () => {
95
95
} ,
96
96
} ) ;
97
97
const preprocessed = await preprocess ( template , opts ) ;
98
- expect ( preprocessed . toString ( ) ) . toContain ( 'Bar ' ) ;
98
+ expect ( preprocessed . toString ( ) ) . toContain ( 'div#green{color:green} ' ) ;
99
99
} ) ;
100
100
} ) ;
Original file line number Diff line number Diff line change 1
1
import { resolve } from 'path' ;
2
2
3
- import { importAny , getIncludePaths } from '../src/utils' ;
3
+ import { getIncludePaths } from '../src/utils' ;
4
+ import { importAny } from '../src/modules/importAny' ;
4
5
5
6
describe ( 'utils - importAny' , ( ) => {
6
7
it ( 'should throw error when none exist' , ( ) => {
You can’t perform that action at this time.
0 commit comments