请求插件
html-webpack-plugin的作用:
filename:"[name]-[hash].js"这样生成的出口文件名是不确定的,那么我在index中引用js文件就很麻烦,用这个插件
就可以让引用的js的名称是打包后的名称,还可以使用原来的index.html为模板,生成一个新的index文件
如果我们去改变index.html里面的内容,重新打包是会更新的
安装htmlwebpackPlugin插件:npm install html-webpack-plugin --save-dev
var HtmlWebpackPlugin = require('html-webpack-plugin');
//模块导出
module.exports = {
// Asset Size chunks Chunk Names
// 文件大小 包含几个模块 模块名字
entry:{
a:"./src/script/a.js",
b:"./src/script/b.js",
c:"./src/script/c.js"
},
//出口:打包完成
// filename:
output:{
//出口目录
path:__dirname+"/dist/js",
//出口文件名
// filename:[name]-[hash].js js文件和index.html文件在一级目录下
filename:"js/[name]-[hash].js", //js文件在js文件夹下面
//上线的时候替换成线上路径
//publicPath:"http://cdn.com/"
},
//插入插件
//插件属性有两个:(有时间整理下这两个属性)
// HtmlWebpackPlugin.files 和 HtmlWebpackPlugin.options
plugins:[
//实例化
//为了使与原来的index.html关联起来 默认是运行脚本的目录,也就是更目录
//可以通过取值的形式传到index.html:如下
//<%= htmlWebpackPlugin.options.data %>
new HtmlWebpackPlugin({
//文件名
//filename:"index-[hash].html",
filename:"a.html",
//是按照那个html文件来打包的
template:"index.html",
//<script>我们放在哪个标签里
//inject:"false",有模板获得,不自己设定
inject:"head",
//设置title
title:"aaaa",
data:new Date(),
//chunks:块:指定包含的chunk,就是js,但是一定是enter里面声明了的
//chunks:["a"];
//chunks有个问题;很麻烦,如果chunks很多的话,一个一个加很慢,就用excludeChunks
//排除这几个不是其余的都加入
excludeChunks:["b","c"]
//压缩html
// minify:{
// //删除注释
// removeComments:true,
// //压缩空格
// collapseWhitespace:true
// }
}),
//相同的文件打包成不同的文件
//也就是说通过index.html一个模板生成不同的文件
new HtmlWebpackPlugin({
filename:"b.html",
template:"index.html",
inject:"head",
title:"bbbb",
data:new Date(),
//chunks:["b"];
excludeChunks:["a","c"]
}),
new HtmlWebpackPlugin({
filename:"c.html",
template:"index.html",
inject:"head",
title:"cccc",
data:new Date(),
//chunks:["c"],
excludeChunks:["b","a"]
}),
]
}
html-webpack-plugin的作用:
filename:"[name]-[hash].js"这样生成的出口文件名是不确定的,那么我在index中引用js文件就很麻烦,用这个插件
就可以让引用的js的名称是打包后的名称,还可以使用原来的index.html为模板,生成一个新的index文件
如果我们去改变index.html里面的内容,重新打包是会更新的
安装htmlwebpackPlugin插件:npm install html-webpack-plugin --save-dev
var HtmlWebpackPlugin = require('html-webpack-plugin');
//模块导出
module.exports = {
// Asset Size chunks Chunk Names
// 文件大小 包含几个模块 模块名字
entry:{
a:"./src/script/a.js",
b:"./src/script/b.js",
c:"./src/script/c.js"
},
//出口:打包完成
// filename:
output:{
//出口目录
path:__dirname+"/dist/js",
//出口文件名
// filename:[name]-[hash].js js文件和index.html文件在一级目录下
filename:"js/[name]-[hash].js", //js文件在js文件夹下面
//上线的时候替换成线上路径
//publicPath:"http://cdn.com/"
},
//插入插件
//插件属性有两个:(有时间整理下这两个属性)
// HtmlWebpackPlugin.files 和 HtmlWebpackPlugin.options
plugins:[
//实例化
//为了使与原来的index.html关联起来 默认是运行脚本的目录,也就是更目录
//可以通过取值的形式传到index.html:如下
//<%= htmlWebpackPlugin.options.data %>
new HtmlWebpackPlugin({
//文件名
//filename:"index-[hash].html",
filename:"a.html",
//是按照那个html文件来打包的
template:"index.html",
//<script>我们放在哪个标签里
//inject:"false",有模板获得,不自己设定
inject:"head",
//设置title
title:"aaaa",
data:new Date(),
//chunks:块:指定包含的chunk,就是js,但是一定是enter里面声明了的
//chunks:["a"];
//chunks有个问题;很麻烦,如果chunks很多的话,一个一个加很慢,就用excludeChunks
//排除这几个不是其余的都加入
excludeChunks:["b","c"]
//压缩html
// minify:{
// //删除注释
// removeComments:true,
// //压缩空格
// collapseWhitespace:true
// }
}),
//相同的文件打包成不同的文件
//也就是说通过index.html一个模板生成不同的文件
new HtmlWebpackPlugin({
filename:"b.html",
template:"index.html",
inject:"head",
title:"bbbb",
data:new Date(),
//chunks:["b"];
excludeChunks:["a","c"]
}),
new HtmlWebpackPlugin({
filename:"c.html",
template:"index.html",
inject:"head",
title:"cccc",
data:new Date(),
//chunks:["c"],
excludeChunks:["b","a"]
}),
]
}