Skip to content

Commit

Permalink
Refactor (#3382)
Browse files Browse the repository at this point in the history
* refactor: Make instantiatePreprocessor work with cache and log all errors

* refactor: use reduce() to create preprocessors Array
  • Loading branch information
anthony-redFox authored and johnjbarton committed Oct 17, 2019
1 parent fbad778 commit df114e1
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions lib/preprocessor.js
Expand Up @@ -54,7 +54,10 @@ function createPriorityPreprocessor (config, preprocessorPriority, basePath, inj
return
}

let p
let p = instances[name]
if (p) {
return p
}

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

if (!p && !alreadyDisplayedErrors[name]) {
alreadyDisplayedErrors[name] = true
log.error(`Failed to instantiate preprocessor ${name}`)
emitter.emit('load_error', 'preprocessor', name)
} else {
instances[name] = p
}

return p
}

Expand Down Expand Up @@ -107,31 +118,20 @@ function createPriorityPreprocessor (config, preprocessorPriority, basePath, inj
})

// Apply preprocessor priority.
let sortedPreprocessorNames = preprocessorNames
const preprocessors = preprocessorNames
.map((name) => [name, preprocessorPriority[name] || 0])
.sort((a, b) => b[1] - a[1])
.map((duo) => duo[0])
.reduce((res, name) => {
const p = instantiatePreprocessor(name)

let preprocessors = []
sortedPreprocessorNames.forEach((name) => {
const p = instances[name] || instantiatePreprocessor(name)

if (p == null) {
if (!alreadyDisplayedErrors[name]) {
alreadyDisplayedErrors[name] = true
log.error(`Failed to instantiate preprocessor ${name}`)
emitter.emit('load_error', 'preprocessor', name)
if (!isBinary || (p && p.handleBinaryFiles)) {
res.push(p)
} else {
log.warn(`Ignored preprocessing ${file.originalPath} because ${name} has handleBinaryFiles=false.`)
}
return
}

instances[name] = p
if (!isBinary || p.handleBinaryFiles) {
preprocessors.push(p)
} else {
log.warn(`Ignored preprocessing ${file.originalPath} because ${name} has handleBinaryFiles=false.`)
}
})
return res
}, [])

runProcessors(preprocessors, file, isBinary ? buffer : buffer.toString()).then(done, done)
})
Expand Down

0 comments on commit df114e1

Please sign in to comment.