You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何合并eslint-plugin-import的多路径pattern以简化import排序配置?

如何合并ESLint import/order中的多个TypeScript别名路径配置

可以通过glob匹配语法的大括号分组来合并多个路径别名的pattern,让配置更简洁,注意不要用正则的|分隔(因为import/order的pattern字段遵循node-glob规则,而非正则表达式)。

正确的简洁配置写法

将多个别名路径用大括号{}包裹、内部用逗号分隔,即可一次性匹配多个前缀:

"import/order": [
  "error",
  {
    "groups": ["builtin", "external", "internal"],
    "pathGroups": [
      {
        "pattern": "react*",
        "group": "external",
        "position": "before"
      },
      {
        "pattern": "{Components,Constants,AnotherAlias}/**",
        "group": "internal"
      }
    ],
    "pathGroupsExcludedImportTypes": ["react", "internal"],
    "alphabetize": {
      "order": "asc",
      "caseInsensitive": true
    }
  }
]

关键说明

  • {Components,Constants,AnotherAlias}/**是标准glob语法,代表匹配以Components/、Constants/或AnotherAlias/开头的任意路径,效果和之前分开写的多个pattern条目完全一致。
  • 若你的别名是精确匹配(比如直接导入Components而非Components/**),可将pattern写成{Components,Constants,AnotherAlias}。
  • 保持pathGroupsExcludedImportTypes中的internal配置,确保这些别名路径不会被误识别为外部包。

内容的提问来源于stack exchange,提问作者user3284707

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:00:03