Skip to content

Commit df114e1

Browse files
anthony-redFoxjohnjbarton
authored andcommitted
Refactor (#3382)
* refactor: Make instantiatePreprocessor work with cache and log all errors * refactor: use reduce() to create preprocessors Array
1 parent fbad778 commit df114e1

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

lib/preprocessor.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ function createPriorityPreprocessor (config, preprocessorPriority, basePath, inj
5454
return
5555
}
5656

57-
let p
57+
let p = instances[name]
58+
if (p) {
59+
return p
60+
}
5861

5962
try {
6063
p = injector.get('preprocessor:' + name)
@@ -68,6 +71,14 @@ function createPriorityPreprocessor (config, preprocessorPriority, basePath, inj
6871
emitter.emit('load_error', 'preprocessor', name)
6972
}
7073

74+
if (!p && !alreadyDisplayedErrors[name]) {
75+
alreadyDisplayedErrors[name] = true
76+
log.error(`Failed to instantiate preprocessor ${name}`)
77+
emitter.emit('load_error', 'preprocessor', name)
78+
} else {
79+
instances[name] = p
80+
}
81+
7182
return p
7283
}
7384

@@ -107,31 +118,20 @@ function createPriorityPreprocessor (config, preprocessorPriority, basePath, inj
107118
})
108119

109120
// Apply preprocessor priority.
110-
let sortedPreprocessorNames = preprocessorNames
121+
const preprocessors = preprocessorNames
111122
.map((name) => [name, preprocessorPriority[name] || 0])
112123
.sort((a, b) => b[1] - a[1])
113124
.map((duo) => duo[0])
125+
.reduce((res, name) => {
126+
const p = instantiatePreprocessor(name)
114127

115-
let preprocessors = []
116-
sortedPreprocessorNames.forEach((name) => {
117-
const p = instances[name] || instantiatePreprocessor(name)
118-
119-
if (p == null) {
120-
if (!alreadyDisplayedErrors[name]) {
121-
alreadyDisplayedErrors[name] = true
122-
log.error(`Failed to instantiate preprocessor ${name}`)
123-
emitter.emit('load_error', 'preprocessor', name)
128+
if (!isBinary || (p && p.handleBinaryFiles)) {
129+
res.push(p)
130+
} else {
131+
log.warn(`Ignored preprocessing ${file.originalPath} because ${name} has handleBinaryFiles=false.`)
124132
}
125-
return
126-
}
127-
128-
instances[name] = p
129-
if (!isBinary || p.handleBinaryFiles) {
130-
preprocessors.push(p)
131-
} else {
132-
log.warn(`Ignored preprocessing ${file.originalPath} because ${name} has handleBinaryFiles=false.`)
133-
}
134-
})
133+
return res
134+
}, [])
135135

136136
runProcessors(preprocessors, file, isBinary ? buffer : buffer.toString()).then(done, done)
137137
})

0 commit comments

Comments
 (0)