Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(preprocessor): Allow preprocessor to handle binary files #3054

Merged
merged 1 commit into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

@lusarz lusarz Jun 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need handleBinaryFiles flag and this whole if statement ? Maybe we can just allow to preprocess binary files, and if something fails, preprocessor will throw error.

I guess in more than 90% there will be no binary files that matches pattern and should be preprocessed (Do I miss something ?).

Just thinking out loud, and I'm curious others opinion about that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just for safe backwards compatibility.
Currently if there is a pattern foo/* that matches binary files, it is ignored. Without this flag, the 10% projects that do include binary files in their pattern will break.

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