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

require-loader

v0.1.0

Published

Add/Remove module dynamically when there is change in the specified directory

Downloads

7

Readme

require-loader

Watch the specified directory and dynamically add/remove module when there is any change. [require-loader] is local to the module.


Purpose

To allow dynamically add / remove module in a simple way. When new file is put into the directory, it uses "require()" to add module as usual, and it can be referenced in the [require-loader] map. When file is removed from the directory, it will be removed from require.cache and the [require-loader] map.

[require-loader] is local to the module implementing it. Therefore you can set up require-loader in 'test-a.js' and 'test-b.js' watching on different directories, and not interferring each other.

Installation

$ npm install require-loader

Quick Start

Assume you have a folder 'loader' you want to watch for adding/removing module, and you already have /loader/test-1.js , in your test.js file, you can:

/* test-.js */
var rl = require("require-loader").create({path:__dirname + '/loader', init:true});

var t1 = rl.getMap()['test-1.js'];
t1.run(); // assume run() is export from test-1.js

Features & Options

You can pass in the following options when you require the 'require-loader':

var options = {
    path    :   String,     /* The full path of the directory you want to watch, default to './' */
    init    :   Boolean,    /* To run an initial load from the directory or not, default to false */
    isWatch :   Boolean,    /* To watch the directory or not.  Default to true */
    onChange:   function(err, data){}    /* Call when any module is added or removed.  It will data as: {action:String, filename:String, resolve_name:String, ref:Object   }, action is either "add" or "remove", ref is the required Module just added. */
}

Example:

var rl = require("../lib/require-loader.js");

var onChange = function(err, data) {
        console.log("call back: " + JSON.stringify(data));
        if (data.action == 'add') {
            data.ref.run();
        }
}

// Set the absolute path of the directory to watch, and start initial function right away.
var rmanager = rl.create({path:__dirname + '/simple-folder', init:true, onChange:onChange});

// get the map
var map = rmanager.getMap();

// check the map keys. 
for (var m in map) {
    console.log("MAP: " + m + " V" + map[m]);
}

// test run it. 
var t1 = map['test-1.js'];
t1.run();

Use cases

One of the use cases is for loading multiple configs for different domains from a directory. See test example "hosts.js". It uses express.js and load different website "app" from the hosts-loader folder.

var express = require('express');
var app = express();
var RL = require("require-loader");

var onDomainChange = function(err, data) {
    if (data.action == 'add') {
        // data.ref is the reference of the app object.
        app.use(express.vhost(data.ref.domain, data.ref.app));
    } 
}

// uses require-loader to watch hosts-loader folder and load the different apps to the main app server. 
var reqLoader = RL.create({path:__dirname + '/hosts-loader', init:true, isWatch:true, onChange:onDomainChange});

// testing for output.
var host1 = reqLoader.getMap()['host1.js'];
host1.output();

app.listen(3000);