-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
75 lines (63 loc) · 1.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict'
var Standard = require('standard')
var format = require('util').format
var loaderUtils = require('loader-utils')
var snazzy = require('snazzy')
var assign = require('object-assign')
module.exports = function standardLoader (input, map) {
var webpack = this
var callback = webpack.async()
webpack.cacheable()
var config = assign({}, loaderUtils.getOptions(webpack))
config.filename = webpack.resourcePath
let standard = Standard
// allow configurable 'standard' e.g. standardx
if (config.standard) {
if (typeof config.standard === 'string') {
standard = require(config.standard)
} else {
standard = config.standard
}
}
delete config.standard
standard.lintText(input, config, function (err, result) {
if (err) return callback(err, input, map)
if (result.errorCount === 0) return callback(null, input, map)
var warnings = result.results.reduce(function (items, result) {
return items.concat(result.messages.map(function (message) {
return format(
'%s:%d:%d: %s%s',
result.filePath, message.line || 0, message.column || 0, message.message,
!config.verbose ? ' (' + message.ruleId + ')' : ''
)
}))
}, [])
if (config.snazzy !== false) {
snazzy({ encoding: 'utf8' })
.on('data', function (data) {
emit(new StandardJSError(data))
})
.end(warnings.join('\n'))
} else {
warnings.forEach(function (warning) {
emit(new StandardJSError(warning))
})
}
callback(null, input, map)
})
function emit (data) {
if (config.error) return webpack.emitError(data)
webpack.emitWarning(data)
}
}
class StandardJSError extends Error {
constructor (messages) {
super()
this.name = 'StandardJSError'
this.message = messages
this.stack = ''
}
inspect () {
return this.message
}
}