npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

esm-directory-import

v1.0.2

Published

Import the files in a directory recursively using dynamic import(import.meta.url)

Downloads

6

Readme

esm-directory-import

Using the import.meta.url file URL to recursively and dynamically import() each of the files inside the directory of the specified URL. The default export for each file (module) is collected into a dictionary that sets the export into a directory delimited structure.

Directory structure example

- directory1
  - index.js (file that calls `importDir(import.meta.url)`
  - file1.js
  - directory2
    - file2.mjs
    - file3.cjs

Returned object structure example

{
  file1: (default export of file1.js),
  directory2: {
    file2: (default export of file2.mjs),
    file3: (default export of file3.cjs),
  }
}

Usage

import importDir from 'import-dir';

export const defaults = await importDir(import.meta.url);
// or
export const defaults = await importDir(import.meta.url, { exclude: ['directory2/file2.mjs'] });

Arguments

The default export function of esm-directory-import accepts 2 arguments, the first is a file URL, typically the value of import.meta.url from the ESM that invokes the function. The second argument is a dictionary of options outlined here:

  • exclude: string[] = [] An array of string file paths relative to the initial file URL that should be excluded from the returned object.
  • glob: (pattern: string, options: GlobOptions) => Promise<string[]> = glob A function that accepts a glob pattern as the first argument and an object that specifies { cwd: dirname(fileUrlToPath(import.meta.url)) }.
  • extensions: string[] = ['.mjs', '.cjs', '.js'] An array of string file extensions that will be used in the glob pattern used for determining the files that should be imported.
  • template: (exts: string[]) => string = (exts) => '**/*{${exts.join(',')}}' A function that takes a list of file extensions that should be looked for and returns a glob file matching pattern.
  • collector: (mod: Module) => any = (mod) => mod.default A function that accepts the result of await import(file) as an argument and returns the value that should be stored as the value of the file in the object structure.