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