Skip to content

Commit

Permalink
RequireJS support in Rhino
Browse files Browse the repository at this point in the history
  • Loading branch information
jrburke committed Feb 11, 2010
1 parent 9d17474 commit 96cc08a
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 177 deletions.
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -341,6 +341,7 @@ Some advanced features:
* Loading code after page load
* require.pause()/require.resume() for build layers/bundles
* Module Modifiers
* Rhino support

## Multiversion Support

Expand Down Expand Up @@ -475,6 +476,24 @@ For the example given above in Modifier Registration, where "my/target1" is the
}
);

## Rhino Support

RequireJS can be used in Rhino, just be sure to load require.js and require/rhino.js before doing any require calls:

load("requirejs/require.js");
load("requirejs/require/rhino.js");

//Set up any config values, baseUrl is required so module names
//will map correctly to paths.
require({
baseUrl: 'path/to/scripts'
});

//Now load the top level script.
require(['startingscript']);

You can see an example of RequireJS working in Rhino by looking at [tests/all-rhino.js](tests/all-rhino.js). The test file is a bit different from above since each test sets its own baseUrl.

# History and Influences

I work a lot on the Dojo Loader. The normal dojo loader uses synchronous XMLHttpRequest (XHR) calls. However, the XHR loader cannot load Dojo modules from other domains because of the same-origin restrictions. So I created the xdomain loader that required a build step to inject function wrappers similar to what RequireJS uses, but more complex, due to i18n bundle loading and dojo.requireIf behavior. Because of the more complex i18n and requireIf requirements and the existence of many dojo modules already out in the world, I did not feel like the Dojo community would consider writing modules with function wrappers manually.
Expand Down
4 changes: 2 additions & 2 deletions require.js
Expand Up @@ -94,7 +94,7 @@ var require;

//Do more work, either
return require.def.apply(require, arguments);
}
};

/**
* The function that handles definitions of modules. Differs from
Expand Down Expand Up @@ -614,7 +614,7 @@ var require;
*
* @returns {Object} the exported module value.
*/
require.get = function(moduleName, contextName) {
require.get = function (moduleName, contextName) {
if (moduleName === "exports" || moduleName === "module") {
throw new Error("require of " + moduleName + " is not allowed.");
}
Expand Down
30 changes: 30 additions & 0 deletions require/rhino.js
@@ -0,0 +1,30 @@
/**
* @license RequireJS rhino Copyright (c) 2010, The Dojo Foundation All Rights Reserved.
* Available via the MIT, GPL or new BSD license.
* see: http://github.com/jrburke/requirejs for details
*/
/*global require: false, readFile: false */

/*
TODO: Work out relative paths, that use ./ and such, and allow loading normal
CommonJS modules, by overriding require.get().
*/

/*globals load: false */
"use strict";

require.load = function (moduleName, contextName) {
var url = require.nameToUrl(moduleName, null, contextName),
context = require.s.contexts[contextName];

//isDone is used by require.ready()
require.s.isDone = false;

//Indicate a the module is in process of loading.
context.loaded[moduleName] = false;

load(url);

//Mark the module loaded.
context.loaded[moduleName] = true;
};
28 changes: 28 additions & 0 deletions tests/all-rhino.js
@@ -0,0 +1,28 @@
/**
* Run the tests in Rhino via this command:
* java -jar ../build/lib/rhino/js.jar all-rhino.js
*
* To run with debugger:
* java -classpath ../build/lib/rhino/js.jar org.mozilla.javascript.tools.debugger.Main all-rhino.js
*/

//A hack to doh to avoid dojo setup stuff in doh/runner.js
skipDohSetup = true;

//Load test framework
load("doh/runner.js");
load("doh/_rhinoRunner.js");

//Load require with rhino extension
load("../require.js");
load("../require/rhino.js");

//Load the tests.
load("simple-tests.js");
load("circular-tests.js");
load("depoverlap-tests.js");

//Hmm, this is an odd requirement, call doh.run() for each test listed above?
//May be because the tests above call doh.run() in a callback sometimes?
doh.run();
doh.run();
25 changes: 25 additions & 0 deletions tests/circular-tests.js
@@ -0,0 +1,25 @@
require({
baseUrl: "./"
},
["require", "two", "funcTwo", "funcThree"],
function(require, two, funcTwo, funcThree) {
var args = two.doSomething();
var twoInst = new funcTwo("TWO");
doh.register(
"circular",
[
function circular(t) {
t.is("small", args.size);
t.is("redtwo", args.color);
},

function circularFunc(t) {
t.is("TWO", twoInst.name);
t.is("ONE-NESTED", twoInst.oneName());
t.is("THREE-THREE_SUFFIX", funcThree("THREE"));
}
]
);
doh.run();
}
);
28 changes: 1 addition & 27 deletions tests/circular.html
Expand Up @@ -5,33 +5,7 @@
<script type="text/javascript" src="../require.js"></script>
<script type="text/javascript" src="doh/runner.js"></script>
<script type="text/javascript" src="doh/_browserRunner.js"></script>
<script type="text/javascript">
require({
baseUrl: "./"
},
["require", "two", "funcTwo", "funcThree"],
function(require, two, funcTwo, funcThree) {
var args = two.doSomething();
var twoInst = new funcTwo("TWO");
doh.register(
"circular",
[
function circular(t) {
t.is("small", args.size);
t.is("redtwo", args.color);
},

function circularFunc(t) {
t.is("TWO", twoInst.name);
t.is("ONE-NESTED", twoInst.oneName());
t.is("THREE-THREE_SUFFIX", funcThree("THREE"));
}
]
);
doh.run();
}
);
</script>
<script type="text/javascript" src="circular-tests.js"></script>
</head>
<body>
<h1>require.js: Circular Test</h1>
Expand Down

0 comments on commit 96cc08a

Please sign in to comment.