简体   繁体   English

React,babel,webpack不解析jsx代码

[英]React, babel, webpack not parsing jsx code

webpack.config.js webpack.config.js

module.exports = {
  context: __dirname + "/app",
  entry: {
    javascript: "./app.js",
    html: "./index.html",
  },
  resolve: {
   extensions: ['', '.js', '.jsx']
 },
  output: {
    filename: "app.js",
    path: __dirname + "/dist",
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: "babel-loader",
      },
      {
        test: /\.html$/,
        loader: "file?name=[name].[ext]",
      },
    ],
  },
}

package.json 的package.json

{
  "name": "react-webpack-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel": "^6.0.15",
    "babel-core": "^6.0.20",
    "babel-loader": "^6.0.1",
    "file-loader": "^0.8.4",
    "webpack": "^1.12.2"
  },
  "dependencies": {
    "react": "^0.14.2"
  }
}

app/app.js 应用程序/ app.js

import React from "react";
import Greeting from "./greeting";

React.render(
  <Greeting name="World"/>,
  document.body
);

I have seen the exact same questions after searching around, but none of the answers seemed to apply to me. 我在搜索后看到了完全相同的问题,但没有一个答案似乎适用于我。 I am getting the following error when running webpack : 运行webpack时出现以下错误:

ERROR in ./app.js ./app.js中的错误
Module build failed: SyntaxError: path/to/project/react-webpack-project/app/app.js: Unexpected token (5:2) 模块构建失败:SyntaxError:path / to / project / react-webpack-project / app / app.js:意外的令牌(5:2)

 React.render( <Greeting name="World"/>, document.body ); 

I am not sure why I am getting this error still. 我不知道为什么我仍然会收到这个错误。 I am guessing it has something to do with my webpack.config.js file, but not 100% what the problem is. 我猜它与我的webpack.config.js文件有关,但不是100%问题所在。

You need to add presets to your babel-loader: 您需要为babel-loader添加预设:

{
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: "babel-loader",
        presets: ['es2015', 'react']  
},

http://babeljs.io/docs/plugins/#presets http://babeljs.io/docs/plugins/#presets

First of all: if you are using React v^0.14, you should render your code using React-Dom. 首先:如果您使用的是React v ^ 0.14,则应使用React-Dom渲染代码。 https://www.npmjs.com/package/react-dom https://www.npmjs.com/package/react-dom

Second, this should resolve your problem: babel-loader jsx SyntaxError: Unexpected token 其次,这应解决您的问题: babel-loader jsx SyntaxError:意外的令牌

I'm currently using React 0.14.3. 我目前正在使用React 0.14.3。 The ReactDOM solution did not work, nor did adding the babel presets into the webpack.config.js. ReactDOM解决方案不起作用,也没有将babel预设添加到webpack.config.js中。 Basically, those solutions appear to work only if you have a single loader defined, but I had both the babel-loader as well as the react-hot loader. 基本上,这些解决方案似乎只有在你定义了一个加载器的情况下才能工作,但是我既有babel-loader也有反应式加载器。

What DID work was to install the babel react presets module: DID的工作原理是安装babel react预设模块:

npm install babel-preset-react

and then create a .babelrc file in my project directory with the following: 然后使用以下命令在我的项目目录中创建一个.babelrc文件:

{
  "presets": ['react']
}

This is documented at http://babeljs.io/docs/plugins/preset-react/ , as pointed to by Abhinav Singi 这一点记录在http://babeljs.io/docs/plugins/preset-react/ ,正如Abhinav Singi所指出的那样

For me the answer was to include the presets in the query block: 对我来说,答案是在查询块中包含预设:

query: {
  presets: ['react', 'es2015']
}

this is help me to solved that issue. 这有助于我解决这个问题。

create new file .babelrc on same directory webpack.config.js. 创建新的文件.babelrc在同一目录webpack.config.js。 Add this to .babelrc 将其添加到.babelrc

  {
  "stage": 2,
  "env": {
    "development": {
      "plugins": [
        "react-display-name",
        "react-transform"
      ],
      "extra": {
        "react-transform": {
          "transforms": [{
            "transform": "react-transform-hmr",
            "imports": ["react"],
            "locals":  ["module"]
          }]
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM