• denodify.js

  • ¶
    (function(global) {
        
        if (global.denodify) return;
        
        var m = {};
        var f = {};
        
        var process = {
            platform: 'browser'
        };
  • ¶

    API

        function require(parent, moduleid) {
            if (moduleid.indexOf('/') === -1) parent = '';
            else {
                if (Path.isAbsolute(moduleid)) parent = '/';
                else moduleid = Path.join(parent, moduleid);
                if (!Path.extname(moduleid)) moduleid += '.js';
            }
            var module = m[moduleid];
            if (module) return module.exports;
            var func = f[moduleid];
            if (!func)
                throw new Error('Couldn\'t resolve module with resolved path of ' +
                                Path.join(Path.dirname(parent), moduleid));
            func(function(__filename) { return require(Path.dirname(moduleid), __filename); },
                 module = m[moduleid] = {},
                 module.exports = {},
                 moduleid,
                 Path.dirname(moduleid),
                 process);
            return module.exports;
        }
  • ¶

    denodify

    Use this to denodify javascript. Wrap your source code with

  • ¶
    denodify('moduleid',function(require, module, exports, __filename, __dirname, process) {
      [your source code]
    });
    
        global.denodify = function (__filename, func) { 
            f[__filename] = func;
        };
  • ¶

    require

    Use this function to pull in or kickstart any defined nodejs modules from outside nodejs modules.

        global.denodify.require = function(__filename) {
            require('', __filename);
        };
  • ¶

    debug

        global.denodify.debug = function(__filename) {
            console.log('modules\n:', m);
            console.log('funcs\n:', f);
        };
  • ¶

    End of API ———————————————————————-

  • ¶

    The rest is path, copied from path-browserify

        var path = (function() {
            var exports  = {};
            function normalizeArray(parts, allowAboveRoot) {
  • ¶

    if the path tries to go above the root, up ends up > 0

                var up = 0;
                for (var i = parts.length - 1; i >= 0; i--) {
                    var last = parts[i];
                    if (last === '.') {
                        parts.splice(i, 1);
                    } else if (last === '..') {
                        parts.splice(i, 1);
                        up++;
                    } else if (up) {
                        parts.splice(i, 1);
                        up--;
                    }
                }
  • ¶

    if the path is allowed to go above the root, restore leading ..s

                if (allowAboveRoot) {
                    for (; up--; up) {
                        parts.unshift('..');
                    }
                }
    
                return parts;
            }
  • ¶

    Split a filename into [root, dir, basename, ext], unix version ‘root’ is just a slash, or nothing.

            var splitPathRe =
                /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
            var splitPath = function(filename) {
                return splitPathRe.exec(filename).slice(1);
            };
  • ¶

    path.resolve([from …], to) posix version

            exports.resolve = function() {
                var resolvedPath = '',
                resolvedAbsolute = false;
    
                for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
                    var path = (i >= 0) ? arguments[i] : process.cwd();
  • ¶

    Skip empty and invalid entries

                    if (typeof path !== 'string') {
                        throw new TypeError('Arguments to path.resolve must be strings');
                    } else if (!path) {
                        continue;
                    }
    
                    resolvedPath = path + '/' + resolvedPath;
                    resolvedAbsolute = path.charAt(0) === '/';
                }
  • ¶

    At this point the path should be resolved to a full absolute path, but handle relative paths to be safe (might happen when process.cwd() fails)

  • ¶

    Normalize the path

                resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) {
                    return !!p;
                }), !resolvedAbsolute).join('/');
    
                return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
            };
  • ¶

    path.normalize(path) posix version

            exports.normalize = function(path) {
                var isAbsolute = exports.isAbsolute(path),
                trailingSlash = substr(path, -1) === '/';
  • ¶

    Normalize the path

                path = normalizeArray(filter(path.split('/'), function(p) {
                    return !!p;
                    }), !isAbsolute).join('/');
    
                if (!path && !isAbsolute) {
                    path = '.';
                }
                if (path && trailingSlash) {
                    path += '/';
                }
    
                return (isAbsolute ? '/' : '') + path;
            };
  • ¶

    posix version

            exports.isAbsolute = function(path) {
                return path.charAt(0) === '/';
            };
  • ¶

    posix version

            exports.join = function() {
                var paths = Array.prototype.slice.call(arguments, 0);
                return exports.normalize(filter(paths, function(p, index) {
                    if (typeof p !== 'string') {
                        throw new TypeError('Arguments to path.join must be strings');
                    }
                    return p;
                }).join('/'));
            };
  • ¶

    path.relative(from, to) posix version

            exports.relative = function(from, to) {
                from = exports.resolve(from).substr(1);
                to = exports.resolve(to).substr(1);
    
                function trim(arr) {
                    var start = 0;
                    for (; start < arr.length; start++) {
                        if (arr[start] !== '') break;
                    }
    
                    var end = arr.length - 1;
                    for (; end >= 0; end--) {
                        if (arr[end] !== '') break;
                    }
    
                    if (start > end) return [];
                    return arr.slice(start, end - start + 1);
                }
    
                var fromParts = trim(from.split('/'));
                var toParts = trim(to.split('/'));
    
                var length = Math.min(fromParts.length, toParts.length);
                var samePartsLength = length;
                for (var i = 0; i < length; i++) {
                    if (fromParts[i] !== toParts[i]) {
                        samePartsLength = i;
                        break;
                    }
                }
    
                var outputParts = [];
                for (var i = samePartsLength; i < fromParts.length; i++) {
                    outputParts.push('..');
                }
    
                outputParts = outputParts.concat(toParts.slice(samePartsLength));
    
                return outputParts.join('/');
            };
    
            exports.sep = '/';
            exports.delimiter = ':';
    
            exports.dirname = function(path) {
                var result = splitPath(path),
                root = result[0],
                dir = result[1];
    
                    if (!root && !dir) {
  • ¶

    No dirname whatsoever

                        return '.';
                    }
    
                if (dir) {
  • ¶

    It has a dirname, strip trailing slash

                    dir = dir.substr(0, dir.length - 1);
                }
    
                return root + dir;
            };
    
    
            exports.basename = function(path, ext) {
                var f = splitPath(path)[2];
  • ¶

    TODO: make this comparison case-insensitive on windows?

                if (ext && f.substr(-1 * ext.length) === ext) {
                    f = f.substr(0, f.length - ext.length);
                }
                return f;
            };
    
    
            exports.extname = function(path) {
                return splitPath(path)[3];
            };
    
            function filter (xs, f) {
                if (xs.filter) return xs.filter(f);
                var res = [];
                for (var i = 0; i < xs.length; i++) {
                    if (f(xs[i], i, xs)) res.push(xs[i]);
                }
                return res;
            }
  • ¶

    String.prototype.substr - negative index don’t work in IE8

            var substr = 'ab'.substr(-1) === 'b'
                ? function (str, start, len) { return str.substr(start, len) }
            : function (str, start, len) {
                if (start < 0) start = str.length + start;
                return str.substr(start, len);
            };
            return { exports: exports };
        })();
        
        
        m['path'] = path;
        var Path = path.exports;
        
    })(this);