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

javascript - Load .jsx files with webpack - Stack Overflow

programmeradmin3浏览0评论

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"
}

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

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

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.

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?

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"
}

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

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

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.

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?

Share Improve this question edited Jan 25, 2017 at 6:03 Ivan Kuzyshyn asked Jul 17, 2016 at 11:29 Ivan KuzyshynIvan Kuzyshyn 1651 gold badge1 silver badge7 bronze badges 2
  • 1 It's failing because of that exclude: [/node_modules/, /public/] based on the error. You could try removing /public/ from the list to see if that fixes the issue. I prefer to maintain include over exclude myself (whitelist over blacklist) as that reads better. – Juho Vepsäläinen Commented Jul 17, 2016 at 14:56
  • Does this answer your question? webpack can't find module if file named jsx – csbarnes Commented Jul 29, 2020 at 15:01
Add a ment  | 

3 Answers 3

Reset to default 4

I solved it by adding .jsx post-fix to each of my imported ponents, like so:

import Somthing from './Something.jsx'

Try this

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

As you have excluded the 'public' folder in your webpack setting, the piler fails to parse the .jsx file.

Update below line your webpack file

exclude: [/node_modules/, /public/],

to

exclude: [/node_modules/],

for both .js and .jsx loaders.

发布评论

评论列表(0)

  1. 暂无评论