Webpack抛出有关jsx组件的错误

时间:2017-08-07 16:57:14

标签: reactjs webpack jsx

我有以下jsx组件:

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    context: path.resolve(__dirname, './src'),
    entry: {
        app: './index.jsx'
    },
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, './dist/assets'),
        publicPath: '/assets',

    },
    devServer: {
        contentBase: path.resolve(__dirname, './src')
    },
    module: {
        rules: [
            {
              test: /\.jsx$/,
              exclude: [/node-modules/],
              use: [
                {
                  loader: "babel-loader",
                  options: { presets: ["es2015"] }
                }
              ]
            },
            {
                test: /\.css$/,
                use:  ExtractTextPlugin.extract({
                    use: [{
                        loader: 'css-loader',
                        options: { importLoaders: 1 },
                    }],
                }),
            },
            {
              test: /\.(sass|scss)$/,
              use: ["style-loader", "css-loader", "sass-loader"]
            }
        ]
    },
    resolve: {
        modules: [ 
            path.resolve(__dirname, './src'),
            'node_modules'
        ]
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: 'common'
        }),
        new ExtractTextPlugin({
            filename: '[name].bundle.css',
            allChunks: true
        })
    ]
}

和我的webpack文件:

 node_modules/.bin/webpack -d

然而,当我跑步时:

<div>

我收到以下错误:

enter image description here

1 个答案:

答案 0 :(得分:0)

添加“babel-preset-react”

npm install babel-preset-react

并在webpack.config.js

中为babel-loader添加“预设”选项

以下是webpack.config.js示例:

  module: {
    loaders: [{
      exclude: /node_modules/,
      loader: 'babel',
      query: {
        presets: ['react', 'es2015', 'stage-1']
      }
    }]
  }