Skip to content

Commit

Permalink
feat(preprocessor): Allow preprocessor to handle binary files (#3054)
Browse files Browse the repository at this point in the history
  • Loading branch information
Keen Yee Liau authored and johnjbarton committed Jun 21, 2018
1 parent dee3615 commit 7b66e18
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
14 changes: 7 additions & 7 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,7 @@ function createPreprocessor (config, basePath, injector) {
var preprocessorNames = []
for (var i = 0; i < patterns.length; i++) {
if (mm(file.originalPath, patterns[i], {dot: true})) {
if (thisFileIsBinary) {
log.warn('Ignoring preprocessing (%s) %s because it is a binary file.',
config[patterns[i]].join(', '), file.originalPath)
} else {
preprocessorNames = combineLists(preprocessorNames, config[patterns[i]])
}
preprocessorNames = combineLists(preprocessorNames, config[patterns[i]])
}
}

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

instances[name] = p
preprocessors.push(p)
if (!thisFileIsBinary || p.handleBinaryFiles) {
preprocessors.push(p)
} else {
log.warn('Ignored preprocessing %s because %s has handleBinaryFiles=false.',
file.originalPath, name)
}
})

nextPreprocessor(null, thisFileIsBinary ? buffer : buffer.toString())
Expand Down
27 changes: 25 additions & 2 deletions test/unit/preprocessor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ describe('preprocessor', () => {
thirdCallback('error')
})

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

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

it('should not preprocess binary files', (done) => {
it('should not preprocess binary files by default', (done) => {
var fakePreprocessor = sinon.spy((content, file, done) => {
done(null, content)
})
Expand All @@ -310,6 +310,29 @@ describe('preprocessor', () => {
})
})

it('should preprocess binary files if handleBinaryFiles=true', (done) => {
const fakePreprocessor = sinon.spy((content, file, done) => {
done(null, content)
})
fakePreprocessor.handleBinaryFiles = true

var injector = new di.Injector([{
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
}, emitterSetting])

pp = m.createPreprocessor({'**/*': ['fake']}, null, injector)

const file = {originalPath: '/some/photo.png', path: 'path'}

pp(file, (err) => {
if (err) throw err

expect(fakePreprocessor).to.have.been.calledOnce
expect(file.content).to.be.an.instanceof(Buffer)
done()
})
})

it('should not preprocess binary files with capital letters in extension', (done) => {
var fakePreprocessor = sinon.spy((content, file, done) => {
done(null, content)
Expand Down

0 comments on commit 7b66e18

Please sign in to comment.