使用webpack加载.jsx文件 [英] Load .jsx files with webpack

查看:170
本文介绍了使用webpack加载.jsx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 webpack 加载 .jsx 文件时遇到问题。
我有这个 webpack.config.js

I have a problem with loading .jsx files using webpack. I have this webpack.config.js:

var webpack = require('webpack');

module.exports = {
    entry: "./static/js/storage/src/index.js",
    output: {
        path: './static/js/storage/public/',
        publicPath: "public/",
        filename: "bundle.js"
},

resolve: {
    extensions: ['', '.js', '.jsx']
},

module: {
    loaders: [
        {
            test: /\.js$/,
            loader: "babel-loader",
            exclude: [/node_modules/, /public/],
            query: {
                plugins: ['transform-runtime'],
                presets: ['es2015', 'stage-0', 'react']
            }
        },
        {
            test: /\.jsx$/,
            loader: "react-hot!babel",
            exclude: [/node_modules/, /public/]
        }
    ]
}
};

我的应用程序包含此软件包:

And I have this packages for my application:

"dependencies": {
    "jquery": "^3.1.0",
    "react": "^15.2.1",
    "react-dom": "^15.2.1"
},
    "devDependencies": {
    "autoprefixer-loader": "^3.2.0",
    "babel": "^6.5.2",
    "babel-core": "^6.10.4",
    "babel-loader": "^6.2.4",
    "babel-plugin-transform-runtime": "^6.9.0",
    "babel-polyfill": "^6.9.1",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.11.1",
    "babel-runtime": "^6.9.2",
    "css-loader": "^0.23.1",
    "file-loader": "^0.9.0",
    "json-loader": "^0.5.4",
    "jsx-loader": "^0.13.2",
    "react": "^15.2.1",
    "react-hot-loader": "^1.3.0",
    "style-loader": "^0.13.1",
    "url-loader": "^0.5.7",
    "webpack": "^1.13.1"
}

当我尝试在控制台中运行webpack时我有这个呃ror:

And when i try to run webpack in console i have this error:


模块解析失败:
/static/js/storage/src/components/StorageApp.jsx
意外的令牌(12:12)你可能需要一个合适的加载器来处理
这个文件类型。

Module parse failed: /static/js/storage/src/components/StorageApp.jsx Unexpected token (12:12) You may need an appropriate loader to handle this file type.

我的webpack不能加载jsx文件。我认为问题出在我的jsx加载器中。但我不知道究竟是什么问题。

My webpack can not load jsx files. I think that problem is in my jsx loader. But I do not know what the exact problem.

我尝试使用react-hot,babel loader和jsx-loader与预设,但没有错误在所有情况下都是相同的。
此装载程序不起作用:

I try to use react-hot, babel loader and jsx-loader with presets and without but error is the same in all cases. This loaders don't work to:

test: /\.jsx$/,
    loader: 'babel',
    query: {
        presets: ['react', 'es2015']
},

有人可以帮忙解决这个问题吗?

Can someone help with this problem?

推荐答案

试试这个

loaders: [
        {
            test: /\.jsx?$/,
            loader: "babel-loader",
            exclude: [/node_modules/, /public/],
            query: {
                plugins: ["react-hot-loader/babel", 'transform-runtime'],
                presets: ['es2015', 'stage-0', 'react']
            }
        }
    ]

这篇关于使用webpack加载.jsx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