From d82bd52e2f224668f716ef29c1fe9ec26dfbe66e Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Mon, 18 May 2015 15:30:42 +0200 Subject: [PATCH] Add minification again --- README.md | 5 ++--- example/webpack.config.js | 9 ++++++++- index.js | 18 ++++++++++++++++-- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index dca91a7..d594cf9 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ HTML Webpack Plugin -=================== +=================== [![npm version](https://badge.fury.io/js/html-webpack-plugin.svg)](http://badge.fury.io/js/html-webpack-plugin) [![Dependency Status](https://david-dm.org/ampedandwired/html-webpack-plugin.svg)](https://david-dm.org/ampedandwired/html-webpack-plugin) [![bitHound Score](https://www.bithound.io/github/ampedandwired/html-webpack-plugin/badges/score.svg)](https://www.bithound.io/github/ampedandwired/html-webpack-plugin) [![Build status](https://travis-ci.org/ampedandwired/html-webpack-plugin.svg)](https://travis-ci.org/ampedandwired/html-webpack-plugin) This is a [webpack](http://webpack.github.io/) plugin that simplifies creation of HTML files to serve your @@ -63,8 +63,7 @@ Allowed values are as follows: - `title`: The title to use for the generated HTML document. - `filename`: The file to write the HTML to. Defaults to `index.html`. You can specify a subdirectory here too (eg: `assets/admin.html`). -- `template`: A html template (supports [blueimp templates](https://github.com/blueimp/JavaScript-Templates)). -- `templateContent`: A html string or a function returning the html (supports [blueimp templates](https://github.com/blueimp/JavaScript-Templates)). +- `template`: Path to the template. Supports loaders e.g. `html!./index.html`. - `inject`: `true | 'head' | 'body' | false` Inject all assets into the given `template` or `templateContent` - When passing `true` or `'body'` all javascript resources will be placed at the bottom of the body element. `'head'` will place the scripts in the head element. - `favicon`: Adds the given favicon path to the output html. - `minify`: `{...} | false` Pass a [html-minifier](https://github.com/kangax/html-minifier#options-quick-reference) options object to minify the output. diff --git a/example/webpack.config.js b/example/webpack.config.js index e090443..c3056a5 100755 --- a/example/webpack.config.js +++ b/example/webpack.config.js @@ -8,7 +8,14 @@ module.exports = { }, plugins: [ new HtmlWebpackPlugin({ - favicon: 'favicon.ico' + favicon: 'favicon.ico', + minify: { + removeComments: true, + collapseWhitespace: true, + conservativeCollapse: false, + minifyJS: true, + minifyCSS: true + } }) ] }; \ No newline at end of file diff --git a/index.js b/index.js index bb974f6..fa7c8d5 100644 --- a/index.js +++ b/index.js @@ -22,6 +22,7 @@ function HtmlWebpackPlugin(options) { inject: true, compile: true, favicon: false, + minify: false, chunks: 'all', excludeChunks: [], title: 'Webpack App' @@ -141,16 +142,29 @@ HtmlWebpackPlugin.prototype.postProcessHtml = function(html, compilation) { } }; if (self.options.compile === true) { - html = tmpl(html, templateParams); + return tmpl(html, templateParams); + } else { + return html; } }) // Inject - .then(function() { + .then(function(html) { if (self.options.inject) { return self.injectAssetsIntoHtml(html, assets); } else { return html; } + }) + // Minify + .then(function(html) { + if (self.options.minify) { + var minify = require('html-minifier').minify; + // If `options.minify` is set to true use the default minify options + var minifyOptions = _.isObject(self.options.minify) ? self.options.minify : {}; + return minify(html, minifyOptions); + } else { + return html; + } }); };