# \[WEBPACK] 5. Webpack : Tạo Project ReactJS (

Ví dụ đã hoàn thành :)

{% file src="<https://2726517656-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M1E4Gk2ppVKb4olmnun%2F-MIZD2VSPzPhzjCwwG5N%2F-MIZD9o-rKz4bpOm1pbE%2Fwebpackreactjs.rar?alt=media&token=c4daa11d-b82b-43ba-a551-a10c2a0c7445>" %}

## Chú ý: Ta có thể viết gọn babel-loader

```
babel-loader như sau :))
```

C:\Users\Administrator\Desktop\abc\webpack.config.js

## Trước

```
const path = require('path');

module.exports = {
  entry: './src/app.js',
  output: {
    path: path.join(__dirname, 'public'),
    filename: 'bundle.js'
  },
  module: {
    rules: [{
        test: /\.js$/,
        exclude: /node_modules/,
        use: [{
          loader: 'babel-loader',
          options: {
            presets: ["@babel/preset-env", "@babel/preset-react"],
            plugins: [
              '@babel/plugin-proposal-class-properties'
            ]

          }
        }]
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      }
    ]
  },
  devtool: 'cheap-module-eval-source-map',
  devServer: {
    contentBase: path.join(__dirname, 'public')
  }
};
```

C:\Users\Administrator\Desktop\abc\webpack.config.js

## Sau&#x20;

```
const path = require('path');

module.exports = {
  entry: './src/app.js',
  output: {
    path: path.join(__dirname, 'public'),
    filename: 'bundle.js'
  },
  module: {
    rules: [{
        test: /\.js$/,
        exclude: /node_modules/,
        use: [{
          loader: 'babel-loader',
        }]
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      }
    ]
  },
  devtool: 'cheap-module-eval-source-map',
  devServer: {
    contentBase: path.join(__dirname, 'public')
  }
};
```

C:\Users\Administrator\Desktop\abc.babelrc

```
{
	"presets": ["@babel/preset-env", "@babel/preset-react"],
	"plugins": ["@babel/plugin-proposal-class-properties"]
}
```

Đọc thêm&#x20;

<https://viblo.asia/p/reactjs-ket-hop-voi-webpack-part-1-Eb85ogr052G>

<https://viblo.asia/p/tu-setup-reactjs-project-co-ban-voi-webpack-4-va-babel-7-phan-1-63vKj2pxK2R>

<https://hashinteractive.com/blog/complete-guide-to-webpack-configuration-for-react/>

C:\Users\Administrator\Desktop\webpack\package.json

```
{
  "name": "webpack",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.11.6",
    "@babel/preset-env": "^7.11.5",
    "@babel/preset-react": "^7.10.4",
    "babel-loader": "^8.1.0",
    "html-loader": "^1.3.1",
    "html-webpack-plugin": "^4.5.0"
  },
  "dependencies": {
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12"
  }
}

```

C:\Users\Administrator\Desktop\webpack\webpack.config.js

```
const HtmlWebPackPlugin = require("html-webpack-plugin");
module.exports = {
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: "html-loader"
          }
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: "./src/index.html",
      filename: "./index.html"
    })
  ]
};
```

Hoặc chúng ta có thể chỉnh định tên thư mục và file trong trường hợp này là public và bundle.js

```
var path = require('path');
const HtmlWebPackPlugin = require("html-webpack-plugin");
module.exports = {
  output: {
    path: path.resolve(__dirname, 'public'),
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: "html-loader"
          }
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: "./src/index.html",
      filename: "./index.html"
    })
  ]
};
```

C:\Users\Administrator\Desktop\webpack\src\index.html

```
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>How to set up React, Webpack, and Babel</title>
</head>
<body>
<div id="container"></div>
</body>
</html>
```

C:\Users\Administrator\Desktop\webpack\src\js\components\Form.js

```
import React, { Component } from "react";
import ReactDOM from "react-dom";
class Form extends Component {
  constructor() {
    super();
    this.state = {
      value: ""
    };
    this.handleChange = this.handleChange.bind(this);
  }
  handleChange(event) {
    const { value } = event.target;
    this.setState(() => {
      return {
        value
      };
    });
  }
  render() {
    return (
      <form>
        <input
          type="text"
          value={this.state.value}
          onChange={this.handleChange}
        />
      </form>
    );
  }
}
export default Form;
const wrapper = document.getElementById("container");
wrapper ? ReactDOM.render(<Form />, wrapper) : false;
```

C:\Users\Administrator\Desktop\webpack\src\index.js

```
import Form from "./js/components/Form";
```

C:\Users\Administrator\Desktop\webpack.babelrc

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascriptuse.gitbook.io/javascript/advanced/5.-webpack-tao-project-reactjs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
