飞道的博客

webpack打包原理入门探究(四)插件探究(下)

515人阅读  评论(0)

webpack打包原理入门探究(四)插件探究(上)

webpack打包原理入门探究(三)入口探究

webpack打包原理入门探究(二)基本配置

webpack打包原理入门探究(一)

多页面 html 生成


   
  1. let path = require( 'path')
  2. let htmlWebpackPlugin = require( 'html-webpack-plugin')
  3. function resolve(o) {
  4. return path.resolve(__dirname, o)
  5. }
  6. module.exports = {
  7. entry: {
  8. main: resolve( 'src/scripts/main.js'),
  9. a: resolve( 'src/scripts/a.js'),
  10. b: resolve( 'src/scripts/b.js'),
  11. c: resolve( 'src/scripts/c.js')
  12. }, // 指定入口文件
  13. output: {
  14. path: resolve( 'dist'),
  15. // filename: '[name]-[hash].js'
  16. filename: "js/[name]-[chunkhash].js"
  17. },
  18. plugins: [
  19. new htmlWebpackPlugin({
  20. // filename: "index-[hash].html",
  21. // filename: 'index-[chunkhash].html',
  22. filename: "a.html", // 生成 dist/a.html
  23. template: 'index.html', // 指定根目录下的 index.html
  24. inject: 'head',
  25. title: 'html webpack plugin aaaaa'
  26. }),
  27. new htmlWebpackPlugin({
  28. // filename: "index-[hash].html",
  29. // filename: 'index-[chunkhash].html',
  30. filename: "b.html", // 生成 dist/b.html
  31. template: 'index.html', // 指定根目录下的 index.html
  32. inject: 'head',
  33. title: 'html webpack plugin bbbbb'
  34. }),
  35. new htmlWebpackPlugin({
  36. // filename: "index-[hash].html",
  37. // filename: 'index-[chunkhash].html',
  38. filename: "c.html", // 生成 dist/c.html
  39. template: 'index.html', // 指定根目录下的 index.html
  40. inject: 'head',
  41. title: 'html webpack plugin cccccc'
  42. }),
  43. ]
  44. }

多页面 html 生成 只加载对应 的chunks


   
  1. let path = require( 'path')
  2. let htmlWebpackPlugin = require( 'html-webpack-plugin')
  3. function resolve(o) {
  4. return path.resolve(__dirname, o)
  5. }
  6. module.exports = {
  7. entry: {
  8. main: resolve( 'src/scripts/main.js'),
  9. a: resolve( 'src/scripts/a.js'),
  10. b: resolve( 'src/scripts/b.js'),
  11. c: resolve( 'src/scripts/c.js')
  12. }, // 指定入口文件
  13. output: {
  14. path: resolve( 'dist'),
  15. // filename: '[name]-[hash].js'
  16. filename: "js/[name]-[chunkhash].js"
  17. },
  18. plugins: [
  19. new htmlWebpackPlugin({
  20. // filename: "index-[hash].html",
  21. // filename: 'index-[chunkhash].html',
  22. filename: "a.html", // 生成 dist/a.html
  23. template: 'index.html', // 指定根目录下的 index.html
  24. inject: 'body',
  25. chunks: [ 'main', 'a'],
  26. title: 'html webpack plugin aaaaa'
  27. }),
  28. new htmlWebpackPlugin({
  29. // filename: "index-[hash].html",
  30. // filename: 'index-[chunkhash].html',
  31. filename: "b.html", // 生成 dist/b.html
  32. template: 'index.html', // 指定根目录下的 index.html
  33. inject: 'body',
  34. chunks: [ 'b'],
  35. title: 'html webpack plugin bbbbb'
  36. }),
  37. new htmlWebpackPlugin({
  38. // filename: "index-[hash].html",
  39. // filename: 'index-[chunkhash].html',
  40. filename: "c.html", // 生成 dist/c.html
  41. template: 'index.html', // 指定根目录下的 index.html
  42. inject: 'body',
  43. chunks: [ 'c'],
  44. title: 'html webpack plugin cccccc'
  45. }),
  46. ]
  47. }

