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 🙏

© 2025 – Pkg Stats / Ryan Hefner

lazy-builder

v0.1.1

Published

Repeatedly and lazily rebuild an immutable map of files

Readme

lazy-builder

Utility for lazily rebuilding an immutable map of files.

This is a micro-framework for use inside a trip plugin. It helps reduce repeat work on partial rebuilds.

NPM version Linux Build Status Windows Build Status Dependency Status devDependency Status peerDependency Status

Usage

const builder = new LazyBuilder(fn);

builder.build(inputFiles).then(outputFiles => {
  // do something with result...
});

The fn is your own 'granular build function'.

The first time .build() is called, it simply calls fn(key, value) for every file in the map, and returns a promise of the aggregated results.

But on subsequent calls to .build() (with a slightly modified input map each time), the builder will call your fn only for the files that actually need rebuilding – those that have changed, plus those that are known to import any of those that have changed.

The .build() method

  • Takes an immutable map of files, processes them according to your granular callback, and returns the results an immutable map of files. For example, if you're using LazyBuilder as a Sass build step, your input files might include .html, .scss and .js files, and the output files would include the same files except for all the entry .scss files are now .css files, and all the Sass partials (_*.scss) are gone.

Your granular build function

The instance is constructed with a single function responsible for handling a single input file at a time, and returning/promising whatever build artifacts you want to output in respect of that input file. Whenever you call .build(), the instance calls your callback multiple times in parallel (once for each file that actually needs rebuilding), and collects all the output files (along with output from previous builds where appropriate) into a new immutable map, and resolves the .build() promise with this map.

new LazyBuilder(function (file, contents) {
  // return or promise something
})

Arguments

Your callback should return (or promise) whatever file(s) you want to output in respect of the given input file.

Return value

You may return any of the following (or a promise that will eventually be fulfilled with one of the following):

  • To output no files, return null. (You cannot return undefined; you must be explicit.)
  • If you want to output one or more files in respect of the given input file, you should return an object with file paths as keys and buffers/strings as values, like in the above CoffeeScript example.
  • For the common case that you're returning some new contents to be output to the exact same path that was passed in as the first argument, you may return a buffer/string on its own.

Importing other files

this.importFile(file)

If your build step needs to satisfy statements like @import 'foo' or require('./foo') (in whatever language you're building), you should use this.importFile(file) rather than just reading it directly from the input files map. This gets you the contents (like inputFiles.get(file)) but it also registers the import, i.e. it tells the builder that file A imports file B. This allows the builder to maintain a list of importer–importee relationships between all the input files, so it can correctly determine the minimal set of files that need to be rebuilt when given input files have changed. It also

Example

A builder that compiles CoffeeScript files:

import {compile} from 'coffee-script';

const builder = new LazyBuilder(function (file, contents) {
  // pass on non-coffee files
  if (!/\.coffee$/.test(file)) return contents;
  
  // compile the coffeescript to get some JS and a sourcemap
  const {js, v3SourceMap} = compile(contents.toString(), {
    bare: false,
    header: false,
    sourceMap: true,
    sourceRoot: false,
    filename: file,
  });
  
  // output 2 files
  const jsFile = file.replace(/\.coffee$/, '.js');
  return {
    [jsFile]: js,
    [jsFile + '.map']: JSON.stringify(v3SourceMap),
  };
});

License

MIT © Callum Locke