Skip to content

Commit cee8c2c

Browse files
committed
Merge pull request #33 from css-modules/preprocess-css
preprocessCss option
2 parents 6530c7c + fdf94e2 commit cee8c2c

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ $ npm i css-modules-require-hook
3232

3333
* `function` **createImportedName** — short alias for the [postcss-modules-extract-imports](https://github.com/css-modules/postcss-modules-extract-imports) plugin's `createImportedName` option.
3434
* `function` **generateScopedName** — short alias for the [postcss-modules-scope](https://github.com/css-modules/postcss-modules-scope) plugin's option. Helps you to specify the custom way to build generic names for the class selectors.
35+
* `function` **preprocessCss** — in rare cases you may want to precompile styles, before they will be passed to the postcss pipeline. You should use **synchronous** transformations, since `require` function is synchronous.
3536
* `function` **processCss** — in rare cases you may want to get compiled styles in runtime, so providing this option helps.
3637
* `string` **rootDir** — absolute path to the project directory. Providing this will result in better generated class names. It can be obligatory, if you run require hook and build tools (like [css-modulesify](https://github.com/css-modules/css-modulesify)) from different working directories.
3738
* `string` **to** — provides `to` option to the [LazyResult instance](https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts).

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Parser from './parser';
1616
let importNr = 0;
1717
let tokensByFile = {};
1818
// processing functions
19-
const preProcess = identity;
19+
let preProcess = identity;
2020
let postProcess;
2121
// defaults
2222
let lazyResultOpts = {};
@@ -27,6 +27,7 @@ let rootDir = process.cwd();
2727
* @param {object} opts
2828
* @param {function} opts.createImportedName
2929
* @param {function} opts.generateScopedName
30+
* @param {function} opts.preprocessCss
3031
* @param {function} opts.processCss
3132
* @param {string} opts.rootDir
3233
* @param {string} opts.to
@@ -37,6 +38,7 @@ export default function setup(opts = {}) {
3738
importNr = 0;
3839
tokensByFile = {};
3940

41+
preProcess = get('preprocessCss', null, 'function', opts) || identity;
4042
postProcess = get('processCss', null, 'function', opts) || null;
4143
rootDir = get('rootDir', ['root', 'd'], 'string', opts) || process.cwd();
4244
// https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts

test/plugins.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import LocalByDefault from 'postcss-modules-local-by-default';
77
import Scope from 'postcss-modules-scope';
88

99
describe('plugins', () => {
10+
beforeEach(() => {
11+
// clearing cache
12+
delete require.cache[require.resolve('awesome-theme/oceanic.css')];
13+
});
14+
1015
describe('custom generateScopedName() function', () => {
1116
before(() => hook({generateScopedName: identity}));
1217

test/public-api.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { constant } from 'lodash';
2+
import { equal, ok } from 'assert';
3+
import hook from '../src';
4+
5+
describe('public api', () => {
6+
beforeEach(() => {
7+
// clearing cache
8+
delete require.cache[require.resolve('awesome-theme/oceanic.css')];
9+
});
10+
11+
describe('preprocessCss', () => {
12+
describe('providing empty string constantly', () => {
13+
before(() => hook({preprocessCss: constant('')}));
14+
15+
it('should return an empty result', () => {
16+
const tokens = require('awesome-theme/oceanic.css');
17+
equal(Object.keys(tokens).length, 0);
18+
});
19+
});
20+
21+
describe('providing nothing should reset preProcess', () => {
22+
before(() => hook());
23+
24+
it('should return the "color" token', () => {
25+
const tokens = require('awesome-theme/oceanic.css');
26+
ok(tokens.color);
27+
});
28+
});
29+
});
30+
});

0 commit comments

Comments
 (0)