├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .github ├── issue_template.md └── workflows │ └── push.yml ├── .gitignore ├── .verb.md ├── LICENSE ├── README.md ├── examples ├── gulpfile.js └── index.html ├── index.js ├── package.json └── test ├── expected ├── collapse.html └── normal.html ├── fixtures └── index.html └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | 15 | [test/fixtures/*] 16 | insert_final_newline = false 17 | trim_trailing_whitespace = false 18 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "eslint:recommended" 4 | ], 5 | 6 | "env": { 7 | "browser": false, 8 | "es6": true, 9 | "node": true, 10 | "mocha": true 11 | }, 12 | 13 | "parserOptions":{ 14 | "ecmaVersion": 9, 15 | "sourceType": "module", 16 | "ecmaFeatures": { 17 | "modules": true, 18 | "experimentalObjectRestSpread": true 19 | } 20 | }, 21 | 22 | "globals": { 23 | "document": false, 24 | "navigator": false, 25 | "window": false 26 | }, 27 | 28 | "rules": { 29 | "accessor-pairs": 2, 30 | "arrow-spacing": [2, { "before": true, "after": true }], 31 | "block-spacing": [2, "always"], 32 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 33 | "comma-dangle": [2, "never"], 34 | "comma-spacing": [2, { "before": false, "after": true }], 35 | "comma-style": [2, "last"], 36 | "constructor-super": 2, 37 | "curly": [2, "multi-line"], 38 | "dot-location": [2, "property"], 39 | "eol-last": 2, 40 | "eqeqeq": [2, "allow-null"], 41 | "generator-star-spacing": [2, { "before": true, "after": true }], 42 | "handle-callback-err": [2, "^(err|error)$" ], 43 | "indent": [2, 2, { "SwitchCase": 1 }], 44 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 45 | "keyword-spacing": [2, { "before": true, "after": true }], 46 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 47 | "new-parens": 2, 48 | "no-array-constructor": 2, 49 | "no-caller": 2, 50 | "no-class-assign": 2, 51 | "no-cond-assign": 2, 52 | "no-const-assign": 2, 53 | "no-control-regex": 2, 54 | "no-debugger": 2, 55 | "no-delete-var": 2, 56 | "no-dupe-args": 2, 57 | "no-dupe-class-members": 2, 58 | "no-dupe-keys": 2, 59 | "no-duplicate-case": 2, 60 | "no-empty-character-class": 2, 61 | "no-eval": 2, 62 | "no-ex-assign": 2, 63 | "no-extend-native": 2, 64 | "no-extra-bind": 2, 65 | "no-extra-boolean-cast": 2, 66 | "no-extra-parens": [2, "functions"], 67 | "no-fallthrough": 2, 68 | "no-floating-decimal": 2, 69 | "no-func-assign": 2, 70 | "no-implied-eval": 2, 71 | "no-inner-declarations": [2, "functions"], 72 | "no-invalid-regexp": 2, 73 | "no-irregular-whitespace": 2, 74 | "no-iterator": 2, 75 | "no-label-var": 2, 76 | "no-labels": 2, 77 | "no-lone-blocks": 2, 78 | "no-mixed-spaces-and-tabs": 2, 79 | "no-multi-spaces": 2, 80 | "no-multi-str": 2, 81 | "no-multiple-empty-lines": [2, { "max": 1 }], 82 | "no-native-reassign": 0, 83 | "no-negated-in-lhs": 2, 84 | "no-new": 2, 85 | "no-new-func": 2, 86 | "no-new-object": 2, 87 | "no-new-require": 2, 88 | "no-new-wrappers": 2, 89 | "no-obj-calls": 2, 90 | "no-octal": 2, 91 | "no-octal-escape": 2, 92 | "no-proto": 0, 93 | "no-redeclare": 2, 94 | "no-regex-spaces": 2, 95 | "no-return-assign": 2, 96 | "no-self-compare": 2, 97 | "no-sequences": 2, 98 | "no-shadow-restricted-names": 2, 99 | "no-spaced-func": 2, 100 | "no-sparse-arrays": 2, 101 | "no-this-before-super": 2, 102 | "no-throw-literal": 2, 103 | "no-trailing-spaces": 0, 104 | "no-undef": 2, 105 | "no-undef-init": 2, 106 | "no-unexpected-multiline": 2, 107 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 108 | "no-unreachable": 2, 109 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 110 | "no-useless-call": 0, 111 | "no-with": 2, 112 | "one-var": [0, { "initialized": "never" }], 113 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 114 | "padded-blocks": [0, "never"], 115 | "quotes": [2, "single", "avoid-escape"], 116 | "radix": 2, 117 | "semi": [2, "always"], 118 | "semi-spacing": [2, { "before": false, "after": true }], 119 | "space-before-blocks": [2, "always"], 120 | "space-before-function-paren": [2, "never"], 121 | "space-in-parens": [2, "never"], 122 | "space-infix-ops": 2, 123 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 124 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 125 | "use-isnan": 2, 126 | "valid-typeof": 2, 127 | "wrap-iife": [2, "any"], 128 | "yoda": [2, "never"] 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | *.* text eol=lf 3 | *.css text eol=lf 4 | *.html text eol=lf 5 | *.js text eol=lf 6 | *.json text eol=lf 7 | *.less text eol=lf 8 | *.md text eol=lf 9 | *.yml text eol=lf 10 | 11 | *.jpg binary 12 | *.gif binary 13 | *.png binary 14 | *.jpeg binary -------------------------------------------------------------------------------- /.github/issue_template.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.github/workflows/push.yml: -------------------------------------------------------------------------------- 1 | on: push 2 | name: Run tests 3 | jobs: 4 | test: 5 | runs-on: ubuntu-latest 6 | strategy: 7 | matrix: 8 | node: [12, 14, 16, 18] 9 | steps: 10 | - uses: actions/checkout@v3 11 | - uses: actions/setup-node@v3 12 | with: 13 | node-version: ${{ matrix.node }} 14 | - run: npm install 15 | - run: npm test 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | build 3 | lib-cov 4 | *.seed 5 | *.log 6 | *.csv 7 | *.dat 8 | *.out 9 | *.pid 10 | *.gz 11 | 12 | pids 13 | logs 14 | results 15 | 16 | npm-debug.log 17 | node_modules 18 | *.sublime* 19 | tmp 20 | package-lock.json 21 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Heads up! 2 | 3 | _**Please do not report issues related to HTML parsing and output on this repository. Report those issues to the [html-minifier-terser](https://github.com/terser/html-minifier-terser/issues) issue tracker.**_ 4 | 5 | ## Usage 6 | 7 | See the [html-minifer-terser docs](https://github.com/terser/html-minifier-terser) for all available options. 8 | 9 | ```js 10 | const gulp = require('gulp'); 11 | const htmlmin = require('gulp-html-minifier-terser'); 12 | 13 | gulp.task('minify', () => { 14 | return gulp.src('src/*.html') 15 | .pipe(htmlmin({ collapseWhitespace: true })) 16 | .pipe(gulp.dest('dist')); 17 | }); 18 | ``` 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-present, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-html-minifier-terser [![NPM version](https://img.shields.io/npm/v/gulp-html-minifier-terser.svg?style=flat)](https://www.npmjs.com/package/gulp-html-minifier-terser) [![NPM monthly downloads](https://img.shields.io/npm/dm/gulp-html-minifier-terser.svg?style=flat)](https://npmjs.org/package/gulp-html-minifier-terser) [![NPM total downloads](https://img.shields.io/npm/dt/gulp-html-minifier-terser.svg?style=flat)](https://npmjs.org/package/gulp-html-minifier-terser) ![Run tests](https://github.com/pioug/gulp-html-minifier-terser/workflows/Run%20tests/badge.svg) 2 | 3 | > gulp plugin to minify HTML. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/): 8 | 9 | ```sh 10 | $ npm install --save gulp-html-minifier-terser 11 | ``` 12 | 13 | ## Heads up! 14 | 15 | _**Please do not report issues related to HTML parsing and output on this repository. Report those issues to the [html-minifier-terser](https://github.com/terser/html-minifier-terser/issues) issue tracker.**_ 16 | 17 | ## Usage 18 | 19 | See the [html-minifer-terser docs](https://github.com/terser/html-minifier-terser) for all available options. 20 | 21 | ```js 22 | const gulp = require('gulp'); 23 | const htmlmin = require('gulp-html-minifier-terser'); 24 | 25 | gulp.task('minify', () => { 26 | return gulp.src('src/*.html') 27 | .pipe(htmlmin({ collapseWhitespace: true })) 28 | .pipe(gulp.dest('dist')); 29 | }); 30 | ``` 31 | 32 | ## About 33 | 34 |
35 | Contributing 36 | 37 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 38 | 39 |
40 | 41 |
42 | Running Tests 43 | 44 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 45 | 46 | ```sh 47 | $ npm install && npm test 48 | ``` 49 | 50 |
51 | 52 |
53 | Building docs 54 | 55 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 56 | 57 | To generate the readme, run the following command: 58 | 59 | ```sh 60 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 61 | ``` 62 | 63 |
64 | 65 | ### Contributors 66 | 67 | | **Commits** | **Contributor** | 68 | | --- | --- | 69 | | 41 | [shinnn](https://github.com/shinnn) | 70 | | 20 | [jonschlinkert](https://github.com/jonschlinkert) | 71 | | 11 | [doowb](https://github.com/doowb) | 72 | | 7 | [stevelacy](https://github.com/stevelacy) | 73 | | 2 | [TheDancingCode](https://github.com/TheDancingCode) | 74 | | 1 | [cwonrails](https://github.com/cwonrails) | 75 | | 1 | [igoradamenko](https://github.com/igoradamenko) | 76 | | 1 | [oblador](https://github.com/oblador) | 77 | | 1 | [jdalton](https://github.com/jdalton) | 78 | | 1 | [JoseChirivella14](https://github.com/JoseChirivella14) | 79 | | 1 | [nschloe](https://github.com/nschloe) | 80 | | 1 | [tomByrer](https://github.com/tomByrer) | 81 | 82 | ### Author 83 | 84 | **Jon Schlinkert** 85 | 86 | * [GitHub Profile](https://github.com/jonschlinkert) 87 | * [Twitter Profile](https://twitter.com/jonschlinkert) 88 | * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) 89 | 90 | **Shinnosuke Watanabe** 91 | 92 | * [GitHub Profile](https://github.com/shinnn) 93 | * [Twitter Profile](https://twitter.com/shinnn_tw) 94 | * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) 95 | 96 | ### License 97 | 98 | Copyright © 2018, [Shinnosuke Watanabe](https://github.com/shinnn). 99 | Released under the [MIT License](LICENSE). 100 | 101 | *** 102 | 103 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 08, 2018._ 104 | -------------------------------------------------------------------------------- /examples/gulpfile.js: -------------------------------------------------------------------------------- 1 | var gulp = require('gulp'); 2 | var htmlmin = require('../'); 3 | 4 | 5 | gulp.task('normal', function(){ 6 | gulp.src('./index.html') 7 | .pipe(htmlmin()) 8 | .pipe(gulp.dest('./build')); 9 | }); 10 | 11 | gulp.task('collapse', function(){ 12 | gulp.src('./index.html') 13 | .pipe(htmlmin({collapseWhitespace: true})) 14 | .pipe(gulp.dest('./build')); 15 | }); -------------------------------------------------------------------------------- /examples/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Home | Assemble 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 30 | 31 | 32 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 59 | 60 | 61 | 62 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 |
128 |
129 |
130 |
131 |

BUILD SOMETHING

132 |

It has never been easier to leverage the full force of powerful frameworks like Bootstrap and Zurb Foundation. Nothing can stop you now.

133 |

Download Assemble 134 |

135 |
136 | 137 |
138 |
139 |
140 | 141 | 142 | 143 | 144 | 145 | 146 | 155 | 156 | 157 |
158 | 159 | 160 | Assemble 161 | The static site generator for Node.js, Grunt.js and Yeoman. 162 | 163 | 164 | 165 | 166 | 167 |
168 | 169 | 170 | 171 | 172 |
173 |
174 | 183 |
184 |
185 | 186 | 187 | 188 | 189 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 218 | 219 | 220 | 221 | 222 | 223 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const PluginError = require('plugin-error'); 4 | const htmlmin = require('html-minifier-terser'); 5 | const through = require('through2'); 6 | 7 | module.exports = options => { 8 | return through.obj(function(file, enc, next) { 9 | if (file.isNull()) { 10 | next(null, file); 11 | return; 12 | } 13 | 14 | const minify = async (buf, _, cb) => { 15 | try { 16 | let contents = Buffer.from(await htmlmin.minify(buf.toString(), options)); 17 | if (next === cb) { 18 | file.contents = contents; 19 | cb(null, file); 20 | return; 21 | } 22 | cb(null, contents); 23 | next(null, file); 24 | } catch (err) { 25 | let opts = Object.assign({}, options, { fileName: file.path }); 26 | let error = new PluginError('gulp-html-minifier-terser', err, opts); 27 | if (next !== cb) { 28 | next(error); 29 | return; 30 | } 31 | cb(error); 32 | } 33 | }; 34 | 35 | if (file.isStream()) { 36 | file.contents = file.contents.pipe(through(minify)); 37 | } else { 38 | minify(file.contents, null, next); 39 | } 40 | }); 41 | }; 42 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-html-minifier-terser", 3 | "description": "gulp plugin to minify HTML.", 4 | "version": "7.1.0", 5 | "homepage": "https://github.com/pioug/gulp-html-minifier-terser", 6 | "authors": [ 7 | { 8 | "name": "Jon Schlinkert", 9 | "url": "https://github.com/jonschlinkert", 10 | "twitter": "jonschlinkert" 11 | }, 12 | { 13 | "name": "Shinnosuke Watanabe", 14 | "url": "https://github.com/shinnn", 15 | "twitter": "shinnn_tw" 16 | } 17 | ], 18 | "contributors": [ 19 | "Brian Woodward (https://twitter.com/doowb)", 20 | "Chris Watson (https://github.com/cwonrails)", 21 | "Igor Adamenko (http://igoradamenko.com)", 22 | "Joel Arvidsson (http://twitter.com/trastknast)", 23 | "John-David Dalton (http://twitter.com/jdalton)", 24 | "Jon Schlinkert (http://twitter.com/jonschlinkert)", 25 | "Jose Chirivella (https://www.jchirivella.com)", 26 | "Nico Schlömer (https://github.com/nschloe)", 27 | "Shinnosuke Watanabe (https://shinnn.github.io)", 28 | "Steve Lacy (http://slacy.me)", 29 | "Tom Byrer (https://github.com/tomByrer)", 30 | "(https://github.com/TheDancingCode)" 31 | ], 32 | "repository": "pioug/gulp-html-minifier-terser", 33 | "bugs": { 34 | "url": "https://github.com/pioug/gulp-html-minifier-terser/issues" 35 | }, 36 | "license": "MIT", 37 | "files": [ 38 | "index.js" 39 | ], 40 | "main": "index.js", 41 | "engines": { 42 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 43 | }, 44 | "scripts": { 45 | "test": "mocha" 46 | }, 47 | "dependencies": { 48 | "html-minifier-terser": "^7.1.0", 49 | "plugin-error": "^2.0.0", 50 | "through2": "^4.0.2" 51 | }, 52 | "devDependencies": { 53 | "gulp": "^4.0.2", 54 | "gulp-format-md": "^2.0.0", 55 | "mocha": "^10.0.0", 56 | "vinyl": "^3.0.0" 57 | }, 58 | "keywords": [ 59 | "format", 60 | "gulp", 61 | "gulpplugin", 62 | "htm", 63 | "html", 64 | "htmlmin", 65 | "minification", 66 | "minifier", 67 | "minify" 68 | ], 69 | "verb": { 70 | "toc": false, 71 | "layout": "default", 72 | "tasks": [ 73 | "readme" 74 | ], 75 | "plugins": [ 76 | "gulp-format-md" 77 | ], 78 | "lint": { 79 | "reflinks": true 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /test/expected/collapse.html: -------------------------------------------------------------------------------- 1 | Home | Assemble

BUILD SOMETHING

It has never been easier to leverage the full force of powerful frameworks like Bootstrap and Zurb Foundation. Nothing can stop you now.

Download Assemble

Assemble The static site generator for Node.js, Grunt.js and Yeoman.
-------------------------------------------------------------------------------- /test/expected/normal.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Home | Assemble 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 30 | 31 | 32 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 59 | 60 | 61 | 62 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 |
128 |
129 |
130 |
131 |

BUILD SOMETHING

132 |

It has never been easier to leverage the full force of powerful frameworks like Bootstrap and Zurb Foundation. Nothing can stop you now.

133 |

Download Assemble 134 |

135 |
136 | 137 |
138 |
139 |
140 | 141 | 142 | 143 | 144 | 145 | 146 | 155 | 156 | 157 |
158 | 159 | 160 | Assemble 161 | The static site generator for Node.js, Grunt.js and Yeoman. 162 | 163 | 164 | 165 | 166 | 167 |
168 | 169 | 170 | 171 | 172 |
173 |
174 | 183 |
184 |
185 | 186 | 187 | 188 | 189 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 218 | 219 | 220 | 221 | 222 | 223 | -------------------------------------------------------------------------------- /test/fixtures/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Home | Assemble 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 30 | 31 | 32 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 59 | 60 | 61 | 62 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 |
128 |
129 |
130 |
131 |

BUILD SOMETHING

132 |

It has never been easier to leverage the full force of powerful frameworks like Bootstrap and Zurb Foundation. Nothing can stop you now.

133 |

Download Assemble 134 |

135 |
136 | 137 |
138 |
139 |
140 | 141 | 142 | 143 | 144 | 145 | 146 | 155 | 156 | 157 |
158 | 159 | 160 | Assemble 161 | The static site generator for Node.js, Grunt.js and Yeoman. 162 | 163 | 164 | 165 | 166 | 167 |
168 | 169 | 170 | 171 | 172 |
173 |
174 | 183 |
184 |
185 | 186 | 187 | 188 | 189 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 218 | 219 | 220 | 221 | 222 | 223 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | const fs = require('fs'); 5 | const assert = require('assert'); 6 | const through = require('through2'); 7 | const File = require('vinyl'); 8 | const minify = require('..'); 9 | 10 | function toStream(contents) { 11 | let stream = through(); 12 | stream.write(contents); 13 | return stream; 14 | } 15 | 16 | let fakeFile = new File({ 17 | path: 'test/fixtures/index.html', 18 | contents: fs.readFileSync('test/fixtures/index.html') 19 | }); 20 | 21 | let errorFileContents = '<
error in this file
'; 22 | let errorFile = new File({ 23 | path: 'test/fixtures/error.html', 24 | contents: Buffer.from(errorFileContents) 25 | }); 26 | 27 | describe('gulp-html-minifier-terser', () => { 28 | describe('file.contents - buffer', () => { 29 | it('should ignore empty file', cb => { 30 | let stream = minify(); 31 | stream.on('error', cb); 32 | stream.on('data', file => { 33 | assert(file.isNull()); 34 | cb(); 35 | }); 36 | stream.write(new File({})); 37 | }); 38 | 39 | it('should minify my HTML files', cb => { 40 | let expected = fs.readFileSync('test/expected/normal.html', 'utf8'); 41 | let stream = minify(); 42 | stream.on('error', cb); 43 | stream.on('data', file => { 44 | assert(file); 45 | assert(file.isBuffer()); 46 | assert.equal(file.contents.toString(), expected); 47 | cb(); 48 | }); 49 | stream.write(fakeFile); 50 | }); 51 | 52 | it('should collapse whitespace', cb => { 53 | let expected = fs.readFileSync('test/expected/collapse.html', 'utf8'); 54 | let stream = minify({ collapseWhitespace: true }); 55 | stream.on('error', cb); 56 | stream.on('data', file => { 57 | assert(file); 58 | assert.equal(file.contents.toString(), expected); 59 | cb(); 60 | }); 61 | stream.write(fakeFile); 62 | }); 63 | 64 | it('should emit a gulp error', cb => { 65 | let stream = minify(); 66 | stream.on('error', err => { 67 | assert.equal(err.message, 'Parse Error: ' + errorFileContents); 68 | assert.equal(err.fileName, errorFile.path); 69 | cb(); 70 | }); 71 | stream.on('end', () => cb(new Error('No error.'))); 72 | stream.write(errorFile); 73 | }); 74 | 75 | it('should emit a plugin error with a stack trace', cb => { 76 | let stream = minify({ showStack: true }); 77 | stream.on('error', err => { 78 | assert.equal(err.message, 'Parse Error: ' + errorFileContents); 79 | assert.equal(err.fileName, errorFile.path); 80 | assert(err.showStack); 81 | cb(); 82 | }); 83 | stream.on('end', () => cb(new Error('No error.'))); 84 | stream.write(errorFile); 85 | }); 86 | }); 87 | 88 | describe('file.contents - stream', () => { 89 | it('should minify my HTML files', cb => { 90 | let fixture = new File({ contents: toStream('
') }); 91 | let stream = minify(); 92 | stream.on('error', cb); 93 | stream.on('data', file => { 94 | assert(file); 95 | assert(file.isStream()); 96 | file.contents.on('data', data => { 97 | assert.equal(data.toString(), '
'); 98 | cb(); 99 | }); 100 | }); 101 | stream.write(fixture); 102 | }); 103 | 104 | it('should emit a plugin error', cb => { 105 | let stream = minify(); 106 | stream.on('error', err => { 107 | assert.equal(err.message, 'Parse Error: ' + errorFileContents); 108 | cb(); 109 | }); 110 | stream.on('end', () => cb(new Error('Expected an error'))); 111 | stream.write(new File({ contents: toStream(errorFileContents) })); 112 | }); 113 | }); 114 | }); 115 | 116 | --------------------------------------------------------------------------------