Skip to content

Commit

Permalink
fix: if preprocessor is async function and doesn't return a content t…
Browse files Browse the repository at this point in the history
…hen await donePromise
  • Loading branch information
anthony-redFox committed Oct 18, 2019
1 parent 1cd87ad commit 5d471ea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ function executeProcessor (process, file, content) {
}
}
})
return process(content, file, done) || donePromise

return (process(content, file, done) || Promise.resolve()).then((content) => {
if (content) {
return content
}
return donePromise
})
}

async function runProcessors (preprocessors, file, content) {
Expand Down
21 changes: 21 additions & 0 deletions test/unit/preprocessor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ describe('preprocessor', () => {
})
})

it('should get content if preprocessor is async function', (done) => {
const fakePreprocessor = sinon.spy(async (content, file, done) => {
file.path = file.path + '-preprocessed'
done(null, 'new-content')
})

const injector = new di.Injector([{
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)

const file = { originalPath: '/some/.dir/a.js', path: 'path' }

pp(file, () => {
expect(fakePreprocessor).to.have.been.called
expect(file.path).to.equal('path-preprocessed')
expect(file.content).to.equal('new-content')
done()
})
})

it('should check patterns after creation when invoked', (done) => {
const fakePreprocessor = sinon.spy((content, file, done) => {
file.path = file.path + '-preprocessed'
Expand Down

0 comments on commit 5d471ea

Please sign in to comment.