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

react with typescript中的Webpack抛出错误“您可能需要适当的加载程序”

毋树
2023-03-14

我尝试用webpack、react、typescript建立一个新的项目。但我不能让它工作。

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"]
  }
};
{
    "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/"
    }
}
import * as React from "react";
import * as ReactDOM from "react-dom";
ReactDOM.render(<></>, document.getElementById("root"));

./src/app.tsx 4:16模块分析失败中的错误:意外标记(4:16)您可能需要适当的加载程序来处理此文件类型,当前没有配置任何加载程序来处理此文件。参见https://webpack.js.org/concepts#loaders import*as ReactDOM from“react-dom”;//从“./pages/Home”导入{Home};

ReactDom.Render(<>,Document.GetElementById(“root”));@multi./src/app app[0]

共有1个答案

夹谷晋
2023-03-14

我试过了,这包括是问题所在。包括:[path.resolve(Path.resolve(__dirname),“./src”)]...不需要从dirname返回,它就能工作。

图片来源:@Jurajkocan

 类似资料: