關於webpack的項目文件:https://gitee.com/codeFarmerPen/webpackjavascript
app下新建ejs目錄,html文件的後綴天然要改爲ejs;css
ejs目錄下的ejs文件是主體文件,html
generate目錄下放的是拼接後的文件,vue
public目錄下放的是公共文件。java
引入ejs-loaderwebpack
$ cnpm install ejs ejs-loader
loaders裏面加入:git
, { test: /\.ejs$/, loader: 'ejs-loader?variable=data' }
webpack.config.js:web
由於引入的文件變成了ejs文件,故而webpack.config.js也要作相應的更改;npm
template這裏的路徑和後綴名作了改變,loaders里加入了內容數組
const path = require('path'); let htmlwebpackplugin = require('html-webpack-plugin');//引入html-webpack-plugin插件 let get_html = function(name,chunk){//封裝 return { template: './app/ejs/generate/'+ name + '.ejs',//這裏改變了路徑和後綴名!!! filename: name+ '.html', chunks : ['main',chunk||null],//這裏引入公共文件main.js,另一個文件按需引入,固然也能夠把這個的值設爲數組,傳入function的第二個值用數組就行 chunksSortMode: 'manual',//將chunks按引入的順序排序 inject : true,//全部JavaScript資源插入到body元素的底部 hash : true, xhtml : true } }; let export_html= { entry: { main:__dirname+"/app/js/main.js",//入口文件 main1:__dirname+"/app/js/main1.js",//入口文件 }, output: { path: __dirname+"/_build/", filename: "js/[name].js",//產出文件,name根據entry的入口文件鍵名定 }, module: { loaders: [ { test: /(\.jsx|\.js)$/, loader: 'babel-loader', query: { presets: ['es2015'] } } , { test: /\.ejs$/, loader: 'ejs-loader?variable=data' } ] } , plugins: [ //new一個模板出來測試一下 new htmlwebpackplugin(get_html("home","main1")) ] }; module.exports=export_html;
app/ejs/public/head.ejs
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="shortcut icon" type="image/x-icon" href="" /> <title>hello!</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no"> <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script> </head>
app/ejs/public/foot.ejs
<footer id="footer" :choose="choose"> <section><div></div></section> <section><div></div></section> <section><div><span v-model="addnumber" :value="addnumber"></span></div></section> <section><div></div></section> </footer> </body> </html>
app/ejs/home.ejs
<body> <p>hello,nice to meet u!</p>
app/ejs/generate/home.ejs
<%= require("../public/head.ejs")() %> <%= require("../home.ejs")() %> <%= require("../public/foot.ejs")() %>
這個文件引入了上面的三個文件,而後這個將作爲htmlwebpackplugin的入口文件放入
webpack一下看看生成了什麼,
_build/home.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="shortcut icon" type="image/x-icon" href="" /> <title>hello!</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no"> <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script> </head> //備註:上是head.ejs文件的內容 <body> <p>hello,nice to meet u!</p> //備註:上面是home.ejs的內容 //備註:下面是foot.ejs的內容 <footer id="footer" :choose="choose"> <section><div></div></section> <section><div></div></section> <section><div><span v-model="addnumber" :value="addnumber"></span></div></section> <section><div></div></section> </footer> <script type="text/javascript" src="js/main.js?a2847a8b5f38621da2fb"></script><script type="text/javascript" src="js/main1.js?a2847a8b5f38621da2fb"></script></body> </html>
成功!
webpack配置(第一步:配置前提):http://www.javashuo.com/article/p-zhierewo-eo.html
webpack配置(第二步:入門篇):http://www.javashuo.com/article/p-zgertkmc-mq.html
webpack配置(第三步:ES6篇):http://www.javashuo.com/article/p-yornwkpw-dt.html
webpack配置(第四步:html篇(基礎篇)):http://www.javashuo.com/article/p-ykguohhe-do.html
webpack配置(第四步:html篇(進階篇)):http://www.javashuo.com/article/p-nqgmvdah-hz.html
webpack配置(第四步:html篇(模板篇)):http://www.javashuo.com/article/p-cfrvmuzd-ea.html
webpack配置(第五步:less/css篇(基礎篇)):http://www.javashuo.com/article/p-ftegbnmw-p.html
webpack配置(第五步:less/css篇(進階篇)):http://www.javashuo.com/article/p-bkdzfetz-mx.html
webpack配置(第五步:less/css篇(url圖片篇)):http://www.javashuo.com/article/p-adtmrppc-mq.html