# \[WEBPACK] 4. Webpack : CSS Loader (nghiepuit)

## Ví dụ 1: css in js

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

```
var path = require('path');
module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
  	rules: [
  		{
  			test: /\.js$/,
  			exclude: /(node_modules)/,
  			use: 'babel-loader',
  		},
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      }
  	]
  }
};
```

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

```
{
  "name": "webpack",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "scripts": {
    "start": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^8.1.0",
    "babel-preset-env": "^1.7.0",
    "css-loader": "^4.3.0",
    "style-loader": "^1.2.1",
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12"
  },
  "devDependencies": {
    "@babel/core": "^7.11.6",
    "@babel/preset-env": "^7.11.5"
  }
}

```

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

```
import './image.css';
import './image.js';
```

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

```
const image = document.createElement("img");
image.src = "https://loremflickr.com/320/240";
image.alt = "Lorem ipsum dolor sit amet";
document.body.appendChild(image);
```

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

```
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<p>Lorem ipsum dolor sit amet.</p>
	<script type="text/javascript" src="bundle.js"></script>
</body>
</html>
```

C:\Users\Administrator\Desktop\webpack\src\image.css

```
img {
	border: 5px solid red;
	padding: 5px;
}
```

![](https://2726517656-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M1E4Gk2ppVKb4olmnun%2F-MITDFaqDjWI6bvFTZlo%2F-MITDRNPGdnPClaxHNPQ%2FScreenshot_1.png?alt=media\&token=72bf8061-2fab-47d3-9fe4-db6201b1a0e1)

## Ví dụ 2: Tách file css

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

```
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');
module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  plugins: [
    new MiniCssExtractPlugin({
      // Options similar to the same options in webpackOptions.output
      // both options are optional
      filename: '[name].css',
      chunkFilename: '[id].css',
    }),
  ],
  module: {
  	rules: [
  		{
  			test: /\.js$/i,
  			exclude: /(node_modules)/,
  			use: 'babel-loader',
  		},
      {
        test: /\.css$/i,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: {
              publicPath: 'dist/',
            },
          },
          'css-loader',
        ],
      }
  	]
  }
};
```

C:\Users\Administrator\Desktop\webpack\dist\main.css

```
img {
	border: 5px solid red;
	padding: 5px;
}
```

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

```
{
  "name": "webpack",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "scripts": {
    "start": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^8.1.0",
    "babel-preset-env": "^1.7.0",
    "css-loader": "^4.3.0",
    "style-loader": "^1.2.1",
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12"
  },
  "devDependencies": {
    "@babel/core": "^7.11.6",
    "@babel/preset-env": "^7.11.5",
    "mini-css-extract-plugin": "^0.11.2"
  }
}

```

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

```
import './image.css';
import './image.js';
```

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

```
const image = document.createElement("img");
image.src = "https://loremflickr.com/320/240";
image.alt = "Lorem ipsum dolor sit amet";
document.body.appendChild(image);
```

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

```
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<p>Lorem ipsum dolor sit amet.</p>
	<script type="text/javascript" src="bundle.js"></script>
</body>
</html>
```

C:\Users\Administrator\Desktop\webpack\src\image.css

```
img {
	border: 5px solid red;
	padding: 5px;
}
```


---

# 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/advanced/4.-webpack-css-loader.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.
