输出带指纹文件

我们可以使用output.filename 替换设置来定义输出文件的名称。webpack提供了一种使用称为替换的括号字符串模板化文件名的方法。该[contenthash]替代将增加基于资产的内容的唯一哈希值。当资产的内容发生变化时,[contenthash]也会发生变化。

  1. const path = require('path');
  2. const CleanWebpackPlugin = require('clean-webpack-plugin');
  3. const HtmlWebpackPlugin = require('html-webpack-plugin');
  4. module.exports = {
  5. entry: './src/index.js',
  6. plugins: [
  7. new CleanWebpackPlugin(['dist']),
  8. new HtmlWebpackPlugin({
  9. - title: 'Output Management'
  10. + title: 'Caching'
  11. })
  12. ],
  13. output: {
  14. - filename: 'bundle.js',
  15. + filename: '[name].[hash].js',
  16. path: path.resolve(__dirname, 'dist')
  17. }
  18. };

提取依赖

  1. // webpack.common.js
  2. const path = require('path');
  3. const HtmlWebpackPlugin = require('html-webpack-plugin'); // 自动生成页面
  4. const CleanWebpackPlugin = require('clean-webpack-plugin'); // 自动清理,清理dist旧文件
  5. const webpack = require('webpack');
  6. module.exports = {
  7. // =============
  8. optimization: {
  9. runtimeChunk: 'single',
  10. splitChunks: {
  11. cacheGroups: {
  12. vendor: {
  13. test: /[\\/]node_modules[\\/]/,
  14. name: 'vendors',
  15. chunks: 'all'
  16. }
  17. }
  18. }
  19. },
  20. // =============
  21. entry: {
  22. app: './src/index.js',
  23. // print: './src/print.js',
  24. },
  25. plugins: [
  26. new CleanWebpackPlugin(['dist']),
  27. new HtmlWebpackPlugin({
  28. title: 'Output Management'
  29. }),
  30. new webpack.HotModuleReplacementPlugin(),
  31. // =============
  32. new webpack.HashedModuleIdsPlugin(),
  33. // ==============
  34. ],
  35. output: {
  36. filename: '[name].[hash].js',
  37. chunkFilename: '[name].[contenthash].js',
  38. path: path.resolve(__dirname, 'dist'),
  39. publicPath: '/'
  40. },
  41. module: {
  42. rules: [
  43. {
  44. test: /\.css$/,
  45. use: [
  46. 'style-loader',
  47. 'css-loader'
  48. ]
  49. },
  50. {
  51. test: /\.(png|svg|jpg|gif)$/,
  52. use: [
  53. 'file-loader'
  54. ]
  55. },
  56. {
  57. test: /\.(woff|woff2|eot|ttf|otf)$/,
  58. use: [
  59. 'file-loader'
  60. ]
  61. },
  62. {
  63. test: /\.(csv|tsv)$/,
  64. use: [
  65. 'csv-loader'
  66. ]
  67. },
  68. {
  69. test: /\.xml$/,
  70. use: [
  71. 'xml-loader'
  72. ]
  73. }
  74. ],
  75. }
  76. };

输出:main的体积变小了。并且还有一个优势,当文件未发生改变时,hash值不变
第一次

  1. Asset Size Chunks Chunk Names
  2. 1ebd0482aadade65f20ec178219fe012.woff2 14.5 KiB [emitted]
  3. app.af00b1a65f2ce2ee3111.js 869 bytes 0 [emitted] app
  4. d19378a95ebe6b15d5ddea281138dcf4.svg 623 bytes [emitted]
  5. index.html 286 bytes [emitted]
  6. runtime.de5527b10e57b056999e.js 9.18 KiB 1 [emitted] runtime
  7. vendors.9d6dbdd696263a8519eb.js 69.4 KiB 2 [emitted] vendors
  8. Entrypoint app = runtime.de5527b10e57b056999e.js app.af00b1a65f2ce2ee3111.js

第二次

  1. Asset Size Chunks Chunk Names
  2. 1ebd0482aadade65f20ec178219fe012.woff2 14.5 KiB [emitted]
  3. app.af00b1a65f2ce2ee3111.js 869 bytes 0 [emitted] app
  4. d19378a95ebe6b15d5ddea281138dcf4.svg 623 bytes [emitted]
  5. index.html 286 bytes [emitted]
  6. runtime.de5527b10e57b056999e.js 9.18 KiB 1 [emitted] runtime
  7. vendors.9d6dbdd696263a8519eb.js 69.4 KiB 2 [emitted] vendors
  8. Entrypoint app = runtime.de5527b10e57b056999e.js app.af00b1a65f2ce2ee3111.js

当index.js发生改变时

  1. 1ebd0482aadade65f20ec178219fe012.woff2 14.5 KiB [emitted]
  2. app.89ef43d73c4a3ce088d0.js 869 bytes 0 [emitted] app
  3. d19378a95ebe6b15d5ddea281138dcf4.svg 623 bytes [emitted]
  4. index.html 286 bytes [emitted]
  5. runtime.5dea5599b4845d56ae40.js 9.18 KiB 1 [emitted] runtime
  6. vendors.9d6dbdd696263a8519eb.js 69.4 KiB 2 [emitted] vendors
  7. Entrypoint app = runtime.5dea5599b4845d56ae40.js app.89ef43d73c4a3ce088d0.js