如何让 Webpack 在我的 index.js 中解析 JSX?

问题描述 投票:0回答:1

我正在尝试将 React 添加到我由 Webpack 构建的项目中。

然而,无论我尝试什么,Webpack 都无法读取我文件中的

<>
(JSX)。这是我每次构建它时抛出的错误。

ERROR in ./src/index.js 16:4
Module parse failed: Unexpected token (16:4)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| 
| ReactDOM.render(
>     <h1>Hello World</h1>,
|   document.getElementById('root')
| );

这是我的进口声明:

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

这里是我的 package.json for Webpack 的相关部分:

 "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.21.0",
    "@babel/core": "^7.21.4",
    "@babel/preset-env": "^7.21.4",
    "@babel/preset-react": "^7.18.6",
    "babel-loader": "^9.1.2",
    "css-loader": "^6.7.3",
    "style-loader": "^3.3.2",
    "webpack": "^5.78.0",
    "webpack-cli": "^5.0.1"
  },
  "dependencies": {
    "core-js": "^3.30.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  }
}

这是我的 webpack.config.js:

const path = require('path');

module.exports = {
  mode: 'production',
  entry: './src/index.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
  },
  module: {
    rules: [
      {
        test: /\.(?:js|mjs|cjs)$/,
        "exclude": [
          /node_modules[\\/]core-js/,
          /node_modules[\\/]webpack[\\/]buildin/,
        ],
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              ['@babel/preset-env', { targets: "defaults" }, '@babel/preset-react']
            ],
            plugins: ['@babel/plugin-proposal-class-properties']
          }
        }
      }
    ]
  },
  module: {
    parser: {
      javascript: {
        // ...
        commonjsMagicComments: true,
      },
    },
  },
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: ['style-loader', 'css-loader'],
      },
    ],
  },
};

最后,这是我的 babel.config.json:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "edge": "17",
          "firefox": "60",
          "chrome": "67",
          "safari": "11.1"
        },
        "useBuiltIns": "usage",
        "corejs": "3.6.5"
      }
    ],
    [
      "@babel/preset-react",
      {
        "pragma": "dom", // default pragma is React.createElement (only in classic runtime)
        "pragmaFrag": "DomFrag", // default is React.Fragment (only in classic runtime)
        "throwIfNamespace": false, // defaults to true
        "runtime": "classic" // defaults to classic
        // "importSource": "custom-jsx-library" // defaults to react (only in automatic runtime)
      }
    ]
  ]
}

备注:

reactjs webpack babeljs jsx babel-loader
1个回答
0
投票

我猜我承认了你的问题。根据我的说法,如果您尝试使用 webpack 和 babel 手动构建您的 React 项目,那么您应该安装“HtmlWebpackPlugin”。附上示例webpack.config.js代码供大家参考:

const HtmlWebpackPlugin =
require('html-webpack-plugin');

module.exports = {
    module: {
        rules: [
            {
                test: /\.js$/,
                use: {
                    loader: 'babel-loader'
                },
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader'],

            },
        ],
    },

    plugins: [
        new HtmlWebpackPlugin({
            template: './public/index.html',
            filename: './index.html',
        }),
    ],
};

在这里,我在 webpack.config.js 文件中导入了“HtmlWebpackPlugin”。而且,如果你看到,你应该考虑更改代码行:

test: /\.(?:js|mjs|cjs)$/

如:

test: /\.jsx?$/

现在,为了检测 HTML 文件,您应该考虑放置代码:

{
            test: /\.html$/,
            use: [
                {
                    loader: "html-loader"
                }
            ]
        }

所以你可以考虑在 webpack.config.js 中插入的最终代码块如下:

{
            test: /\.(js|jsx)$/,
            exclude: /node_modules/,
            use: {
                loader: "babel-loader"
            }
        },
        {
            test: /\.html$/,
            use: [
                {
                    loader: "html-loader"
                }
            ]
        }

所以,按照我的说法,尝试按照指定的方式配置这个webpack.config.js,可以帮助你解决你的错误。

© www.soinside.com 2019 - 2024. All rights reserved.