Skip to content

Commit 3c22a20

Browse files
committed
fix: 🐛 run globalRule only if postcss is installed
1 parent efbb9f3 commit 3c22a20

File tree

8 files changed

+58
-31
lines changed

8 files changed

+58
-31
lines changed

src/autoProcess.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
Options,
1717
Processed,
1818
} from './types';
19+
import { hasPostCssInstalled } from './modules/hasPostcssInstalled';
1920

2021
interface Transformers {
2122
typescript?: TransformerOptions<Options.Typescript>;
@@ -261,26 +262,27 @@ export function autoPreprocess(
261262
dependencies = concat(dependencies, transformed.dependencies);
262263
}
263264

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, {
266278
content: code,
267279
map,
268280
filename,
269281
});
270-
271282
code = transformed.code;
272283
map = transformed.map;
273284
}
274285

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-
284286
return { code, map, dependencies };
285287
},
286288
};

src/modules/hasPostcssInstalled.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

src/modules/importAny.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}

src/transformers/scss.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Result } from 'sass';
22

3-
import { importAny, getIncludePaths } from '../utils';
3+
import { getIncludePaths } from '../utils';
44
import { Transformer, Processed, Options } from '../types';
5+
import { importAny } from '../modules/importAny';
56

67
let sass: Options.Sass['implementation'];
78

src/utils.ts

+1-13
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,4 @@ export const runTransformer = async (
158158
`Error transforming '${name}'.\n\nMessage:\n${e.message}\n\nStack:\n${e.stack}`,
159159
);
160160
}
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+
};

test/transformers/globalRule.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import autoProcess from '../../src';
22
import { preprocess } from '../utils';
33

44
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+
512
it('wraps selector in :global(...) modifier', async () => {
613
const template = `<style>:global div{color:red}:global .test{}</style>`;
714
const opts = autoProcess();

test/transformers/scss.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Options } from '../../src/types';
77
const implementation: Options.Sass['implementation'] = {
88
render(options, callback) {
99
callback(null, {
10-
css: Buffer.from('Foo'),
10+
css: Buffer.from('div#red{color:red}'),
1111
stats: {
1212
entry: 'data',
1313
start: 0,
@@ -18,7 +18,7 @@ const implementation: Options.Sass['implementation'] = {
1818
});
1919
},
2020
renderSync: () => ({
21-
css: Buffer.from('Bar'),
21+
css: Buffer.from('div#green{color:green}'),
2222
stats: {
2323
entry: 'data',
2424
start: 0,
@@ -58,7 +58,7 @@ describe('transformer - scss', () => {
5858
},
5959
});
6060
const preprocessed = await preprocess(template, opts);
61-
expect(preprocessed.toString()).toContain('Foo');
61+
expect(preprocessed.toString()).toContain('div#red{color:red}');
6262
});
6363

6464
it('should prepend scss content via `data` option property - via renderSync', async () => {
@@ -95,6 +95,6 @@ describe('transformer - scss', () => {
9595
},
9696
});
9797
const preprocessed = await preprocess(template, opts);
98-
expect(preprocessed.toString()).toContain('Bar');
98+
expect(preprocessed.toString()).toContain('div#green{color:green}');
9999
});
100100
});

test/utils.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { resolve } from 'path';
22

3-
import { importAny, getIncludePaths } from '../src/utils';
3+
import { getIncludePaths } from '../src/utils';
4+
import { importAny } from '../src/modules/importAny';
45

56
describe('utils - importAny', () => {
67
it('should throw error when none exist', () => {

0 commit comments

Comments
 (0)