Skip to content

Failure to build with asynchronous-loading plugin #604

Open

Description

@njoyard

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:19

Making 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

  1. jrburke commented on Jan 11, 2014

    @jrburke
    Member

    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.

  2. njoyard commented on Jan 11, 2014

    @njoyard
    Author

    That clarifies things. I'm building with node.js so I will just switch to the synchronous fs functions.
    Thanks !

  3. modified the milestones: 2.2.0, 3.0 on Mar 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions