You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. 'use strict';
  2. const test = require('ava');
  3. const fs = require('fs');
  4. const Vinyl = require('vinyl');
  5. const image = require('..');
  6. test.cb('should minify PNG images with pngquant', t => {
  7. t.plan(1);
  8. const stream = image({
  9. pngquant : true,
  10. optipng : false,
  11. zopflipng : false
  12. });
  13. stream.on('data', file => {
  14. const before = fs.statSync('test/fixtures/test.png').size;
  15. const after = file.contents.length;
  16. t.true(after < before);
  17. t.end();
  18. });
  19. stream.write(new Vinyl({
  20. path : `${__dirname}/fixtures/test.png`,
  21. contents : fs.readFileSync(`${__dirname}/fixtures/test.png`)
  22. }));
  23. stream.end();
  24. });
  25. test.cb('should minify PNG images with optipng', t => {
  26. t.plan(1);
  27. const stream = image({
  28. pngquant : false,
  29. optipng : true,
  30. zopflipng : false
  31. });
  32. stream.on('data', file => {
  33. const before = fs.statSync('test/fixtures/test.png').size;
  34. const after = file.contents.length;
  35. t.true(after < before);
  36. t.end();
  37. });
  38. stream.write(new Vinyl({
  39. path : `${__dirname}/fixtures/test.png`,
  40. contents : fs.readFileSync(`${__dirname}/fixtures/test.png`)
  41. }));
  42. stream.end();
  43. });
  44. test.cb('should minify PNG images with zopflipng', t => {
  45. t.plan(1);
  46. const stream = image({
  47. pngquant : false,
  48. optipng : false,
  49. zopflipng : true
  50. });
  51. stream.on('data', file => {
  52. const before = fs.statSync('test/fixtures/test.png').size;
  53. const after = file.contents.length;
  54. t.true(after < before);
  55. t.end();
  56. });
  57. stream.write(new Vinyl({
  58. path : `${__dirname}/fixtures/test.png`,
  59. contents : fs.readFileSync(`${__dirname}/fixtures/test.png`)
  60. }));
  61. stream.end();
  62. });
  63. test.cb('should not minify PNG images when related options are disabled', t => {
  64. t.plan(1);
  65. const stream = image({
  66. pngquant : false,
  67. optipng : false,
  68. zopflipng : false
  69. });
  70. stream.on('data', file => {
  71. const before = fs.statSync('test/fixtures/test.png').size;
  72. const after = file.contents.length;
  73. t.true(after === before);
  74. t.end();
  75. });
  76. stream.write(new Vinyl({
  77. path : `${__dirname}/fixtures/test.png`,
  78. contents : fs.readFileSync(`${__dirname}/fixtures/test.png`)
  79. }));
  80. stream.end();
  81. });
  82. test.cb('should minify JPG images with jpegRecompress', t => {
  83. t.plan(1);
  84. const stream = image({
  85. jpegRecompress : true,
  86. mozjpeg : false,
  87. guetzli : false
  88. });
  89. stream.on('data', file => {
  90. const before = fs.statSync('test/fixtures/test.jpg').size;
  91. const after = file.contents.length;
  92. t.true(after < before);
  93. t.end();
  94. });
  95. stream.write(new Vinyl({
  96. path : `${__dirname}/fixtures/test.jpg`,
  97. contents : fs.readFileSync(`${__dirname}/fixtures/test.jpg`)
  98. }));
  99. stream.end();
  100. });
  101. test.cb('should minify JPG images with mozjpeg', t => {
  102. t.plan(1);
  103. const stream = image({
  104. jpegRecompress : false,
  105. mozjpeg : true,
  106. guetzli : false
  107. });
  108. stream.on('data', file => {
  109. const before = fs.statSync('test/fixtures/test.jpg').size;
  110. const after = file.contents.length;
  111. t.true(after < before);
  112. t.end();
  113. });
  114. stream.write(new Vinyl({
  115. path : `${__dirname}/fixtures/test.jpg`,
  116. contents : fs.readFileSync(`${__dirname}/fixtures/test.jpg`)
  117. }));
  118. stream.end();
  119. });
  120. test.cb('should minify JPG images with guetzli', t => {
  121. t.plan(1);
  122. const stream = image({
  123. jpegRecompress : false,
  124. mozjpeg : false,
  125. guetzli : true
  126. });
  127. stream.on('data', file => {
  128. const before = fs.statSync('test/fixtures/test.jpg').size;
  129. const after = file.contents.length;
  130. t.true(after < before);
  131. t.end();
  132. });
  133. stream.write(new Vinyl({
  134. path : `${__dirname}/fixtures/test.jpg`,
  135. contents : fs.readFileSync(`${__dirname}/fixtures/test.jpg`)
  136. }));
  137. stream.end();
  138. });
  139. test.cb('should not minify JPG images when related options are disabled', t => {
  140. t.plan(1);
  141. const stream = image({
  142. jpegRecompress : false,
  143. mozjpeg : false,
  144. guetzli : false
  145. });
  146. stream.on('data', file => {
  147. const before = fs.statSync('test/fixtures/test.jpg').size;
  148. const after = file.contents.length;
  149. t.true(after === before);
  150. t.end();
  151. });
  152. stream.write(new Vinyl({
  153. path : `${__dirname}/fixtures/test.jpg`,
  154. contents : fs.readFileSync(`${__dirname}/fixtures/test.jpg`)
  155. }));
  156. stream.end();
  157. });
  158. test.cb('should minify GIF images', t => {
  159. t.plan(1);
  160. const stream = image();
  161. stream.on('data', file => {
  162. const before = fs.statSync('test/fixtures/test.gif').size;
  163. const after = file.contents.length;
  164. t.true(after < before);
  165. t.end();
  166. });
  167. stream.write(new Vinyl({
  168. path : `${__dirname}/fixtures/test.gif`,
  169. contents : fs.readFileSync(`${__dirname}/fixtures/test.gif`)
  170. }));
  171. stream.end();
  172. });
  173. test.cb('should not minify GIF images when related options are disabled', t => {
  174. t.plan(1);
  175. const stream = image({
  176. gifsicle : false
  177. });
  178. stream.on('data', file => {
  179. const before = fs.statSync('test/fixtures/test.gif').size;
  180. const after = file.contents.length;
  181. t.true(after === before);
  182. t.end();
  183. });
  184. stream.write(new Vinyl({
  185. path : `${__dirname}/fixtures/test.gif`,
  186. contents : fs.readFileSync(`${__dirname}/fixtures/test.gif`)
  187. }));
  188. stream.end();
  189. });
  190. test.cb('should minify SVG images', t => {
  191. t.plan(1);
  192. const stream = image();
  193. stream.on('data', file => {
  194. const before = fs.statSync('test/fixtures/test.svg').size;
  195. const after = file.contents.length;
  196. t.true(after < before);
  197. t.end();
  198. });
  199. stream.write(new Vinyl({
  200. path : `${__dirname}/fixtures/test.svg`,
  201. contents : fs.readFileSync(`${__dirname}/fixtures/test.svg`)
  202. }));
  203. });
  204. test.cb('should not minify SVG images when related options are disabled', t => {
  205. t.plan(1);
  206. const stream = image({
  207. svgo : false
  208. });
  209. stream.on('data', file => {
  210. const before = fs.statSync('test/fixtures/test.svg').size;
  211. const after = file.contents.length;
  212. t.true(after === before);
  213. t.end();
  214. });
  215. stream.write(new Vinyl({
  216. path : `${__dirname}/fixtures/test.svg`,
  217. contents : fs.readFileSync(`${__dirname}/fixtures/test.svg`)
  218. }));
  219. });
  220. test.cb('should skip unsupported images', t => {
  221. t.plan(1);
  222. const stream = image();
  223. stream.on('data', file => {
  224. t.is(file.contents, null);
  225. t.end();
  226. });
  227. stream.write(new Vinyl({
  228. path : `${__dirname}fixtures/test.bmp`
  229. }));
  230. stream.end();
  231. });