typescript - 当 Webpack ts-loader 显示 "Module parse failed: Unexpected token"时,tsconfig.json jsx 被保留

标签 typescript jsx ts-loader

我想用webpack将.tsx转为.jsx,查了些资料发现只要在tsconfig.json中设置jsx保留,但是webpack ts-loader解析失败,显示错误代码:

ERROR in ./index.tsx 4:15
Module parse failed: Unexpected token (4:15)
You may need an appropriate loader to handle this file type.
| export default class Home extends React.Component {
|     render() {
>         return <div>
|       <p>hello world</p>
|     </div>;

如果jsx是react就没有这个错误,请问如何处理?

这是我的一些文件:

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "jsx": "preserve"
  },
  "compileOnSave": false
}

索引.tsx

import * as React from 'react'

type Props = {}

export default class Home extends React.Component<Props>{
  render(): React.ReactNode {
    return <div>
      <p>hello world</p>
    </div>
  }
}

包.json

{
  "name": "test",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "devDependencies": {
    "fork-ts-checker-webpack-plugin": "^0.5.2",
    "happypack": "^5.0.1",
    "ts-loader": "^5.3.3",
    "typescript": "^3.3.3",
    "webpack": "^4.29.3",
    "webpack-cli": "^3.2.3"
  },
  "dependencies": {
    "react": "^16.8.1"
  }
}

webpack.config.js

const fs = require('fs')
const path = require('path')
const HappyPack = require('happypack')
const os = require('os')
const happyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length })
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')

module.exports = {
  entry: './index.tsx',
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'index.js'
  },
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/,
        exclude: /node_modules/,
        use: 'happypack/loader?id=ts'
      }
    ]
  },
  plugins: [
    new HappyPack({
      id: 'ts',
      loaders: [
        {
          loader: 'ts-loader',
          query: { happyPackMode: true }
        }
      ],
      threadPool: happyThreadPool,
      verbose: true
    }),
    new ForkTsCheckerWebpackPlugin({ checkSyntacticErrors: true })
  ]
}

最佳答案

我能够通过添加 babel-loader 来解决类似的问题。尝试这样的事情:

  rules: [
    {
      test: /\.(tsx|ts)$/,
      exclude: /node_modules/,
      use: [
        {
          loader: 'babel-loader',
        },
        {
          loader: 'happypack/loader?id=ts'
        },
      ],
    },

关于typescript - 当 Webpack ts-loader 显示 "Module parse failed: Unexpected token"时,tsconfig.json jsx 被保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54696257/

相关文章:

javascript - this.props 上的属性不存在

java - 如何使用 Spring Boot 和 Angular 7 配置 CRSF

javascript - 使用 mithril.js 和 jsx 构建一些 UI

javascript - 尽管调试器另有说明,为什么该变量不被视为 0?

typescript - 来自 index.ts 的 Webpack 依赖项以错误的顺序解析

node.js - TS Loader + Babel Polyfill : You may need an additional loader to handle the result of these loaders while running npm run build

javascript - 如何在按钮单击时使用 Angular 使用 ng2-file-upload 获取文件值

javascript - 在 Angular 服务中包含 Jquery

javascript - 带有 jsx/react 替代品的这种空警报