简体   繁体   English

在 .eslintrc.js 文件中使用导出默认值而不是 module.exports

[英]Use export default in .eslintrc.js file instead of module.exports

I would like to use export default obj instead of module.exports = obj in my .eslintrc.js file, because everywhere else in the codebase we are using export .我想在我的.eslintrc.js文件中使用export default obj而不是module.exports = obj ,因为代码库中的其他地方我们都在使用export

So far no luck, it was difficult to search for this problem.到目前为止没有运气,很难搜索这个问题。

The error I get:我得到的错误:

> eslint src

Cannot read config file: src/.eslintrc.js
Error: Unexpected token export
src/.eslintrc.js:23
export default foo;
^^^^^^

SyntaxError: Unexpected token export

To use the ES2015 syntax for default exports in an ESLint config file, one can use a combination of Babel for transpilation and Pirates for hooks to hijack a require statement that imports your ESLint config file written in ES2015.要在 ESLint 配置文件中使用 ES2015 语法进行默认导出,可以将 Babel 用于转译和 Pirates 用于钩子的组合来劫持导入 ES2015 编写的 ESLint 配置文件的require语句。 Allow me to explain.请允许我解释一下。

Usually, ESLint will look for a .eslintrc.js file in the project root, and it usually is parsed by Node.js without Babel.通常,ESLint 会在项目根目录中寻找一个.eslintrc.js文件,它通常由 Node.js 解析而不使用 Babel。 We will be repurposing this file so that it does two things: registering a hook and importing the ES2015 ESLint config file.我们将重新利用这个文件,让它做两件事:注册一个钩子和导入 ES2015 ESLint 配置文件。

Assuming you already have a .eslintrc.js using the ES2015 default export syntax, cut the contents out of that file and paste it into a new file named .es2015lintrc.js or similar.假设你已经有一个.eslintrc.js使用ES2015默认的导出语法,切内容指出,文件并将其粘贴到一个新的文件名为.es2015lintrc.js或相似。 The name is irrelevant;名称无关紧要; it can be called anything you want.它可以被称为任何你想要的。

// .es2015lintrc.js

export default {

// ESLint config properties...

}

Make sure you have the @babel/preset-env , @babel/core , eslint , and pirates packages installed before continuing.请确保您有@babel/preset-env@babel/coreeslintpirates继续之前安装的软件包。 Next, create a file that defines your hook and its behavior.接下来,创建一个定义钩子及其行为的文件。 We'll call it hook.js for simplicity.为简单起见,我们将其称为hook.js

// hook.js

const {addHook} = require('pirates');
const {transform} = require('@babel/core');

module.exports = options =>
  addHook(
    function(source, filename) {
      return transform(source, {
        presets: [
          [
            '@babel/preset-env',
            {
              modules: 'cjs',
              targets: {
                node: process.versions.node
              }
            }
          ]
        ]
      }).code;
    },
    {
      exts: ['.js'],
      ignoreNodeModules: true,
      ...options
    }
  );

Then import this module into the original .eslintrc.js using a require statement and register the hook that hijacks and transforms all files imported with future require statements.然后使用require语句将此模块导入原始.eslintrc.js并注册钩子,该钩子可以劫持和转换所有使用未来的require语句导入的文件。 Finally, the ESLint config file written in ES2015 syntax can be imported using our hijacked require statement.最后,可以使用我们劫持的require语句导入以 ES2015 语法编写的 ESLint 配置文件。

// .eslintrc.js

const registerHook = require('./hook');
registerHook();

module.exports = require('./.es2015lintrc').default;

Now you should be good to go!现在你应该可以走了! One can use this same method when writing a Babel config in ES2015 syntax too.在用 ES2015 语法编写 Babel 配置时也可以使用相同的方法。 Enjoy!享受!

If you already have @babel/register installed, then just rename your .eslintrc.js to .eslintrc.babel.js (or whatever you like) and add this to .eslintrc.js :如果您已经安装了@babel/register ,那么只需将您的.eslintrc.js重命名为.eslintrc.babel.js (或您喜欢的任何名称)并将其添加到.eslintrc.js

require('@babel/register')
module.exports = require('./.eslintrc.babel.js').default

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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