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

@pyrodash/file-loader

v1.0.4

Published

A runtime file loader for JavaScript/TypeScript with Hot Reloading support

Downloads

8

Readme

@pyrodash/file-loader

A module that allows you to load files and instantiate classes dynamically at runtime, with support for hot reloading files.

Install

npm install @pyrodash/file-loader

Notes

Nested mode

The loader can index and load files at the root level of a directory (nested: false), and it can also index and load files nested inside folders (nested: true) such as the following structure:

└── modules
    ├── bakery
        └── main.ts
    ├── cafe
        └── main.ts
    └── store
        └── main.ts

Terminology

Filenames used by the parameters of functions like loadFromFileName() and the ignored config property, do not always refer to filenames. In nested mode, they refer to the name of the folder (e.g. bakery, cafe and store in the previous example). When nested mode is turned off, they just refer to filenames.

Names, which are used in the parameters of functions like unload() and reload(), are identical to filenames, but they have the file extension omitted.

Examples

With nested: true

import { Loader } from '@pyrodash/file-loader'

const loader = new Loader({
    path: '/path/to/directory',
    nested: true,
    mainFile: 'main.ts',
    ignored: ['hello'],
    classes: {
        instantiate: true,
        params: ['param 1', 'param 2'], // these parameters are passed to class constructors
    },
})

With nested: false

import { Loader } from '@pyrodash/file-loader'

const loader = new Loader({
    path: '/path/to/files',
    nested: false,
    ignored: ['hello.ts'],
    classes: {
        instantiate: true,
        params: ['param 1', 'param 2'], // these parameters are passed to class constructors
    },
})

API

loader.onReady(() => ...)

// loading files
await loader.loadFiles('/path/to/files', isNested) // use this when you have autoLoad off and want to run/await the initial loading
await loader.loadFiles() // defaults to configured path and nested properties

await loader.loadFromFileName(name)
await loader.loadFromPath(path)
await loader.loadFromPath(path, name) // explicitly pass a name when loading a file that doesn't follow your configuration (e.g. your loader is in nested mode, but the path you're loading isn't meant to be named like such)

// unloading files
await loader.unloadFromPath(path)
await loader.unload(name)

// reloading files
await loader.reloadFromPath(path)
await loader.reload(name)

// events
loader.on('ready', () => ...) // emitted after first initial loading
loader.on('error', (err) => ...)
loader.on('warning', (err) => ...)
loader.on('load-many', (files) => ...) // emitted after loadFiles() runs
loader.on('load', (name, file) => ...)
loader.on('unload', (file) => ...)
loader.on('reload', (newFile, oldFile) => ...)

LoaderConfig

  • path: string

Directory to load files from

  • nested?: boolean

Whether or not to enable nested mode

  • mainFile?: string | ((file: string) => string)

Refers to the actual name of the main file inside nested files, can be a string or a function that takes an absolute folder path and returns an absolute path to the main file

  • ignored?: string[]

An array of filenames (folder names in case of nested mode, filenames otherwise) to be ignored by the loader

  • autoLoad?: boolean

Whether or not the loader should immediately begin loading files automatically

File extensions loaded by the loader

  • watch?: boolean (experimental)

Whether or not to watch loaded files for changes and automatically reload them

Defaults

{
    nested: false,
    mainFile: 'index.js' | 'index.ts', // depends on if you're running in js or ts at runtime
    ignored: [],
    autoLoad: true,
    classes: {
        instantiate: true,
        params: [],
    },
    allowedFileExts: ['.js', '.ts']
}

ClassOptions[T]

  • instantiate?: boolean

Whether or not to instantiate the class

  • params?: unknown[]

Parameters to be passed the class constructors

  • findConstructor?: (name: string, mdl: any) => ConstructorType[T] | null

A function to extract a constructor from a module's exports

  • destroy?: (instance: T) => void | Promise[void]

A function to destroy/deconstruct an instance when it's being unloaded

  • reload?: (newInstance: T, oldInstance: any) => void | Promise[void]

A function to rebuild a reloaded instance with the data of the old instance

TODO

  • Improve unit tests
  • Watch loaded files for changes