上文咱們對html-webpack-plugin的實例htmlWebpackPlugin進行了遍歷分析,講解了幾個經常使用屬性( inject, minify )以及自定義屬性的添加,本文,咱們繼續深刻他的配置選項的探討.css
1、chunks選項html
這個屬性很是有用,能夠指定某個頁面加載哪些chunk( 如:js文件 )前端
咱們能夠用他作多個頁面模板的生成. 好比,咱們在實際開發中,作一個博客網站,通常來講有首頁,文章列表頁,文章詳情頁等等,這些頁面都有一個特色,都要引入一些公共的js文件以及該頁面特有的js文件,好比:webpack
首頁( index.html ) 引入 main.js, index.jsweb
文章列表頁( list.html ) 引入 main.js, list.jsnpm
文章詳情頁( detail.html ) 引入 main.js, detail.js性能
傳統方式,一個個的打開文件,拷貝修改,若是後期維護,又是一堆文件中,查找,拷貝,修改。很容易出錯,並且效率低下,咱們看下webpack是如何提升效率,開啓前端工業化開發革命道路網站
webpack.dev.config.js文件代碼:ui
var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry : { main : './src/js/main.js', index : './src/js/index.js', list : './src/js/list.js', detail : './src/js/detail.js' }, output : { //__dirname,就是當前webpack.config.js文件所在的絕對路徑 path : __dirname + '/dist', //輸出路徑,要用絕對路徑 filename : 'js/[name]-[hash].bundle.js', //打包以後輸出的文件名 }, plugins: [ new HtmlWebpackPlugin({ template : './index.html', title : '博客首頁-by ghostwu', filename : 'index.html', inject : true, chunks : ['main', 'index'] }), new HtmlWebpackPlugin({ template : './index.html', title : '列表頁-by ghostwu', filename : 'list.html', inject : true, chunks : ['main', 'list'] }), new HtmlWebpackPlugin({ template : './index.html', title : '文章詳情頁-by ghostwu', filename : 'detail.html', inject : true, chunks : ['main', 'detail'] }) ] };
而後在src的js目錄下面,建立main.js, index.js,list.js,detail.js文件,執行打包( npm run d )就會在dist下面生成3個文件,各自引入到各自的js文件,下次要維護的時候,只要修改這個配置文件,再次打包就能夠了,是否是很方便spa
2、excludeChunks選項
這個很好理解,就是有不少chunks,排除不要加載的
webpack.dev.config.js文件代碼:
var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry : { main : './src/js/main.js', index : './src/js/index.js', list : './src/js/list.js', detail : './src/js/detail.js' }, output : { //__dirname,就是當前webpack.config.js文件所在的絕對路徑 path : __dirname + '/dist', //輸出路徑,要用絕對路徑 filename : 'js/[name]-[hash].bundle.js', //打包以後輸出的文件名 }, plugins: [ new HtmlWebpackPlugin({ template : './index.html', title : '博客首頁-by ghostwu', filename : 'index.html', inject : true, excludeChunks : ['list','detail'] }), new HtmlWebpackPlugin({ template : './index.html', title : '列表頁-by ghostwu', filename : 'list.html', inject : true, excludeChunks : ['index','detail'] }), new HtmlWebpackPlugin({ template : './index.html', title : '文章詳情頁-by ghostwu', filename : 'detail.html', inject : true, excludeChunks : ['list','index'] }) ] };
把配置文件修改以後,再用npm run d執行一次打包,跟使用chunks的效果是同樣的
三,把頁面src引入文件的方式,改爲用script標籤嵌入的方式,減小http請求( 提升加載性能)
要達到這個目的,咱們再安裝一個插件html-webpack-inline-source-plugin
安裝:npm install --save-dev html-webpack-inline-source-plugin
webpack.dev.config.js文件代碼:
var HtmlWebpackPlugin = require('html-webpack-plugin'); var HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin'); module.exports = { entry : { main : './src/js/main.js', index : './src/js/index.js', list : './src/js/list.js', detail : './src/js/detail.js' }, output : { //__dirname,就是當前webpack.config.js文件所在的絕對路徑 path : __dirname + '/dist', //輸出路徑,要用絕對路徑 filename : 'js/[name]-[hash].bundle.js', //打包以後輸出的文件名 }, plugins: [ new HtmlWebpackPlugin({ template : './index.html', title : '博客首頁-by ghostwu', filename : 'index.html', inject : true, excludeChunks : ['list','detail'], inlineSource : '.(js|css)$' //所有內嵌 }), new HtmlWebpackInlineSourcePlugin(), new HtmlWebpackPlugin({ template : './index.html', title : '列表頁-by ghostwu', filename : 'list.html', inject : true, excludeChunks : ['index','detail'] }), new HtmlWebpackPlugin({ template : './index.html', title : '文章詳情頁-by ghostwu', filename : 'detail.html', inject : true, excludeChunks : ['list','index'] }) ] };
執行npm run d打包命令以後,就會把dist/index.html文件的js和css改爲內嵌方式