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

javascript - Webpack with react modules giving unexpected token - Stack Overflow

programmeradmin6浏览0评论

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']
    }
};

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']
    }
};
Share Improve this question asked Jul 13, 2015 at 1:58 ApathyBearApathyBear 9,60515 gold badges59 silver badges90 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

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).

发布评论

评论列表(0)

  1. 暂无评论