将React应用程序添加到一个使用webpack的div中。

huangapple go评论70阅读模式
英文:

Adding a React Application to a div using webpack

问题

根据你提供的信息,你遇到了一个Webpack的错误,需要配置适当的loader来处理.js文件。你可以按照以下步骤来解决这个问题:

  1. 确保你的项目中已经安装了必要的Babel和Webpack相关依赖,可以使用以下命令来安装:

    npm install babel-loader @babel/core @babel/preset-react webpack webpack-cli --save-dev
    
  2. 确保你的Webpack配置文件 webpack.config.js 中的 module.rules 部分正确配置了Babel loader,可以使用以下配置:

    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /node_modules/,
          use: {
            loader: 'babel-loader',
            options: {
              presets: ['@babel/preset-react'],
            },
          },
        },
        // 其他规则...
      ],
    },
    
  3. 确保你的项目中有一个有效的Babel配置文件(如 .babelrcbabel.config.js)以及所需的Babel预设(@babel/preset-react)。

完成这些步骤后,尝试重新运行Webpack构建,应该能够解决这个错误。然后,你就可以将生成的bundle包含到现有的HTML中。

英文:

As i read, the best option to add a react app build to a existing website div is generate a bundle with webpack, but i have this error:

package.json

...
"devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^9.1.2",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "webpack": "^5.83.1",
    "webpack-cli": "^5.1.1"
  }

webpack.config.js

const path = require("path");
module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "dist"),
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: "babel-loader",
        options: {
          presets: [
            [
              "@babel/preset-react",
              {
                runtime: "automatic",
              },
              "env",
              "react",
              "es2015",
              "stage-2",
            ],
          ],
        },
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
    ],
  },
  externals: {
    react: "React",
    "react-dom": "ReactDOM",
  },
};

webpack give me this error:

asset main.js 1.6 KiB [emitted] (name: main)
./src/index.js 470 bytes [built] [code generated] [1 error]

ERROR in ./src/index.js 16:2
Module parse failed: Unexpected token (16:2)
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
| const root = ReactDOM.createRoot(document.getElementById("root"));
| root.render(
>   <Provider store={store}>
|     <App />
|   </Provider>

webpack 5.83.1 compiled with 1 error in 108 ms

How can i solve it? I want to include my build in a existing html.

答案1

得分: 1

我相信你出现错误的原因是它尝试使用JavaScript预设来转译你的JSX。预设是从底部到顶部运行的,所以最后一个先运行。由于JavaScript转译器不理解JSX,你需要首先运行React转译器。

我创建了一个使用Webpack转译React的基本应用程序。我进行了测试,它可以正常工作。你可能需要基于babel-env预设的特定规则。

// webpack.config.js
const path = require("path");
module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "dist"),
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: "babel-loader",
        options: {
          presets: [
            "@babel/preset-env",
            "@babel/preset-react",
          ],
        },
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
    ],
  },
  externals: {
    react: "React",
    "react-dom": "ReactDOM",
  },
};
// package.json
{
  "name": "simple-webpack",
  "version": "1.0.0",
  "main": "src/index.js",
  "license": "MIT",
  "devDependencies": {
    "@babel/core": "^7.21.8",
    "@babel/preset-env": "^7.21.5",
    "@babel/preset-react": "^7.18.6",
    "babel-loader": "^9.1.2",
    "webpack": "^5.83.1",
    "webpack-cli": "^5.1.1"
  }
}
英文:

The reason I believe you are getting the error is that it is trying to transpile your JSX using a JavaScript preset. Presets run from bottom to top, so the last one runs first. As JavaScipt transpilers don't understand JSX, you need to run the React transpiler first.

I created a basic app with webpack transpiling React. I tested it and it works. You may need specific rules based on the babel-env preset.

// webpack.config.js
const path = require("path");
module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "dist"),
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: "babel-loader",
        options: {
          presets: [
            "@babel/preset-env",
            "@babel/preset-react",
          ],
        },
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
    ],
  },
  externals: {
    react: "React",
    "react-dom": "ReactDOM",
  },
};
// package.json
{
  "name": "simple-webpack",
  "version": "1.0.0",
  "main": "src/index.js",
  "license": "MIT",
  "devDependencies": {
    "@babel/core": "^7.21.8",
    "@babel/preset-env": "^7.21.5",
    "@babel/preset-react": "^7.18.6",
    "babel-loader": "^9.1.2",
    "webpack": "^5.83.1",
    "webpack-cli": "^5.1.1"
  }
}

huangapple
  • 本文由 发表于 2023年5月24日 20:29:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定