1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| //webpack.config.js const path = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin') //模版loader const CleanWebpackPlugin = require('clean-webpack-plugin') module.exports = { entry:{ index:'./src/index.js', test:'./src/test.js', test1:'./src/test1.js', }, output:{ path:path.join(__dirname,'./dist/js'), filename:'[name]-[hash].js', publicPath:"http://cdn.com" }, plugins:[ new HtmlWebpackPlugin({ title: 'this a index.html', //每个html 的title template:'hello.html', filename:'index.html', excludeChunks:['test','test1'] //排除名为test.js,test1.js打包的js文件 }), new HtmlWebpackPlugin({ title:'this a test.js', template:'hello.html', filename:'test.html', excludeChunks:['index','test1'] }), new HtmlWebpackPlugin({ title:'this a test1.js', template:'hello.html', filename:'test1.html', excludeChunks:['test','index'] }), new CleanWebpackPlugin(['./dist/js']) ] }
//hello.html 模版内容 <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title><%= htmlWebpackPlugin.options.title%></title> <script type="text/javascript"> <%= compilation.assets[htmlWebpackPlugin.files.chunks.test1.entry.substr (htmlWebpackPlugin.files.publicPath.length)].source() %> </script> </head> <body> <% for(let k in htmlWebpackPlugin.files.chunks) {%> <% if(k != 'index') {%> <script src="<%=htmlWebpackPlugin.files.chunks[k].entry %>"></script> <% }%> <% } %> </body>
|