1 webpack.config.js
Const WEBPACK = require (' Webpack '), Htmlwebpackplugin= Require (' Html-webpack-plugin '), Path= Require (' path '); Module.exports={entry: {main:'./src/script/main.js ', A:'./src/script/a.js '}, Output: {path:path.resolve (__dirname,'./dist '), FileName:' Js/[name]-[chunkhash].js ', Publicpath:' http://cdn.com/'//path output When the directory Publicpath understood as a placeholder, need to address the line}, plugins: [NewHtmlwebpackplugin ({//filename: ' index-[hash].html ',FileName: ' index.html ',//avoid generating different names each time. html is generated index.html at this timeTemplate: ' index.html ', //inject: ' Head ',Injectfalse, Title:' Webpack is good module pack ', Date:NewDate (), minify: {//compress the current HTML fileRemovecomments:true,//Delete a commentCollapsewhitespace:true //Remove Spaces } }) ]};
2 root directory index.html
The. html here is the same as the. Ejs template, you can use the JS syntax <%=%> <%%>, let in, loops, etc.
<! DOCTYPE html> htmlwebpackplugin) {%> <%= key%> <%}%> <!--comment Content- <script type= "Text/javascript" src= "<%= Htmlwebpackplugin.files.chunks.a.entry%> "></script></body>
3 compiling
<! DOCTYPE html>//cdn.com/js/ Main-723f6c1682e09396ec6f.js "></script>
Can see the compression, transfer parameters, the header body is not introduced into JS, (online) are to achieve
Webpack2 use ch4-to index.html file parameters to the root directory and use arguments to compress HTML using the on-line resource