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

pre-require

v1.2.0

Published

Pre-require is a small global script, helps you create a module of array object with required assets from the folder that you point out.

Readme

Pre-require

Pre-require is a small global script, which helps you create a module of array objects with required assets from the folder that you direct it to.

This small trick helps you in situations that you might need to use variables while fetching assets.

For Example:

Lets say I have 10 images named as:

  • image1.png
  • image2.png
  • ...
  • image10.png
let i = 1;
let imageArray = [];
while(i <= 10){
  imageArray.push(require("image"+ i +".png"));
  // Won't work.
  i++;
}

Require works before the logic of your script starts working, so using variables in require() is not the optimal direction. But, as you might know, using require() in React works really well and you will inevitably have situations where if only require would support variables, it would be perfect for your needs.

Pre-require, requires all the files and creates an array from the folder you direct it to, so you can import this array and use is the way you would use require, but with variables. Additionally, you can do array searches in your assets.

How to use?

First install the pre-require globally:

npm install pre-require -g

Pre-require takes 2 parameters: First parameter is the path to the folder that your assets exist in. The second parameter is the output Javascript file that you will be importing to your script which is returning a required asset map.

pre-require images/ assets.js

You might use this manually, when you add new assets to the destination file, or bind this command to your file watcher script, or webpack config or just add it to your npm run build command from your package.json

Once more, an example of rewriting image1 to image10 with pre-require:

import Assets from './assets.js'
// assets.js would be the path of your output file, that pre-require command created.

let i = 1;
let imageArray = [];
while(i <= 10){
  imageArray.push(Assets["image"+ i +"_png"]);
  i++;
}

Methods

The asset.js file exposes a range of methods for interacting with the data structure

Assets.search

This will return an array of assets matching the given pattern, currently this is just the matched asset name. In the future this will accept a regexp or partial match.

import Assets from './assets.js'

let thirdImage = Assets.search("image3");

Assets.format

Similar to Assets.search this will return all assets matching a given filetype

import Assets from './assets.js'

let pngs = Assets.format("png");

For a full list of methods and usage see the API reference

pre-require -h

NOTE: Be careful that file extension dot (.) is changed into underscore ( _ ). Also if your asset file has hyphen (-), it will automatically be transformed into an underscore.

You can use this module with any file that require() supports; image files (png, jpg, svg, etc), json files or, even in some extreme cases, Javascript files.

NOTE: There is a lot to do, for example;

  • Built-in asset search
  • Choosing the type of the asset that (eg: regex folder parameter)
  • Adding -h info. (done)

Help me make it better with your pull requests, they are welcome.

### Development & Contribution

// Todo: Add information about how to contribute.

Tests

We use Jest for tests, to start tests locally;

npm run test

To watch the changes made in the test scripts, use;

npm run test:watch

Licence

MIT