Skip to content

Commit

Permalink
Merge 1df33f8 into e246461
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjbarton authored Jan 22, 2021
2 parents e246461 + 1df33f8 commit d2d38a1
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 208 deletions.
34 changes: 33 additions & 1 deletion lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,36 @@ function resolve (plugins, emitter) {
return modules
}

exports.resolve = resolve
const pluginInstances = new Map()

function createInstantiatePlugin (injector) {
const emitter = injector.get('emitter')
return function instantiatePlugin (kind, name) {
if (pluginInstances.has(name)) {
return pluginInstances.get(name)
}

let p
try {
p = injector.get(`${kind}:${name}`)
if (!p) {
log.error(`Failed to instantiate ${kind} ${name}`)
emitter.emit('load_error', `${kind}`, name)
}
} catch (e) {
if (e.message.includes(`No provider for "${kind}:${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)
}
emitter.emit('load_error', `${kind}`, name)
}

pluginInstances.set(name, p)
return p
}
}

createInstantiatePlugin.$inject = ['injector']

module.exports = { resolve, createInstantiatePlugin }
36 changes: 4 additions & 32 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,36 +70,8 @@ async function runProcessors (preprocessors, file, content) {
file.sha = CryptoUtils.sha1(content)
}

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

function instantiatePreprocessor (name) {
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)
}
emitter.emit('load_error', 'preprocessor', name)
}

instances.set(name, p)
return p
}
_.union.apply(_, Object.values(config)).forEach(instantiatePreprocessor)

function createPriorityPreprocessor (config = {}, preprocessorPriority, basePath, instantiatePlugin) {
_.union.apply(_, Object.values(config)).forEach((name) => instantiatePlugin('preprocessor', name))
return async function preprocess (file) {
const buffer = await tryToRead(file.originalPath, log)
let isBinary = file.isBinary
Expand All @@ -121,7 +93,7 @@ function createPriorityPreprocessor (config = {}, preprocessorPriority, basePath
.sort((a, b) => b[1] - a[1])
.map((duo) => duo[0])
.reduce((preProcs, name) => {
const p = instantiatePreprocessor(name)
const p = instantiatePlugin('preprocessor', name)

if (!isBinary || (p && p.handleBinaryFiles)) {
preProcs.push(p)
Expand All @@ -135,5 +107,5 @@ function createPriorityPreprocessor (config = {}, preprocessorPriority, basePath
}
}

createPriorityPreprocessor.$inject = ['config.preprocessors', 'config.preprocessor_priority', 'config.basePath', 'injector']
createPriorityPreprocessor.$inject = ['config.preprocessors', 'config.preprocessor_priority', 'config.basePath', 'instantiatePlugin']
exports.createPriorityPreprocessor = createPriorityPreprocessor
1 change: 1 addition & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Server extends KarmaEventEmitter {
watcher: ['value', watcher],
launcher: ['factory', Launcher.factory],
config: ['value', config],
instantiatePlugin: ['factory', plugin.createInstantiatePlugin],
preprocess: ['factory', preprocessor.createPriorityPreprocessor],
fileList: ['factory', FileList.factory],
webServer: ['factory', createWebServer],
Expand Down
Loading

0 comments on commit d2d38a1

Please sign in to comment.