Skip to content

Commit

Permalink
refactor: Remove lodash usages and make instantiatePreprocessor works…
Browse files Browse the repository at this point in the history
… with cache and log errors
  • Loading branch information
anthony-redFox committed Oct 9, 2019
1 parent 7a92477 commit 5371987
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions lib/preprocessor.js
Expand Up @@ -3,7 +3,6 @@
const fs = require('graceful-fs')
const mm = require('minimatch')
const isBinaryFile = require('isbinaryfile')
const _ = require('lodash')
const CryptoUtils = require('./utils/crypto-utils')

const log = require('./logger').create('preprocess')
Expand Down Expand Up @@ -54,7 +53,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,13 +70,19 @@ 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
}

let allPreprocessors = []
patterns.forEach((pattern) => {
allPreprocessors = _.union(allPreprocessors, config[pattern])
})
const allPreprocessors = new Set()
patterns.forEach((pattern) => (config[pattern] || []).forEach((proc) => allPreprocessors.add(proc)))
allPreprocessors.forEach(instantiatePreprocessor)

return function preprocess (file, done) {
Expand All @@ -99,39 +107,28 @@ function createPriorityPreprocessor (config, preprocessorPriority, basePath, inj
throw err
}

let preprocessorNames = []
const preprocessorNames = new Set()
patterns.forEach((pattern) => {
if (mm(file.originalPath, pattern, { dot: true })) {
preprocessorNames = _.union(preprocessorNames, config[pattern])
(config[pattern] || []).forEach((name) => preprocessorNames.add(name))
}
})

// Apply preprocessor priority.
let sortedPreprocessorNames = preprocessorNames
const preprocessors = Array.from(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 5371987

Please sign in to comment.