简体   繁体   English

如何在 eslintrc 文件中使用导入?

[英]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

  1. Install the esm module, npm i esm -D (Here I'm choosing as a devDependency ).安装esm模块, npm i esm -D (这里我选择作为devDependency )。
  2. Create a new file as a sibling to .eslintrc.js called .eslintrc.esm.js .创建一个名为.eslintrc.esm.js的新文件作为.eslintrc.js的同级文件。
  3. Inside of .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 }
  4. Inside .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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM