webpack的功能

  1. 打包:将不同的类型的资源按照模块处理进行打包
  2. 静态:打包后最终产出静态资源
  3. webpack支持不同规范的模块化开发

体验webpack

依赖导图

文件

体验 - 图1

webpack.config.js 依赖

体验 - 图2

babel.config.js依赖

体验 - 图3

postcss.config.js依赖

体验 - 图4

安装流程

安装webpack

  1. yarn webpack webpack-cli

新建webpack配置文件

  1. touch webpack.config.js

安装webpack 插件依赖

  1. yarn add html-webpack-plugin copy-webpack-plugin webpack-dev-server -D

安装 webpack loader

  1. yarn add css-loader style-loader postcss-loader postcss postcss-preset-env sass-loader sass -D

安装babel preset 和插件

  1. yarn add babel-loader @babel/core @babel/preset-env core-js@3 -D

新建 babel.config.js 文件

  1. {
  2. "presets": [
  3. [
  4. "@babel/preset-env",
  5. {
  6. // 按需加载
  7. "useBuiltIns": "usage",
  8. "corejs": {
  9. "version": 3
  10. }
  11. }
  12. ]
  13. ]
  14. }

新建 .browserslistrc 文件

  1. >1%
  2. last 2 version
  3. ie 9-11

新建 postcss.config.js 文件

  1. module.exports = {
  2. plugins: ["postcss-preset-env"],
  3. };

webpack.config.js 文件内容

  1. const path = require('path');
  2. const htmlWebpackPlugin = require('html-webpack-plugin');
  3. const { DefinePlugin } = require('webpack');
  4. const CopyWebpackPlugin = require('copy-webpack-plugin');
  5. const publicPath = '';
  6. module.exports = {
  7. mode: 'development',
  8. target: 'web',
  9. entry: './src/index.js',
  10. output: {
  11. filename: 'index.js',
  12. path: path.resolve(__dirname, 'dist'),
  13. publicPath: publicPath,
  14. clean: true,
  15. },
  16. resolve: {
  17. alias: {
  18. '@': path.resolve(__dirname, './src'),
  19. },
  20. },
  21. devServer: {
  22. port: '3001', // 默认是 8080
  23. hot: true,
  24. hotOnly: true,
  25. stats: 'errors-only', // 终端仅打印 error
  26. compress: true, // 是否启用 gzip 压缩
  27. publicPath: publicPath,
  28. contentBase: path.resolve(__dirname, './public'),
  29. watchContentBase: true,
  30. proxy: {
  31. '/api': {
  32. target: 'http://0.0.0.0:80',
  33. pathRewrite: {
  34. '/api': '',
  35. },
  36. },
  37. },
  38. },
  39. module: {
  40. rules: [
  41. {
  42. test: /\.css$/,
  43. use: [
  44. 'style-loader',
  45. {
  46. loader: 'css-loader',
  47. options: {
  48. importLoaders: 1,
  49. },
  50. },
  51. 'postcss-loader',
  52. ],
  53. },
  54. {
  55. test: /\.scss$/,
  56. use: [
  57. 'style-loader',
  58. {
  59. loader: 'css-loader',
  60. options: {
  61. importLoaders: 2,
  62. },
  63. },
  64. 'postcss-loader',
  65. 'sass-loader',
  66. ],
  67. },
  68. {
  69. test: /\.(png|jpe?g|gif|svg)$/,
  70. // type: "asset/resource", file-loader的效果
  71. // type: "asset/inline", url-loader
  72. type: 'asset',
  73. generator: {
  74. filename: 'img/[name].[hash:6][ext]',
  75. },
  76. parser: {
  77. dataUrlCondition: {
  78. maxSize: 1 * 1024,
  79. },
  80. },
  81. },
  82. {
  83. test: /\.js$/,
  84. use: 'babel-loader',
  85. exclude: [[path.resolve(__dirname, 'node_modules')]],
  86. },
  87. ],
  88. },
  89. plugins: [
  90. new htmlWebpackPlugin({
  91. template: path.resolve(__dirname, './public/index.html'),
  92. title: 'webpack5配置文件',
  93. }),
  94. new DefinePlugin({
  95. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  96. }),
  97. new CopyWebpackPlugin({
  98. patterns: [
  99. {
  100. from: 'public',
  101. to: 'static',
  102. globOptions: {
  103. ignore: ['**/index.html', '**/.DS_Store', '**/abc.txt'],
  104. },
  105. },
  106. ],
  107. }),
  108. ],
  109. };

依赖总结

webpack5

  • webpack
  • webpack-cli
  • plugin
    • html-webpack-plugin
    • copy-webpack-plugin
    • webpack-dev-server
  • loader
    • css-loader style-loader postcss-loader sass-loader babel-loader
  • 其他
    • sass

babel 插件

  • @babel/core
  • @babel/preset-env
  • core-js@3

postcss 插件

  • postcss
  • ppostcss-preset-env