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