Skip to content

Commit

Permalink
Adds circular transpiler test from requirejs and fix for it to work
Browse files Browse the repository at this point in the history
  • Loading branch information
jrburke committed Oct 3, 2012
1 parent f57804e commit e0efab4
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 3 deletions.
8 changes: 5 additions & 3 deletions almond.js
Expand Up @@ -157,7 +157,7 @@ var requirejs, require, define;
main.apply(undef, args);
}

if (!defined.hasOwnProperty(name)) {
if (!defined.hasOwnProperty(name) && !defining.hasOwnProperty(name)) {
throw new Error('No ' + name);
}
return defined[name];
Expand Down Expand Up @@ -276,12 +276,14 @@ var requirejs, require, define;
} else if (depName === "module") {
//CommonJS module spec 1.1
cjsModule = args[i] = handlers.module(name);
} else if (defined.hasOwnProperty(depName) || waiting.hasOwnProperty(depName)) {
} else if (defined.hasOwnProperty(depName) ||
waiting.hasOwnProperty(depName) ||
defining.hasOwnProperty(depName)) {
args[i] = callDep(depName);
} else if (map.p) {
map.p.load(map.n, makeRequire(relName, true), makeLoad(depName), {});
args[i] = defined[depName];
} else if (!defining[depName]) {
} else {
throw new Error(name + ' missing ' + depName);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/all.js
Expand Up @@ -18,6 +18,7 @@ doh.registerUrl("missing", "../missing/missing.html");
doh.registerUrl("insertRequire", "../insertRequire/insertRequire.html", 4000);
doh.registerUrl("circular", "../circular/circular.html");
doh.registerUrl("circular414", "../circular/414/414.html");
doh.registerUrl("circularTranspiler", "../circular/transpiler/transpiler.html");
doh.registerUrl("relativePaths", "../relativePaths/relativePaths.html");
doh.registerUrl("errback", "../errback/errback.html");
doh.registerUrl("specialDeps", "../specialDeps/specialDeps.html");
18 changes: 18 additions & 0 deletions tests/circular/transpiler/transpiler.html
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>almond: Circular Transpiler Plugin Test</title>
<script type="text/javascript" src="../../doh/runner.js"></script>
<script type="text/javascript" src="../../doh/_browserRunner.js"></script>
<script src="../../../almond.js"></script>
<script type="text/javascript" src="transpiler.js"></script>
</head>
<body>
<h1>almond: Circular Transpiler Plugin Test</h1>

<p>Test support for transpiled modules with cycles in them More info:
<a href="https://github.com/jrburke/requirejs/issues/356">356</a>.</p>

<p>Check console for messages</p>
</body>
</html>
192 changes: 192 additions & 0 deletions tests/circular/transpiler/transpiler.js
@@ -0,0 +1,192 @@


/*jslint strict: false, plusplus: false */
/*global define: false, require: false, XMLHttpRequest: false, ActiveXObject: false,
window: false, Packages: false, java: false, process: false */

(function () {
//Load the text plugin, so that the XHR calls can be made.
var buildMap = {}, fetchText, fs,
progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];

function createXhr() {
//Would love to dump the ActiveX crap in here. Need IE 6 to die first.
var xhr, i, progId;
if (typeof XMLHttpRequest !== "undefined") {
return new XMLHttpRequest();
} else {
for (i = 0; i < 3; i++) {
progId = progIds[i];
try {
xhr = new ActiveXObject(progId);
} catch (e) {}

if (xhr) {
progIds = [progId]; // so faster next time
break;
}
}
}

if (!xhr) {
throw new Error("require.getXhr(): XMLHttpRequest not available");
}

return xhr;
}

if (typeof window !== "undefined" && window.navigator && window.document) {
fetchText = function (url, callback) {
var xhr = createXhr();
xhr.open('GET', url, true);
xhr.onreadystatechange = function (evt) {
//Do not explicitly handle errors, those should be
//visible via console output in the browser.
if (xhr.readyState === 4) {
callback(xhr.responseText);
}
};
xhr.send(null);
};
} else if (typeof process !== "undefined" &&
process.versions &&
!!process.versions.node) {
//Using special require.nodeRequire, something added by r.js.
fs = require.nodeRequire('fs');

fetchText = function (url, callback) {
callback(fs.readFileSync(url, 'utf8'));
};
} else if (typeof Packages !== 'undefined') {
//Why Java, why is this so awkward?
fetchText = function (url, callback) {
var encoding = "utf-8",
file = new java.io.File(url),
lineSeparator = java.lang.System.getProperty("line.separator"),
input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)),
stringBuffer, line,
content = '';
try {
stringBuffer = new java.lang.StringBuffer();
line = input.readLine();

// Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
// http://www.unicode.org/faq/utf_bom.html

// Note that when we use utf-8, the BOM should appear as "EF BB BF", but it doesn't due to this bug in the JDK:
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
if (line && line.length() && line.charAt(0) === 0xfeff) {
// Eat the BOM, since we've already found the encoding on this file,
// and we plan to concatenating this buffer with others; the BOM should
// only appear at the top of a file.
line = line.substring(1);
}

stringBuffer.append(line);

while ((line = input.readLine()) !== null) {
stringBuffer.append(lineSeparator);
stringBuffer.append(line);
}
//Make sure we return a JavaScript string and not a Java string.
content = String(stringBuffer.toString()); //String
} finally {
input.close();
}
callback(content);
};
}

define('refine',[],function () {
return {
load: function (name, parentRequire, load, config) {
var url = parentRequire.toUrl(name + '.refine');
fetchText(url, function (text) {
text = text.replace(/refine\s*\(/g, 'define(');

if (config.isBuild) {
buildMap[name] = text;
}

//Add in helpful debug line
text += "\r\n//@ sourceURL=" + url;

load.fromText(text);

parentRequire([name], function (value) {
load(value);
});
});
},

write: function (pluginName, name, write) {
if (name in buildMap) {
var text = buildMap[name];
write.asModule(pluginName + "!" + name, text);
}
}
};
});

}());

define('refine!c',['refine!a', 'exports'], function (a, exports) {
exports.name = 'c';
exports.a = a;
});

define('refine!b',['refine!c', 'exports'], function (c, exports) {
exports.name = 'b';
exports.c = c;
});

define('refine!a',['refine!b', 'exports'], function (b, exports) {
exports.name = 'a';
exports.b = b;
});

define('refine!e',['refine!d'], function(d) {
function e() {
return e.name + require('refine!d').name;
}

e.name = 'e';

return e;
});
define('refine!d',['refine!e'], function(e) {
function d() {
return require('refine!e')();
}

d.name = 'd';

return d;
});
require({
baseUrl: requirejs.isBrowser ? './' : './circular/transpiler',
paths: {
'text': '../../../../text/text',
'refine': '../../plugins/fromText/refine'
}
},
["require", "refine!a", "refine!b", "refine!d"],
function(require, a, b, d) {
doh.register(
"circularTranspiler",
[
function circularTranspiler(t) {
t.is("a", a.name);
t.is("b", a.b.name);
t.is("c", a.b.c.name);
t.is("b", b.name);
t.is("c", b.c.name);
t.is("ed", d());
}
]
);
doh.run();
}
);

define("transpiler-tests", function(){});

0 comments on commit e0efab4

Please sign in to comment.