简体   繁体   English

你可能需要一个合适的加载器来处理 ReactJS 文件中发生的这种文件类型错误

[英]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.

相关问题 ReactJS 错误您可能需要适当的加载程序来处理此文件类型 - ReactJS error You may need an appropriate loader to handle this file type 您可能需要一个合适的加载器来处理这种文件类型。 ReactJs - You may need an appropriate loader to handle this file type. ReactJs ReactJS,Webpack:您可能需要适当的加载器来处理此文件类型 - ReactJS ,Webpack : You may need an appropriate loader to handle this file type 带有Sass的Next.js发生错误,提示“您可能需要适当的加载程序来处理此文件类型错误” - Next.js with Sass occurs on error saying “ You may need an appropriate loader to handle this file type error” Webpack错误:“您可能需要适当的加载程序来处理此文件类型” - Webpack error: “You may need an appropriate loader to handle this file type” 您可能需要适当的加载程序来处理此文件类型错误 - You may need an appropriate loader to handle this file type-error webpack错误-您可能需要适当的加载程序来处理此文件类型 - error with webpack - You may need an appropriate loader to handle this file type glsify - 错误'您可能需要适当的加载程序来处理此文件类型'? - glsify - error 'You may need an appropriate loader to handle this file type'? CSS ReactJs - 您可能需要适当的加载程序来处理此文件类型,目前没有配置加载程序来处理此文件 - CSS ReactJs - You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file ReactJS:模块解析失败&#39;,, ... / index.js&#39;,您可能需要适当的加载器来处理此文件类型 - ReactJS: Module parse failed ',,…/index.js' you may need appropriate loader to handle this file type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM