eslint-plugin-import: import/no-unused-modules can't recognize loading rules with moduleSuffixes

I use this rule in my multiplatform app and I load specific parts of code by moduleSuffixes. And I get warnings in platform-specific modules.

my .tsconfig

{
  "compilerOptions": {
    "jsx": "react",
    ...
    "moduleResolution": "node",
    "moduleSuffixes": [".p2", ".connect", ""]
  },
  "include": ["src/**/*", "jest/*"],
  "exclude": ["node_modules"]
}

my webpack config

export const getBaseConfig = (
  options: IWebpackConfigOptions,
): Configuration => {
  // target is launching platform name 
  const {target, isDev} = options
  /*...*/
  const baseConfig: Configuration = {
    entry: {/*...*/},
    stats: {
      children: true,
    },
    cache: true,
    optimization: {/*...*/},
    resolve: {
      extensions: [`.${target}.ts`, '.ts', '.tsx', '.js', '.jsx'],
      alias: {
        // allows to import from 'src/....'
        src: srcDir,
      },
    },
    /*...*/
  }
  if (target === 'connect') {
    plugins.push(...getCloudWebpackPlugins(isDev))
  }
  if (target === 'p2') {
    plugins.push(...getServerWebpackPlugins())
  }

  if (isDev) {
    baseConfig['devServer'] = getDevserverConfig(target)
  }
  return baseConfig
}

then I import some specific module from shared code as

  import {myModule} from './my-module'

where module is defined as

./my-module.p2.ts

  // this export is recognized as used
  export const myModule = () => 'p2'

./my-module.connect.ts

  // here I have to add `eslint-disable-next-line import/no-unused-modules`
  export const myModule = () => 'connect'

I see that it’s an unusual way of importing configured by webpack, but if the import/no-unused-modules rule had ignorePattern option, I would pass it as ‘*.connect.ts’ and it would be enough

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 20 (8 by maintainers)

Most upvoted comments

I’ve made simple resolver package to solve that (and that) issue.

Doing some digging - it looks like it doesn’t. It’s a relatively new TS feature (TS 4.7 - May 2022) and it’s only really used by the react native community - so it’s not surprising that support hasn’t been added yet.

I’d file an issue over at eslint-import-resolver-typescript so they can add support!