-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
fix(plugins): refactor instantiatePlugin from preproprocessor #3628
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't you inject the emitter directly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is existing code. I don't understand your comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is true that it is existing code. I just wonder why emitter is not directly injected and instead injector is injected, from which we get emitter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My new answer: because we need the injector for the |
||
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?`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: remove space: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} else { | ||
log.error(`Can not load "${name}"!\n ` + e.stack) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
} | ||
emitter.emit('load_error', `${kind}`, name) | ||
} | ||
|
||
pluginInstances.set(name, p) | ||
return p | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tell me if I am wrong, but this seems like dependency injection and we already have a module for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which module? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that the cache is completely redundant and I will remove that part. The rest is error handling which could be in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if the cache was there to prevent multiple attempts to instantiate a missing preprocessor and consequently the same error being logged a lot of times 🤔 |
||
|
||
createInstantiatePlugin.$inject = ['injector'] | ||
|
||
module.exports = { resolve, createInstantiatePlugin } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you take this variable outside the factory function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done