fix: should not throw when a plugin listed in optionalDependencies is not installed

This bug is due to `normalize-package-data` (required by `read-pkg`)
adding `optionalDependencies` to `dependencies`.
This commit is contained in:
Haoqun Jiang
2018-10-13 00:25:42 +08:00
parent c6ab80fd7a
commit 7ea080b9d1

View File

@@ -159,7 +159,23 @@ module.exports = class Service {
const projectPlugins = Object.keys(this.pkg.devDependencies || {})
.concat(Object.keys(this.pkg.dependencies || {}))
.filter(isPlugin)
.map(idToPlugin)
.map(id => {
if (
this.pkg.optionalDependencies &&
id in this.pkg.optionalDependencies
) {
let apply = () => {}
try {
apply = require(id)
} catch (e) {
warn(`Optional dependency ${id} is not installed.`)
}
return { id, apply }
} else {
return idToPlugin(id)
}
})
plugins = builtInPlugins.concat(projectPlugins)
}