如果我在 tsconfig 中使用自定义路径,Webpack Dev Server 将无法找到带有 Typescript 的模块 [英] Webpack Dev Server cannot find modules with Typescript if I use custom paths in tsconfig

查看:34
本文介绍了如果我在 tsconfig 中使用自定义路径,Webpack Dev Server 将无法找到带有 Typescript 的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 React + TypeScript 并使用 Webpack Dev 服务器从头开始构建一个项目.我想使用组件的相对路径,这对于更灵活的开发来说并不严格.

I'm building a project from scratch on React + TypeScript and using the Webpack Dev server. I want to use relative paths to components, which will be not strict for a more flexible development.

在我的 App.tsx 中,我正在尝试导入组件:

In my App.tsx, I'm trying to import component:

import EntityTypes from "components/EntityTypes/EntityTypes";

然后出现错误

Module not found: Error: Can't resolve 'components/EntityTypes/EntityTypes' in '/home/eugene/prj/work/crud-react/src'

此路径的文件存在(/src/components/EntityTypes/EntityTypes.js)并导出类

File by this path exists (/src/components/EntityTypes/EntityTypes.js) and exports the class like

export default EntityTypes;

我的 tsconfig.json:

My tsconfig.json:

{
  "compilerOptions": {
    "sourceMap": true,
    "noImplicitAny": false,
    "module": "commonjs",
    "target": "es5",
    "lib": [
      "es2015",
      "es2017",
      "dom"
    ],
    "removeComments": true,
    "allowSyntheticDefaultImports": false,
    "jsx": "react",
    "allowJs": true,
    "baseUrl": ".",
    "paths": {
      "components/*": [
        "src/components/*"
      ]
    }
  }
}

webpack.config.js:

webpack.config.js:

const HtmlWebPackPlugin = require( 'html-webpack-plugin' );
const path = require( 'path' );
module.exports = {
    context: __dirname,
    entry: './src/index.js',
    resolve: {
        extensions: ['.ts', '.tsx', '.js', '.jsx']
    },
    output: {
        path: path.resolve( __dirname, 'dist' ),
        filename: 'main.js',
        publicPath: '/',
    },
    devServer: {
        historyApiFallback: true
    },
    module: {
        rules: [
            {
                test: /\.(tsx|ts)?$/,
                loader: 'awesome-typescript-loader'
            },
            {
                test: /\.js$/,
                use: 'babel-loader',
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader'],
            },
            {
                test: /\.(png|j?g|svg|gif)?$/,
                use: 'file-loader'
            }
        ]
    },
    plugins: [
        new HtmlWebPackPlugin({
            template: path.resolve( __dirname, 'public/index.html' ),
            filename: 'index.html'
        })
    ]
};

我已经检查了关于路径"的打字稿文档,文件存在,我不明白是什么问题.

I've checked the documentation of typescript about "paths", the file is exists, I don't understand what the problem.

推荐答案

我已经整理出如何使用我的文件的路径并使它们变得灵活,这样我就可以轻松地重新组织项目和管理路径,而无需进行大量严格的更改相对路径.

I have sorted out how to use paths for my files and make them flexible, that I can reorganize the project and manage paths easy without a lot of changes of strict relative paths.

问题与 webpack 的配置有关.接下来是最终配置:

The problem was related to configuration of the webpack. Final configuration is next:

tsconfig.json

{
  "compilerOptions": {
    // ...
    "paths": {
      "~/components/*": [
        "./src/components/*"
      ],
      "~/services/*": [
        "./src/services/*"
      ],
      "~/interfaces/*": [
        "./src/interfaces/*"
      ]
    },
  }
}

webpack.config.js

const path = require('path');
module.exports = {
  // ...
  resolve: {
    // ...
    alias: {
      '~/components': path.resolve(process.cwd(), './src/components'),
      '~/interfaces': path.resolve(process.cwd(), './src/interfaces'),
      '~/services': path.resolve(process.cwd(), './src/services'),
    }
  },
}

使用示例

import {IEntities} from "~/interfaces/entities.interface";
// ...
import EntityForm from "~/components/EntityForm/EntityForm";

这篇关于如果我在 tsconfig 中使用自定义路径,Webpack Dev Server 将无法找到带有 Typescript 的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