gpt4 book ai didi

javascript - 使用 webpack 加载 .jsx 文件

转载 作者:行者123 更新时间:2023-12-03 14:28:50 24 4
gpt4 key购买 nike

我在使用 webpack 加载 .jsx 文件时遇到问题。我有这个 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/]
}
]
}
};

我的应用程序有这个软件包:

"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 时,出现此错误:

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 加载器中。但我不知 Prop 体是什么问题。

我尝试使用react-hot、babel loader和jsx-loader(带预设和不带预设),但在所有情况下错误都是相同的。此加载程序不适用于:

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

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

最佳答案

我通过向每个导入的组件添加 .jsx 后修复解决了这个问题,如下所示:

import Somthing from './Something.jsx'

关于javascript - 使用 webpack 加载 .jsx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38420720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com