Skip to content

Commit

Permalink
feat(preprocessor): Instantiate preprocessors early to avoid race con…
Browse files Browse the repository at this point in the history
…ditions
  • Loading branch information
dignifiedquire committed Jul 17, 2015
1 parent b3c462a commit 8a9c8c7
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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])
})
}
}
}
Expand Down

0 comments on commit 8a9c8c7

Please sign in to comment.