Skip to content

Commit 6e0a4fa

Browse files
committed
Add compaction
* If you don't provide the --compact command, nothing will happen * If you don't provide a compact config for your bundle, nothing will happen * If you set compact to "images", the default image compact functions will be used. * Otherwise, it will use whatever is provided compact needs to be an object that maps file extensions to functions. These functions take a buffer and return either a buffer or a Promise yielding a buffer. All packages from the imagemin project follow this signature. Also adjusted the .travis.yml to download the example images and install the required APTs to compile the C dependencies. Also adjusted the editorconfig, because YAML files apparently are only allowed to contain spaces, not tabs.
1 parent 136acee commit 6e0a4fa

File tree

16 files changed

+183
-5
lines changed

16 files changed

+183
-5
lines changed

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ indent_size = 4
1414
[*.md]
1515
indent_style = space
1616

17+
[*.yml]
18+
indent_style = space
19+
indent_size = 2
20+
1721
[COMMIT_EDITMSG]
1822
trim_trailing_whitespace = false
1923
max_line_length = 72

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@
55
/samples/output/
66

77
/package-lock.json
8+
9+
# Ignore the images
10+
*.jpg
11+
*.png
12+
*.webp
13+
*.gif
14+
*.svg

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,18 @@ node_js:
44
- 8
55
- 10
66

7+
addons:
8+
apt:
9+
packages:
10+
- autoconf
11+
- automake
12+
- libtool
13+
- nasm
14+
- make
15+
- pkg-config
16+
717
script:
818
- npm test
19+
20+
before_install:
21+
- npm run test:prepare

index.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ let FileFinder = require("faucet-pipeline-core/lib/util/files/finder");
55
let readFile = promisify(require("fs").readFile);
66
let stat = promisify(require("fs").stat);
77

8-
module.exports = (pluginConfig, assetManager) => {
8+
module.exports = (pluginConfig, assetManager, { compact }) => {
99
let copiers = pluginConfig.map(copyConfig =>
10-
buildCopier(copyConfig, assetManager));
10+
buildCopier(copyConfig, assetManager, { compact }));
1111

1212
return files => Promise.all(copiers.map(copier => copier(files)));
1313
};
1414

