Repository navigation
Description
I'm working on a template library with requirejs plugin support. The plugin loads templates asynchronously and compiles them as AMD modules, and also has support for loading dependencies (ie. included templates). It works well in the browser but fails when building with r.js 2.1.10.
Basically I nailed down the issue to a simple test case. Here is the plugin code (an over-simplified copy of what requirejs-handlebars does, which is itself inspired by requirejs-text):
// myplugin.js
define(function() {
// Simulate fetching module code asynchronously
function getCode(name, callback) {
var code;
switch(name) {
case 'first':
code = 'define("myplugin!first", ["myplugin!second"], function(second) { return second.value; })';
break;
case 'second':
code = 'define("myplugin!second", function() { return { value: "secondValue" }; });';
break;
}
setTimeout(function() { callback(code); }, 100);
}
var buildMap = {};
return {
write: function(plugin, name, write) {
if (name in buildMap) {
write(buildMap[name]);
}
},
load: function(name, req, onload, config) {
getCode(name, function(code) {
if (!code) {
onload.error("Cannot find myplugin!" + name);
} else {
buildMap[name] = code;
onload.fromText(code);
}
});
}
}
});Here is the main code:
// main.js
require(['myplugin!first'], function(value) {
console.log("Loaded module, value is " + value);
});And the build configuration:
{
"name": "main",
"out": "main-built.js",
"optimize": "none"
}I get the following error when building :
$ node_modules/.bin/r.js -o build.conf
Tracing dependencies for: main
Error: Error: Loader plugin did not call the load callback in the build:
myplugin:
myplugin!first_unnormalized2
myplugin!first
Module loading did not complete for: main
at Function.build.checkForErrors (/home/niko/dev/rjs-test/node_modules/requirejs/bin/r.js:27064:19Making the plugin near-synchronous (ie. replacing the setTimeout call with callback(code);) makes the build succeed.
Am I doing something wrong there, or is this a situation r.js cannot handle ?
Activity
jrburke commentedon Jan 11, 2014
Right now the build runs synchronously, so that it works on other platforms like rhino and xpcshell without extra plumbing. So yes, it will be best if config.isBuild (passed to the load method) is true to call the callback immediately.
I have thought about enabling async building in node since it has nice built in primitives for it, but have not gotten to that yet, mostly because it requires upgrading my tests which assume sync build completion.
I will keep this bug open to track that, but it will not happen soon, maybe not until 3.0, but I will put it in the 2.2 bucket for consideration.
njoyard commentedon Jan 11, 2014
That clarifies things. I'm building with node.js so I will just switch to the synchronous fs functions.
Thanks !