a.html


   
  1. <!DOCTYPE html>
  2. <html lang= "en">
  3. <head>
  4. <meta charset= "UTF-8">
  5. <meta name= "viewport" content= "width=device-width, initial-scale=1.0">
  6. <meta http-equiv= "X-UA-Compatible" content= "ie=edge">
  7. <title>html webpack plugin aaaaa</title>
  8. </head>
  9. <body>
  10. <!-- 哈哈哈哈哈 -->
  11. <h5>htmlWebpackPlugin object</h5>
  12. files
  13. ---
  14. [object Object]
  15. options
  16. ---
  17. [object Object]
  18. <h5>htmlWebpackPlugin.files</h5>
  19. publicPath
  20. ---
  21. chunks
  22. ---
  23. [object Object]
  24. js
  25. ---
  26. js/main -8a0feb42c16e4b4ccc97.js,js/a-a1ad07a706a1b2702814.js
  27. css
  28. ---
  29. manifest
  30. ---
  31. <h5>htmlWebpackPlugin.options</h5>
  32. template
  33. ---
  34. e:\webpackdemo\node_modules\_html-webpack-plugin@ 3.2 .0@html-webpack-plugin\lib\loader.js!e:\webpackdemo\index.html
  35. templateParameters
  36. ---
  37. function templateParametersGenerator (compilation, assets, options) {
  38. return {
  39. compilation: compilation,
  40. webpack: compilation.getStats().toJson(),
  41. webpackConfig: compilation.options,
  42. htmlWebpackPlugin: {
  43. files: assets,
  44. options: options
  45. }
  46. };
  47. }
  48. filename
  49. ---
  50. a.html
  51. hash
  52. ---
  53. false
  54. inject
  55. ---
  56. body
  57. compile
  58. ---
  59. true
  60. favicon
  61. ---
  62. false
  63. minify
  64. ---
  65. false
  66. cache
  67. ---
  68. true
  69. showErrors
  70. ---
  71. true
  72. chunks
  73. ---
  74. main,a
  75. excludeChunks
  76. ---
  77. chunksSortMode
  78. ---
  79. auto
  80. meta
  81. ---
  82. [object Object]
  83. title
  84. ---
  85. html webpack plugin aaaaa
  86. xhtml
  87. ---
  88. false
  89. <script type= "text/javascript" src= "js/main-8a0feb42c16e4b4ccc97.js"></script><script type= "text/javascript" src= "js/a-a1ad07a706a1b2702814.js"></script></body>
  90. </html>

b.html


   
  1. <!DOCTYPE html>
  2. <html lang= "en">
  3. <head>
  4. <meta charset= "UTF-8">
  5. <meta name= "viewport" content= "width=device-width, initial-scale=1.0">
  6. <meta http-equiv= "X-UA-Compatible" content= "ie=edge">
  7. <title>html webpack plugin bbbbb</title>
  8. </head>
  9. <body>
  10. <!-- 哈哈哈哈哈 -->
  11. <h5>htmlWebpackPlugin object</h5>
  12. files
  13. ---
  14. [object Object]
  15. options
  16. ---
  17. [object Object]
  18. <h5>htmlWebpackPlugin.files</h5>
  19. publicPath
  20. ---
  21. chunks
  22. ---
  23. [object Object]
  24. js
  25. ---
  26. js/b -4af1b36b33599d5438fc.js
  27. css
  28. ---
  29. manifest
  30. ---
  31. <h5>htmlWebpackPlugin.options</h5>
  32. template
  33. ---
  34. e:\webpackdemo\node_modules\_html-webpack-plugin@ 3.2 .0@html-webpack-plugin\lib\loader.js!e:\webpackdemo\index.html
  35. templateParameters
  36. ---
  37. function templateParametersGenerator (compilation, assets, options) {
  38. return {
  39. compilation: compilation,
  40. webpack: compilation.getStats().toJson(),
  41. webpackConfig: compilation.options,
  42. htmlWebpackPlugin: {
  43. files: assets,
  44. options: options
  45. }
  46. };
  47. }
  48. filename
  49. ---
  50. b.html
  51. hash
  52. ---
  53. false
  54. inject
  55. ---
  56. body
  57. compile
  58. ---
  59. true
  60. favicon
  61. ---
  62. false
  63. minify
  64. ---
  65. false
  66. cache
  67. ---
  68. true
  69. showErrors
  70. ---
  71. true
  72. chunks
  73. ---
  74. b
  75. excludeChunks
  76. ---
  77. chunksSortMode
  78. ---
  79. auto
  80. meta
  81. ---
  82. [object Object]
  83. title
  84. ---
  85. html webpack plugin bbbbb
  86. xhtml
  87. ---
  88. false
  89. <script type= "text/javascript" src= "js/b-4af1b36b33599d5438fc.js"></script></body>
  90. </html>

