is-only-optional.js 674 B

12345678910111213141516171819202122
  1. 'use strict'
  2. module.exports = isOptional
  3. const isOptDep = require('./is-opt-dep.js')
  4. const moduleName = require('../utils/module-name.js')
  5. function isOptional (node, seen) {
  6. if (!seen) seen = new Set()
  7. // If a node is not required by anything, then we've reached
  8. // the top level package.
  9. if (seen.has(node) || node.requiredBy.length === 0) {
  10. return false
  11. }
  12. seen.add(node)
  13. const swOptional = node.fromShrinkwrap && node.package._optional
  14. const result = node.requiredBy.every(function (req) {
  15. if (req.fakeChild && swOptional) return true
  16. return isOptDep(req, moduleName(node)) || isOptional(req, seen)
  17. })
  18. seen.delete(node)
  19. return result
  20. }