如何避免使用无限制进口的相对进口

发布于 2025-01-24 04:33:09 字数 740 浏览 2 评论 0原文

我想要的是

我要避免使用../../../的深度相对导入,我只想允许相对导入package.json file ,因此我设置了Eslint 无限制的Imports这样的规则:

.eslintrc.js

module.exports = {
  rules: {
    'no-restricted-imports': [
      'error',
      {
        patterns: [
          {
            group: ['**/../../*', '!**/../../package.json'],
            message: 'Please use alias instead of deep relative imports.',
          },
        ],
      },
    ],
  },
};

此配置的问题

是,它正在从package.json 也是如此,这是丢弃错误:导入 *作为packagejson从'../../../../ pockage.json';

我如何避免除> 以外的所有相对导入package.json一个?

What I want

I want to avoid the deep relative imports like ../../../, and I want to allow only the relative import of package.json file, so I set up the eslint no-restricted-imports rule like so:

.eslintrc.js

module.exports = {
  rules: {
    'no-restricted-imports': [
      'error',
      {
        patterns: [
          {
            group: ['**/../../*', '!**/../../package.json'],
            message: 'Please use alias instead of deep relative imports.',
          },
        ],
      },
    ],
  },
};

The problem

The problem with this configuration is that it is taking the imports from package.json as well, so this is throwing error: import * as PackageJSON from '../../../package.json';

how can I avoid all relative imports except the package.json one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文