Prefix urls in javascript code with a cdn URL


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

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

var prefixer = require("js-prefixer"),
    code     = "var fooga = \"/googa/nooga.txt\";";
prefixer(code, { prefix : "//abcdefg123.cloudfront.net" }, function(errsrc) {
    if(err) {
        throw new Error(err);
    console.log(src); // writes out: var fooga = "//abcdefg123.cloudfront.net/googa/nooga.txt"; 
  • code {String} JS code string
  • options {Object}
  • cb {Function}
    • err {Error | null}
    • src {String} Code with cdn-prefixed URLs
  • prefix {String} URL used to prefix elements.
  • codegen {Object} escodegen options (see ./codegen.json for defaults & escodegen docs for descriptions)
  • list {Array} Array of strings to be replaced (will be used instead of scanning for valid-looking URIs)

Support for JSON is enabled within esprima by attempting to JSON.parse incoming code, then modifying it by prepending var ___jsprefixer___ = to it so it parses as valid JavaScript. This may have unintended side-effects and hasn't been thoroughly tested against real-world JSON.