diff --git a/README.md b/README.md index a6241cd..f00a60d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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: diff --git a/lib/index.js b/lib/index.js index 70fccc3..abd894d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -26,7 +26,7 @@ module.exports = function setupHook({ ignore, preprocessCss = identity, processCss, - to, + processorOpts, append = [], prepend = [], createImportedName, @@ -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)); diff --git a/lib/validate.js b/lib/validate.js index b12cd6f..9ddc925 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -9,7 +9,7 @@ const rules = { ignore: 'function|regex|string', preprocessCss: 'function', processCss: 'function', - to: 'string', + processorOpts: 'object', // plugins append: 'array', prepend: 'array', @@ -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, }; diff --git a/package.json b/package.json index 3bbe049..7177a41 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", @@ -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" } } diff --git a/test/api/fixture/oceanic.less b/test/api/fixture/oceanic.less new file mode 100644 index 0000000..7e4881c --- /dev/null +++ b/test/api/fixture/oceanic.less @@ -0,0 +1,4 @@ +.color +{ + background: #1e2a35; +} diff --git a/test/api/processorOpts.js b/test/api/processorOpts.js new file mode 100644 index 0000000..3d0a69e --- /dev/null +++ b/test/api/processorOpts.js @@ -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'); + }); +});