[TOC]
## 1.配置前的准备//将仓库改为淘宝
* 使用webpack必须安装node.js环境;
### npm安装太慢可以安装cnpm,走淘宝的仓库
```
npm install -g cnpm --registry=https://registry.npm.taobao.org
```
## 2.webpack环境配置
```
npm init -y
npm i webpack webpack-cli -g
//走完这俩步如图 出现`webpack.json`文件
```
![](images/1.PNG)
```
npm i webpack webpack-cli --save-dev
//运行完成出现如下文件
```
![](https://box.kancloud.cn/0dd9c2db2c6283a35a63d496b8772608_333x237.PNG)
* 建一个 `src / index.js `文件和 `webpack.config.js `文件
* 文件内容
```
//index.js
var a = 10;
console.log(10);
```
[webpack官网](https://www.webpackjs.com/)
> webpack.config.js 直接复制官网
```
//webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
}
};
```
* 控制台输入`webpack` 出现如下图的文件及目录 `dist/bundle.js`
![](https://box.kancloud.cn/7b296c77a68fa83ec235d7f690294284_952x479.PNG)
## 3.html-webpack-plugin
### 3.1安装
```
npm i html-webpack-plugin --save-dev
```
### 3.2引入
在webpack.config.js文件中
```
//webpack.config.js
const htmlWebpackPlugin = require('html-webpack-plugin')
```
### 3.3 使用
```
//webpack.config.js
//在plugins中使用++
plugins:[
new htmlWebpackPlugin({
title:"打包文件",
inject:"head"
})
]
};
```
* webpack 出现 `dist/index.html`
![](https://box.kancloud.cn/a09272f9ff34adc7404f95e726a62e2b_973x509.PNG)
![](https://box.kancloud.cn/9cfa2f769be838165b3e4c47a953977e_1185x520.PNG)
## 4. style-loader,css-loader
### 4.1 安装
```
npm i style-loader css-loader --save-dev
```
### 4.2 在入口的js中引入css
![](https://box.kancloud.cn/7f2deb1e20ef44bf89fb3eb0146b36f0_744x529.PNG)
### 4.3 使用
```
module: {
rules: [
{
test: /\.css$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" }
]
}
]
},
```
![](https://box.kancloud.cn/f299334a26f634187c9f76cb20950d14_1023x616.PNG)
## 5.clean-webpack-plugin
## 5.1安装
```
npm i clean-webpack-plugin --save-dev
```
### 5.2引入
```
//webpack.config.js
const cleanWebpackPlugin = require('clean-webpack-plugin');
```
### 5.3 为了不重名 html 需要改成随机的
#### 1.删掉dist
#### 2.建一个 index.html
![](https://box.kancloud.cn/4a59c763b25a565187305b017e97733f_1205x545.PNG)
#### 3. 修改webpack.config.js
```
//webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin'); // 通过 npm 安装
const CleanWebpackPlugin = require('clean-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[hash].bundle.js'
},
plugins: [
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
title:"打包文件",
inject:"head",
template:"index.html",
filename:'[hash]-index.html',
date:new Date(),
minify:{
removeComments:true,
collapseInlineTagWhitespace:te=true,
collapseWhitespace:true
}
})
],
module: {
rules: [
{
test: /\.css$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" }
]
}
]
},
mode: 'production'
};
```
![](https://box.kancloud.cn/8c04fc62eaa53e7796aa4dee79caef1a_1196x978.PNG)
#### 在控制台输入`webpack` 打包
> 引入const CleanWebpackPlugin = require('clean-webpack-plugin') //删除多余的文件
* 生成文件夹 `dist` 及其内容文件
![](https://box.kancloud.cn/0afca961fa361b606dbdeb52637ac7e7_417x571.PNG)