最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Babelwebpack not reading jsx - Stack Overflow

programmeradmin3浏览0评论

Having a bit of trouble with my react/webpack set up, the first bit of JSX it hits, I "Unexpected Token" - as in the first < in the JSX . Here is my Webpack config :

 const path = require('path');

 const webpack = require('webpack');

module.exports = {
entry: [
    'webpack-dev-server/client?http://localhost:8080',
    'webpack/hot/only-dev-server',
    './app/index.jsx'
],
module: {
    loaders: [{
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
    }]
},
resolve: {
    extensions: ['', '.js', '.jsx']
},
output: {
    path: path.join(__dirname, 'dist'),
    publicPath: '/',
    filename: 'bundle.js'
},
devServer: {
    contentBase: './dist',
    hot: true
},
plugins: [
    new webpack.HotModuleReplacementPlugin()
]

};

I noticed if i swap the loaders to use react-hot, it no longer knows how to read the es6 imports :

  module: {
    loaders: [{
        test: /\.jsx?$/,
        exclude: /node_modules/,
        include: path.join(__dirname, 'src'),
        loader: 'react-hot!babel'
    }]
},

(gives the error - Unexpected token You may need an appropriate loader to handle this file type. Referencing the import lines )

Unsure what I am missing here, could use a some help. Thanks!

Having a bit of trouble with my react/webpack set up, the first bit of JSX it hits, I "Unexpected Token" - as in the first < in the JSX . Here is my Webpack config :

 const path = require('path');

 const webpack = require('webpack');

module.exports = {
entry: [
    'webpack-dev-server/client?http://localhost:8080',
    'webpack/hot/only-dev-server',
    './app/index.jsx'
],
module: {
    loaders: [{
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
    }]
},
resolve: {
    extensions: ['', '.js', '.jsx']
},
output: {
    path: path.join(__dirname, 'dist'),
    publicPath: '/',
    filename: 'bundle.js'
},
devServer: {
    contentBase: './dist',
    hot: true
},
plugins: [
    new webpack.HotModuleReplacementPlugin()
]

};

I noticed if i swap the loaders to use react-hot, it no longer knows how to read the es6 imports :

  module: {
    loaders: [{
        test: /\.jsx?$/,
        exclude: /node_modules/,
        include: path.join(__dirname, 'src'),
        loader: 'react-hot!babel'
    }]
},

(gives the error - Unexpected token You may need an appropriate loader to handle this file type. Referencing the import lines )

Unsure what I am missing here, could use a some help. Thanks!

Share Improve this question edited Nov 13, 2015 at 15:45 Leonid Beschastny 51.5k10 gold badges121 silver badges124 bronze badges asked Nov 11, 2015 at 18:24 ajmajmajmaajmajmajma 14.2k25 gold badges83 silver badges138 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

If you're using Babel 6.0, it won't transpile your code by default anymore. (https://babeljs.io/blog/2015/10/29/6.0.0/), it says:

Since Babel is focusing on being a platform for JavaScript tooling and not an ES2015 transpiler, we’ve decided to make all of the plugins opt-in. This means when you install Babel it will no longer transpile your ES2015 code by default.

You need to install two presets if you want to transpile your code:

npm install --save-dev babel-preset-es2015 babel-preset-react

In your webpack.config.js you can then specify to use the presets like the following:

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

You Should add .babelrc config file.

{
  "presets": ["env", "react"]
}
发布评论

评论列表(0)

  1. 暂无评论