Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack with react modules giving unexpected token

Been trying to use the react-spin npm module, but when I try and build a bundle.js with webpack, I receive the following error:

Module parse failed: /Users/nir/browsewidget/node_modules/react-spin/src/main.js Line 29: Unexpected token <
You may need an appropriate loader to handle this file type.
|   render: function() {
|     return (
|       <span ref="container" />
|     );
|   }
 @ ./js/widget.jsx 4:14-35

I am guessing that this module has jsx in it, but I don't understand why it can't be built? Does react-spin need some extra configuration when building a bundle?

Here is my full webpack.config.js:

module.exports = {
    entry: "./js/widget.jsx",
    output: {
        path: __dirname,
        filename: "bundle.js"
    },
    module: {
        loaders: [
            {
                    test: /\.jsx$/, 
                    loader: 'jsx-loader?insertPragma=React.DOM&harmony'
            }
        ]
    },
    externals: {
        //don't bundle the 'react' npm package with our bundle.js
        //but get it from a global 'React' variable
        'react': 'React'
    },
    resolve: {
        extensions: ['','.js','.jsx']
    }
};
like image 280
ApathyBear Avatar asked Jul 13 '15 01:07

ApathyBear


1 Answers

Your loader is configured to only transform files that end in .jsx:

 test: /\.jsx$/,

(The dollar sign means end-of-string.)

You could change it to

test: /\.jsx?$/,

to transform both .js and .jsx files, but running every JavaScript file in node_modules through the JSX loader will probably be fairly slow.

I believe you should be able to set an exclude option of /node_modules/ and then an include option for the specific module you care about (react-spin), but the best solution is that packages not use JSX in the published version of the JavaScript—the author of react-spin might be open to a pull request to this effect. (Edit: it appears there already is one, see thomasboyt/react-spin#3)

Finally, react-spin is very small, so you man consider implementing it yourself in your own project (so that you don't have to worry about the webpack loader issues).

like image 85
Michelle Tilley Avatar answered Oct 23 '22 18:10

Michelle Tilley