Code Monkey home page Code Monkey logo

ts-module-loader's Introduction

Typescript module loader

npm version github actions status code style: prettier

How to load every single modules from a directory?

src/
├── index.ts
└── modules/
    ├── Article.ts
    ├── Category.ts
    └── Image.ts

The basic idea is to import each module one-by-one. Then list the modules one-by-one to create the map. But what appends when a new module is added to the directory? It must be added twice in the code. That is error prone and a potential source of headache.

// Don't do that

import Article from 'modules/Article';
import Category from 'modules/Category';
import Image from 'modules/Image';

const moduleMap = {
  Article: Article,
  Category: Category,
  Image: Image,
};

Async

import loadModuleMap from '@prismamedia/ts-module-loader';
# or import { loadModuleMap } from '@prismamedia/ts-module-loader';

# [...]

# The simplest call, the result will be a "Map<string, any>"
const moduleMap = await loadModuleMap(`${__dirname}/modules`);

# A call with params
const moduleMap = await loadModuleMap({
  directory: `${__dirname}/modules`,
  include: /^[_A-Za-z][_0-9A-Za-z]*$/,
  strict: true
});

# A call with a known module type/interface, the result will be a "Map<string, ModuleInterface>"
interface ModuleInterface {}
const moduleMap = await loadModuleMap<ModuleInterface>(`${__dirname}/modules`);

Sync

import { loadModuleMapSync } from '@prismamedia/ts-module-loader';

# [...]

const moduleMap = loadModuleMapSync(`${__dirname}/modules`);

const moduleMap = loadModuleMapSync({
  directory: `${__dirname}/modules`,
  include: /^[_A-Za-z][_0-9A-Za-z]*$/,
  strict: true
});

ts-module-loader's People

Contributors

gromnan avatar yvann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

gromnan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.