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

Preproc #3390

Merged
merged 3 commits into from
Oct 31, 2019
Merged

Preproc #3390

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
53 changes: 18 additions & 35 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,78 +70,61 @@ async function runProcessors (preprocessors, file, content) {
file.sha = CryptoUtils.sha1(content)
}

function createPriorityPreprocessor (config, preprocessorPriority, basePath, injector) {
function createPriorityPreprocessor (config = {}, preprocessorPriority, basePath, injector) {
const emitter = injector.get('emitter')
const alreadyDisplayedErrors = {}
const instances = {}
let patterns = Object.keys(config)
const instances = new Map()

function instantiatePreprocessor (name) {
if (alreadyDisplayedErrors[name]) {
return
}

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

let p
try {
p = injector.get('preprocessor:' + name)
if (!p) {
log.error(`Failed to instantiate preprocessor ${name}`)
emitter.emit('load_error', 'preprocessor', name)
}
} catch (e) {
if (e.message.includes(`No provider for "preprocessor:${name}"`)) {
log.error(`Can not load "${name}", it is not registered!\n Perhaps you are missing some plugin?`)
} else {
log.error(`Can not load "${name}"!\n ` + e.stack)
}
alreadyDisplayedErrors[name] = true
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
}

instances.set(name, p)
return p
}

let allPreprocessors = []
patterns.forEach((pattern) => {
allPreprocessors = _.union(allPreprocessors, config[pattern])
})
allPreprocessors.forEach(instantiatePreprocessor)
_.union.apply(_, Object.values(config)).forEach(instantiatePreprocessor)

return async function preprocess (file) {
patterns = Object.keys(config)

const buffer = await tryToRead(file.originalPath, log)
const isBinary = await isBinaryFile(buffer, buffer.length)

let preprocessorNames = []
patterns.forEach((pattern) => {
const preprocessorNames = Object.keys(config).reduce((ppNames, pattern) => {
if (mm(file.originalPath, pattern, { dot: true })) {
preprocessorNames = _.union(preprocessorNames, config[pattern])
ppNames = _.union(ppNames, config[pattern])
}
})
return ppNames
}, [])

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

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

await runProcessors(preprocessors, file, isBinary ? buffer : buffer.toString())
Expand Down