首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack5 ts-loader无法编译react文件

Webpack5 ts-loader无法编译react文件
EN

Stack Overflow用户
提问于 2021-11-12 03:09:52
回答 1查看 132关注 0票数 0

文件夹结构:

在图片中,编译器说有一个问题,App引用了一个值balabala。当我运行npm run serve时,它也失败了,并且不能成功编译。如何修复它?

webpack.config:

代码语言:javascript
复制
const path = require('path')
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
  mode: 'development',
  devtool: 'source-map',
  entry: './src/index.ts',
  output: {
    filename: 'js/build.js',
    path: path.resolve(__dirname, 'dist'),
  },
  devServer: {
    open: true,
  },
  resolve: {
    alias: {
      "@": path.resolve(__dirname, 'src'),
    },
    extensions: ['.ts', '.tsx', '.js', '.jsx'],
  },
  module: {
    rules: [
      { test: /\.tsx?$/, loader: "ts-loader" },
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      title: 'My Test Webpack',
      template: './public/index.html'
    })
  ]
}

tsconfig.json:

代码语言:javascript
复制
{
  "compilerOptions": {
    "sourceMap": true,
    "target": "es6",
    "jsx": "react",
    "module": "esnext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "declaration": true,
    "noImplicitAny": false,
    "noImplicitReturns": false,
    "noUnusedLocals": false,
    "removeComments": true,
    "strictNullChecks": false,
    "outDir": "./dist/",
    "preserveConstEnums": true,
    "noLib": false,
    "rootDirs": ["src", "test"],
    "lib": ["es6", "dom"]
  },
  "include": ["src/**/*"],
  "exclude": ["dist", "node_modules", "static"]
}

package.json

代码语言:javascript
复制
{
  "name": "webpack5test",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "@babel/plugin-transform-runtime": "^7.16.0",
    "@babel/preset-react": "^7.16.0",
    "browserslist": "^4.17.5",
    "core-js": "^3.19.1",
    "file-loader": "^6.2.0",
    "less": "^4.1.2",
    "less-loader": "^10.2.0",
    "postcss": "^8.3.11",
    "postcss-loader": "^6.2.0",
    "postcss-preset-env": "^6.7.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "regenerator-runtime": "^0.13.9",
    "ts-loader": "^9.2.6",
    "typescript": "^4.4.4",
    "url-loader": "^4.1.1",
    "webpack": "^5.60.0",
    "webpack-cli": "^4.9.1"
  },
  "scripts": {
    "serve": "webpack serve",
    "build": "webpack --mode=development",
    "build:watch": "webpack --mode=development --watch"
  },
  "devDependencies": {
    "@babel/cli": "^7.16.0",
    "@babel/core": "^7.16.0",
    "@babel/plugin-transform-arrow-functions": "^7.16.0",
    "@babel/plugin-transform-block-scoping": "^7.16.0",
    "@babel/preset-env": "^7.16.0",
    "@types/react": "^17.0.34",
    "@types/react-dom": "^17.0.11",
    "babel-loader": "^8.2.3",
    "clean-webpack-plugin": "^4.0.0",
    "copy-webpack-plugin": "^9.0.1",
    "css-loader": "^6.5.0",
    "html-webpack-plugin": "^5.5.0",
    "style-loader": "^3.3.1",
    "webpack-dev-server": "^4.4.0"
  }
}
EN

回答 1

Stack Overflow用户

发布于 2021-11-12 04:45:00

您正试图在常规typescript ts文件中使用jsx/tsx语法。将index.ts重命名为index.tsx,然后将Webpack配置中的entry修改为'./src/index.tsx'

或者,如果您希望继续使用index.ts文件的.ts扩展名,则可以手动创建vDOM元素,如下所示:

代码语言:javascript
复制
import * as React from 'react';

ReactDOM.render(
  React.createElement(App),
  document.getElementById('app'));
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69937492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档