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

@universal-packages/module-loader

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@universal-packages/module-loader

Imports deeply all modules in a directory as a list to use later

  • 1.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15K
decreased by-16.91%
Maintainers
1
Weekly downloads
 
Created
Source

Module Loader

npm version Testing codecov

Imports deeply all modules in a directory as a list to use later, this is useful when you are expecting a arbitrary amount of modules that behaves the same and can be used without the need of importing them manually or even importing them manually will be a hazel, it can also be useful to activate modules (probably with decorators) that need to be activated before being imported manually later.

Install

npm install @universal-packages/module-loader

Global methods

loadModules(location: string, [options])

Load recursively a directory until it finds a package, index file or just a file in that order, If it finds a package.json it will just import the main file in the package, If it find and index file it will just import that index file, If any of above it will just import files deep in the hierarchy.

import { loadModules } from '@universal-packages/module-loader'

async function test() {
  const modules = await loadModules('./modules')

  console.log(modules)
}

test()

// > [
// >   { location: './modules/module.js' exports: { load: Function, unload: Function }, type: 'file' }
// >   { location: './modules/utils.js' exports: { pad: Function }, type: 'file' }
// >   ...
// > ]

Options

  • onlyDefault boolean When loading the module directly only set exports as the exports.default, useful if you are importing classes or components that predominate in the module.

  • conventionPrefix string Only load modules that follow the convention <module>.<conventionPrefix>.js, exp: User.model.js.

    import { loadModules } from '@universal-packages/module-loader'
    
    async function test() {
      const modules = await loadModules('./modules', { conventionPrefix:  })
    
      console.log(modules)
    }
    
    test()
    
    // > [
    // >   { location: './models/Post.model.js' exports: [class Post], type: 'file' }
    // >   { location: './models/User.model.js' exports: [class User], type: 'file' }
    // >   ...
    // > ]
    

Typescript

This library is developed in TypeScript and shipped fully typed.

Contributing

The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.

License

MIT licensed.

FAQs

Package last updated on 02 Feb 2024

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