简体   繁体   English

ESLint - 首选导出默认值到 Module.Exports

[英]ESLint - Prefer Export Default to Module.Exports

I was wondering if an ESLint rule exists, or how to make one, that does the following:我想知道是否存在 ESLint 规则,或者如何制定一个,它执行以下操作:

Allows exports only in the form export default foo and not in the form module.exports = foo仅允许以export default foo形式导出,而不允许以module.exports = foo形式导出

Is there a way to do this?有没有办法做到这一点?

There are no core rules that can do this, but the following plugin rule might be what you are looking for:没有可以做到这一点的核心规则,但以下插件规则可能是您正在寻找的:

https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-commonjs.md https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-commonjs.md

It will report any usage of CommonJS-style modules:它将报告 CommonJS 样式模块的任何使用情况:

Invalid :无效

/*eslint no-commonjs: "error"*/
module.exports = foo;

Valid :有效

/*eslint no-commonjs: "error"*/
export default foo;

module.exports is specific to Node. module.exports 是特定于 Node 的。 so add it to the env, like below所以将它添加到环境中,如下所示

env: {
    browser: true,
    node: true,
    es2021: true,
},

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

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