[英]You may need an appropriate loader to handle this file type error occurs in ReactJS file
I have divided my configuration in to two production and development mode and merging them using webpack-merge.我将我的配置分为两种生产和开发模式,并使用 webpack-merge 合并它们。
Whenever i try to build or run the code it gives me you may need an appropriate loader error even though i have define loader for the jsx or js files.每当我尝试构建或运行它给我的代码时,即使我已经为 jsx 或 js 文件定义了加载器,您也可能需要一个适当的加载器错误。
Common Config File code is here...通用配置文件代码在这里...
const path = require("path");
const HtmlWebpackPlugin = require('html-webpack-plugin'); //installed via npm
const webpack = require('webpack'); //to access built-in plugins
module.exports = {
entry : "./src/index.js",
module: {
rules: [
{
test: /\.scss$/,
use: [
"style-loader",
"css-loader",
"sass-loader"
]
},
{
test: /.(js|jsx)$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react']
}
}
]
},
plugins: [
new webpack.ProgressPlugin(),
new HtmlWebpackPlugin({template: './src/index.html'})
]
}
Production Config Code is生产配置代码是
const path = require("path");
const common = require("./webpack.config");
const merge = require("webpack-merge");
module.exports = (common, {
mode : "production",
output: {
filename : "bundle.[contentHash].js",
path : path.resolve(__dirname, "dist")
}
});
Development Config Code开发配置代码
const path = require("path");
const common = require("./webpack.config.js");
const merge = require("webpack-merge");
module.exports = (common, {
mode : "development",
output: {
filename : "bundle.js",
path : path.resolve(__dirname, "dist")
}
});
index.js code index.js 代码
import "./assets/scss/main.scss";
import React from 'react';
import ReactDOM from 'react-dom';
class App extends React.Component {
render() {
return <h1>Hello</h1>
}
}
ReactDOM.render(<App/>, document.getElementById('app'));
.babelrc code .babelrc 代码
{
"presets": ["env", "react","@babel/preset-env"],
"plugins": [
"transform-class-properties"
]
}
That error occurs because your JSX is not being compiled to JavaScript.发生该错误是因为您的 JSX 未编译为 JavaScript。
The test in your config file that handles this is:处理此问题的配置文件中的测试是:
test: /.(js|jsx)$/,
loader: 'babel-loader',
I see that you've imported "webpack-merge" but it does not appear that you're using it (at least in the code that is displayed in your question).So it seems like you're not merging the disparate config files in the way you're expecting (or maybe at all).我看到您已经导入了“webpack-merge”,但似乎您没有使用它(至少在您的问题中显示的代码中)。所以看起来您没有合并不同的配置文件以您期望的方式(或者可能完全如此)。
Instead of splitting your config into multiple files, usually [from what I've seen] developers prefer to use one file with some logic in it based on an environmental variable: https://webpack.js.org/guides/environment-variables/与其将您的配置拆分为多个文件,通常 [根据我所见] 开发人员更喜欢使用一个文件,其中包含一些基于环境变量的逻辑: https : //webpack.js.org/guides/environment-variables /
process.env.NODE_ENV === 'dev' ? doDevStuff : doProdStuff
You will need to call merge
on both your files, merging common
with the object specified for each case:您将需要调用
merge
两个文件,合并common
为每个案件指定的对象:
// webpack.prod.js
const path = require("path");
const common = require("./webpack.config");
const merge = require("webpack-merge");
module.exports = merge(common, {
mode : "production",
output: {
filename : "bundle.[contentHash].js",
path : path.resolve(__dirname, "dist")
}
});
// webpack.dev.js
const path = require("path");
const common = require("./webpack.config.js");
const merge = require("webpack-merge");
module.exports = merge(common, {
mode : "development",
output: {
filename : "bundle.js",
path : path.resolve(__dirname, "dist")
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.