Skip to content

Commit 017626a

Browse files
committedMar 7, 2016
Merge pull request #63 from css-modules/processor-opts
Processor opts
2 parents b1bc1ef + ea79a17 commit 017626a

File tree

6 files changed

+67
-25
lines changed

6 files changed

+67
-25
lines changed
 

Diff for: ‎README.md

+31-20
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,23 @@ hook({
120120
});
121121
```
122122

123-
### `append` array
123+
### `devMode` boolean
124124

125-
Appends custom plugins to the end of the PostCSS pipeline. Since the `require` function is synchronous, you should provide synchronous plugins only.
125+
Helps you to invalidate cache of all `require` calls. Usually used for the development purpose. Also overrides behavior, imposed by `NODE_ENV` environment variable. For example:
126126

127-
### `prepend` array
127+
```javascript
128+
hook({
129+
devMode: false,
130+
});
131+
```
128132

129-
Prepends custom plugins to the beginning of the PostCSS pipeline. Since the `require` function is synchronous, you should provide synchronous plugins only.
133+
### `extensions` array
130134

131-
### `use` array
135+
Attach the require hook to additional file extensions (for example `['.scss']`).
132136

133-
Provides the full list of PostCSS plugins to the pipeline. Providing this cancels `append`, `prepend`, `createImportedName`, `generateScopedName` options. Synchronous plugins only.
137+
### `ignore` function|regex|string
138+
139+
Provides possibility to exclude particular files from processing. Supports glob and regular expressions syntax. Also you may provide custom function.
134140

135141
### `preprocessCss` function
136142

@@ -163,31 +169,31 @@ hook({
163169
});
164170
```
165171

166-
### `devMode` boolean
172+
### `processorOpts` object
167173

168-
Helps you to invalidate cache of all `require` calls. Usually used for the development purpose. Also overrides behavior, imposed by `NODE_ENV` environment variable. For example:
174+
Provides possibility to pass custom options to the [LazyResult instance](https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts). It can be usefull if you want to set the custom parser, for example: [postcss-less](https://github.com/gilt/postcss-less).
175+
176+
```javascript
177+
const hook = require('css-modules-require-hook');
178+
const lessParser = require('postcss-less').parse;
169179

170-
```bash
171180
hook({
172-
devMode: false,
181+
extensions: '.less',
182+
processorOpts: {parser: lessParser},
173183
});
174184
```
175185

176-
### `extensions` array
177-
178-
Attach the require hook to additional file extensions (for example `['.scss']`).
179-
180-
### `ignore` function|regex|string
186+
### `append` array
181187

182-
Provides possibility to exclude particular files from processing. Supports glob and regular expressions syntax. Also you may provide custom function.
188+
Appends custom plugins to the end of the PostCSS pipeline. Since the `require` function is synchronous, you should provide synchronous plugins only.
183189

184-
### `rootDir` string
190+
### `prepend` array
185191

186-
Provides 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.
192+
Prepends custom plugins to the beginning of the PostCSS pipeline. Since the `require` function is synchronous, you should provide synchronous plugins only.
187193

188-
### `to` string
194+
### `use` array
189195

190-
Provides `to` option to the [LazyResult instance](https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts).
196+
Provides the full list of PostCSS plugins to the pipeline. Providing this cancels `append`, `prepend`, `createImportedName`, `generateScopedName` options. Synchronous plugins only.
191197

192198
### `createImportedName` function
193199

@@ -224,6 +230,11 @@ hook({
224230

225231
Short alias for the [postcss-modules-local-by-default](https://github.com/css-modules/postcss-modules-local-by-default) plugin's option.
226232

233+
### `rootDir` string
234+
235+
Provides 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.
236+
237+
227238
## Debugging
228239

229240
[debug](https://www.npmjs.com/package/debug) package is used for debugging. So to turn it on simply specify the **DEBUG** environment variable:

Diff for: ‎lib/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = function setupHook({
2626
ignore,
2727
preprocessCss = identity,
2828
processCss,
29-
to,
29+
processorOpts,
3030
append = [],
3131
prepend = [],
3232
createImportedName,
@@ -96,7 +96,7 @@ module.exports = function setupHook({
9696

9797
const source = preprocessCss(readFileSync(filename, 'utf8'), filename);
9898
// https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts
99-
const lazyResult = runner.process(source, assign({}, {from: filename}));
99+
const lazyResult = runner.process(source, assign({}, processorOpts, {from: filename}));
100100

101101
// https://github.com/postcss/postcss/blob/master/docs/api.md#lazywarnings
102102
lazyResult.warnings().forEach(message => console.warn(message.text));

Diff for: ‎lib/validate.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const rules = {
99
ignore: 'function|regex|string',
1010
preprocessCss: 'function',
1111
processCss: 'function',
12-
to: 'string',
12+
processorOpts: 'object',
1313
// plugins
1414
append: 'array',
1515
prepend: 'array',
@@ -24,6 +24,7 @@ const tests = {
2424
array: require('lodash').isArray,
2525
boolean: require('lodash').isBoolean,
2626
function: require('lodash').isFunction,
27+
object: require('lodash').isPlainObject,
2728
regex: require('lodash').isRegExp,
2829
string: require('lodash').isString,
2930
};

Diff for: ‎package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "css-modules-require-hook",
3-
"version": "3.0.0",
3+
"version": "4.0.0",
44
"description": "A require hook to compile CSS Modules on the fly",
55
"main": "lib/index.js",
66
"engines": {
@@ -42,7 +42,7 @@
4242
"glob-to-regexp": "^0.1.0",
4343
"icss-replace-symbols": "^1.0.2",
4444
"lodash": "^4.3.0",
45-
"postcss": "^5.0.15",
45+
"postcss": "^5.0.19",
4646
"postcss-modules-extract-imports": "^1.0.0",
4747
"postcss-modules-local-by-default": "^1.0.1",
4848
"postcss-modules-parser": "^1.1.0",
@@ -57,6 +57,7 @@
5757
"in-publish": "^2.0.0",
5858
"isparta": "^4.0.0",
5959
"mocha": "^2.4.5",
60+
"postcss-less": "^0.2.0",
6061
"sinon": "^1.17.3"
6162
}
6263
}

Diff for: ‎test/api/fixture/oceanic.less

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.color
2+
{
3+
background: #1e2a35;
4+
}

Diff for: ‎test/api/processorOpts.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const detachHook = require('../sugar').detachHook;
2+
const dropCache = require('../sugar').dropCache;
3+
const identity = require('lodash').identity;
4+
5+
const lessParser = require('postcss-less').parse;
6+
7+
// https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts
8+
suite('api/processorOpts()', () => {
9+
test('should provide possibility to specify custom processor options, for example: parser', () => {
10+
const tokens = require('./fixture/oceanic.less');
11+
assert.deepEqual(tokens, {color: '_test_api_fixture_oceanic__color'});
12+
});
13+
14+
setup(() => {
15+
hook({
16+
extensions: '.less',
17+
processorOpts: {parser: lessParser},
18+
});
19+
});
20+
21+
teardown(() => {
22+
detachHook('.less');
23+
dropCache('./api/fixture/oceanic.less');
24+
});
25+
});

0 commit comments

Comments
 (0)
Please sign in to comment.