首页 > Web开发 > 详细

Webpack入门教程十四

时间:2017-02-20 14:08:25      阅读:168      评论:0      收藏:0      [点我收藏+]

77.在webpack.config.js中添加minify,设置removeComments为true,删除模板中的注释,修改内容如下

var webpack = require(‘webpack‘);
var HtmlWebpackPlugin = require(‘html-webpack-plugin‘);

module.exports = {
	entry:  __dirname + "/app/Greeter.js",
	output: {
		path: __dirname + "/build",
		filename: "bundle.js"
	},
	devServer:{
		contentBase:"./public",
		historyApiFallback:true,
		inline:true
	},
	module:{
		loaders:[
			{
				test:/\.json$/,
				loader:"json-loader"
			},
			{
				test:/\.js$/,
				exclude:/node_modules/,
				loader:‘babel-loader‘
			},
			{
				test:/\.css$/,
				loader:‘style-loader!css-loader?modules‘
			}
		]
	},
	plugins:[
		new webpack.BannerPlugin("copyright suyan"),
		new HtmlWebpackPlugin({
			template:__dirname + "/app/index.tmpl.html",
			title:‘htmlwebpackplugin filename test‘,
			filename:‘filename.html‘,
			inject:true,
			hash:true,
			minify:{
				removeComments:true
			}
		})
	]
}

78.修改index.tmpl.html文件,修改内容如下

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title><%= htmlWebpackPlugin.options.title %></title>
	<link rel="stylesheet" href="">
</head>
<body>
	<div id="root"></div>
	<!-- comment -->
	
</body>
</html>

79.使用webpack命令打包

技术分享

80.查看生成的filename.html文件,生成内容如下

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>htmlwebpackplugin filename test</title>
	<link rel="stylesheet" href="">
</head>
<body>
	<div id="root"></div>
	
	
<script type="text/javascript" src="bundle.js?7262b52dedc502c7d5fe"></script></body>
</html>

说明

在index.tmpl.html文件中我们编写了注释内容<!-- comment -->,但因我们在webpack.config.js文件中配置了minify:{
removeComments:true},所以在我们查看filename.html文件时并没有注释内容


本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1899419

Webpack入门教程十四

原文:http://suyanzhu.blog.51cto.com/8050189/1899419

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!