From 4b2753cdbdf5335d96593c402a3710ff3710360b Mon Sep 17 00:00:00 2001 From: Anton Shchekota Date: Tue, 22 Oct 2019 16:10:57 -0400 Subject: [PATCH 1/3] refactor: simplify logic with get processors names --- lib/preprocessor.js | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/lib/preprocessor.js b/lib/preprocessor.js index 05af99c34..9200bc283 100644 --- a/lib/preprocessor.js +++ b/lib/preprocessor.js @@ -70,11 +70,10 @@ 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) function instantiatePreprocessor (name) { if (alreadyDisplayedErrors[name]) { @@ -108,25 +107,18 @@ function createPriorityPreprocessor (config, preprocessorPriority, basePath, inj 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((res, pattern) => { if (mm(file.originalPath, pattern, { dot: true })) { - preprocessorNames = _.union(preprocessorNames, config[pattern]) + res = _.union(res, config[pattern]) } - }) + return res + }, []) // Apply preprocessor priority. const preprocessors = preprocessorNames From da6004695c6c916bbcc6a7aa2ebee0c2935215f3 Mon Sep 17 00:00:00 2001 From: Anton Shchekota Date: Tue, 22 Oct 2019 16:26:47 -0400 Subject: [PATCH 2/3] refactor: Remove alreadyDisplayedErrors and use Map for preprocessor instances --- lib/preprocessor.js | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/lib/preprocessor.js b/lib/preprocessor.js index 9200bc283..3018dd2ee 100644 --- a/lib/preprocessor.js +++ b/lib/preprocessor.js @@ -72,39 +72,30 @@ async function runProcessors (preprocessors, file, content) { function createPriorityPreprocessor (config = {}, preprocessorPriority, basePath, injector) { const emitter = injector.get('emitter') - const alreadyDisplayedErrors = {} - const instances = {} + 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 } _.union.apply(_, Object.values(config)).forEach(instantiatePreprocessor) From 0a627a7ecfded62e5a2c72169b8b529d1a5fb9c6 Mon Sep 17 00:00:00 2001 From: Anton Shchekota Date: Thu, 31 Oct 2019 10:15:28 +0300 Subject: [PATCH 3/3] refactor: rename res on better naming --- lib/preprocessor.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/preprocessor.js b/lib/preprocessor.js index 3018dd2ee..a8fa3a892 100644 --- a/lib/preprocessor.js +++ b/lib/preprocessor.js @@ -104,11 +104,11 @@ function createPriorityPreprocessor (config = {}, preprocessorPriority, basePath const buffer = await tryToRead(file.originalPath, log) const isBinary = await isBinaryFile(buffer, buffer.length) - const preprocessorNames = Object.keys(config).reduce((res, pattern) => { + const preprocessorNames = Object.keys(config).reduce((ppNames, pattern) => { if (mm(file.originalPath, pattern, { dot: true })) { - res = _.union(res, config[pattern]) + ppNames = _.union(ppNames, config[pattern]) } - return res + return ppNames }, []) // Apply preprocessor priority. @@ -116,15 +116,15 @@ function createPriorityPreprocessor (config = {}, preprocessorPriority, basePath .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())