初始化

  1. npm init -y

安装webpack、webpack-cli

  1. npm i webpack webpack-cli -D

创建文件

image.png
hello-world.js

  1. function helloWorld() {
  2. console.log('hello world');
  3. }
  4. export default helloWorld

index.js

  1. import helloWorld from './hello-world'
  2. helloWorld()

查看当前路径 image.png

运行

因为这里不是全局安装,所以不能直接使用 webpack 的命令 但是可以使用 npx 这个命令 npx 会自动查找当前依赖包中的可执行文件,如果找不到,就会去 PATH 里找。如果依然找不到,就会帮你安装

查看 webpack 的版本

  1. node_modules/.bin/webpack -v
  2. # or npx webpack -v

image.png
在当前项目目录下运行webpack

  1. node_modules/.bin/webpack
  2. # or npx webpack

image.png
现在已经生成了打包后的文件
image.png

webpack —stats detailed 查看打包信息 webpack —help 查看相关命令

image.png
上面的意思就是 运行 webpack 的时候设置入口 设置开发模式

文件配置

一些webpack的参数在 webpack.config.js 中进行配置

  1. const path = require('path')
  2. module.exports = {
  3. // 入口
  4. entry: './src/index.js',
  5. // 输出路径
  6. output: {
  7. // 输出路径文件名称
  8. filename: 'bundle.js',
  9. // 输出路径出口路径 __dirname当前文件目录
  10. path: path.resolve(__dirname,'./dist')
  11. },
  12. // 模式 开发模式 or 生产模式
  13. mode:'none'
  14. }

插件

插件列表:https://webpack.docschina.org/plugins/

通俗点在webpack中,提供额外功能的东西就叫做插件
image.png

HtmlWebpackPlugin

这个插件可以复制一份html文件并引入打包的资源
安装

  1. npm i html-webpack-plugin -D

引入使用
单独 new HtmlWebpackPlugin() 会自动生成一个html文件,进行配置后会使用你设置的模板

  1. const path = require('path')
  2. const HtmlWebpackPlugin = require('html-webpack-plugin')
  3. module.exports = {
  4. // 入口
  5. entry: './src/index.js',
  6. // 出口
  7. output: {
  8. // 打包文件名称
  9. filename: 'bundle.js',
  10. // 打包出口路径 __dirname当前文件目录
  11. path: path.resolve(__dirname,'./dist')
  12. },
  13. // 模式 开发模式 or 生产模式
  14. mode:'none',
  15. plugins: [
  16. new HtmlWebpackPlugin({
  17. template: './index.html',
  18. filename: 'app.name',
  19. inject: 'body'
  20. })
  21. ]
  22. }

清除dist

在 output 中设置 clean: true 就可以了
image.png

mode选项

有两个选项 development 和 production

source map

因为打包后的js文件不易阅读,报错话不能定位到原来位置,需要进行设置 devtool: ‘inline-source-map’

  1. const path = require('path')
  2. const HtmlWebpackPlugin = require('html-webpack-plugin')
  3. module.exports = {
  4. // 入口
  5. entry: './src/index.js',
  6. // 出口
  7. output: {
  8. // 打包文件名称
  9. filename: 'bundle.js',
  10. // 打包出口路径 __dirname当前文件目录
  11. path: path.resolve(__dirname,'./dist'),
  12. // 每次打包清除 dist 文件夹的内容
  13. clean: true
  14. },
  15. // 模式 开发模式 or 生产模式
  16. mode:'development',
  17. devtool: 'inline-source-map',
  18. plugins: [
  19. new HtmlWebpackPlugin({
  20. template: './index.html',
  21. filename: 'app.html',
  22. inject: 'body'
  23. })
  24. ]
  25. }

使用watch mode

image.png

webpack-dev-server

这个插件可以实时加载页面

  1. npm i webpack-dev-server -D

配置

  1. devServer: {
  2. static: './dist'
  3. }

启动

  1. npx webpack-dev-server

资源模块

webpack5 中使用了资源模块(asset modules)来引入各种类型的资源

resource 资源

resource 会发送一个单独的文件并导出URL

generator 和 assetModuleFilename 控制了打包后的资源文件夹和文件名,不过generator 的优先级更高
8db63a1b-5638-42b8-8d7f-8c185f6da093.png

image.png

contentbash:生成哈希 ext:原始后缀

image.png
生成10位hash
image.png

inline 资源

inline 资源用于导出一个资源的 Data URL,如svg文件
image.png

source 资源

用于导出资源的源代码

  1. module: {
  2. rules: [
  3. {
  4. test: /\.png$/,
  5. type: 'asset/resource',
  6. generator: {
  7. filename: 'image/[hash:10][ext]'
  8. }
  9. },
  10. {
  11. test: /\.svg/,
  12. type: 'asset/inline'
  13. },
  14. {
  15. test: /\.txt/,
  16. type: 'asset/source'
  17. }
  18. ]
  19. }

asset 资源

会在导出一个Data URL 与发送一个 文件之间自动进行选择

  1. module: {
  2. rules: [
  3. {
  4. test: /\.png$/,
  5. type: 'asset/resource',
  6. generator: {
  7. filename: 'image/[hash:10][ext]'
  8. }
  9. },
  10. {
  11. test: /\.svg$/,
  12. type: 'asset/inline'
  13. },
  14. {
  15. test: /\.txt$/,
  16. type: 'asset/source'
  17. },
  18. {
  19. test: /\.jpg$/,
  20. type: 'asset',
  21. parser: {
  22. dataUrlCondition: {
  23. maxSize: 4 * 1024 * 1024 // 大于 4M 的不进行处理,小于 4M转成base64
  24. }
  25. }
  26. }
  27. ]
  28. }
  29. }

