sareed
sareed

Reputation: 780

React Unexpected token with babel-loader

I am using Webpack, Gulp and React to use a React app in my Jekyll static site. I am getting an unexpected token error on the React element:

Module parse failed: Unexpected token (8:4)
You may need an appropriate loader to handle this file type.
| window.mount = function(id, type) {
|   render(
|     <App type={type} />,
|     document.getElementById(id)
|   )

I have tried various methods and babel presets.

webpack.config.js

module.exports = {
  entry: {
    app: './webpack/app.js'
  },
  output: {
    path: __dirname + '/src/_assets/js',
    filename: 'app.js'
  },
  module: {
    rules: [{
      // I've tried node_modules/my-app as well
      include: __dirname + 'node_modules/my-app/src',
      test: /\.(js|jsx)$/,
      use: {
        loader: "babel-loader",
        options: {
          // I've tried several variations of this:
          presets: ["react", "env", "stage-0", "es2015", { targets: { node: true } }],
        }
      }
    }]
  }
};

Pertinent gulp code:

const webpack_stream = require('webpack-stream');
const webpack_config = require('./webpack.config.js');

// I have tried gulp-webpack and gulp ( no webpack ) with the babel-loader
gulp.task('app', function () {
  return webpack_stream(webpack_config).pipe(gulp.dest('src/_assets/js/'));
});

Can anyone see anything I am missing, have wrong, or are there some issues with versions/presets?

I have looked through the other similar questions and answers like this and this with no luck.

Edit 1:

If I change the webpack.config.js to use the babel-polyfill and exclude instead of include I can get past the first error, but I still error on a <Provider> element.

**webpack.config**

module.exports = {
  entry: './webpack/rfi.js',
  output: {
    path: __dirname + '/src/_assets/js',
    filename: 'rfi.js'
  },
  module: {
    rules: [{
      exclude: /node_modules/,
      test: /\.(js|jsx)$/,
      use: {
        loader: "babel-loader",
        options: {
          presets: ["react", "env", "stage-3", "es2015"],
        }
      }
    }]
  }
};

**App.js**

class Rfi extends Component {
  render() {
    return (
        <Provider store={store}>
          <Form />
        </Provider>
    );
  }
}

**error**

Module parse failed: Unexpected token (36:8)
You may need an appropriate loader to handle this file type.
|
|     return (
|         <Provider store={store}>
|           <Form />
|         </Provider>

And yes Provider is imported import { Provider } from 'react-redux';

Edit 2 ( per comment request )

For some reason I'm not getting a good trace in my messages anymore. Here's one from yesterday though.

enter image description here

Upvotes: 1

Views: 807

Answers (1)

Aaqib
Aaqib

Reputation: 10350

One of the ways is make use of babel-polyfill , You can install it as a project dependency npm install --save babel-polyfill and inside your webpack.config.js you can pass it to the entry point (You can amend this accordingly):

entry: ["babel-polyfill", "./webpack/app.js"],

Edit 1 Answer :

You need to make sure your store is on the top of hierarchy , like (app.js) (You can amend this accordingly) :

import { render } from "react-dom";

import { Provider } from "react-redux";

render(
    <Provider store={store}>
        <Form />
    </Provider>,
    document.getElementById("app") // Ammend this accordignly
);

You can read more about babel Polyfill Here

Upvotes: 2

Related Questions