webpack配置(第四步:html篇(进阶篇))


声明:本文转载自https://my.oschina.net/u/3797834/blog/1649196,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

如果需要看(基础篇)请移步:https://my.oschina.net/u/3797834/blog/1649157

webpack.config.js文件

const path = require('path'); let htmlwebpackplugin = require('html-webpack-plugin');//引入html-webpack-plugin插件   let export_html= {     entry:  {     	main:__dirname+"/app/js/main.js",//入口文件     	main1:__dirname+"/app/js/main1.js",//入口文件     },     output: {         path: __dirname+"/_build/",         filename: "js/[name].js",//产出文件,name根据entry的入口文件键名定     },     module: {         loaders: [ 			{ 			    test: /(\.jsx|\.js)$/, 			    loader: 'babel-loader', 			    query: { 			      presets: ['es2015'] 			    } 			},         ]     }     ,  	plugins: [  		//new一个模板出来,这一个不使用chunks   		new htmlwebpackplugin({ 	        template: './app/home.html',//入口文件 	        filename:  'home1.html',//产出文件 		}),  		//new一个模板出来   		new htmlwebpackplugin({ 	        template: './app/home.html',//入口文件 	        filename:  'home2.html',//产出文件 	        chunks  : ['main'],//可以设置chunks按需引入JS文件,不设置就会引入所有产出的js 	        chunksSortMode: 'manual',//将chunks按引入的顺序排序,不用这个的话,引入到html的JS可能是错乱排序的 		})  	]      }; module.exports=export_html;	

看plugins这里

  		//new一个模板出来,这一个不使用chunks                 new htmlwebpackplugin({ 	        template: './app/home.html', 	        filename:  'home1.html',// 会生成home1.html 		}),  		//new一个模板出来   		new htmlwebpackplugin({ 	        template: './app/home.html', 	        filename:  'home2.html',//会生成home2.html 	        chunks  : ['main'],//注意:chunks里面的值是对应entry入口的键名来的 	        chunksSortMode: 'manual', 		})

app目录下的home.html文件

_build目录下的home1.html文件

_build目录下的home2.html文件

可以看到,home1.html引入了两个js文件,而且main1.js排在main.js前面,

而home2.html,只引入了指定的main.js;

在home2.html的chunks加上:main1

//new一个模板出来   		new htmlwebpackplugin({ 	        template: './app/home.html',//入口文件 	        filename:  'home2.html',//产出文件 	        chunks  : ['main',"main1"],//可以设置chunks按需引入JS文件,不设置就会引入所有产出的js 	        chunksSortMode: 'manual',//将chunks按引入的顺序排序,不用这个的话,引入到html的JS可能是错乱排序的 		})

因为chunks里,main在main1之前,所以引入的文件也是按照这个顺序来的;

顺序的问题主要归功于:这一条属性

chunksSortMode: 'manual',//将chunks按引入的顺序排序,不用这个的话,引入到html的JS可能是错乱排序的

更进一步:

每次都这样new很麻烦,故而写个函数简化过程

let get_html = function(name,chunk){//封装     return {         template: './app/ejs for html/'+ name + '.ejs',         filename:  name+ '.html',         chunks  : ['main',chunk||name],//可以设置chunks按需引入JS文件,不设置就会引入所有产出的js         chunksSortMode: 'manual',//将chunks按引入的顺序排序         inject  : true,         hash    : true, 		xhtml	: true     } };

然后在plugin里面new一个测试一下;

此时,webpack.config.js:

const path = require('path'); let htmlwebpackplugin = require('html-webpack-plugin');//引入html-webpack-plugin插件  let get_html = function(name,chunk){//封装     return {         template: './app/'+ name + '.html',         filename:  name+ '.html',         chunks  : ['main',chunk||null],//这里引入公共文件main.js,另外一个文件按需引入,当然也可以把这个的值设为数组,传入function的第二个值用数组就行         chunksSortMode: 'manual',//将chunks按引入的顺序排序         inject  : true,//所有JavaScript资源插入到body元素的底部         hash    : true,//避免缓存 		xhtml	: true //规范html书写     } };  let export_html= {     entry:  {     	main:__dirname+"/app/js/main.js",//入口文件     	main1:__dirname+"/app/js/main1.js",//入口文件     },     output: {         path: __dirname+"/_build/",         filename: "js/[name].js",//产出文件,name根据entry的入口文件键名定     },     module: {         loaders: [ 			{ 			    test: /(\.jsx|\.js)$/, 			    loader: 'babel-loader', 			    query: { 			      presets: ['es2015'] 			    } 			},         ]     }     ,  	plugins: [  		//new一个模板出来测试一下   		new htmlwebpackplugin(get_html("home","main1"))  	]      }; module.exports=export_html;	  

结果:

成功!

本文发表于2018年03月22日 22:38
(c)注:本文转载自https://my.oschina.net/u/3797834/blog/1649196,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 1985 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1