当前位置: 首页 > 知识库问答 >
问题:

Webpack投掷错误"你可能需要一个合适的加载器"在与打字稿反应

夏和雅
2023-03-14

我试图用webpack、react和typescript设置一个新项目。但我无法让它工作。

我尝试了html加载器,并尝试了各种教程来解决这个问题。

网页包配置:

const path = require("path");

module.exports = {
  entry: {
    app: ["./src/app"]
  },
  module: {
    rules: [
      {
        test: /\.(ts|js)x?$/,
        use: [
          {
            loader: "ts-loader",
            options: {
              transpileOnly: true,
              compilerOptions: {
                module: "es2015",
                allowJs: true
              }
            }
          }
        ],
        include: [path.resolve(path.resolve(__dirname, "../"), "./src")]
      }
    ]
  },
  resolve: {
    extensions: [".js", ".jsx", ".ts", ".tsx"]
  }
};

tsconfig.json

{
    "compilerOptions": {
        "baseUrl": ".",
        "moduleResolution": "node",
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": false,
        "jsx": "react",
        "allowJs": true,
        "noImplicitThis": true,
        "removeComments": false,
        "skipDefaultLibCheck": true,
        "skipLibCheck": true,
        "lib": ["dom", "es5", "scripthost", "es2015", "es2017"],
        "downlevelIteration": true,
        "allowSyntheticDefaultImports": true,
        "experimentalDecorators": true,
        "esModuleInterop": true,
        "outDir": "./dist/"
    }
}

app.tsx

import * as React from "react";
import * as ReactDOM from "react-dom";
ReactDOM.render(<></>, document.getElementById("root"));

但是,在编译时:webpack-模式生产:-抛出错误:

./src/app.tsx 4:16模块解析中出错失败:意外令牌(4:16)您可能需要适当的加载程序来处理此文件类型,目前没有配置加载程序来处理此文件。看见https://webpack.js.org/concepts#loaders |从“react dom”导入*作为react dom//从“/pages/Home”导入{Home};

ReactDOM.render(

共有1个答案

武彭薄
2023-03-14

我尝试了它,这包括是问题。包括:[path.resolve(path.resolve(__dirname),"。/src")]...没有必要从Dirname返回,它会工作。

信用证:@JurajKocan

 类似资料: