[英]How to use import inside eslintrc file?
I'm trying to use imported object to setup some restrictions for globals inside .eslintrc.js file, but import doesnt work.我正在尝试使用导入的对象为 .eslintrc.js 文件中的全局变量设置一些限制,但导入不起作用。 How can i make dynamic eslint config?如何进行动态 eslint 配置?
import {loadedGlobals} from '@/util/globals'
module.exports = {
'globals': Object.keys(loadedGlobals).reduce((acum, key) => {
acum[key] = false
return acum
}, acum),
// ...
}
You might notice that you are using the old syntax when exporting your object.您可能会注意到在导出对象时使用的是旧语法。 You could try using require()
instead of import.您可以尝试使用require()
而不是 import。
Alternatively, you could look into Shareable Configs .或者,您可以查看Shareable Configs 。
How to use import inside eslintrc file?如何在 eslintrc 文件中使用导入?
ESLint currently doesn't support a configuration file by the name of eslintrc
so I'm going to assume you mean .eslintrc.js
. ESLint 目前不支持名为eslintrc
的配置文件,所以我假设你的意思是.eslintrc.js
。
ESLint currently does not support ES Modules as you can see from the JavaScript (ESM) bullet item on their configuration file formats documentation. ESLint 当前不支持 ES 模块,您可以从其配置文件格式文档中的JavaScript (ESM)项目符号中看到。
If you are willing to install another dependency here is how you can use import
inside of .eslintrc.js
:如果您愿意在此处安装另一个依赖项,您可以在.eslintrc.js
使用import
的.eslintrc.js
:
esm
module, npm i esm -D
(Here I'm choosing as a devDependency
).安装esm
模块, npm i esm -D
(这里我选择作为devDependency
)。.eslintrc.js
called .eslintrc.esm.js
.创建一个名为.eslintrc.esm.js
的新文件作为.eslintrc.js
的同级文件。.eslintrc.esm.js
include your ESLint configuration.在.eslintrc.esm.js
里面包含你的 ESLint 配置。 Here you can use import
and you should export your configuration as export default { // Your config }
.在这里您可以使用import
并且您应该将您的配置export default { // Your config }
为export default { // Your config }
。.eslintrc.esm.js
include the following code:在.eslintrc.esm.js
包含以下代码:require = require('esm')(module)
module.exports = require('./.eslintrc.esm').default
Now you should be able to run eslint
as usual.现在你应该可以像往常一样运行eslint
。 A bit clunky with the extra file, but you can organize them in a directory if you like and use the --config
option of eslint
to point to the new location.额外的文件有点笨重,但是如果您愿意,可以将它们组织在一个目录中,并使用eslint
的--config
选项指向新位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.