|
1 | 1 | import assert from 'assert'; |
2 | | -import {bundle, distDir, outputFS} from '@parcel/test-utils'; |
| 2 | +import {bundle, distDir, inputFS, outputFS} from '@parcel/test-utils'; |
3 | 3 | import path from 'path'; |
4 | 4 | import sharp from 'sharp'; |
5 | 5 |
|
@@ -88,4 +88,50 @@ describe('image', function() { |
88 | 88 | ); |
89 | 89 | }); |
90 | 90 | }); |
| 91 | + |
| 92 | + it('should optimise JPEGs', async function() { |
| 93 | + let img = path.join(__dirname, '/integration/image/image.jpg'); |
| 94 | + let b = await bundle(img, { |
| 95 | + defaultTargetOptions: { |
| 96 | + shouldOptimize: true, |
| 97 | + }, |
| 98 | + }); |
| 99 | + |
| 100 | + const imagePath = b.getBundles().find(b => b.type === 'jpg').filePath; |
| 101 | + |
| 102 | + let input = await inputFS.readFile(img); |
| 103 | + let inputRaw = await sharp(input) |
| 104 | + .toFormat('raw') |
| 105 | + .toBuffer(); |
| 106 | + let output = await outputFS.readFile(imagePath); |
| 107 | + let outputRaw = await sharp(output) |
| 108 | + .toFormat('raw') |
| 109 | + .toBuffer(); |
| 110 | + |
| 111 | + assert(outputRaw.equals(inputRaw)); |
| 112 | + assert(output.length < input.length); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should optimise PNGs', async function() { |
| 116 | + let img = path.join(__dirname, '/integration/image/clock.png'); |
| 117 | + let b = await bundle(img, { |
| 118 | + defaultTargetOptions: { |
| 119 | + shouldOptimize: true, |
| 120 | + }, |
| 121 | + }); |
| 122 | + |
| 123 | + const imagePath = b.getBundles().find(b => b.type === 'png').filePath; |
| 124 | + |
| 125 | + let input = await inputFS.readFile(img); |
| 126 | + let inputRaw = await sharp(input) |
| 127 | + .toFormat('raw') |
| 128 | + .toBuffer(); |
| 129 | + let output = await outputFS.readFile(imagePath); |
| 130 | + let outputRaw = await sharp(output) |
| 131 | + .toFormat('raw') |
| 132 | + .toBuffer(); |
| 133 | + |
| 134 | + assert(outputRaw.equals(inputRaw)); |
| 135 | + assert(output.length < input.length); |
| 136 | + }); |
91 | 137 | }); |
0 commit comments