mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
24f43903b4
PR-URL: https://github.com/nodejs/node/pull/14235 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com>
36 lines
1.2 KiB
JavaScript
36 lines
1.2 KiB
JavaScript
'use strict'
|
|
module.exports = isOnlyDev
|
|
|
|
const moduleName = require('../utils/module-name.js')
|
|
const isDevDep = require('./is-dev-dep.js')
|
|
const isProdDep = require('./is-prod-dep.js')
|
|
|
|
// Returns true if the module `node` is only required direcctly as a dev
|
|
// dependency of the top level or transitively _from_ top level dev
|
|
// dependencies.
|
|
// Dual mode modules (that are both dev AND prod) should return false.
|
|
function isOnlyDev (node, seen) {
|
|
if (!seen) seen = new Set()
|
|
return node.requiredBy.length && node.requiredBy.every(andIsOnlyDev(moduleName(node), seen))
|
|
}
|
|
|
|
// There is a known limitation with this implementation: If a dependency is
|
|
// ONLY required by cycles that are detached from the top level then it will
|
|
// ultimately return true.
|
|
//
|
|
// This is ok though: We don't allow shrinkwraps with extraneous deps and
|
|
// these situation is caught by the extraneous checker before we get here.
|
|
function andIsOnlyDev (name, seen) {
|
|
return function (req) {
|
|
const isDev = isDevDep(req, name)
|
|
const isProd = isProdDep(req, name)
|
|
if (req.isTop) {
|
|
return isDev && !isProd
|
|
} else {
|
|
if (seen.has(req)) return true
|
|
seen.add(req)
|
|
return isOnlyDev(req, seen)
|
|
}
|
|
}
|
|
}
|