├── .gitignore ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | node_modules 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ember-cli-html-minifier 2 | 3 | Uses [html-minifier](https://github.com/kangax/html-minifier) to minify your ember-cli app's html as well as any inline js/css. 4 | 5 | ## Installation 6 | ``` 7 | npm install ember-cli-html-minifier --save-dev 8 | ``` 9 | 10 | By default, html minification will automatically happen on **production** builds. 11 | You can also manually control if it is enabled, and tune the minfier options to your liking: 12 | 13 | ```js 14 | // Brocfile.js 15 | 16 | var app = new EmberApp({ 17 | minifyHTML: { 18 | enabled: true, 19 | htmlFiles: ['index.html'], 20 | minifierOptions: { ... } 21 | } 22 | }); 23 | ``` 24 | 25 | ## Minifier Options 26 | See: https://github.com/kangax/html-minifier#options-quick-reference 27 | By default, the **html-minifier** module has all options turned off, so **ember-cli-html-minifier** sets some sensible options: 28 | 29 | ```js 30 | { 31 | collapseWhitespace : true, 32 | removeComments : true, 33 | minifyJS : true, 34 | minifyCSS : true 35 | } 36 | ``` 37 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Funnel = require('broccoli-funnel'); 2 | var Filter = require('broccoli-filter'); 3 | var mergeTrees = require('broccoli-merge-trees'); 4 | var minify = require('html-minifier-terser').minify; 5 | var extend = require('util-extend'); 6 | 7 | 8 | function HtmlMinifierFilter(inputTree, options) { 9 | if (!(this instanceof HtmlMinifierFilter)) { 10 | return new HtmlMinifierFilter(inputTree, options); 11 | } 12 | this.inputTree = inputTree; 13 | this.minifierOptions = extend(defaultMinifierOptions, options); 14 | Filter.call(this, inputTree); 15 | } 16 | 17 | HtmlMinifierFilter.prototype = Object.create(Filter.prototype); 18 | HtmlMinifierFilter.prototype.constructor = HtmlMinifierFilter; 19 | HtmlMinifierFilter.prototype.extensions = ['html']; 20 | HtmlMinifierFilter.prototype.targetExtension = 'html'; 21 | HtmlMinifierFilter.prototype.processString = function(string) { 22 | return minify(string, this.minifierOptions); 23 | }; 24 | 25 | 26 | var defaultMinifierOptions = { 27 | collapseWhitespace : true, 28 | removeComments : true, 29 | minifyJS : true, 30 | minifyCSS : true, 31 | ignoreCustomComments: [ /^\s*EMBER_CLI_FASTBOOT_BODY|EMBER_CLI_FASTBOOT_HEAD/ ] 32 | }; 33 | 34 | function EmberCliHtmlMinifier(project) { 35 | this.name = 'ember-cli-html-minifier'; 36 | } 37 | 38 | EmberCliHtmlMinifier.prototype.included = function(app) { 39 | this.app = app; 40 | this.options = app.options.minifyHTML || {}; 41 | this.enabled = typeof this.options.enabled !== 'undefined' ? this.options.enabled : app.env === 'production'; 42 | }; 43 | 44 | EmberCliHtmlMinifier.prototype.postprocessTree = function(type, tree) { 45 | if (type === 'all' && this.enabled) { 46 | var htmlFiles = this.options.htmlFiles; 47 | if(!htmlFiles || !Array.isArray(htmlFiles)){ 48 | var htmlFilePath = this.app.options.outputPaths.app.html || 'index.html'; 49 | htmlFiles = [htmlFilePath]; 50 | } 51 | var htmlFileTree = new Funnel( tree, { 52 | files: htmlFiles 53 | }); 54 | 55 | var minifiedHtml = HtmlMinifierFilter(htmlFileTree, this.options.minifierOptions); 56 | return mergeTrees([tree, minifiedHtml], { overwrite: true }); 57 | } 58 | 59 | return tree; 60 | }; 61 | 62 | module.exports = EmberCliHtmlMinifier; 63 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ember-cli-html-minifier", 3 | "version": "1.1.0", 4 | "description": "Uses html-minifier to minify your ember-cli app's html as well as any inline js/css", 5 | "main": "index.js", 6 | "author": "Garth Poitras ", 7 | "license": "MIT", 8 | "repository": { 9 | "type": "git", 10 | "url": "git@github.com:gpoitch/ember-cli-html-minifier.git" 11 | }, 12 | "keywords": [ 13 | "ember-addon", 14 | "minify", 15 | "compress", 16 | "html", 17 | "html-minifier" 18 | ], 19 | "ember-addon": { 20 | "after": [ 21 | "ember-cli-fastboot" 22 | ] 23 | }, 24 | "dependencies": { 25 | "broccoli-filter": "^1.3.0", 26 | "broccoli-funnel": "^2.0.1", 27 | "broccoli-merge-trees": "^3.0.1", 28 | "html-minifier-terser": "^5.0.2", 29 | "util-extend": "^1.0.3" 30 | } 31 | } 32 | --------------------------------------------------------------------------------