简体   繁体   English

Webpack 2(beta)无法处理JSX,尽管提供了带有React预设的Babel加载程序

[英]Webpack 2 (beta) fails to handle JSX despite being provided with a Babel loader with React preset

I have used this guide from Webpack docs as a reference to create a Webpack config with HMR, but I get an error stating that I don't have a loader for JSX. 我已经使用Webpack docs中的这个指南作为使用HMR创建Webpack配置的参考,但是我得到一个错误,指出我没有JSX的加载器。 I have installed all of the needed packages listed in the reference. 我已经安装了参考中列出的所有必需的软件包。 Note, I've also tried putting my Babel config in .babelrc in the first place, but it gave me the same results. 注意,我也尝试将我的Babel配置放在.babelrc中,但它给了我相同的结果。

ERROR in ./src/index.js
Module parse failed: /Users/macbem/Documents/Coding/Back/typeahead-todo/src/index.js Unexpected token (10:2)
You may need an appropriate loader to handle this file type.
|
| ReactDOM.render(
|   <AppContainer>
|     <App />
|   </AppContainer>,
 @ multi main

My config files are in the root of the project, .js files are in /src/ . 我的配置文件位于项目的根目录中, .js文件位于/src/ My webpack config looks as follows: 我的webpack配置如下:

// webpack.config.js
const { resolve } = require('path');
const webpack = require('webpack');

module.exports = {
  entry: [
    'react-hot-loader/patch',
    'webpack-dev-server/client?http://localhost:8080',
    'webpack/hot/only-dev-server',
    './index.js'
  ],
  output: {
    filename: 'bundle.js',
    path: resolve(__dirname, 'dist'),
    publicPath: '/'
  },
  context: resolve(__dirname, 'src'),
  devtool: 'inline-source-map',
  devServer: {
    hot: true,
    contentBase: resolve(__dirname, 'dist'),
    publicPath: '/'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              "presets": [
                  [ "es2015", { "modules": false } ],
                  "stage-2",
                  "react"
                ],
              "plugins": [
                "react-hot-loader/babel"
              ]
            }
          },
        ],
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader?modules',
          'postcss-loader',
        ],
      },
    ],
  },

  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin()
  ],
};

Also, this is my index.js file: 另外,这是我的index.js文件:

import React from 'react';
import ReactDOM from 'react-dom';

import { AppContainer } from 'react-hot-loader';
// AppContainer is a necessary wrapper component for HMR

import App from './components/App';

ReactDOM.render(
  <AppContainer>
    <App />
  </AppContainer>,
  document.getElementById('root')
);

// Hot Module Replacement API
if (module.hot) {
  module.hot.accept('./components/App', () => {
    const NewApp = require('./components/App').default
    render(NewApp)
  });
}

Edit: all I said before was wrong, ignore it, lots of stuff changed in webpack 2 and what I said doesn't apply anymore. 编辑:以前我说的都是错的,忽略它,webpack 2中的很多东西都改了,我说的不再适用了。

Went ahead and tried it out for myself and I think I found the issue: the version of webpack in the link (beta.20) doesn't actually support the "module.rules.use" syntax. 前进并为自己尝试了一下,我认为我发现了问题:链接中的webpack版本(beta.20)实际上并不支持“module.rules.use”语法。 I'm not exactly sure which version of the beta started supporting it, but beta.25 seems to work. 我不确定哪个版本的测试版开始支持它,但beta.25似乎有效。

So if you do a npm uninstall webpack --save and npm install webpack@2.1.0-beta.25 --save , it should (hopefully) all work. 因此,如果您执行npm uninstall webpack --savenpm install webpack@2.1.0-beta.25 --save ,它应该(希望)所有工作。

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

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