哪个加载器适合处理 React JSX?

Which is the appropriate loader to handle React JSX?

我试图让 webpack4 生成带有 main.js 的输出文件夹,但一直返回 "may need an appropriate loader to handle this file type" 错误并指向 jsx 行。怎么让webpack转换jsx,或者需要什么样的loader?

我认为问题可能出在@babel/preset-env 和@babel/preset-react 已经过时或者我需要使用更新的版本,但看起来情况并非如此。

webpack.config.js

module.exports = {
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: { loader: 'babel-loader' }
            }
        ]
    }
};

.babelrc

{
    "presets": [ "@babel/preset-env", "@babel/preset-react" ]
}

package.json

{
  "name": "github_battle",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack --mode development",
    "build": "webpack --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.2.2",
    "@babel/preset-env": "^7.2.3",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.5",
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "webpack": "^4.28.3",
    "webpack-cli": "^3.2.0"
  }
} 

App.js

import React from 'react';
import ReactDOM from 'react-dom';

class App extends React.Component {
    render (
        <p>React in here</p>
    );
};

export default App;

ReactDOM.render(
    <App />,
    document.getElementById('root')
);

index.js

import App from './App';

我希望反应代码能够正确转换并捆绑到 dist/main.js.

实际结果:

> github_battle@1.0.0 dev D:\WORK\github_battle
> webpack --mode development

Hash: a88a6be7f290c6ab30a1
Version: webpack 4.28.3
Time: 186ms
Built at: 01/04/2019 9:06:55 AM
    Asset      Size  Chunks             Chunk Names
main.js  4.58 KiB    main  [emitted]  main
Entrypoint main = main.js
[./src/App.js] 238 bytes {main} [built] [failed] [1 error]
[./src/index.js] 28 bytes {main} [built]

ERROR in ./src/App.js 7:8
Module parse failed: Unexpected token (7:8)
You may need an appropriate loader to handle this file type.
| class App extends React.Component {
|     render (
>         <p>React in here</p>
|     );
| };
 @ ./src/index.js 2:0-24
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! github_battle@1.0.0 dev: `webpack --mode development`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the github_battle@1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely 
additional logging output above.

主要错误是您错误地命名了 webpack 配置文件。

您将其命名为 webpack.conf.jswebpack 命令需要一个名为 webpack.config.js.

的文件

所以先重命名。

现在在 webpack 中你没有提到 entryoutput 是在你完成重命名后导致错误的。

因此将您的 webpack 配置文件重写为。

module.exports = {
    entry: "./src/App.js",
    output: {
        filename: "bundle.js"
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: { loader: 'babel-loader' }
            }
        ]
    }
};

现在 App.js 你写错了 ReactJS 代码。应该是

import React from 'react';
import ReactDOM from 'react-dom';

class App extends React.Component {
    render() {
        return (
            <p> React in here</p >
        );
    }
};

export default App;

ReactDOM.render(
    <App />,
    document.getElementById('root')
);

那就改变吧。

执行这些步骤后它应该可以工作。