c.html


   
  1. <!DOCTYPE html>
  2. <html lang= "en">
  3. <head>
  4. <meta charset= "UTF-8">
  5. <meta name= "viewport" content= "width=device-width, initial-scale=1.0">
  6. <meta http-equiv= "X-UA-Compatible" content= "ie=edge">
  7. <title>html webpack plugin cccccc</title>
  8. </head>
  9. <body>
  10. <!-- 哈哈哈哈哈 -->
  11. <h5>htmlWebpackPlugin object</h5>
  12. files
  13. ---
  14. [object Object]
  15. options
  16. ---
  17. [object Object]
  18. <h5>htmlWebpackPlugin.files</h5>
  19. publicPath
  20. ---
  21. chunks
  22. ---
  23. [object Object]
  24. js
  25. ---
  26. js/c -5055009ef9e9d5580df1.js
  27. css
  28. ---
  29. manifest
  30. ---
  31. <h5>htmlWebpackPlugin.options</h5>
  32. template
  33. ---
  34. e:\webpackdemo\node_modules\_html-webpack-plugin@ 3.2 .0@html-webpack-plugin\lib\loader.js!e:\webpackdemo\index.html
  35. templateParameters
  36. ---
  37. function templateParametersGenerator (compilation, assets, options) {
  38. return {
  39. compilation: compilation,
  40. webpack: compilation.getStats().toJson(),
  41. webpackConfig: compilation.options,
  42. htmlWebpackPlugin: {
  43. files: assets,
  44. options: options
  45. }
  46. };
  47. }
  48. filename
  49. ---
  50. c.html
  51. hash
  52. ---
  53. false
  54. inject
  55. ---
  56. body
  57. compile
  58. ---
  59. true
  60. favicon
  61. ---
  62. false
  63. minify
  64. ---
  65. false
  66. cache
  67. ---
  68. true
  69. showErrors
  70. ---
  71. true
  72. chunks
  73. ---
  74. c
  75. excludeChunks
  76. ---
  77. chunksSortMode
  78. ---
  79. auto
  80. meta
  81. ---
  82. [object Object]
  83. title
  84. ---
  85. html webpack plugin cccccc
  86. xhtml
  87. ---
  88. false
  89. <script type= "text/javascript" src= "js/c-5055009ef9e9d5580df1.js"></script></body>
  90. </html>

多页面 html 生成 只加载对应 的 excludeChunks


   
  1. let path = require( 'path')
  2. let htmlWebpackPlugin = require( 'html-webpack-plugin')
  3. function resolve(o) {
  4. return path.resolve(__dirname, o)
  5. }
  6. module.exports = {
  7. entry: {
  8. main: resolve( 'src/scripts/main.js'),
  9. a: resolve( 'src/scripts/a.js'),
  10. b: resolve( 'src/scripts/b.js'),
  11. c: resolve( 'src/scripts/c.js')
  12. }, // 指定入口文件
  13. output: {
  14. path: resolve( 'dist'),
  15. // filename: '[name]-[hash].js'
  16. filename: "js/[name]-[chunkhash].js",
  17. // publicPath: ''
  18. },
  19. plugins: [
  20. new htmlWebpackPlugin({
  21. // filename: "index-[hash].html",
  22. // filename: 'index-[chunkhash].html',
  23. filename: "a.html", // 生成 dist/a.html
  24. template: 'index.html', // 指定根目录下的 index.html
  25. inject: 'body',
  26. chunks: [ 'main', 'a'],
  27. excludeChunks: [ 'b'],
  28. title: 'html webpack plugin aaaaa'
  29. }),
  30. new htmlWebpackPlugin({
  31. // filename: "index-[hash].html",
  32. // filename: 'index-[chunkhash].html',
  33. filename: "b.html", // 生成 dist/b.html
  34. template: 'index.html', // 指定根目录下的 index.html
  35. inject: 'body',
  36. chunks: [ 'b'],
  37. excludeChunks: [ 'c', 'a'],
  38. title: 'html webpack plugin bbbbb'
  39. }),
  40. new htmlWebpackPlugin({
  41. // filename: "index-[hash].html",
  42. // filename: 'index-[chunkhash].html',
  43. filename: "c.html", // 生成 dist/c.html
  44. template: 'index.html', // 指定根目录下的 index.html
  45. inject: 'body',
  46. chunks: [ 'c'],
  47. excludeChunks: [ 'a', 'b'],
  48. title: 'html webpack plugin cccccc'
  49. }),
  50. ]
  51. }

嗯,这一节课就到此为止了


转载:https://blog.csdn.net/qq_36772866/article/details/106513919
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场