Skip to content

Commit 7b66e18

Browse files
Keen Yee Liaujohnjbarton
authored andcommitted
feat(preprocessor): Allow preprocessor to handle binary files (#3054)
1 parent dee3615 commit 7b66e18

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

lib/preprocessor.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,7 @@ function createPreprocessor (config, basePath, injector) {
9898
var preprocessorNames = []
9999
for (var i = 0; i < patterns.length; i++) {
100100
if (mm(file.originalPath, patterns[i], {dot: true})) {
101-
if (thisFileIsBinary) {
102-
log.warn('Ignoring preprocessing (%s) %s because it is a binary file.',
103-
config[patterns[i]].join(', '), file.originalPath)
104-
} else {
105-
preprocessorNames = combineLists(preprocessorNames, config[patterns[i]])
106-
}
101+
preprocessorNames = combineLists(preprocessorNames, config[patterns[i]])
107102
}
108103
}
109104

@@ -125,7 +120,12 @@ function createPreprocessor (config, basePath, injector) {
125120
}
126121

127122
instances[name] = p
128-
preprocessors.push(p)
123+
if (!thisFileIsBinary || p.handleBinaryFiles) {
124+
preprocessors.push(p)
125+
} else {
126+
log.warn('Ignored preprocessing %s because %s has handleBinaryFiles=false.',
127+
file.originalPath, name)
128+
}
129129
})
130130

131131
nextPreprocessor(null, thisFileIsBinary ? buffer : buffer.toString())

test/unit/preprocessor.spec.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ describe('preprocessor', () => {
272272
thirdCallback('error')
273273
})
274274

275-
it('should tbrow after 3 retries', (done) => {
275+
it('should throw after 3 retries', (done) => {
276276
var injector = new di.Injector([{}, emitterSetting])
277277

278278
var pp = m.createPreprocessor({'**/*.js': []}, null, injector)
@@ -288,7 +288,7 @@ describe('preprocessor', () => {
288288
})
289289
})
290290

291-
it('should not preprocess binary files', (done) => {
291+
it('should not preprocess binary files by default', (done) => {
292292
var fakePreprocessor = sinon.spy((content, file, done) => {
293293
done(null, content)
294294
})
@@ -310,6 +310,29 @@ describe('preprocessor', () => {
310310
})
311311
})
312312

313+
it('should preprocess binary files if handleBinaryFiles=true', (done) => {
314+
const fakePreprocessor = sinon.spy((content, file, done) => {
315+
done(null, content)
316+
})
317+
fakePreprocessor.handleBinaryFiles = true
318+
319+
var injector = new di.Injector([{
320+
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
321+
}, emitterSetting])
322+
323+
pp = m.createPreprocessor({'**/*': ['fake']}, null, injector)
324+
325+
const file = {originalPath: '/some/photo.png', path: 'path'}
326+
327+
pp(file, (err) => {
328+
if (err) throw err
329+
330+
expect(fakePreprocessor).to.have.been.calledOnce
331+
expect(file.content).to.be.an.instanceof(Buffer)
332+
done()
333+
})
334+
})
335+
313336
it('should not preprocess binary files with capital letters in extension', (done) => {
314337
var fakePreprocessor = sinon.spy((content, file, done) => {
315338
done(null, content)

0 commit comments

Comments
 (0)