Rewriting require to allow for relative paths within a module Reviewed by Bryan Higgins <bhigg...@blackberry.com>
Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/8b06f297 Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/8b06f297 Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/8b06f297 Branch: refs/heads/master Commit: 8b06f297fd415835dc0dfa729e96e7ce4fc074ad Parents: b205fd1 Author: Jeffrey Heifetz <jheif...@blackberry.com> Authored: Mon Apr 8 15:58:13 2013 -0400 Committer: Bryan Higgins <bhigg...@blackberry.com> Committed: Fri May 3 09:50:04 2013 -0400 ---------------------------------------------------------------------- lib/scripts/require.js | 21 ++++++++++++++++----- test/test.require.js | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-js/blob/8b06f297/lib/scripts/require.js ---------------------------------------------------------------------- diff --git a/lib/scripts/require.js b/lib/scripts/require.js index 5dbc905..93f8d16 100644 --- a/lib/scripts/require.js +++ b/lib/scripts/require.js @@ -23,17 +23,28 @@ var require, define; (function () { - var modules = {}; + var modules = {}, // Stack of moduleIds currently being built. - var requireStack = []; + requireStack = [], // Map of module ID -> index into requireStack of modules currently being built. - var inProgressModules = {}; + inProgressModules = {}, + SEPERATOR = "."; + + function build(module) { - var factory = module.factory; + var factory = module.factory, + localRequire = function (id) { + var resultantId = id; + //Its a relative path, so lop off the last portion and add the id (minus "./") + if (id.charAt(0) === ".") { + resultantId = module.id.slice(0, module.id.lastIndexOf(SEPERATOR)) + SEPERATOR + id.slice(2); + } + return require(resultantId); + }; module.exports = {}; delete module.factory; - factory(require, module.exports, module); + factory(localRequire, module.exports, module); return module.exports; } http://git-wip-us.apache.org/repos/asf/cordova-js/blob/8b06f297/test/test.require.js ---------------------------------------------------------------------- diff --git a/test/test.require.js b/test/test.require.js index 7734e41..19d179d 100644 --- a/test/test.require.js +++ b/test/test.require.js @@ -93,7 +93,7 @@ describe("require + define", function () { define("dino", factory); require("dino"); - expect(factory).toHaveBeenCalledWith(require, + expect(factory).toHaveBeenCalledWith(jasmine.any(Function), {}, { id: "dino", exports: {}