Skip to content

Commit

Permalink
Add support for configurable 'standard' e.g. 'standardx'
Browse files Browse the repository at this point in the history
  • Loading branch information
timoxley committed Mar 3, 2018
1 parent 213fbe7 commit 9956901
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 17 deletions.
31 changes: 20 additions & 11 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ npm install --save-dev standard-loader standard

## Usage

### Webpack 1

Webpack 1.x is no longer supported as of [`standard-loader` version `6.0.0`](https://github.com/timoxley/standard-loader/compare/5.0.0...6.0.0). PRs for Webpack 1.x support will be accepted on the [5.x branch](https://github.com/timoxley/standard-loader/tree/5.x).

### Webpack 2
### Webpack 2+

```js
// webpack.config.js
Expand All @@ -39,11 +35,7 @@ const config = {
loader: 'standard-loader',
exclude: /(node_modules|bower_components)/,
options: {
// Emit errors instead of warnings (default = false)
error: false,
// enable snazzy output (default = true)
snazzy: true,
// other config options to be passed through to standard e.g.
// config options to be passed through to standard e.g.
parser: 'babel-eslint'
}
},
Expand All @@ -52,9 +44,26 @@ const config = {
}
}

module.exports = config
```

### Webpack 1

Webpack 1.x is no longer supported as of [`standard-loader` version `6.0.0`](https://github.com/standard/standard-loader/compare/5.0.0...6.0.0). PRs for Webpack 1.x support will be accepted on the [5.x branch](https://github.com/standard/standard-loader/tree/5.x).

### Configuration Options

```js
{
// Emit errors instead of warnings (default = false)
error: false,
// enable snazzy output (default = true)
snazzy: true,
// configure alternative checker e.g. 'standardx' (default = 'standard')
standard: 'standard',
// all other config options are passed through to standard e.g.
parser: 'babel-eslint'
}
```

### Example Input

Expand Down
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

var standard = require('standard')
var Standard = require('standard')
var format = require('util').format
var loaderUtils = require('loader-utils')
var snazzy = require('snazzy')
Expand All @@ -9,10 +9,22 @@ 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
webpack.cacheable()
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)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"babel-loader": "^6.3.2",
"node-libs-browser": "^2.0.0",
"standard": "^9.0.0",
"standardx": "^1.0.3",
"tape": "^4.6.3",
"webpack": "^2.2.1"
},
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"semi": 0
}
}
42 changes: 42 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,48 @@ test('can disable snazzy output', function (t) {
})
})

test('can work with standardx', function (t) {
var preloader = assign({}, config.module.rules[0], {
options: {
standard: 'standardx'
}
})

const old = config.module.rules[0]
config.module.rules[0] = preloader
webpack(config, function (err, stats) {
t.ifError(err)
t.ok(!stats.compilation.errors.length, 'has no errors')
t.ok(stats.compilation.warnings.length, 'has some warnings')
t.ok(!stats.compilation.warnings.some(function (warning) {
return /semicolon/gm.test(warning.message)
}), 'has no error about semicolon')
t.end()
})
config.module.rules[0] = old
})

test('can work with standardx as a module', function (t) {
var preloader = assign({}, config.module.rules[0], {
options: {
standard: require('standardx')
}
})

const old = config.module.rules[0]
config.module.rules[0] = preloader
webpack(config, function (err, stats) {
t.ifError(err)
t.ok(!stats.compilation.errors.length, 'has no errors')
t.ok(stats.compilation.warnings.length, 'has some warnings')
t.ok(!stats.compilation.warnings.some(function (warning) {
return /semicolon/gm.test(warning.message)
}), 'has no error about semicolon')
t.end()
})
config.module.rules[0] = old
})

test('logs error', function (t) {
var preloader = assign({}, config.module.rules[0], {
options: {
Expand Down
Loading

0 comments on commit 9956901

Please sign in to comment.