webpack构建工具配置和常用插件总结

webpack构建工具已经火了好几年,也是当下狠火狠方便的构建工具,我们没有理由不去学习。既然选择webpack就要跟着时代走,我们要追随大牛的步伐,大牛等等我。

一、webpack基础 

1. 在根目录使用npm init 命令创建package.json,创建过程中一路回车。
2. 本地安装webpack命令:npm install webpack webpack-cli --save-dev 安装成功写入package.js的devDependencies中,可以通过 npm node_modules/.bin/webpack -v 命令查看它的版本。
3. 全局安装npm install -g webpack 不推荐全局安装 webpack。它会把你的项目锁定全局安装的版本,也可能导致不同的webpack版本中构建失败。

静态资源文件目录

> ​ -- src [ 项目源文件目录 ]> ​ -- dist [ 打包文件目录 ]> ​ -- dist [ 打包文件目录 ]> ​ -- webpack.config.js [ webpack配置文件 ]

> ​ -- package.json [ NPM包管理配置文件 ]

> ​ -- node_modules [ 项目中的依赖存放目录 ]

二、webpack.config.js

 1 const webpack = require('webpack');
 2 module.exports = {
 3   mote:"development",//指当前的环境
 4   /*
 5     development:开发环境
 6     production:生产环境
 7     none:不做任何处理
 8   */
 9   //入口文件,如果需求多个入口可改为对象
10     entry: './src/index.js',
11   /*
12    hot配置是否启用模块的热替换功能,devServer的默认行为是在发现代码被改后通过自动刷新整个页面来做到事实预览,然后设置hot后,
13    会在不刷新整个页面的情况下用新模块替换老模块来做到实时更新。
14    如果选用hot:true方式来达到热更新的效果需要引用webpack.HotModuleReplacementPlugin插件配合达到你需要的效果,另外推荐一
15    种简便的方式在package.json中scripts设置如下
16    "scripts": {
17     "start": "webpack-dev-server --hot --inline",
18    },
19   */
20   // 服务器环境
21   devServer: {
22     hot: true, //建议使用第二种方案 启动服务使用命令 npm run start
23     port:"8080",//端口默认8080,可以自行设置
24     host:"192.168.xx.xx",
25     /*
26       host配置devServer服务监听的地址,也可以使用localhost进行访问 浏览器输入192.168.xx.xx/index.html
27       简便方法在package.json中设置如下
28       "scripts": {
29         "start": "webpack-dev-server --hot --inline ",
30       },
31 */
32 
33 },
34   //插件
35   plugins: [
36     //热加载插件
37     new webpack.HotModuleReplacementPlugin(), 
38   ],
39   //输出
40   output: {
41     //filename:输出的文件名,可以自定义一些规则
42     filename: '[name].bundle.js',
43     //path,配置输出文件存放在本地的目录
44     path: path.resolve(__dirname, 'dist')
45   }
46 };

三、插件

1、HtmlWebpackPlugin

 1 const HtmlWebpackPlugin = require('html-webpack-plugin')
 2 plugins: [
 3   new HtmlWebpackPlugin({ // 打包输出HTML
 4   title: 'Hello World',//文件的标题
 5   minify: { //minify 的作用是对 html 文件进行压缩
 6     removeComments: true, // 移除HTML中的注释
 7     collapseWhitespace: true, // 删除空白符与换行符
 8     minifyCSS: true, // //是否压缩html里的css 默认值false;
 9     caseSensitive: true, //是否对大小写敏感,默认false
10     ollapseWhitespace: true, //是否去除空格,默认false
11     minifyJS: true, //是否压缩html里的js
12     removeAttributeQuotes: true, //是否移除属性的引号 默认false
13     removeComments: true, //是否移除注释 默认false
14     emoveCommentsFromCDATA: true, //从脚本和样式删除的注释 默认false
15     emoveEmptyAttributes: true, //是否删除空属性,默认false
16     removeRedundantAttributes: true, //删除多余的属性
17     removeScriptTypeAttributes: true, //删除script的类型属性,在h5下面script的type默认值:text/javascript 默认值false
18   },
19   filename: 'index.html', //输出的html的文件名称
20   template: 'index.html', //html模板在的文件路径
21   hash: true,//hash作用是给生成的js文件一个独特的hash值,默认值为false 被构建过后的html引用js效果如下
22     // <script type=text/javascript src=bundle.js?22b9692e22e7be37b57e></script>
23   }),
24 ]

 


2、CleanWebpackPlugin

1 const { CleanWebpackPlugin } = require('clean-webpack-plugin');
2 plugins: [
3   //该插件主要用于自动删除webpack里dist目录中已不需要的文件
4   new CleanWebpackPlugin()
5 ]

 


官方介绍:

By default, this plugin will remove all files inside webpack's output.path directory, as well as all unused webpack assets after every successful rebuild

3、ExtractTextWebpackPlugin

先把我们需要的东西下载好
npm install webpack css-loader style-loader extract-text-webpack-plugin --save-dev

1. 首先配置webpack,先不使用插件完成

 1 module.exports = {
 2   module : {
 3     rules: [
 4       {
 5         test: /\.css$/,
 6         use:[
 7             { loader: "style-loader" },
 8             { loader: "css-loader" }
 9         ]
10       }
11     ]
12   }
13 }

 


配置好之后在js入口文件中引入样式文件,打包查看打包结果bundle.js,会发现css被打包到了js里面,以字符串的形式存在。如果index.html中已引入打包后的bundle.js,使用浏览器打开index.html文件会发现css以style的形式被插到了head当中。

2.使用插件

 1 const path = require('path');
 2 const ExtractTextPlugin = require("extract-text-webpack-plugin");
 3 const HtmlWebpackPlugin = require('html-webpack-plugin');
 4 module.exports = {
 5   module : {
 6     rules: [
 7        {
 8         test: /\.css$/,
 9         use : ExtractTextPlugin.extract({
10           fallback: "style-loader",
11           use: "css-loader"
12         })
13        }
14 /*
15 use: 指需要什么样的loader去编译文件
16 fallback: 编译后用什么loader来提取css文件
17 */
18     ]
19   },
20   plugins: [
21     new ExtractTextPlugin("styles.css"),
22     new HtmlWebpackPlugin({ // 关于HtmlWebpackPlugin上文有提到
23       title: 'extract-text-webpack-plugin',
24       filename: 'index.html',
25       template: path.resolve(__dirname, 'index.html'),
26       inject : 'head'
27     })
28   ]
29 }

 

以上内容配置好之后打包查看,可以发现css文件以link的方式被引在index.html的head中。期间配合HtmlWebpackPlugin插件自动插入index.html中

本人才疏学浅,在此记录一下学习的过程也有很多不懂的地方通过查阅一些资料进行一个总结,也是怕忘了,如果有不对的地方还请多多指正。

posted @ 2020-05-10 21:06  self-challenge  阅读(190)  评论(0)    收藏  举报