Skip to content
Browse files

RequireJS support in Rhino

  • Loading branch information...
1 parent 9d17474 commit 96cc08ac4a1066344f4d57726c17be117a7113ef @jrburke jrburke committed Feb 10, 2010
Showing with 255 additions and 177 deletions.
  1. +19 −0 README.md
  2. +2 −2 require.js
  3. +30 −0 require/rhino.js
  4. +28 −0 tests/all-rhino.js
  5. +25 −0 tests/circular-tests.js
  6. +1 −27 tests/circular.html
  7. +130 −127 tests/doh/runner.js
  8. +1 −1 tests/doh/runner.sh
  9. +18 −0 tests/simple-tests.js
  10. +1 −20 tests/simple.html
View
19 README.md
@@ -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
@@ -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.
View
4 require.js
@@ -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
@@ -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.");
}
View
30 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;
+};
View
28 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();
View
25 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();
+ }
+);
View
28 tests/circular.html
@@ -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>
View
257 tests/doh/runner.js
@@ -1367,130 +1367,133 @@ doh.average = function(/* Number [] */ a){
tests = doh;
-(function(){
- // scope protection
- var x;
- try{
- if(typeof dojo != "undefined"){
- dojo.platformRequire({
- browser: ["doh._browserRunner"],
- rhino: ["doh._rhinoRunner"],
- spidermonkey: ["doh._rhinoRunner"]
- });
- try{
- var _shouldRequire = dojo.isBrowser ? (dojo.global == dojo.global["parent"] || !Boolean(dojo.global.parent.doh) ) : true;
- }catch(e){
- //can't access dojo.global.parent.doh, then we need to do require
- _shouldRequire = true;
- }
- if(_shouldRequire){
- if(dojo.isBrowser){
- dojo.addOnLoad(function(){
- if (dojo.global.registerModulePath){
- dojo.forEach(dojo.global.registerModulePath, function(m){
- dojo.registerModulePath(m[0], m[1]);
- });
- }
- if(dojo.byId("testList")){
- var _tm = ( (dojo.global.testModule && dojo.global.testModule.length) ? dojo.global.testModule : "dojo.tests.module");
- dojo.forEach(_tm.split(","), dojo.require, dojo);
- setTimeout(function(){
- doh.run();
- }, 500);
- }
- });
- }else{
- // dojo.require("doh._base");
- }
- }
- }else{
- if(typeof load == "function" &&
- (typeof Packages == "function" || typeof Packages == "object")){
- throw new Error();
- }else if(typeof load == "function"){
- throw new Error();
- }
-
- if(this["document"]){
- /*
- // if we survived all of that, we're probably in a browser but
- // don't have Dojo handy. Load _browserRunner.js using a
- // document.write() call.
-
- // find runner.js, load _browserRunner relative to it
- var scripts = document.getElementsByTagName("script"), runnerFile;
- for(x=0; x<scripts.length; x++){
- var s = scripts[x].src;
- if(s){
- if(!runnerFile && s.substr(s.length - 9) == "runner.js"){
- runnerFile = s;
- }else if(s.substr(s.length - 17) == "_browserRunner.js"){
- runnerFile = null;
- break;
- }
- }
- }
- if(runnerFile){
- document.write("<scri"+"pt src='" + runnerFile.substr(0, runnerFile.length - 9)
- + "_browserRunner.js' type='text/javascript'></scr"+"ipt>");
- }
- */
- }
- }
- }catch(e){
- print("\n"+doh._line);
- print("The Dojo Unit Test Harness, $Rev: 20389 $");
- print("Copyright (c) 2009, The Dojo Foundation, All Rights Reserved");
- print(doh._line, "\n");
-
- try{
- var dojoUrl = "../../dojo/dojo.js";
- var testUrl = "";
- var testModule = "dojo.tests.module";
- var dohBase = "";
- for(x=0; x<arguments.length; x++){
- if(arguments[x].indexOf("=") > 0){
- var tp = arguments[x].split("=");
- if(tp[0] == "dohBase"){
- dohBase = tp[1];
- //Convert slashes to unix style and make sure properly
- //ended.
- dohBase = dohBase.replace(/\\/g, "/");
- if(dohBase.charAt(dohBase.length - 1) != "/"){
- dohBase += "/";
- }
- }
- if(tp[0] == "dojoUrl"){
- dojoUrl = tp[1];
- }
- if(tp[0] == "testUrl"){
- testUrl = tp[1];
- }
- if(tp[0] == "testModule"){
- testModule = tp[1];
- }
- }
- }
-
- load(dohBase + "_rhinoRunner.js");
-
- if(dojoUrl.length){
- if(!this["djConfig"]){
- djConfig = {};
- }
- djConfig.baseUrl = dojoUrl.split("dojo.js")[0];
- load(dojoUrl);
- }
- if(testUrl.length){
- load(testUrl);
- }
- if(testModule.length){
- dojo.forEach(testModule.split(","), dojo.require, dojo);
- }
- }catch(e){
- print("An exception occurred: " + e);
- }
-
- doh.run();
- }
-}).apply(this, typeof arguments != "undefined" ? arguments : [null]);
+if (typeof skipDohSetup === "undefined") {
+
+ (function(){
+ // scope protection
+ var x;
+ try{
+ if(typeof dojo != "undefined"){
+ dojo.platformRequire({
+ browser: ["doh._browserRunner"],
+ rhino: ["doh._rhinoRunner"],
+ spidermonkey: ["doh._rhinoRunner"]
+ });
+ try{
+ var _shouldRequire = dojo.isBrowser ? (dojo.global == dojo.global["parent"] || !Boolean(dojo.global.parent.doh) ) : true;
+ }catch(e){
+ //can't access dojo.global.parent.doh, then we need to do require
+ _shouldRequire = true;
+ }
+ if(_shouldRequire){
+ if(dojo.isBrowser){
+ dojo.addOnLoad(function(){
+ if (dojo.global.registerModulePath){
+ dojo.forEach(dojo.global.registerModulePath, function(m){
+ dojo.registerModulePath(m[0], m[1]);
+ });
+ }
+ if(dojo.byId("testList")){
+ var _tm = ( (dojo.global.testModule && dojo.global.testModule.length) ? dojo.global.testModule : "dojo.tests.module");
+ dojo.forEach(_tm.split(","), dojo.require, dojo);
+ setTimeout(function(){
+ doh.run();
+ }, 500);
+ }
+ });
+ }else{
+ // dojo.require("doh._base");
+ }
+ }
+ }else{
+ if(typeof load == "function" &&
+ (typeof Packages == "function" || typeof Packages == "object")){
+ throw new Error();
+ }else if(typeof load == "function"){
+ throw new Error();
+ }
+
+ if(this["document"]){
+ /*
+ // if we survived all of that, we're probably in a browser but
+ // don't have Dojo handy. Load _browserRunner.js using a
+ // document.write() call.
+
+ // find runner.js, load _browserRunner relative to it
+ var scripts = document.getElementsByTagName("script"), runnerFile;
+ for(x=0; x<scripts.length; x++){
+ var s = scripts[x].src;
+ if(s){
+ if(!runnerFile && s.substr(s.length - 9) == "runner.js"){
+ runnerFile = s;
+ }else if(s.substr(s.length - 17) == "_browserRunner.js"){
+ runnerFile = null;
+ break;
+ }
+ }
+ }
+ if(runnerFile){
+ document.write("<scri"+"pt src='" + runnerFile.substr(0, runnerFile.length - 9)
+ + "_browserRunner.js' type='text/javascript'></scr"+"ipt>");
+ }
+ */
+ }
+ }
+ }catch(e){
+ print("\n"+doh._line);
+ print("The Dojo Unit Test Harness, $Rev: 20389 $");
+ print("Copyright (c) 2009, The Dojo Foundation, All Rights Reserved");
+ print(doh._line, "\n");
+
+ try{
+ var dojoUrl = "../../dojo/dojo.js";
+ var testUrl = "";
+ var testModule = "dojo.tests.module";
+ var dohBase = "";
+ for(x=0; x<arguments.length; x++){
+ if(arguments[x].indexOf("=") > 0){
+ var tp = arguments[x].split("=");
+ if(tp[0] == "dohBase"){
+ dohBase = tp[1];
+ //Convert slashes to unix style and make sure properly
+ //ended.
+ dohBase = dohBase.replace(/\\/g, "/");
+ if(dohBase.charAt(dohBase.length - 1) != "/"){
+ dohBase += "/";
+ }
+ }
+ if(tp[0] == "dojoUrl"){
+ dojoUrl = tp[1];
+ }
+ if(tp[0] == "testUrl"){
+ testUrl = tp[1];
+ }
+ if(tp[0] == "testModule"){
+ testModule = tp[1];
+ }
+ }
+ }
+
+ load(dohBase + "_rhinoRunner.js");
+
+ if(dojoUrl.length){
+ if(!this["djConfig"]){
+ djConfig = {};
+ }
+ djConfig.baseUrl = dojoUrl.split("dojo.js")[0];
+ load(dojoUrl);
+ }
+ if(testUrl.length){
+ load(testUrl);
+ }
+ if(testModule.length){
+ dojo.forEach(testModule.split(","), dojo.require, dojo);
+ }
+ }catch(e){
+ print("An exception occurred: " + e);
+ }
+
+ doh.run();
+ }
+ }).apply(this, typeof arguments != "undefined" ? arguments : [null]);
+}
View
2 tests/doh/runner.sh
@@ -1,3 +1,3 @@
#!/bin/sh
-java -jar ../shrinksafe/js.jar runner.js "$@"
+java -jar ../../build/lib/rhino/js.jar runner.js "$@"
View
18 tests/simple-tests.js
@@ -0,0 +1,18 @@
+require({
+ baseUrl: "./"
+ },
+ ["require", "simple", "dimple", "func"],
+ function(require, simple, dimple, func) {
+ doh.register(
+ "simple",
+ [
+ function colors(t){
+ t.is("blue", simple.color);
+ t.is("dimple-blue", dimple.color);
+ t.is("You called a function", func());
+ }
+ ]
+ );
+ doh.run();
+ }
+);
View
21 tests/simple.html
@@ -5,26 +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", "simple", "dimple", "func"],
- function(require, simple, dimple, func) {
- doh.register(
- "simple",
- [
- function colors(t){
- t.is("blue", simple.color);
- t.is("dimple-blue", dimple.color);
- t.is("You called a function", func());
- }
- ]
- );
- doh.run();
- }
- );
- </script>
+ <script type="text/javascript" src="simple-tests.js"></script>
</head>
<body>
<h1>require.js: Simple Test</h1>

0 comments on commit 96cc08a

Please sign in to comment.
Something went wrong with that request. Please try again.