postcss-reporter
Advanced tools
Comparing version 7.0.3 to 7.0.4
@@ -1,2 +0,2 @@ | ||
var { bold, underline, yellow } = require('nanocolors'); | ||
var pico = require('picocolors'); | ||
var path = require('path'); | ||
@@ -6,12 +6,18 @@ var sortBy = require('lodash.sortby'); | ||
var supportsLargeCharset = process.platform !== 'win32' || process.env.CI || process.env.TERM === 'xterm-256color'; | ||
var supportsLargeCharset = | ||
process.platform !== 'win32' || | ||
process.env.CI || | ||
process.env.TERM === 'xterm-256color'; | ||
var warningSymbol = supportsLargeCharset ? '⚠' : '!!'; | ||
module.exports = function(opts) { | ||
module.exports = function (opts) { | ||
var options = opts || {}; | ||
var sortByPosition = (typeof options.sortByPosition !== 'undefined') ? options.sortByPosition : true; | ||
var sortByPosition = | ||
typeof options.sortByPosition !== 'undefined' | ||
? options.sortByPosition | ||
: true; | ||
var positionless = options.positionless || 'first'; | ||
return function(input) { | ||
var messages = input.messages.filter(function(message) { | ||
return function (input) { | ||
var messages = input.messages.filter(function (message) { | ||
return typeof message.text === 'string'; | ||
@@ -25,3 +31,3 @@ }); | ||
messages, | ||
function(m) { | ||
function (m) { | ||
if (!m.line) return 1; | ||
@@ -32,7 +38,7 @@ if (positionless === 'any') return 1; | ||
}, | ||
function(m) { | ||
function (m) { | ||
if (!sortByPosition) return 1; | ||
return m.line; | ||
}, | ||
function(m) { | ||
function (m) { | ||
if (!sortByPosition) return 1; | ||
@@ -46,6 +52,6 @@ return m.column; | ||
if (source) { | ||
output += bold(underline(logFrom(source))) + '\n'; | ||
output += pico.bold(pico.underline(logFrom(source))) + '\n'; | ||
} | ||
orderedMessages.forEach(function(w) { | ||
orderedMessages.forEach(function (w) { | ||
output += messageToString(w) + '\n'; | ||
@@ -61,7 +67,7 @@ }); | ||
if (location.line) { | ||
str += bold(location.line); | ||
str += pico.bold(location.line); | ||
} | ||
if (location.column) { | ||
str += bold(':' + location.column); | ||
str += pico.bold(':' + location.column); | ||
} | ||
@@ -74,3 +80,3 @@ | ||
if (!options.noIcon && message.type === 'warning') { | ||
str += yellow(warningSymbol + ' '); | ||
str += pico.yellow(warningSymbol + ' '); | ||
} | ||
@@ -80,3 +86,3 @@ | ||
if (!options.noPlugin) { | ||
str += yellow(' [' + message.plugin + ']'); | ||
str += pico.yellow(' [' + message.plugin + ']'); | ||
} | ||
@@ -83,0 +89,0 @@ return str; |
var defaultFormatter = require('./formatter'); | ||
var { red, bold } = require('nanocolors'); | ||
var pico = require('picocolors'); | ||
var util = require('./util'); | ||
@@ -8,7 +8,9 @@ var groupBy = require('lodash.groupby'); | ||
module.exports = function(opts = {}) { | ||
var formatter = opts.formatter || defaultFormatter({ | ||
noIcon: opts.noIcon, | ||
noPlugin: opts.noPlugin, | ||
}); | ||
module.exports = function (opts = {}) { | ||
var formatter = | ||
opts.formatter || | ||
defaultFormatter({ | ||
noIcon: opts.noIcon, | ||
noPlugin: opts.noPlugin, | ||
}); | ||
@@ -18,6 +20,12 @@ var pluginFilter; | ||
// Every plugin | ||
pluginFilter = function() { return true; }; | ||
} else if (opts.plugins.every(function(plugin) { return plugin[0] === '!'; })) { | ||
pluginFilter = function () { | ||
return true; | ||
}; | ||
} else if ( | ||
opts.plugins.every(function (plugin) { | ||
return plugin[0] === '!'; | ||
}) | ||
) { | ||
// Deny list | ||
pluginFilter = function(message) { | ||
pluginFilter = function (message) { | ||
return opts.plugins.indexOf('!' + message.plugin) === -1; | ||
@@ -27,3 +35,3 @@ }; | ||
// Allow list | ||
pluginFilter = function(message) { | ||
pluginFilter = function (message) { | ||
return opts.plugins.indexOf(message.plugin) !== -1; | ||
@@ -33,7 +41,7 @@ }; | ||
var messageFilter = opts.filter || (message => message.type === 'warning'); | ||
var messageFilter = opts.filter || ((message) => message.type === 'warning'); | ||
return { | ||
postcssPlugin: 'postcss-reporter', | ||
OnceExit (css, { result }) { | ||
OnceExit(css, { result }) { | ||
var messagesToLog = result.messages | ||
@@ -43,6 +51,7 @@ .filter(pluginFilter) | ||
var resultSource = (!result.root.source) ? '' | ||
var resultSource = !result.root.source | ||
? '' | ||
: result.root.source.input.file || result.root.source.input.id; | ||
var sourceGroupedMessages = groupBy(messagesToLog, message => { | ||
var sourceGroupedMessages = groupBy(messagesToLog, (message) => { | ||
return util.getLocation(message).file || resultSource; | ||
@@ -52,3 +61,3 @@ }); | ||
var report = ''; | ||
forOwn(sourceGroupedMessages, function(messages, source) { | ||
forOwn(sourceGroupedMessages, function (messages, source) { | ||
report += formatter({ | ||
@@ -69,3 +78,2 @@ messages: messages, | ||
if (!report) return; | ||
@@ -76,3 +84,7 @@ | ||
if (opts.throwError && shouldThrowError()) { | ||
throw new Error(red(bold('\n** postcss-reporter: warnings or errors were found **'))); | ||
throw new Error( | ||
pico.red( | ||
pico.bold('\n** postcss-reporter: warnings or errors were found **') | ||
) | ||
); | ||
} | ||
@@ -82,4 +94,4 @@ | ||
return ( | ||
messagesToLog.length | ||
&& messagesToLog.some(message => { | ||
messagesToLog.length && | ||
messagesToLog.some((message) => { | ||
return message.type === 'warning' || message.type === 'error'; | ||
@@ -86,0 +98,0 @@ }) |
var get = require('lodash.get'); | ||
exports.getLocation = function(message) { | ||
exports.getLocation = function (message) { | ||
var messageNode = message.node; | ||
@@ -15,3 +15,4 @@ | ||
var originLocation = messageInput.origin && messageInput.origin(message.line, message.column); | ||
var originLocation = | ||
messageInput.origin && messageInput.origin(message.line, message.column); | ||
if (originLocation) return originLocation; | ||
@@ -18,0 +19,0 @@ |
{ | ||
"name": "postcss-reporter", | ||
"version": "7.0.3", | ||
"version": "7.0.4", | ||
"description": "Log PostCSS messages in the console", | ||
@@ -33,4 +33,4 @@ "main": "index.js", | ||
"lodash.sortby": "^4.7.0", | ||
"nanocolors": "^0.2.6" | ||
"picocolors": "^1.0.0" | ||
} | ||
} |
143
README.md
@@ -11,142 +11,3 @@ # postcss-reporter | ||
## Purpose | ||
As of PostCSS 4.1, a single PostCSS process can accumulate messages from all of the plugins it uses. | ||
Most of these messages are [warnings](https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#32-use-resultwarn-for-warnings). | ||
Presumably, plugin authors want you to see those messages. | ||
So this plugin exists to read the accumulated messages (or messages from only the plugins you've specified), format them, and print them to the console. | ||
By default, the messages are formatted for human legibility and sorted according to the line/column positions attached to the messages. But another formatting function can be passed in with an option, and sorting can be turned off with an option. | ||
*By default, only warnings are logged*. If you would like to see more messages, you can change the `filter` function. | ||
## Example Output | ||
![Example](example.png?raw=true) | ||
## Installation | ||
```shell | ||
npm install --save-dev postcss postcss-reporter | ||
``` | ||
Version 1.0.0+ is compatible with PostCSS 5+. (Earlier versions are compatible with PostCSS 4.) | ||
## Usage | ||
Add it to your plugin list *after any plugins whose messages you want to log*, and optionally pass it an object of options. | ||
For example, using [gulp-postcss](https://github.com/postcss/gulp-postcss): | ||
```js | ||
gulp.task('css', function() { | ||
return gulp.src('./src/*.css') | ||
.pipe(postcss([ | ||
bemLinter(), | ||
customProperties(), | ||
calc(), | ||
rejectAllColors(), | ||
reporter(myOptions) // <------ ding | ||
])) | ||
.pipe(gulp.dest('./dist')); | ||
}); | ||
``` | ||
## Options | ||
**clearReportedMessages** (boolean, default = `false`) | ||
If true, the plugin will clear the result's messages after it logs them. This prevents other plugins, or the task runner you use, from logging the same information again and causing confusion. | ||
See also `clearAllMessages`. | ||
**formatter** (function, default = the default formatter) | ||
By default, this reporter will format the messages for human legibility in the console. | ||
To use another formatter, pass a function that | ||
- accepts an object containing a `messages` array and a `source` string | ||
- returns the string to report | ||
For example, you could write a formatter like this: | ||
```js | ||
reporter({ | ||
formatter: function(input) { | ||
return input.source + ' produced ' + input.messages.length + ' messages'; | ||
} | ||
}) | ||
``` | ||
**plugins** (array of strings, default = `[]`) | ||
If `plugins` is empty (as it is by default), the reporter will log messages from every PostCSS plugin. | ||
There are 2 ways to limit output: | ||
- **Whitelist:** Provide an array of the plugins whose messages you would like to show. | ||
For example, `{ plugins: ['postcss-bem-linter'] }` will only log messages from the `postcss-bem-linter` plugin. | ||
- **Blacklist:** Prefix all plugins in the array with `!` to specify only those plugins whose messages you would like to hide. | ||
(All other plugins will be shown.) | ||
For example, `{ plugins: ['!postcss-bem-linter'] }` will never log messages from the `postcss-bem-linter` plugin; but will log messages from every other plugin. | ||
**filter** (function) | ||
Provide a filter function. It receives the message object and returns a truthy or falsy value, indicating whether that particular message should be reported or not. | ||
By default, only messages with `type: "warning"` are logged. (These are the messages produced when the plugin author uses PostCSS's `warn()` function.) | ||
For example, `function(message) { return true }` will return every message, regardless of whether or not the plugin declares it as a warning. | ||
**clearAllMessages** (boolean, default = `false`) | ||
If `true`, the plugin won't pass any messages into other plugins, or the task runner you use. | ||
See also `clearReportedMessages`. | ||
**throwError** (boolean, default = `false`) | ||
If `true`, after the plugin logs your messages it will throw an error if it found any warnings. | ||
**sortByPosition** (boolean, default = `true`) | ||
If `false`, messages will not be sorted by line/column position. | ||
**positionless** (`"first"|"last"|"any"`, default = `"first"`) | ||
By default, messages without line/column positions will be grouped at the beginning of the output. | ||
To put them at the end, instead, use `"last"`. | ||
To not bother sorting these, use `"any"`. | ||
**noIcon** (boolean, default = `false`) | ||
If `true`, no exclamatory triangle icons will be printed next to warnings. | ||
**noPlugin** (boolean, default = `false`) | ||
If `true`, plugin names will not be printed in brackets after messages. | ||
## How to get output without colors | ||
If you would like no colors in the console output, simply pass `--no-colors` when you invoke whatever command runs this plugin. (This works because of [chalk](https://github.com/sindresorhus/chalk).) | ||
## Standalone formatter | ||
You can also use this module's formatter as a library, with following API: | ||
```js | ||
var formatter = require('postcss-reporter/lib/formatter'); | ||
var myFormatter = formatter(myOptions); | ||
// to use defaults, just pass no options: `formatter()` | ||
var warningLog = myFormatter({ | ||
messages: someMessages, | ||
source: someSource | ||
}); | ||
console.log(warningLog); | ||
``` | ||
These are the formatter's options: | ||
- sortByPosition (boolean, default = `true`) | ||
- noIcon (boolean, default = `false`) - Do not print any warning exclamatory triangle icons | ||
- noPlugin (boolean, default = `false`) - Do not print plugin names | ||
## Docs | ||
Read **[full docs](https://github.com/postcss/postcss-reporter#readme)** on GitHub. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
173
7565
13
+ Addedpicocolors@^1.0.0
- Removednanocolors@^0.2.6
- Removednanocolors@0.2.13(transitive)