无法从.ts文件中导入.tsx文件

时间:2019-01-11 13:53:17

标签: reactjs typescript webpack tsx

我正在尝试像这样从.tsx文件中导入.ts组件:

src / index.ts:

import { Icon } from './components/Icon/Icon';

但是我遇到了这个错误:

./src/index.ts
Module not found: Can't resolve './components/Icon/Icon' in '/Applications/project/src'

这些是代码的导入部分:

src / components / Icon / Icon.tsx:

export class Icon extends Component<Props> {
   /* component code here */
}

webpack.config.js:

/* code */
resolve: {
   extensions: ['.js', '.json', '.ts', '.tsx'],
},
/* more code */
{
    test: /\.jsx?$/,
    exclude: /node_modules/,
    use: 'babel-loader',
},
{
    test: /\.tsx?$/,
    exclude: /node_modules/,
    use: [
        {
            loader: 'awesome-typescript-loader',
            options: {
                query: {
                   declaration: false,
                },
            },
         },
     ],
},

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "jsx": "react",
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "strictFunctionTypes": true,
    "noImplicitReturns": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "alwaysStrict": true,
    "moduleResolution": "node",
    "declaration": true,
    "strictNullChecks": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts", "**/*.spec.tsx"]
}

0 个答案:

没有答案