Search code examples
reactjstypescriptwebpackwebpack-4ts-loader

Attempted import error: 'SomeObject' is not exported from file


I've upgraded to Webpack 4 from 3. Since then, I get a lot of warnings regarding imports that were not exported from certain files.

./packages/utils/logging/index.ts
Attempted import error: ‘Options' is not exported from './loggers/log'.
 @ ./packages/utils/index.ts
 @ ./src/App.tsx
 @ multi whatwg-fetch @babel/polyfill ./src/App.tsx

I use ts-loader along with ForkTsCheckerWebpackPlugin. I've examined the exports from the warnings and they look good. The code does work, however I still get those warnings.

tsconfig.json for reference:

{
  "compilerOptions": {
    "outDir": "build/dist",
    "module": "esnext",
    "target": "es5",
    "lib": ["es6", "dom"],
    "baseUrl": ".",
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react",
    "moduleResolution": "node",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true,
    "resolveJsonModule": true
  },
  "include": ["packages/**/*"],
  "exclude": [
    "node_modules",
    "build",
    "scripts",
    "webpack",
    "**/__tests__/*"
  ]
}

Webpack config:

...
module: {
    rules: [
      {
        test: /\.(ts|tsx|d.ts)$/,
        exclude: /node_modules/,
        use: {
          loader: 'ts-loader',
          options: {
            transpileOnly: true,
          },
        },
      },
      {
        test: /\.(js|jsx|mjs)$/,
        include: [paths.packagesSrc, ...paths.modulesToTranspile],
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader?cacheDirectory=true',
          options: {
            configFile: paths.configFiles.babel,
          },
        },
      },
      ...
    ]
},
plugins: [
    ...
    new ForkTsCheckerWebpackPlugin({
      async: options.asyncTypeChecking,
      checkSyntacticErrors: true,
      tsconfig: paths.appTsConfig,
      watch: paths.packagesSrc,
    }),
    ...
],
...

Solution

  • Seems like there is a problem with ts-loader in Webpack 4 when re-exporting types in Typescript.
    The warnings can be ignored.

    Reference