標籤:分享 idt 上線 注釋 目錄 code options main chunk
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輸出的時候的目錄 publicPath理解為預留位置,需要上線地址 }, plugins: [ new htmlWebpackPlugin({ //filename: ‘index-[hash].html‘, filename: ‘index.html‘, //避免每次產生不同名的.html此時每次都會產生 index.html template: ‘index.html‘, //inject: ‘head‘, inject: false, title: ‘webpack is good module pack‘, date: new Date(), minify: { //對當前的html檔案進行壓縮 removeComments: true, //刪除注釋 collapseWhitespace: true //刪除空格 } }) ]};
2 根目錄 index.html
這裡的.html同.ejs模板一樣,可以使用js文法 <%= %> <% %> 、let in 、迴圈等
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title><%= htmlWebpackPlugin.options.title %></title> <script type="text/javascript" src="<%=htmlWebpackPlugin.files.chunks.main.entry %>"></script></head><body> <%= htmlWebpackPlugin.options.date %> <% for(let key in htmlWebpackPlugin) {%> <%= key %> <% } %> <!--注釋內容--> <script type="text/javascript" src="<%=htmlWebpackPlugin.files.chunks.a.entry %>"></script></body></html>
3 編譯
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>webpack is good module pack</title><script type="text/javascript" src="http://cdn.com/js/main-723f6c1682e09396ec6f.js"></script></head><body>Wed Apr 05 2017 21:49:33 GMT+0800 (中國標準時間) files options<script type="text/javascript" src="http://cdn.com/js/a-28982e930ed9e24890a5.js"></script></body></html>
可以看到壓縮、傳參、 header body不地方引入js、(線上)都以實現
webpack2使用ch4-向根目錄index.html檔案傳參並使用參數 使用線上資源 壓縮html