ibm-js / requirejs-dplugins

AMD plugins for RequireJS
http://ibm-js.github.io/requirejs-dplugins/
Other
6 stars 9 forks source link

Add a jquery plugin. #7

Closed clmath closed 9 years ago

clmath commented 9 years ago

Created the issue to track progress and have it in the release milestone.

wkeese commented 9 years ago

I submitted PR #8.

But @clmath, I'm stuck on the build. When I do a build, grunt-amd-build executes the code in each module (technically, it executes each modules' factory), and fails on https://github.com/jquery/jquery/blob/master/src/core.js because it accesses window which isn't defined in Node.

IIRC this is why r.js doesn't actually execute the code in the modules being included into a build, but you took a different route. So, what should we do? The possibilities I can think of are:

  1. Use the exclude directive so that grunt-amd-build doesn't try to roll jquery into the build. Rather, require that the app manually load jquery.js via a <script> tag. I don't like that solution because it goes against the main goal of AMD: granular dependencies.
  2. Maybe the developer can manually figure out which jquery modules are needed and then include only them by following step (1) and then using the includeShallow directive. This is a bad solution too because the developer has to manually list which jquery modules are required.
  3. Modify grunt-amd-build to load https://github.com/tmpvar/jsdom, so that it can run the jquery modules. (There may be a more lightweight solution such as just defining global variables like window and document, but not sure.)
  4. Try to get the jQuery guys to modify their modules so they at least load in node. But that would probably take a while.

What do you think?

PS: Why is grunt-amd-build executing that factory anyway? Why not just look at the dependencies listed as the first argument to the define() function?

clmath commented 9 years ago

There is a much simpler solution:

load: function (resource, req, onLoad, config) {
    /* global jQuery */
    /* global $ */
    if (config.isBuild) {
        onLoad();
    } else if (typeof jQuery !== "undefined") {
        onLoad(jQuery);
    } else if (typeof $ !== "undefined") {
        onLoad($);
    } else {
        require(getModules(resource), function ($) {
            onLoad($);
        });
    }
}

For more information you can look at http://requirejs.org/docs/plugins.html#apiload, especially the Build considerations. (grunt-amd-build follow the same api)