js-prefixer

1.1.0 • Public • Published

js-prefixer NPM Version Build Status

Prefix relative URLs in JavaScript & JSON code with a cdn URL.

Turns var a = "/fooga.js"; into var a = "http://woogabooga.com/fooga.js";

Usage

var prefixer = require("js-prefixer"),
    code     = "var fooga = \"/googa/nooga.txt\";";
 
prefixer(
    code,
    { prefix : "//abcdefg123.cloudfront.net" },
    function(err, src) {
        if(err) {
            throw new Error(err);
        }
        
        console.log(src); // writes out: var fooga = "//abcdefg123.cloudfront.net/googa/nooga.txt";
    }
);

API

prefixer(code, [options], cb)

  • code {String} JS code string
  • options {Object}
  • cb {Function}
    • err {Error | null}
    • src {String} Code with cdn-prefixed URLs

Options

  • prefix {String} URL used to prefix elements.
  • list {Array} Array of strings to be replaced (will be used instead of scanning for valid-looking URIs)

Caveats

Support for JSON is enabled by prepending it with var ___jsprefixer___ = so it parses as valid JavaScript. This may have unintended side-effects but hasn't been a problem for us in the last year+ of daily usage.

Package Sidebar

Install

npm i js-prefixer

Weekly Downloads

0

Version

1.1.0

License

MIT

Last publish

Collaborators

  • tivac