webpack2使用ch4-向根目錄index.html檔案傳參並使用參數 使用線上資源 壓縮html

來源:互聯網
上載者:User

標籤:分享   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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.