Skip to content

Commit

Permalink
fix: 🐛 run globalRule only if postcss is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisermann committed Jun 5, 2020
1 parent 6c99240 commit 6294750
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 31 deletions.
26 changes: 14 additions & 12 deletions src/autoProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Options,
Processed,
} from './types';
import { hasPostCssInstalled } from './modules/hasPostcssInstalled';

interface Transformers {
typescript?: TransformerOptions<Options.Typescript>;
Expand Down Expand Up @@ -261,26 +262,27 @@ export function autoPreprocess(
dependencies = concat(dependencies, transformed.dependencies);
}

if (attributes.global) {
const transformed = await runTransformer('globalStyle', null, {
if (await hasPostCssInstalled()) {
if (attributes.global) {
const transformed = await runTransformer('globalStyle', null, {
content: code,
map,
filename,
});

code = transformed.code;
map = transformed.map;
}

const transformed = await runTransformer('globalRule', null, {
content: code,
map,
filename,
});

code = transformed.code;
map = transformed.map;
}

const transformed = await runTransformer('globalRule', null, {
content: code,
map,
filename,
});

code = transformed.code;
map = transformed.map;

return { code, map, dependencies };
},
};
Expand Down
17 changes: 17 additions & 0 deletions src/modules/hasPostcssInstalled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
let cachedResult: boolean;

export async function hasPostCssInstalled() {
if (cachedResult != null) {
return cachedResult;
}

let result = false;
try {
await import('postcss');
result = true;
} catch (e) {
result = false;
}

return (cachedResult = result);
}
11 changes: 11 additions & 0 deletions src/modules/importAny.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export async function importAny(...modules: string[]) {
try {
const mod = await modules.reduce(
(acc, moduleName) => acc.catch(() => import(moduleName)),
Promise.reject(),
);
return mod;
} catch (e) {
throw new Error(`Cannot find any of modules: ${modules}`);
}
}
3 changes: 2 additions & 1 deletion src/transformers/scss.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Result } from 'sass';

import { importAny, getIncludePaths } from '../utils';
import { getIncludePaths } from '../utils';
import { Transformer, Processed, Options } from '../types';
import { importAny } from '../modules/importAny';

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

Expand Down
14 changes: 1 addition & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,4 @@ export const runTransformer = async (
`Error transforming '${name}'.\n\nMessage:\n${e.message}\n\nStack:\n${e.stack}`,
);
}
};

export const importAny = async (...modules: string[]) => {
try {
const mod = await modules.reduce(
(acc, moduleName) => acc.catch(() => import(moduleName)),
Promise.reject(),
);
return mod;
} catch (e) {
throw new Error(`Cannot find any of modules: ${modules}`);
}
};
};
7 changes: 7 additions & 0 deletions test/transformers/globalRule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import autoProcess from '../../src';
import { preprocess } from '../utils';

describe('transformer - globalRule', () => {
it('does nothing if postcss is not installed', async () => {
const template = `<style>:global div{color:red}:global .test{}</style>`;
const opts = autoProcess();

expect(async () => await preprocess(template, opts)).not.toThrow();
});

it('wraps selector in :global(...) modifier', async () => {
const template = `<style>:global div{color:red}:global .test{}</style>`;
const opts = autoProcess();
Expand Down
8 changes: 4 additions & 4 deletions test/transformers/scss.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Options } from '../../src/types';
const implementation: Options.Sass['implementation'] = {
render(options, callback) {
callback(null, {
css: Buffer.from('Foo'),
css: Buffer.from('div#red{color:red}'),
stats: {
entry: 'data',
start: 0,
Expand All @@ -18,7 +18,7 @@ const implementation: Options.Sass['implementation'] = {
});
},
renderSync: () => ({
css: Buffer.from('Bar'),
css: Buffer.from('div#green{color:green}'),
stats: {
entry: 'data',
start: 0,
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('transformer - scss', () => {
},
});
const preprocessed = await preprocess(template, opts);
expect(preprocessed.toString()).toContain('Foo');
expect(preprocessed.toString()).toContain('div#red{color:red}');
});

it('should prepend scss content via `data` option property - via renderSync', async () => {
Expand Down Expand Up @@ -95,6 +95,6 @@ describe('transformer - scss', () => {
},
});
const preprocessed = await preprocess(template, opts);
expect(preprocessed.toString()).toContain('Bar');
expect(preprocessed.toString()).toContain('div#green{color:green}');
});
});
3 changes: 2 additions & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { resolve } from 'path';

import { importAny, getIncludePaths } from '../src/utils';
import { getIncludePaths } from '../src/utils';
import { importAny } from '../src/modules/importAny';

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

0 comments on commit 6294750

Please sign in to comment.