Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot ES6 import JSX modules in Webpack

For some reason I can't seem to get Webpack to import modules from .JSX files. Every time I try to run Webpack I'll get this message:

ERROR in ./src/Example.jsx
Module parse failed: /path/to/project/src/Example.jsx Unexpected token (6:12)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (6:12)

The thing is; there isn't a whole lot in ./src/Example.jsx. In fact this is all that it contains:

import React from 'react';


export default class Example extends React.Component{
  render() {
    return (<h2>Hello world!!</h2>);
  }
};

Webpack did not have any issues when I had the class in my index.jsx file but when I moved it to its own file webpack all of a sudden couldn figure out what to do. I've tried to resolve this by using babel-plugin-transform-react-jsx but that didn't seem to solve my problem. What do I need to do to get Webpack to properly convert/parse .JSX files?

/* package.json */

{
  ...,
  "dependencies": {
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.11.1",
    "react": "^15.2.1",
    "react-dom": "^15.2.1",
    "webpack": "1.13.1"
  },
  "devDependencies": {
    "concurrently": "^2.2.0",
    "eslint": "^3.1.1",
    "eslint-plugin-react": "^5.2.2",
    "jest-cli": "^13.2.3",
    "react-addons-test-utils": "^15.2.1",
    "webpack-dev-server": "^1.14.1"
  },
  "scripts": {
    "build": "webpack -p",
    "dev": "webpack-dev-server --port 9999",
    "start": "npm run build && python -m SimpleHTTPServer 9999",
    "test": "jest --verbose --coverage --config jest.config.json"
  }
}

/* webpack.config.js */

var path    = require('path');
var webpack = require('webpack');

var BUILD_DIR  = path.resolve(__dirname, 'build/');
var SOURCE_DIR = path.resolve(__dirname, 'src/');

module.exports = {
  entry: SOURCE_DIR + '/index.jsx',
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  module: {
    loaders: [
      {
        test: /[^\.spec]+\.jsx$/,
        include: SOURCE_DIR,
        loader: 'babel'
      }
    ]
  }
};

/* .baberc */

{
  "presets": ["es2015", "react"]
}

/* src/index.jsx */

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

import Example from './Example'


ReactDOM.render(<Example />, document.getElementById('floor-plan'));
like image 592
Mathijs Avatar asked Jul 22 '16 14:07

Mathijs


2 Answers

I got your setup working with these few modifications. I isolated the problem to your test property.

var path    = require('path');
var webpack = require('webpack');

var BUILD_DIR  = path.resolve(__dirname, 'build/');
var SOURCE_DIR = path.resolve(__dirname, 'src/');

module.exports = {
  entry: SOURCE_DIR + '/index.jsx',
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
  resolve: {
    extensions: ['.js', '.jsx']
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        include: SOURCE_DIR,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
          presets: ['es2015', 'react']
        },
      }
    ]
  }
};
like image 50
Meow Avatar answered Nov 14 '22 03:11

Meow


The reason could be import Example from './Example' in index.jsx

webpack will treat this as js import instead of jsx.

try

import Example from './Example.jsx'

like image 4
Jibin George Avatar answered Nov 14 '22 05:11

Jibin George