【问题标题】:Module fails to parse at JSX, cannot find error in webpack, babel configs模块在 JSX 解析失败,在 webpack、babel 配置中找不到错误
【发布时间】:2017-10-11 15:23:39
【问题描述】:

这个设置的细节我已经复习了很多次了,从前它是醒着的,现在出现了一个错误......

ERROR in ./src/index.js
Module parse failed: /Users/Jeff/javascript/testbuildwords/src/index.js Unexpected token (5:11)
You may need an appropriate loader to handle this file type.
| class MyComponent extends React.Component {
|   render() {
|     return <div>This is my component</div>;
|   }
| }

我觉得错误一定在这些文件的某个地方,但我已经查看了它们并与其他文件进行了 1000 次比较,但我找不到错误

webpack.config.js

const path = require("path");

module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "build"),
    filename: "index.js",
    libraryTarget: "commonjs2"
  },
  module: {
    rules: [
      {
        test: /.js$/,
        include: path.resolve(__dirname, "src"),
        exclude: /(node_modules|bower_components|build)/,
        use: "babel-loader"
      },
      {
        test: /.css$/,
        use: ["style-loader", "css-loader"]
      },
      {
        test: /.(png|jpg|gif|eot|svg|ttf|woff|woff2)$/,
        use: [
          {
            loader: "file-loader",
            options: {}
          }
        ]
      }
    ]
  },
  externals: {
    react: "commonjs react"
  }
};

.babelrc

{
  "presets": ["env"],
  "plugins": [
    "transform-object-rest-spread",
    "transform-react-jsx",
    "transform-class-properties"
    ]
}

package.json

{
  "name": "testbuildwords",
  "version": "0.1.0",
  "main": "build/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack --watch",
    "build": "webpack"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "css-loader": "^0.28.7",
    "file-loader": "^1.1.5",
    "prop-types": "^15.6.0",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "style-loader": "^0.19.0",
    "webpack": "^3.6.0"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-plugin-transform-react-jsx": "^6.24.1",
    "babel-preset-env": "^1.6.0",
    "eslint-plugin-class-property": "^1.0.6"
  }
}

./src/index.js

import React from "react";

class MyComponent extends React.Component {
  render() {
    return <div>This is my component</div>;
  }
}
export default MyComponent;

【问题讨论】:

  • 您的日志显示 src/index.js 中有错误,您也可以粘贴吗?
  • @Gautam... 添加到帖子
  • 在下面试试我的答案,它应该可以工作。
  • 不确定,但也许在 devDependencies 中添加 babel-preset-react 并修改 .babelrc在预设中添加“react”应该可以工作
  • 不幸的是,这不起作用@Daniele

标签: javascript reactjs webpack


【解决方案1】:

尝试添加babel react-preset

{
  "presets": ["env", "react"]
}

【讨论】:

    【解决方案2】:

    我认为 Webpack 无法隐式解析 .jsx 文件。 如果将应该解析的扩展添加到 webpack.config.js 文件中怎么办?:

    resolve: {
      extensions: ['', '.js', '.jsx']
    }
    

    【讨论】:

      【解决方案3】:

      添加一个带有预设的规则来解析 jsx 扩展

      test: /.jsx$/,
      include: path.resolve(__dirname, "src"),
      exclude: /(node_modules|bower_components|build)/,
      use: {
          loader: 'babel-loader',
          options: {
            presets: ['es2015', 'react', 'babel-preset-stage-0']
          }
      }
      

      并在解析块中添加类似 jsx 的条目

      resolve: {
         extensions: ['', '.jsx', '.js']
      }
      

      【讨论】:

        【解决方案4】:

        没有一个答案对我有帮助。我最终将 webpack 添加到 dependencies 而不是 devDependencies 因为 webpack 使用的大多数其他东西都列在那里,之后问题就解决了

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-03
          • 2021-09-10
          • 2018-07-05
          • 2021-11-26
          相关资源
          最近更新 更多