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

bagel-module-loader

v1.0.1

Published

Flexible module loader

Downloads

8

Readme

Description

bagel-module-loader contains all module loading logic for bagel.

Installation

npm install --save bagel-module-loader

Usage

import createLoader from 'bagel-module-loader';

// initialize loader
const load = createLoader({resolvers: [testResolver]});

// If desired, assemble some context data which will be accessible at multiple points during the module load process
const context = {foo: 'foo'};

// load some modules!
const loaded = load('foo', __dirname, {});

Configuration Options

{
  // list of module resolvers for custom resolving
  resolvers?: Array<Resolver>,
  // list of methods to perform an action before default require() is called
  interceptors?: Array<Interceptor>,
  // list of methods to convert source code
  sourceCodeTransformers?: Array<Transformer>,
  // method to wrap the module being served
  wrapModule?: source: string => result: string,
  // method to customize cache key generation within bagel
  generateModuleCacheKey?: GenerateModuleCacheKey,
}

Request Context

A number of the hooks in the module loader provide access to a context object which can be passed in with each call to the modoule loader. The core bagel module supplies a JobHandlerRequest as context.

({ jobRequest, parentBatchRequest, jobResponseMetadata, batchResponseMetadata }: JobHandlerRequest)

Module Resolvers

type Resolver = (
  dependencyID: string,
  from: string,
  requestContext: {}
) => string | null;

Bagel's resolve method loops through the resolvers provided, falling back on default node resolution if none of the provided resolvers are able to resolve the module.

Module Interceptors

Module Interceptors Flow

type Interceptor = ({
  moduleID: string,
  requestContext: {[string]: any},
  next: string => any
}) => any;

Interceptors allow developers to tap into the module loading process. In the process of loading a module, bagel will sequentially step through the interceptors supplied before the actual 'require' function is invoked. These methods have access to a specific job's moduleID, requestContext and next. Interceptors can modify the requestContext supplied and can invoke next() to delegate to the next interceptor in the chain. Interceptors can also short-circuit the module loading process by returning early. These two flows are illustrated in the diagram above.

Source Code Transformers

type Transformer = ({path: string, source: string}) => {
  errors: Array<string>,
  transformedSource: string
};

Provide a source code transformer if you would like to transform the initial module source code.

Wrap Module

Pass in your own wrapper function if you would like to customize how a module is wrapped before it is loaded.

Generate Module Cache Key

type GenerateModuleCacheKey = ({
  moduleID: string,
  requestContext: Object,
  pathToSourceFile: string
}) => string | null;

// ie)
({moduleID}) => `${moduleID}_${pretendCacheBuster}`;

bagel-module-loader caches compiled source code. If a cache key isn't found, the source code will be loaded from disk in the course of loading the module. generateModuleCacheKey is a method to generate these cache keys.