Skip to content

Commit e91340b

Browse files
committed
fix: exlude files for imageOptimizer
1 parent eff8d15 commit e91340b

File tree

4 files changed

+93
-20
lines changed

4 files changed

+93
-20
lines changed

lib/optimizeImage.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@ function OptimizeImage() {
1818
// Init.
1919
var hexo = this,
2020
options = hexo.config.image_minifier,
21-
targetfile = ['gif', 'jpg', 'png', 'svg'],
21+
targetfile = ['jpg', 'gif', 'png', 'svg'],
2222
route = hexo.route;
2323

2424
// Return if disabled.
2525
if (options.enable === false) return;
26+
// filter target files
2627
if (options.exclude && options.exclude.length) {
27-
for (var i = 0, len = options.exclude.length; i < len; ++i) {
28-
var idx = targetfile.indexOf(options.exclude[i]);
29-
if (idx !== -1) {
30-
targetfile.splice(-1, 1);
31-
}
32-
}
28+
targetfile = targetfile.filter(t => options.exclude.every(p =>!p.includes(t, p)));
3329
}
3430

3531
// exclude image
File renamed without changes.

test/fixture/svg.svg

+36
Loading

test/optimizeImage.test.js

+54-13
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,49 @@
22
const fs = require('fs');
33
const path = require('path');
44
const expect = require('chai').expect;
5+
const minimatch = require('minimatch');
56

67
// Local modules.
78
const optimizeImage = require('../lib/optimizeImage');
89

910
// Configure.
10-
const fixture = path.join(__dirname, 'fixture.png');
11-
const size = fs.statSync(fixture).size;
11+
const fileSize = {};
12+
const fixtures = [];
13+
fs.readdir(path.resolve(__dirname, './fixture'), (err, files) => {
14+
if (err) {
15+
console.error(err);
16+
return [];
17+
}
18+
files.forEach(file => {
19+
const filePath = path.resolve(__dirname, './fixture', file)
20+
fixtures.push(filePath);
21+
fileSize[filePath] = fs.statSync(filePath).size;
22+
})
23+
});
1224

1325
// Stub hexo.route.
1426
const hexoRoute = {
15-
buffer: null,
27+
buffer: {},
1628
get: function (name) {
1729
return fs.createReadStream(name);
1830
},
1931
list: function () {
20-
return [fixture];
32+
return fixtures;
2133
},
2234
set: function (name, buffer) {
23-
this.buffer = buffer; // Save.
35+
this.buffer[name] = buffer; // Save.
2436
}
2537
};
2638

2739
// Test suite.
2840
describe('hexo-image-minifier', function () {
2941
// Reset the buffer.
3042
beforeEach('hexoRoute', function () {
31-
hexoRoute.buffer = null;
43+
hexoRoute.buffer = {};
3244
});
3345

3446
// Tests.
35-
it('should minify an image.', function () {
47+
it('should minify an image.', () => {
3648
// Configure.
3749
const hexo = {
3850
config: {
@@ -49,13 +61,15 @@ describe('hexo-image-minifier', function () {
4961
};
5062
// Filter and test.
5163
const promise = optimizeImage.call(hexo);
52-
return promise.then(function () {
53-
expect(hexoRoute.buffer !== null);
54-
expect(size > hexoRoute.buffer.length)
64+
return promise.then(() => {
65+
for (const file of fixtures) {
66+
expect(hexoRoute.buffer[file]).to.be.ok;
67+
expect(fileSize[file]).to.be.greaterThan(hexoRoute.buffer[file].length);
68+
}
5569
});
5670
});
5771

58-
it('should do nothing if disabled.', function () {
72+
it('should do nothing if disabled.', () => {
5973
// Configure.
6074
const hexo = {
6175
config: {
@@ -65,7 +79,34 @@ describe('hexo-image-minifier', function () {
6579
};
6680

6781
// Filter and test.
68-
optimizeImage.call(hexo);
69-
expect(hexoRoute.buffer).to.be.null;
82+
expect(optimizeImage.call(hexo)).to.be.undefined;
83+
expect(hexoRoute.buffer).to.be.empty;
84+
});
85+
86+
it('should exclude files when the file extensions are listed in `exclude` options', () => {
87+
const exclude = ['*.svg'];
88+
// Configure.
89+
const hexo = {
90+
config: {
91+
image_minifier: {
92+
exclude,
93+
optimizationLevel: 3,
94+
}
95+
},
96+
route: hexoRoute
97+
};
98+
99+
// Filter and test.
100+
const promise = optimizeImage.call(hexo);
101+
return promise.then(() => {
102+
for (const file of fixtures) {
103+
if (exclude.every(pattern => !minimatch(file, pattern, { nocase: true, matchBase: true }))) {
104+
expect(hexoRoute.buffer[file]).to.be.ok;
105+
expect(fileSize[file]).to.be.greaterThan(hexoRoute.buffer[file].length);
106+
} else {
107+
expect(hexoRoute.buffer[file]).to.be.undefined;
108+
}
109+
}
110+
});
70111
});
71112
});

0 commit comments

Comments
 (0)