Skip to content

Processor opts #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,23 @@ hook({
});
```

### `append` array
### `devMode` boolean

Appends custom plugins to the end of the PostCSS pipeline. Since the `require` function is synchronous, you should provide synchronous plugins only.
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:

### `prepend` array
```javascript
hook({
devMode: false,
});
```

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

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

Provides the full list of PostCSS plugins to the pipeline. Providing this cancels `append`, `prepend`, `createImportedName`, `generateScopedName` options. Synchronous plugins only.
### `ignore` function|regex|string

Provides possibility to exclude particular files from processing. Supports glob and regular expressions syntax. Also you may provide custom function.

### `preprocessCss` function

Expand Down Expand Up @@ -163,31 +169,31 @@ hook({
});
```

### `devMode` boolean
### `processorOpts` object

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:
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).

```javascript
const hook = require('css-modules-require-hook');
const lessParser = require('postcss-less').parse;

```bash
hook({
devMode: false,
extensions: '.less',
processorOpts: {parser: lessParser},
});
```

### `extensions` array

Attach the require hook to additional file extensions (for example `['.scss']`).

### `ignore` function|regex|string
### `append` array

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

### `rootDir` string
### `prepend` array

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

### `to` string
### `use` array

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

### `createImportedName` function

Expand Down Expand Up @@ -224,6 +230,11 @@ hook({

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

### `rootDir` string

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.


## Debugging

[debug](https://www.npmjs.com/package/debug) package is used for debugging. So to turn it on simply specify the **DEBUG** environment variable:
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = function setupHook({
ignore,
preprocessCss = identity,
processCss,
to,
processorOpts,
append = [],
prepend = [],
createImportedName,
Expand Down Expand Up @@ -96,7 +96,7 @@ module.exports = function setupHook({

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

// https://github.com/postcss/postcss/blob/master/docs/api.md#lazywarnings
lazyResult.warnings().forEach(message => console.warn(message.text));
Expand Down
3 changes: 2 additions & 1 deletion lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const rules = {
ignore: 'function|regex|string',
preprocessCss: 'function',
processCss: 'function',
to: 'string',
processorOpts: 'object',
// plugins
append: 'array',
prepend: 'array',
Expand All @@ -24,6 +24,7 @@ const tests = {
array: require('lodash').isArray,
boolean: require('lodash').isBoolean,
function: require('lodash').isFunction,
object: require('lodash').isPlainObject,
regex: require('lodash').isRegExp,
string: require('lodash').isString,
};
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "css-modules-require-hook",
"version": "3.0.0",
"version": "4.0.0",
"description": "A require hook to compile CSS Modules on the fly",
"main": "lib/index.js",
"engines": {
Expand Down Expand Up @@ -42,7 +42,7 @@
"glob-to-regexp": "^0.1.0",
"icss-replace-symbols": "^1.0.2",
"lodash": "^4.3.0",
"postcss": "^5.0.15",
"postcss": "^5.0.19",
"postcss-modules-extract-imports": "^1.0.0",
"postcss-modules-local-by-default": "^1.0.1",
"postcss-modules-parser": "^1.1.0",
Expand All @@ -57,6 +57,7 @@
"in-publish": "^2.0.0",
"isparta": "^4.0.0",
"mocha": "^2.4.5",
"postcss-less": "^0.2.0",
"sinon": "^1.17.3"
}
}
4 changes: 4 additions & 0 deletions test/api/fixture/oceanic.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.color
{
background: #1e2a35;
}
25 changes: 25 additions & 0 deletions test/api/processorOpts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const detachHook = require('../sugar').detachHook;
const dropCache = require('../sugar').dropCache;
const identity = require('lodash').identity;

const lessParser = require('postcss-less').parse;

// https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts
suite('api/processorOpts()', () => {
test('should provide possibility to specify custom processor options, for example: parser', () => {
const tokens = require('./fixture/oceanic.less');
assert.deepEqual(tokens, {color: '_test_api_fixture_oceanic__color'});
});

setup(() => {
hook({
extensions: '.less',
processorOpts: {parser: lessParser},
});
});

teardown(() => {
detachHook('.less');
dropCache('./api/fixture/oceanic.less');
});
});