Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-module-deps

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-module-deps

Babel plugin to annotate CJS modules with what they require

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

babel-plugin-module-deps

This Babel plugin redefines require within all (CJS) modules to record any required modules in an array of resolved filenames called module.deps. (Note that the array may have duplicates.) It only works with Node.js CommonJS modules (though with @babel/plugin-transform-modules-commonjs you can still use ESM syntax).

The module.deps arrays let a tool later discover what other modules each module depends on, by inspecting require.cache[filename].deps. This can be useful to deep-reload a module, or to detect when a module should be called according to whether a generated file is older than the module or dependency code.

Usage

Add the module as a plugin to your Babel configuration, as in:

{
  "plugins": ["babel-plugin-module-deps"]
}

Generally you want this plugin to run last, so list it last in the "plugins" array. In particular, this plugin should run after @babel/plugin-transform-modules-commonjs (if you're using ESM syntax in CJS), so that require is redefined before imports get executed (as they are simulated by require).

Then you can access the current module's dependencies via module.deps, or an arbitrary module's dependencies via require.cache[filename].deps, where filename is the require.resolved filename for a module.

Recursive Dependencies

If you've just run require(modname) with @babel/register configured to run this plugin, then calling the function below as walkDeps(modname) should give an array of all recursive module dependencies that modname requires or imports as require.resolved filenames (including require.resolve(modname) itself, but excluding duplicates).

function walkDeps(modname) {
  const deps = {};
  function recurse(submodname) {
    deps[submodname] = true;
    const submod = require.cache[submodname];
    if (!submod) return;
    const subdeps = submod.deps;
    if (!subdeps) return;
    for (dep of subdeps)
      if (!dep in deps)
        recurse(dep);
  }
  recurse(require.resolve(modname));
  return Object.keys(deps);
}

FAQs

Package last updated on 27 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc