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

Refactor #3382

Merged
merged 2 commits into from
Oct 17, 2019
Merged
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
42 changes: 21 additions & 21 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
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