diff --git a/lib/preprocessor.js b/lib/preprocessor.js index c11689f33..bfeae6dbb 100644 --- a/lib/preprocessor.js +++ b/lib/preprocessor.js @@ -17,14 +17,39 @@ extensions.forEach(function (extension) { isBinary['.' + extension] = true }) -// TODO(vojta): instantiate preprocessors at the start to show warnings immediately var createPreprocessor = function (config, basePath, injector) { - var alreadyDisplayedWarnings = Object.create(null) + var alreadyDisplayedWarnings = {} + var instances = {} + var patterns = Object.keys(config) - return function (file, done) { - var patterns = Object.keys(config) + var instantiatePreprocessor = function (name) { + if (alreadyDisplayedWarnings[name]) { + return + } + + try { + instances[name] = injector.get('preprocessor:' + name) + } catch (e) { + if (e.message.indexOf('No provider for "preprocessor:' + name + '"') !== -1) { + log.warn('Can not load "%s", it is not registered!\n ' + + 'Perhaps you are missing some plugin?', name) + } else { + log.warn('Can not load "%s"!\n ' + e.stack, name) + } + + alreadyDisplayedWarnings[name] = true + } + } + + patterns.forEach(function (pattern) { + config[pattern].forEach(instantiatePreprocessor) + }) + + return function preprocess (file, done) { + patterns = Object.keys(config) var thisFileIsBinary = isBinary[path.extname(file.originalPath)] var preprocessors = [] + var nextPreprocessor = function (error, content) { // normalize B-C if (arguments.length === 1 && typeof error === 'string') { @@ -47,34 +72,19 @@ var createPreprocessor = function (config, basePath, injector) { preprocessors.shift()(content, file, nextPreprocessor) } - var instantiatePreprocessor = function (name) { - if (alreadyDisplayedWarnings[name]) { - return - } - - try { - preprocessors.push(injector.get('preprocessor:' + name)) - } catch (e) { - if (e.message.indexOf('No provider for "preprocessor:' + name + '"') !== -1) { - log.warn('Can not load "%s", it is not registered!\n ' + - 'Perhaps you are missing some plugin?', name) - } else { - log.warn('Can not load "%s"!\n ' + e.stack, name) - } - - alreadyDisplayedWarnings[name] = true - } - } - // collects matching preprocessors - // TODO(vojta): should we cache this ? for (var i = 0; i < patterns.length; i++) { if (mm(file.originalPath, patterns[i])) { if (thisFileIsBinary) { log.warn('Ignoring preprocessing (%s) %s because it is a binary file.', config[patterns[i]].join(', '), file.originalPath) } else { - config[patterns[i]].forEach(instantiatePreprocessor) + config[patterns[i]].forEach(function (name) { + if (!instances[name]) { + instantiatePreprocessor(name) + } + preprocessors.push(instances[name]) + }) } } }