Skip to content

Commit f91be24

Browse files
anthony-redFoxjohnjbarton
authored andcommittedOct 18, 2019
fix: if preprocessor is async function and doesn't return a content then await donePromise (#3387)
1 parent 259be0d commit f91be24

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed
 

‎lib/preprocessor.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ function executeProcessor (process, file, content) {
2424
}
2525
}
2626
})
27-
return process(content, file, done) || donePromise
27+
28+
return (process(content, file, done) || Promise.resolve()).then((content) => {
29+
if (content) {
30+
// async process correctly returned content
31+
return content
32+
}
33+
// process called done() (Either old sync api or an async function that did not return content)
34+
return donePromise
35+
})
2836
}
2937

3038
async function runProcessors (preprocessors, file, content) {

‎test/unit/preprocessor.spec.js

+42
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,48 @@ describe('preprocessor', () => {
8080
})
8181
})
8282

83+
it('should get content if preprocessor is an async function or return Promise with content', (done) => {
84+
const fakePreprocessor = sinon.spy(async (content, file, done) => {
85+
file.path = file.path + '-preprocessed'
86+
return 'new-content'
87+
})
88+
89+
const injector = new di.Injector([{
90+
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
91+
}, emitterSetting])
92+
pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)
93+
94+
const file = { originalPath: '/some/.dir/a.js', path: 'path' }
95+
96+
pp(file, () => {
97+
expect(fakePreprocessor).to.have.been.called
98+
expect(file.path).to.equal('path-preprocessed')
99+
expect(file.content).to.equal('new-content')
100+
done()
101+
})
102+
})
103+
104+
it('should get content if preprocessor is an async function still calling done()', (done) => {
105+
const fakePreprocessor = sinon.spy(async (content, file, done) => {
106+
file.path = file.path + '-preprocessed'
107+
done(null, 'new-content')
108+
})
109+
110+
const injector = new di.Injector([{
111+
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
112+
}, emitterSetting])
113+
pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)
114+
115+
const file = { originalPath: '/some/.dir/a.js', path: 'path' }
116+
117+
pp(file, () => {
118+
expect(fakePreprocessor).to.have.been.called
119+
expect(file.path).to.equal('path-preprocessed')
120+
expect(file.content).to.equal('new-content')
121+
done()
122+
})
123+
})
124+
83125
it('should check patterns after creation when invoked', (done) => {
84126
const fakePreprocessor = sinon.spy((content, file, done) => {
85127
file.path = file.path + '-preprocessed'

0 commit comments

Comments
 (0)
Please sign in to comment.