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

@devjoyvn/autoloader-ts

v2.2.2

Published

A autoloader for typescript

Downloads

4

Readme

AutoloaderTS

AutoloaderTS is a nodejs autoloader for typescript

Why and What ?

Importing files written in typescript in a nodejs environment can be rather tricky, Since you not only have to transpile the code but also have to be extra carefull to not mess up the scope and context. For this reason Autoloader-ts was born. It transpiles and requires all the selected files, and allow you to continue to use compiler plugins like tsconfig-paths.

Docs

RECOMMENDED: Take a look at our samples:

  1. Loading files
  2. Injecting Code
  3. Loading from a glob

Loader Types

There are 2 kinds of loaders this lib implements.

  1. DynamicImport-Autoloader (Recommended)

    This Autoloader uses the import(PATH) function for import.

    const loader = Autoloader.dynamicImport();
  2. NodeEval-Autoloader

    This Autoloader uses the "node-eval" lib and uses the fs api to load and dynamically invoke files.

    The ? after the parameter means "optional"

    const loader = Autoloader.nodeEval(customCompilerOptions?, tsConfigFilePath?);

You can instantiate one of the other by choosing the correct main AutoloaderTS method. For one or the other you may have to pass some parameters.

ATTENTION: Schemantics!

What I mean by this is: On the one loader you may need to pass a absolute path and on the other you can pass both a relative and a absolute path!

Even tho both loader have the SAME API! the schemantics may not be the same! I hope to implement libary level compatability layers in the future, so both can be used as a "drop-in" replacement.

Example

To use the autoloader you first need to create a instance of it. Most of the library is async so make sure to handle it!

import { Autoloader } from "autoloader-ts";

const loader = await Autoloader.nodeEval();

Then you can use the instance methods to load files. There are 2 main ways to load files.

  1. From directories
await loader.fromDirectories(`${__dirname}/autoload`);
  1. From a glob
await loader.fromGlob("*/**/*.autoload.ts");

If you need to inject code into you might want to call this method BEFORE loading the files

The loader will inject this JS code before evaluating it. You may use it to register compiler plugins like tsconfig-paths. DO NOT misuse this. If you are not careful you might override something and create very hard to find bugs. Also make sure to include a line-break or a semicolon at the end. The inject code will be concatenated with the transpiled code so make sure it is error free!.

loader.injectCode("require('tsconfig-register/paths'");

When you have specified all the files you want to load, access the "result" field.

console.log(loader.result.exports);

This result field contains all the exported classes / fields / functions from the loaded files. For example take this file:

export class A {
	public greet() {
		console.log("Hello World");
	}
}

If you then load the file with a method from above, the result.exports will be: [ [Function A] ] You can then loop through it and access the file.

Concret Usecase

Take this example: You build a http framework, this framework allows the developer to define Jobs that will be run by the framework

So you create a Job Decorator

import "reflect-metadata";

export function Job() {
	return Reflect.metadata([KEY], [VAL]);
}

Then the user of the framework can define subclasses of that type.

import { Job } from "lib/Job";

@Job()
export class UpdateUsersJob {
	run() {
		console.log("Updating user...");
	}
}

Without autoloading the user of the framework would have to import the class somewhere in the project or register it like this

Jobs.register(UpdateUserJob);

But with autoloading you dont haveto import your Jobs anywhere. Lets say all jobs are stored in app/jobs Then you can just create a loader and load the classes.

// Create the loader
const loader = await Autoloader.dynamicImport();

// Load and transpile all files from app/jobs
await loader.fromDirectories(`${__dirname}/app/jobs`);

// Iterate ofer the exports
for (const exported of loader.result.exports) {
	// Get the metadata of the export.
	const metadata: string = Reflect.getMetadata(JobMetadataKey, exported);

	// You can then access members of the class dynamically!
	Jobs.register(new exported());
}

Tada... Now you have a much more convenient and less boilerplate solution for jobs loading.