Giter Club home page Giter Club logo

aliasify's Introduction

Aliasify is a transform for browserify which lets you rewrite calls to require.

Installation

Install with npm install --save-dev aliasify.

Usage

To use, add a section to your package.json:

{
    "aliasify": {
        "aliases": {
            "d3": "./shims/d3.js",
            "underscore": "lodash"
        }
    }
}

Now if you have a file in src/browserify/index.js which looks like:

d3 = require('d3')
_ = require('underscore')
...

This will automatically be transformed to:

d3 = require('../../shims/d3.js')
_ = require('lodash')
...

Any replacement that starts with a "." will be resolved as a relative path (as "d3" above.) Replacements that start with any other character will be replaced verbatim (as with "underscore" above.)

Configuration

Configuration can be loaded in multiple ways; You can put your configuration directly in package.json, as in the example above, or you can use an external json or js file. In your package.json:

{
    "aliasify": "./aliasifyConfig.js"
}

Then in aliasifyConfig.js:

module.exports = {
    aliases: {
        "d3": "./shims/d3.js"
    },
    verbose: false
};

Note that using a js file means you can change your configuration based on environment variables.

Alternatively, if you're using the Browserify API, you can configure your aliasify programatically:

aliasifyConfig = {
    aliases: {
        "d3": "./shims/d3.js"
    },
    verbose: false
}

var b = browserify();
b.transform(aliasify, aliasifyConfig);

note that using the browserify API, './shims/d3.js' will be resolved against the current working directory.

Configuration options:

  • aliases - An object mapping aliases to their replacements.
  • verbose - If true, then aliasify will print modificiations it is making to stdout.
  • configDir - An absolute path to resolve relative paths against. If you're using package.json, this will automatically be filled in for you with the directory containing package.json. If you're using a .js file for configuration, set this to __dirname.
  • appliesTo - Controls which files will be transformed. By default, only JS type files will be transformed ('.js', '.coffee', etc...). See browserify-trasnform-tools documentation for details.

Relative Requires

When you specify:

aliases: {
    "d3": "./shims/d3.js"
}

The "./" means this will be resolved relative to the current working directory (or relative to the configuration file which contains the line, in the case where configuration is loaded from package.json.) Sometimes it is desirable to literally replace an alias; to resolve the alias relative to the file which is doing the require call. In this case you can do:

aliases: {
    "d3": {"relative": "./shims/d3.js"}
}

This will cause all occurences of require("d3") to be replaced with require("./shims/d3.js"), regardless of where those files are in the directory tree.

Alternatives

aliasify is essentially a fancy version of the browser field from package.json, which is interpreted by browserify.

Using the browser field is probably going to be faster, as it doesn't involve running a transform on each of your files. On the other hand, aliasify gives you a finer degree of control and can be run before other transforms (for example, you can run aliasify before debowerify, which will let you replace certain components that debowerify would otherwise replace.)

aliasify's People

Contributors

jeanlauliac avatar jmm avatar justinhoward avatar jwalton avatar linuxbasic avatar martinheidegger avatar mkuklis avatar roncli avatar zaim avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.