[js高手之路]深入淺出webpack教程系列6-插件使用之html-webpack-plugin配置(下)


[js高手之路]深入淺出webpack教程系列索引目錄:

上文我們對html-webpack-plugin的實例htmlWebpackPlugin進行了遍歷分析,講解了幾個常用屬性( inject, minify )以及自定義屬性的添加,本文,我們繼續深入他的配置選項的探討.

一、chunks選項

這個屬性非常有用,可以指定某個頁面加載哪些chunk( 如:js文件 )

我們可以用他做多個頁面模板的生成. 比如,我們在實際開發中,做一個博客網站,一般來說有首頁,文章列表頁,文章詳情頁等等,這些頁面都有一個特點,都要引入一些公共的js文件以及該頁面特有的js文件,比如:

首頁( index.html ) 引入  main.js, index.js

文章列表頁( list.html ) 引入  main.js, list.js

文章詳情頁( detail.html ) 引入  main.js, detail.js

傳統方式,一個個的打開文件,拷貝修改,如果后期維護,又是一堆文件中,查找,拷貝,修改。很容易出錯,而且效率低下,我們看下webpack是如何提高效率,開啟前端工業化開發革命道路

webpack.dev.config.js文件代碼: 

 1 var HtmlWebpackPlugin = require('html-webpack-plugin');
 2 module.exports = {
 3     entry : {
 4         main : './src/js/main.js',
 5         index : './src/js/index.js',
 6         list : './src/js/list.js',
 7         detail : './src/js/detail.js'
 8     },
 9     output : {
10         //__dirname,就是當前webpack.config.js文件所在的絕對路徑
11         path : __dirname + '/dist', //輸出路徑,要用絕對路徑
12         filename : 'js/[name]-[hash].bundle.js', //打包之后輸出的文件名
13     },
14     plugins: [
15         new HtmlWebpackPlugin({
16             template : './index.html',
17             title : '博客首頁-by ghostwu',
18             filename : 'index.html',
19             inject : true,
20             chunks : ['main', 'index']
21         }),
22         new HtmlWebpackPlugin({
23             template : './index.html',
24             title : '列表頁-by ghostwu',
25             filename : 'list.html',
26             inject : true,
27             chunks : ['main', 'list']
28         }),
29         new HtmlWebpackPlugin({
30             template : './index.html',
31             title : '文章詳情頁-by ghostwu',
32             filename : 'detail.html',
33             inject : true,
34             chunks : ['main', 'detail']
35         })
36     ]
37 };

然后在src的js目錄下面,創建main.js, index.js,list.js,detail.js文件,執行打包( npm run d )就會在dist下面生成3個文件,各自引入到各自的js文件,下次要維護的時候,只要修改這個配置文件,再次打包就可以了,是不是很方便

二、excludeChunks選項

 這個很好理解,就是有很多chunks,排除不要加載的

webpack.dev.config.js文件代碼:

 1 var HtmlWebpackPlugin = require('html-webpack-plugin');
 2 module.exports = {
 3     entry : {
 4         main : './src/js/main.js',
 5         index : './src/js/index.js',
 6         list : './src/js/list.js',
 7         detail : './src/js/detail.js'
 8     },
 9     output : {
10         //__dirname,就是當前webpack.config.js文件所在的絕對路徑
11         path : __dirname + '/dist', //輸出路徑,要用絕對路徑
12         filename : 'js/[name]-[hash].bundle.js', //打包之后輸出的文件名
13     },
14     plugins: [
15         new HtmlWebpackPlugin({
16             template : './index.html',
17             title : '博客首頁-by ghostwu',
18             filename : 'index.html',
19             inject : true,
20             excludeChunks : ['list','detail']
21         }),
22         new HtmlWebpackPlugin({
23             template : './index.html',
24             title : '列表頁-by ghostwu',
25             filename : 'list.html',
26             inject : true,
27             excludeChunks : ['index','detail']
28         }),
29         new HtmlWebpackPlugin({
30             template : './index.html',
31             title : '文章詳情頁-by ghostwu',
32             filename : 'detail.html',
33             inject : true,
34             excludeChunks : ['list','index']
35         })
36     ]
37 };

把配置文件修改之后,再用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文件代碼:

 1 var HtmlWebpackPlugin = require('html-webpack-plugin');
 2 var HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
 3 module.exports = {
 4     entry : {
 5         main : './src/js/main.js',
 6         index : './src/js/index.js',
 7         list : './src/js/list.js',
 8         detail : './src/js/detail.js'
 9     },
10     output : {
11         //__dirname,就是當前webpack.config.js文件所在的絕對路徑
12         path : __dirname + '/dist', //輸出路徑,要用絕對路徑
13         filename : 'js/[name]-[hash].bundle.js', //打包之后輸出的文件名
14     },
15     plugins: [
16         new HtmlWebpackPlugin({
17             template : './index.html',
18             title : '博客首頁-by ghostwu',
19             filename : 'index.html',
20             inject : true,
21             excludeChunks : ['list','detail'],
22             inlineSource : '.(js|css)$' //全部內嵌
23         }),
24         new HtmlWebpackInlineSourcePlugin(),
25         new HtmlWebpackPlugin({
26             template : './index.html',
27             title : '列表頁-by ghostwu',
28             filename : 'list.html',
29             inject : true,
30             excludeChunks : ['index','detail']
31         }),
32         new HtmlWebpackPlugin({
33             template : './index.html',
34             title : '文章詳情頁-by ghostwu',
35             filename : 'detail.html',
36             inject : true,
37             excludeChunks : ['list','index']
38         })
39     ]
40 };

執行npm run d打包命令之后,就會把dist/index.html文件的js和css改成內嵌方式


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM