Webpack错误与反应 [英] Webpack error with react

查看:75
本文介绍了Webpack错误与反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据此教程并继续得到相同的错误。我无法调试这两条消息:

I am trying to configure webpack according to this tutorial and keep getting the same error. I am having trouble debugging these 2 messages:

ERROR in ./app.js
Module parse failed: /path/react/react-webpack-babel/app/app.js Line 1: Unexpected reserved word
You may need an appropriate loader to handle this file type.
| import React from "react";
| import Greeting from "./greeting";
|

ERROR in ./index.html
Module parse failed: /path/react/react-webpack-babel/app/index.html Line 1: Unexpected token <
You may need an appropriate loader to handle this file type.
| <!DOCTYPE html>
| <html>
|

这是我的 webpack.configure.js

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

这是反应组件

app / greeting.js

import React from "react/addons";

export default React.createClass({
  render: function() {
    return (
      <div className="greeting">
        Hello, {this.props.name}!
      </div>
    );
  },
});

app / app.js

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

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

app / index.html

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <title>Webpack + React</title>
  </head>

  <body></body>

  <script src="app.js"></script>

</html>

如果它有用,这是我的package.json与依赖

In case it's helpful, here's my package.json with dependencies

{
  "name": "project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "private": true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Me",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^5.8.22",
    "babel-loader": "^5.3.2",
    "file-loader": "^0.8.4",
    "webpack": "^1.11.0"
  },
  "dependencies": {
    "react": "^0.13.3"
  }
}


推荐答案

loaders 选项应该嵌套在模块对象中,如下所示:

the loaders option should be nested in a module object like so:

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

我最后还添加了一个缺少的分号;)

I also added a missing semi-colon at the end ;)

这篇关于Webpack错误与反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