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

redirectify

v1.4.0

Published

A require() plugin for Browserify for overriding a required file based on directory.

Downloads

94

Readme

Redirectify

A transformer for Browserify to override the returned file from require().


Browserify bundles together files referenced using the require() function. When a call to require() is made Redirectify will step in and return a file matching a specified format where available. If the file to override with does not exist then the deault is used instead.

Redirectify can redirect the require call based on a file prefix, suffix or directory. For examples of these in action see the examples in this repo.

Installation

Using npm:

npm install redirectify browserify --save

Usage

browserify -t [ redirectify <options> ] source.js -o output.js

Options

  • prefix - text to prepend to the filename

    Example

    browserify -t [ redirectify --prefix="foo" ] source.js -o output.js

    require("./bar.js"); will be return content of "./foobar.js" where available

  • suffix - text to append to the filename

    Example

    browserify -t [ redirectify --suffix="foo" ] source.js -o output.js

    require("./bar.js"); will be return content of "./barfoo.js" where available

  • dir - relative path to the directory containing the overriding file

    Example

    browserify -t [ redirectify --dir="foo" ] source.js -o output.js

    require("./bar.js"); will be return content of "./foo/bar.js" where available

  • base - used for specifying the common root for overriding sub directories

    Example

    browserify -t [ redirectify --base="foo" --dir="../baz" ] source.js -o output.js

    require("./foo/bar.js"); will be return content of "./baz/other/bar.js" where available

Config

With Package.json

When executing browserify you can specify redirectify as a transformer in your package.json:

{
  "name": "foo",
  ...
  "browserify": {
    "transform: ["redirectify"]
  }
}

Also withing your package.json you should provide the overrideDir as config for redirectify:

{
  "browserify": ...
  "redirectify": {
    "dir": "dir/to/nest/to"
  }
}

For example for the above directory structure your package.json should look like this:

{
  "name": "foo",
  ...
  "browserify": {
    transform: ["redirectify"]
  },
  "redirectify": {
    "dir": "overridingDir"
  }
}

For this example Browserify will now include ./overridingDir/file instead of ./file.

If the specified directory or override file with the same name do not exist then the original will acts as a default.

On the command line

Alternatively you can specify the transform option on the command line:

browserify input.js -t [ redirectify --dir="overridingDir" ] -o output.js

You can also overwrite the config by using an environment variable:

This will be deprecated in a future release

REDIRECT_DIR=overridingDir browserify input.js -t redirectify  -o output.js

Overriding deeply nested files

For instance, with the following directory structure:

 .
 ├ project
 │ ├ src
 │ │ └ subDir
 │ │   ├ nestedSubDir
 │ │   │ └ otherFile.txt
 │ │   └ file.txt
 │ └ override
 │   └ subDir
 │     ├ nestedSubDir
 │     │ └ otherFile.txt
 │     └ file.txt
 

And the following config:

{
  "name": "foo",
  ...
  "browserify": {
    transform: ["redirectify"]
  },
  "redirectify": {
    "dir": "../override",
    "base": "/path/to/project"
  }
}

Requiring project/src/subDir/file.txt will load project/override/subDir/file.txt. This works in a nested fashion so requiring project/src/subDir/nestedSubDir/otherFile.txt will load project/override/subDir/nestedSubDir/otherFile.txt.

Tests

run the tests with

npm test

Change history

1.4

  • Adding support for prefix and suffix redirection.

1.3

  • Adds support for overriding contents of subdirectories.

1.2

  • Adds support for setting config on the command line with --dir.

1.1

  • Adds support for setting config using environment variables.