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

fix: if preprocessor is async function and doesn't return a content t… #3387

Merged
merged 1 commit into from
Oct 18, 2019
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
10 changes: 9 additions & 1 deletion lib/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ function executeProcessor (process, file, content) {
}
}
})
return process(content, file, done) || donePromise

return (process(content, file, done) || Promise.resolve()).then((content) => {
if (content) {
anthony-redFox marked this conversation as resolved.
Show resolved Hide resolved
// async process correctly returned content
return content
}
anthony-redFox marked this conversation as resolved.
Show resolved Hide resolved
// process called done() (Either old sync api or an async function that did not return content)
return donePromise
})
}

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

it('should get content if preprocessor is an async function or return Promise with content', (done) => {
const fakePreprocessor = sinon.spy(async (content, file, done) => {
file.path = file.path + '-preprocessed'
return '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 get content if preprocessor is an async function still calling done()', (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