137.以外链形式引用script脚本,修改webpack.config.js文件代码如下
var htmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry:{
main:'./src/script/main.js',
a:'./src/script/a.js',
b:'./src/script/b.js',
c:'./src/script/c.js'
},
output:{
path:'./dist',
filename:'js/[name]-[hash].js',
publicPath:'http://cdn.com/'
},
plugins:[
new htmlWebpackPlugin({
template:'index.html',
filename:'a.html',
inject:false,
title:'this is a.html',
excludeChunks:['b','c']
}),
new htmlWebpackPlugin({
template:'index.html',
filename:'b.html',
inject:false,
title:'this is b.html',
excludeChunks:['a','c']
}),
new htmlWebpackPlugin({
template:'index.html',
filename:'c.html',
inject:false,
title:'this is c.html',
excludeChunks:['a','b']
})
]
}
138.修改模板文件index.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="">
<script type="text/javascript">
<%= compilation.assets[htmlWebpackPlugin.files.chunks.main.entry.substr(html
WebpackPlugin.files.publicPath.length)].source() %>
</script>
</head>
<body>
<% for (var key in htmlWebpackPlugin.files.chunks) {%>
<% if (key !== 'main') { %>
<script type="text/javascript" src="<%= htmlWebpackPlugin.files.chunks[key]
.entry %>"></script>
<% } %>
<% } %>
</body>
</html>
139.使用cnpm run webpack命令重新打包
140.查看生成的a.html,b.html,c.html文件