15-
function buildCopier(copyConfig, assetManager) {
15+
function buildCopier(copyConfig, assetManager, { compact }) {
1616
let source = assetManager.resolvePath(copyConfig.source);
1717
let target = assetManager.resolvePath(copyConfig.target, {
1818
enforceRelative: true
@@ -22,19 +22,36 @@ function buildCopier(copyConfig, assetManager) {
2222
filter: copyConfig.filter
2323
});
2424
let { fingerprint } = copyConfig;
25+
let plugins = determinePlugins(compact, copyConfig);
2526

2627
return files => {
2728
return Promise.all([
2829
(files ? fileFinder.match(files) : fileFinder.all()),
2930
determineTargetDir(source, target)
3031
]).then(([fileNames, targetDir]) => {
3132
return processFiles(fileNames, {
32-
assetManager, source, target, targetDir, fingerprint
33+
assetManager, source, target, targetDir, plugins, fingerprint
3334
});
3435
});
3536
};
3637
}
3738

39+
function determinePlugins(compact, copyConfig) {
40+
if(!compact) {
41+
return {};
42+
}
43+
44+
if(copyConfig.compact === "images") {
45+
return {
46+
jpg: require("imagemin-mozjpeg")({ quality: 80 }),
47+
png: require("imagemin-pngquant")(),
48+
svg: require("imagemin-svgo")()
49+
};
50+
}
51+
52+
return copyConfig.compact || {};
53+
}
54+
3855
// If `source` is a directory, `target` is used as target directory -
3956
// otherwise, `target`'s parent directory is used
4057
function determineTargetDir(source, target) {
@@ -47,11 +64,16 @@ function processFiles(fileNames, config) {
4764
}
4865

4966
function processFile(fileName,
50-
{ source, target, targetDir, fingerprint, assetManager }) {
67+
{ source, target, targetDir, fingerprint, assetManager, plugins }) {
5168
let sourcePath = path.join(source, fileName);
5269
let targetPath = path.join(target, fileName);
5370

5471
return readFile(sourcePath).
72+
then(content => {
73+
let type = determineFileType(sourcePath);
74+
let plugin = type && plugins[type];
75+
return plugin ? plugin(content) : content;
76+
}).
5577
then(content => {
5678
let options = { targetDir };
5779
if(fingerprint !== undefined) {
@@ -60,3 +82,7 @@ function processFile(fileName,
6082
return assetManager.writeFile(targetPath, content, options);
6183
});
6284
}
85+
86+
function determineFileType(sourcePath) {
87+
return path.extname(sourcePath).substr(1).toLowerCase();
88+
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"scripts": {
77
"test": "npm-run-all --parallel lint test:cli",
88
"test:cli": "./test/run",
9+
"test:prepare": "./test/prepare",
910
"lint": "eslint --cache index.js test && echo ✓"
1011
},
1112
"repository": {
@@ -23,6 +24,9 @@
2324
},
2425
"devDependencies": {
2526
"eslint-config-fnd": "^1.6.0",
27+
"imagemin-mozjpeg": "^7.0.0",
28+
"imagemin-pngquant": "^6.0.0",
29+
"imagemin-svgo": "^7.0.0",
2630
"json-diff": "^0.5.2",
2731
"npm-run-all": "^4.1.3",
2832
"release-util-fnd": "^1.1.1"

test/prepare

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
set -xeuo pipefail
3+
4+
curl -o "test/test_compact_images/src/example.jpg" "https://images.unsplash.com/photo-1498496294664-d9372eb521f3?fm=jpg&q=85"
5+
curl -o "test/test_compact_images/src/example.png" "https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/560px-PNG_transparency_demonstration_1.png"
6+
curl -o "test/test_compact_images/src/example.gif" "https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif"
7+
curl -o "test/test_compact_images/src/example.svg" "https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg"
8+
curl -o "test/test_compact_images/src/example.webp" "https://www.gstatic.com/webp/gallery3/2_webp_a.webp"
9+
10+
cp test/test_compact_images/src/example.jpg test/test_compact_undefined/src/example.jpg
11+
cp test/test_compact_images/src/example.png test/test_compact_undefined/src/example.png
12+
cp test/test_compact_images/src/example.gif test/test_compact_undefined/src/example.gif
13+
cp test/test_compact_images/src/example.svg test/test_compact_undefined/src/example.svg
14+
cp test/test_compact_images/src/example.webp test/test_compact_undefined/src/example.webp
15+
16+
cp test/test_compact_images/src/example.jpg test/test_compact_custom/src/example.jpg
17+
cp test/test_compact_images/src/example.png test/test_compact_custom/src/example.png
18+
cp test/test_compact_images/src/example.gif test/test_compact_custom/src/example.gif
19+
cp test/test_compact_images/src/example.svg test/test_compact_custom/src/example.svg
20+
cp test/test_compact_images/src/example.webp test/test_compact_custom/src/example.webp

test/run

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ root=`node -r fs -p "fs.realpathSync(process.argv[1]);" "$root"`
66

77
. "$root/../node_modules/faucet-pipeline-core/test/cli_harness.sh"
88

9+
# ensures the second file is smaller than the first file
10+
function assert_smaller_size {
11+
original=$(wc -c < "${1:?}")
12+
result=$(wc -c < "${2:?}")
13+
14+
if [ $(bc <<< "$result < $original") != 1 ]; then
15+
fail "file \`$2\` is not smaller than \`$1\`"
16+
else
17+
true
18+
fi
19+
}
20+
921
begin "$root/test_basic"
1022
faucet
1123
assert_identical "./dist/test.txt" "./src/test.txt"
@@ -70,4 +82,44 @@ begin "$root/test_match_multiple"
7082
assert_missing "./dist/nope.json"
7183
end
7284

85+
begin "$root/test_compact_undefined"
86+
faucet --compact
87+
assert_identical src/test.txt dist/test.txt
88+
assert_identical src/example.jpg dist/example.jpg
89+
assert_identical src/example.png dist/example.png
90+
assert_identical src/example.gif dist/example.gif
91+
assert_identical src/example.svg dist/example.svg
92+
assert_identical src/example.webp dist/example.webp
93+
end
94+
95+
begin "$root/test_compact_images"
96+
faucet
97+
assert_identical src/test.txt dist/test.txt
98+
assert_identical src/example.jpg dist/example.jpg
99+
assert_identical src/example.png dist/example.png
100+
assert_identical src/example.gif dist/example.gif
101+
assert_identical src/example.svg dist/example.svg
102+
assert_identical src/example.webp dist/example.webp
103+
end
104+
105+
begin "$root/test_compact_images"
106+
faucet --compact
107+
assert_identical src/test.txt dist/test.txt
108+
assert_smaller_size src/example.jpg dist/example.jpg
109+
assert_smaller_size src/example.png dist/example.png
110+
assert_identical src/example.gif dist/example.gif
111+
assert_smaller_size src/example.svg dist/example.svg
112+
assert_identical src/example.webp dist/example.webp
113+
end
114+
115+
begin "$root/test_compact_custom"
116+
faucet --compact
117+
assert_identical src/test.txt dist/test.txt
118+
assert_identical src/example.jpg dist/example.jpg
119+
assert_identical src/example.png dist/example.png
120+
assert_identical src/example.gif dist/example.gif
121+
assert_smaller_size src/example.svg dist/example.svg
122+
assert_identical src/example.webp dist/example.webp
123+
end
124+
73125
echo; echo "SUCCESS: all tests passed"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
let path = require("path");
3+
4+
module.exports = {
5+
static: [{
6+
source: "./src",
7+
target: "./dist",
8+
compact: {
9+
svg: require("imagemin-svgo")()
10+
}
11+
}],
12+
plugins: {
13+
"static": {
14+
plugin: path.resolve("../.."),
15+
bucket: "static"
16+
}
17+
}
18+
};

test/test_compact_custom/src/.keep

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Just copy me, ok?

0 commit comments

Comments
 (0)