Vue-CLI@4——html-webpack-plugin默认配置的获取与修改

获取默认配置

配置vue.config.js

在项目根目录下创建vue.config.js文件,键入如下代码:

 
  1. const HtmlWebpackPlugin = require('html-webpack-plugin')

  2.  
  3. module.exports = {

  4.     configureWebpack: config => {

  5.         config.plugins.forEach((val) => {

  6.             if (val instanceof HtmlWebpackPlugin) {

  7.                 console.log(val)

  8.                 console.log(val.options.templateParameters.toString())

  9.             }

  10.         })    

  11.     }

  12. }

其中config就是vue-cli中,webpack的默认配置

修改eslintrc.js

在该文件的eslintre.js中找到rules字段,删除no-console:

运行npm run build

运行npm run build ,控制台就会打印出相关的配置

有些外部工具可能需要通过一个文件访问解析好的 webpack 配置,比如那些需要提供 webpack 配置路径的 IDE 或 CLI。在这种情况下你可以使用如下路径:

<projectRoot>/node_modules/@vue/cli-service/webpack.config.js

html-webpack-plugin默认配置

 
  1. {

  2. options: {

  3. template: 'D:\\nodeProgram\\vue-demo\\public\\index.html',

  4. templateParameters: (compilation, assets, pluginOptions) => {

  5. // enhance html-webpack-plugin's built in template params

  6. let stats

  7. return Object.assign({

  8. // make stats lazy as it is expensive

  9. get webpack () {

  10. return stats || (stats = compilation.getStats().toJson())

  11. },

  12. compilation: compilation,

  13. webpackConfig: compilation.options,

  14. htmlWebpackPlugin: {

  15. files: assets,

  16. options: pluginOptions

  17. }

  18. }, resolveClientEnv(options, true /* raw */))

  19. },

  20. filename: 'index.html',

  21. hash: false,

  22. inject: true,

  23. compile: true,

  24. favicon: false,

  25. minify: {

  26. removeComments: true,

  27. collapseWhitespace: true,

  28. removeAttributeQuotes: true,

  29. collapseBooleanAttributes: true,

  30. removeScriptTypeAttributes: true

  31. },

  32. cache: true,

  33. showErrors: true,

  34. chunks: 'all',

  35. excludeChunks: [],

  36. chunksSortMode: 'auto',

  37. meta: {},

  38. title: 'Webpack App',

  39. xhtml: false

  40. }

  41. }

参数详解

title:生成的html文档的标题。配置该项,它并不会替换指定模板文件中的title元素的内容,除非html模板文件中使用了模板引擎语法来获取该配置项值,如下ejs模板语法形式:

<title>{%= o.htmlWebpackPlugin.options.title %}</title>

filename:输出文件的文件名称,默认为index.html,不配置就是该文件名;此外,还可以为输出文件指定目录位置(例如'html/index.html')

 
  1. 1、filename配置的html文件目录是相对于webpackConfig.output.path路径而言的,不是相对于当前项目目录结构的。

  2. 2、指定生成的html文件内容中的link和script路径是相对于生成目录下的,写路径的时候请写生成目录下的相对路径。

template:指定你生成的文件所依赖哪一个html文件模板,支持加载器(如handlebars、ejs、undersore、html等),使用自定义的模板文件的时候,需要安装对应的loader

inject :向template或者templateContent中注入所有静态资源,不同的配置值注入的位置不经相同。

 
  1. true //默认值,script标签位于html文件的 body 底部

  2. body //script标签位于html文件的 body 底部

  3. head //script标签位于html文件的 head中

  4. false //不插入生成的js文件,这个几乎不会用到的

templateContent: string|function,可以指定模板的内容,不能与template共存。配置值为function时,可以直接返回html字符串,也可以异步调用返回html字符串。

favicon:html文件favicon,添加特定favicon路径到输出的html文档中,这个同title配置项,需要在模板中动态获取其路径值

hash:true|false,是否为所有注入的静态资源添加webpack每次编译产生的唯一hash值,添加hash形式如下所示:

html <script type="text/javascript" src="common.js?a3e1396b501cdd9041be"></script>

minify: {....}|false;minify会对生成的html文件进行压缩。默认是false,可以对minify进行配置

传递 html-minifier 选项给 minify 输出,false就是不使用html压缩,minify具体配置参数请点击html-minifier

 
  1. plugins: [

  2.     new HtmlWebpackPlugin({

  3.         minify: {

  4.             removeAttributeQuotes: true // 移除属性的引号

  5.         }

  6.     })

  7. ]

cache:true|fasle, 默认true;内容变化的时候生成一个新的文件,如果为true表示在对应的thunk文件修改后就会emit文件。

showErrors: true|false,默认true;是否将错误信息输出到html页面中。webpack报错的时候,会把错误信息包裹再一个pre中,默认是true。在生成html文件的过程中有错误信息,输出到页面就能看到错误相关信息便于调试。

chunks:chunks主要用于多入口文件,当你有多个入口文件,那就回编译后生成多个打包后的文件,选择使用那些js文件 ,没有设置chunks选项,那么默认是全部显示(当配置vue开发多页面时,记得配置chunks,否则会有html引入不需要的js报错 [Vue warn]: Cannot find element:)

excludeChunks:与chunks配置项正好相反,用来配置不允许注入的thunk。

xhtml:true|fasle, 默认false;如果为 true ,则以兼容 xhtml 的模式引用文件。

chunksSortMode:none | auto| function,默认auto; 允许指定的thunk在插入到html文档前进行排序。

 
  1. function值可以指定具体排序规则;

  2. auto基于thunk的id进行排序; 

  3. none就是不排序

修改默认配置

直接修改

在获取到html-webpack-plugin的默认配置对象后,可通过修改该对象直接修改配置,比如修改输出文件名:

 
  1. configureWebpack: config => {

  2.     config.plugins.forEach((val) => {

  3.         if (val instanceof HtmlWebpackPlugin) {

  4.             console.log(val)

  5.             console.log(val.options.templateParameters.toString())

  6.             val.options.filename = 'indexx.html' // 修改输出文件名

  7.         }

  8.     })

  9. }

链式修改

比直接修改表达能力更强而且更加安全,需要用到chainWebpack,以修改模板路径为例,代码如下:

 
  1. chainWebpack: config => {

  2.   config

  3.     .plugin('html')

  4.     .tap(args => {

  5.       args[0].template = '/Users/username/proj/app/templates/index.html'

  6.       return args

  7.     })

  8. }

参考文章

https://blog.csdn.net/huzhenv5/article/details/104040077

https://segmentfault.com/a/1190000019920162

https://blog.csdn.net/D_claus/article/details/84140751

https://www.cnblogs.com/wonyun/p/6030090.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值