is-only-dev.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict'
  2. module.exports = isOnlyDev
  3. const moduleName = require('../utils/module-name.js')
  4. const isDevDep = require('./is-dev-dep.js')
  5. const isProdDep = require('./is-prod-dep.js')
  6. // Returns true if the module `node` is only required direcctly as a dev
  7. // dependency of the top level or transitively _from_ top level dev
  8. // dependencies.
  9. // Dual mode modules (that are both dev AND prod) should return false.
  10. function isOnlyDev (node, seen) {
  11. if (!seen) seen = new Set()
  12. return node.requiredBy.length && node.requiredBy.every(andIsOnlyDev(moduleName(node), seen))
  13. }
  14. // There is a known limitation with this implementation: If a dependency is
  15. // ONLY required by cycles that are detached from the top level then it will
  16. // ultimately return true.
  17. //
  18. // This is ok though: We don't allow shrinkwraps with extraneous deps and
  19. // these situation is caught by the extraneous checker before we get here.
  20. function andIsOnlyDev (name, seen) {
  21. return function (req) {
  22. const isDev = isDevDep(req, name)
  23. const isProd = isProdDep(req, name)
  24. if (req.isTop) {
  25. return isDev && !isProd
  26. } else {
  27. if (seen.has(req)) return true
  28. seen.add(req)
  29. const result = isOnlyDev(req, seen)
  30. seen.delete(req)
  31. return result
  32. }
  33. }
  34. }