-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(main|extract): adds builtInModules option to tweak what to co…
…nsider built-in (/ core) modules (#847) ## Description - adds a `builtInModules` option to tweak what to consider built-in (/ core) modules ## Motivation and Context By default dependency-cruiser considers nodejs built-in modules as core modules. In other contexts this might be not (entirely) right: - when targeting the browser, the core modules are either not available or you're using a shim (e.g. https://www.npmjs.com/package/path). In these cases dependency-cruiser should respectively flag the included core module as unresolvable or resolve it to node_modules/path - when targeting a platform built on top of nodejs, like electron, you might want to specify the packages built into that platform as. This PR adds options for both of these. See the documentation included in this PR for details Fixes #845 ## How Has This Been Tested? - [x] green ci - [x] additional automated tests ## Types of changes - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] Documentation only change - [ ] Refactor (non-breaking change which fixes an issue without changing functionality) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change)
- Loading branch information
Showing
16 changed files
with
377 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { builtinModules } from "node:module"; | ||
|
||
function getBuiltIns(pResolveOptions) { | ||
// builtinModules does not expose all builtin modules for #reasons - | ||
// see https://github.com/nodejs/node/issues/42785. In stead we could use | ||
// isBuiltin, but that is not available in node 16.14, the lowest version | ||
// of node dependency-cruiser currently supports. So we add the missing | ||
// modules here. | ||
let lReturnValue = builtinModules.concat(["test", "node:test"]); | ||
|
||
if (pResolveOptions?.builtInModules?.override) { | ||
lReturnValue = pResolveOptions?.builtInModules?.override; | ||
} | ||
if (pResolveOptions?.builtInModules?.add) { | ||
lReturnValue = lReturnValue.concat(pResolveOptions.builtInModules.add); | ||
} | ||
return lReturnValue; | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} pModuleName - the unresolved module name | ||
* @param {*} pResolveOptions | ||
* @returns {boolean} - true if the module is a built-in module | ||
*/ | ||
export function isBuiltin(pModuleName, pResolveOptions) { | ||
return getBuiltIns(pResolveOptions).includes(pModuleName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.