Socket
Book a DemoInstallSign in
Socket

import-cartographer

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

import-cartographer

Explore the ESM import tree of a file

latest
npmnpm
Version
0.0.4
Version published
Maintainers
1
Created
Source

import-cartographer

Explore the ESM import tree of a file

Usage

import importer from 'import-cartographer'
// mock compile function
const compile = async name => compileSomeBundle()

const tree = importer(compile)

for await (let [str, location] of tree('./file.js')) {
  if (typeof location === 'string') {
    // local file
  } else {
    location = await location
    // named import, the result comes from the compile function
  }
}

Local files are loaded recursively, so a single tree() call will follow the entire local tree only terminating its recursion at each named import.

For every named import, the compile function is only run once and then cached indefinitely. The same result (most likely a promise from an async function) of the first named import will be emitted for every subsequent import of that name.

This means you can run tree() multiple times on different local files and continue to get the same cached result for any named imports.

const tree = importer(compile)

const run = async filename => {
  for await (let [str, location] of tree('./file.js')) {
    // do stuff
  }
}
const results = await Promise.all([
  run('./test/test-one.js'),
  run('./test/test-two.js')
])

As you can see, you can safely run this concurrently and still be guaranteed to only run each compile function for a named import once.

FAQs

Package last updated on 10 Oct 2020

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