My project uses Webpack and es6 modules for most of the files. These files run in browser, and bundled by Webpack.
There are just a small number of files, that run in node. They are not touched by Webpack and I don't see any benefit including them in webpack. They don't support import
as it's not implemented yet in node (or V8).
Now in .eslintrc
, if I set parserOptions.sourceType
to script
, it errs out in the browser files ("import
and export
only allowed in module!"). If parserOptions.sourceType
set to module
, it errs out in node files.
So how to do per-file parserOptions
? /* eslint-env xxx */
doesn't work in this case
I can probably use directory-specific .eslintrc
, but that would mean duplicate all other configs for the sake of changing just one option. Any better option?
Another option is using overrides
as such :
{
"extends": "eslint:recommended",
"overrides": [{
"files": ["path/to/some/file.js", "path/to/some/folder/**.js"],
"parserOptions": {
"sourceType": "module"
}
}]
}
See https://eslint.org/docs/user-guide/configuring#example-configuration
The benefits of such a solution :
You should be able to take advantage of the hierarchical nature of ESLint configuration (.eslintrc
) files:
ESLint will automatically look for them in the directory of the file to be linted, and in successive parent directories all the way up to the root directory of the filesystem. This option is useful when you want different configurations for different parts of a project or when you want others to be able to use ESLint directly without needing to remember to pass in the configuration file.
Note that .eslintrc
files in child directories inherit the configurations from .eslintrc
files in parent directories, so it's easy to override particular settings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With