Skip to content
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

feat!: use eslint-webpack-plugin #40

Merged
merged 2 commits into from
Oct 7, 2020
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
68 changes: 47 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,89 +57,113 @@ export default {

## Options

You can pass [eslint options](http://eslint.org/docs/developer-guide/nodejs-api#cliengine).
You can pass [eslint options](https://eslint.org/docs/developer-guide/nodejs-api#-new-eslintoptions).

**Note**: That the config option you provide will be passed to the `CLIEngine`. This is a different set of options than what you'd specify in `package.json` or `.eslintrc`. See the [eslint docs](http://eslint.org/docs/developer-guide/nodejs-api#cliengine) for more detail.
Note that the config option you provide will be passed to the `ESLint` class.
This is a different set of options than what you'd specify in `package.json` or `.eslintrc`.
See the [eslint docs](https://eslint.org/docs/developer-guide/nodejs-api#-new-eslintoptions) for more details.

### `cache`
**Warning**: In eslint-webpack-plugin version 1 the options were passed to the now deprecated [CLIEngine](https://eslint.org/docs/developer-guide/nodejs-api#cliengine).

- Type: `Boolean|String`
- Default: `false`
### `cache``

- Type: `Boolean`
- Default: `true`

This option will enable caching of the linting results into a file. This is particularly useful in reducing linting time when doing a full build.
**Note**: The cache is enabled by default to decrease execution time.

This can either be a `boolean` value or the cache directory path(ex: `'./.eslint-loader-cache'`).
### `context`

If `cache: true` is used, the cache is written to the `./node_modules/.cache/eslint-loader` directory. This is the recommended usage.
- Type: `String`
- Default: `srcDir`

A string indicating the root of your files.

### `eslintPath`

- Type: `String`
- Default: `eslint`

Path to `eslint` instance that will be used for linting. If the `eslintPath` is a folder like a official eslint, or specify a `formatter` option. Now you dont have to install `eslint`.
Path to `eslint` instance that will be used for linting.

### `files`

- Type: `String|Array[String]`
- Default: `'.'`

Specify directories, files, or globs. Must be relative to `options.context`.
Directories are traversed recursively looking for files matching `options.extensions`.
File and glob patterns ignore `options.extensions`.

### `extensions`

- Type: `Array[String]`
- Type: `String|Array[String]`
- Default: `['ts', 'js', 'vue']`

Extensions that will be used by the loader.
Specify extensions that should be checked.

### `fix`

- Type: `Boolean`
- Default: `false`

This option will enable [ESLint autofix feature](http://eslint.org/docs/user-guide/command-line-interface#fix).
Will enable [ESLint autofix feature](https://eslint.org/docs/developer-guide/nodejs-api#cliengineoutputfixes).

**Be careful: this option will change source files.**

### `formatter`

- Type: `String|Function`
- Default: `stylish`
- Default: `'stylish'`

Accepts a function that will have one argument: an array of eslint messages (object). The function must return the output as a string. You can use official [eslint formatters](https://eslint.org/docs/user-guide/formatters/).

### `lintDirtyModulesOnly`

- Type: `Boolean`
- Default: `true`

This option accepts a function that will have one argument: an array of eslint messages (object). The function must return the output as a string. You can use official [eslint formatters](https://eslint.org/docs/user-guide/formatters/).
Lint only changed files, skip lint on start.

### Errors and Warning

**By default the loader will auto adjust error reporting depending on eslint errors/warnings counts.** You can still force this behavior by using `emitError` **or** `emitWarning` options:
**By default the plugin will auto adjust error reporting depending on eslint errors/warnings counts.**
You can still force this behavior by using `emitError` **or** `emitWarning` options:

#### `emitError`

- Type: `Boolean`
- Default: `false`

Will always return errors, if this option is set to `true`.
Will always return errors, if set to `true`.

#### `emitWarning`

- Type: `Boolean`
- Default: `false`

Will always return warnings, if option is set to `true`.
Will always return warnings, if set to `true`.

#### `failOnError`

- Type: `Boolean`
- Default: `false`

Will cause the module build to fail if there are any errors, if option is set to `true`.
Will cause the module build to fail if there are any errors, if set to `true`.

#### `failOnWarning`

- Type: `Boolean`
- Default: `false`

Will cause the module build to fail if there are any warnings, if option is set to `true`.
Will cause the module build to fail if there are any warnings, if set to `true`.

#### `quiet`

- Type: `Boolean`
- Default: `false`

Will process and report errors only and ignore warnings, if this option is set to `true`.
Will process and report errors only and ignore warnings, if set to `true`.

#### `outputReport`

Expand All @@ -148,7 +172,9 @@ Will process and report errors only and ignore warnings, if this option is set t

Write the output of the errors to a file, for example a checkstyle xml file for use for reporting on Jenkins CI.

The `filePath` is an absolute path or relative to the webpack config: `output.path`. You can pass in a different `formatter` for the output file, if none is passed in the default/configured formatter will be used.
The `filePath` is an absolute path or relative to the webpack config: `output.path`.
You can pass in a different `formatter` for the output file,
if none is passed in the default/configured formatter will be used.

## Development

Expand Down
30 changes: 15 additions & 15 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ const logger = require('./logger')
const { moduleExists } = require('./utils')

module.exports = function (moduleOptions) {
if (!moduleExists('eslint')) {
logger.warn(
'The dependency `eslint` not found.',
'Please run `yarn add eslint --dev` or `npm install eslint --save-dev`'
)
return
}

const options = {
context: this.options.srcDir,
eslintPath: 'eslint',
extensions: ['ts', 'js', 'vue'],
cache: true,
lintDirtyModulesOnly: true,
...this.options.eslint,
...moduleOptions
}

if (!moduleExists(options.eslintPath)) {
logger.warn(
`The dependency \`${options.eslintPath}\` not found.`,
'Please run `yarn add eslint --dev` or `npm install eslint --save-dev`'
)
return
}

const filesToWatch = [
'.eslintrc',
'.eslintrc.json',
Expand All @@ -31,13 +35,9 @@ module.exports = function (moduleOptions) {

this.extendBuild((config, { isDev, isClient }) => {
if (isDev && isClient) {
config.module.rules.push({
enforce: 'pre',
test: RegExp(`\\.(${options.extensions.join('|')})$`),
loader: 'eslint-loader',
exclude: /(node_modules)/,
options
})
const EslintPlugin = require('eslint-webpack-plugin')

config.plugins.push(new EslintPlugin(options))
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"dependencies": {
"consola": "^2.15.0",
"eslint-loader": "^4.0.2"
"eslint-webpack-plugin": "^2.1.0"
},
"devDependencies": {
"@commitlint/cli": "latest",
Expand Down
Loading