Loader

webpack本身只能理解js和json,其他类型的文件(css、less…)就需要loader来解析

加载css

解析css安装两个loader,按顺序从右往左执行

  1. npm i css-loader style-laoder -D
  1. module: {
  2. rules: [
  3. // 详细loader配置
  4. // 不同文件必须配置不同loader
  5. {
  6. // 匹配哪些文件
  7. test: /\.css$/,
  8. // 使用哪些loader进行处理
  9. use: [
  10. // use数组中loader执行顺序:从右到左,从下到上 依次执行
  11. // 创建style 标签,将js中的样式资源进行插入,添加到head中生效
  12. 'style-loader',
  13. // css文件变成commonjs模块 加载js中,里面的内容是样式字符串
  14. 'css-loader'
  15. ]
  16. },
  17. ]
  18. }

解析less 需要安装如下loader

  1. npm i less-loader less -D
  1. {
  2. test: /\.less$/,
  3. use: [
  4. 'style-loader',
  5. 'css-loader',
  6. // less文件编译成 css文件
  7. 'less-loader'
  8. ]
  9. },

css 和 less 写在一起

  1. {
  2. test: /\.(css|less$)/,
  3. use: ['style-loader','css-loader','less-loader']
  4. }

抽离和压缩css

抽离css需要安装以下插件 mini-css-extract-plugin
安装

  1. npm i mini-css-extract-plugin -D

使用
image.png
设置导出路径和名称
image.png

压缩
安装

  1. npm i css-minimizer-webpack-plugin -D

使用
image.png

  1. const path = require('path')
  2. const HtmlWebpackPlugin = require('html-webpack-plugin')
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  4. // css代码压缩
  5. const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
  6. module.exports = {
  7. // 入口
  8. entry: './src/index.js',
  9. // 出口
  10. output: {
  11. // 打包文件名称
  12. filename: 'bundle.js',
  13. // 打包出口路径 __dirname当前文件目录
  14. path: path.resolve(__dirname, './dist'),
  15. // 每次打包清除 dist 文件夹的内容
  16. clean: true
  17. },
  18. // 模式 开发模式 or 生产模式
  19. mode: 'production',
  20. devtool: 'inline-source-map',
  21. plugins: [
  22. new HtmlWebpackPlugin({
  23. template: './index.html',
  24. filename: 'index.html',
  25. inject: 'body'
  26. }),
  27. new MiniCssExtractPlugin({
  28. filename: 'styles/[contenthash].css'
  29. })
  30. ],
  31. devServer: {
  32. static: './dist'
  33. },
  34. module: {
  35. rules: [
  36. {
  37. test: /\.png$/,
  38. type: 'asset/resource',
  39. generator: {
  40. filename: 'image/[hash:10][ext]'
  41. }
  42. },
  43. {
  44. test: /\.svg/,
  45. type: 'asset/inline'
  46. },
  47. {
  48. test: /\.txt/,
  49. type: 'asset/source'
  50. },
  51. {
  52. test: /\.jpg$/,
  53. type: 'asset',
  54. parser: {
  55. dataUrlCondition: {
  56. maxSize: 4 * 1024 * 1024 // 大于 4M 的不进行处理,小于 4M转成base64
  57. }
  58. }
  59. },
  60. {
  61. test: /\.(css|less$)/,
  62. use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader']
  63. }
  64. ]
  65. },
  66. // 优化配置
  67. optimization: {
  68. minimizer: [
  69. new CssMinimizerPlugin()
  70. ]
  71. }
  72. }

处理字体

  1. {
  2. test: /\.(woff|woff2|eot|ttf|otf)$/i,
  3. type: 'asset/resource'
  4. },

加载数据

此外,可以加载的有用资源还有数据,如 JSON 文件,CSV、TSV 和 XML。类似于 NodeJS,JSON 支持实际上是内置的,也就是说 import Data from ‘./data.json’ 默认将正常运行。要导入 CSV、TSV 和 XML,你可以使用 csvloader 和 xml-loader。让我们处理加载这三类文件:

  1. npm i csv-loader xml-loader -D
  1. {
  2. test: /\.(csv|tsv)$/,
  3. use: 'csv-loader'
  4. },
  5. {
  6. test: /\.xml$/,
  7. use: 'xml-loader'
  8. }

自定义JSON模块parser

通过使用 自定义 parser 替代特定的 webpack loader,可以将任何 toml 、 yaml 或 json5 文件作为 JSON 模块导入。

  1. npm i toml yaml json5 -D
  1. module: {
  2. rules: [
  3. {
  4. test: /\.toml$/i,
  5. type: 'json',
  6. parser: {
  7. parse: toml.parse
  8. }
  9. },
  10. {
  11. test: /\.yaml$/i,
  12. type: 'json',
  13. parser: {
  14. parse: yaml.parse
  15. }
  16. },
  17. {
  18. test: /\.json5$/i,
  19. type: 'json',
  20. parser: {
  21. parse: json5.parse
  22. }
  23. }
  24. ]
  25. },

babel-loader

把 es6 的代码转化成 es5

  1. npm install -D babel-loader @babel/core @babel/preset-env