Skip to content

AMD plugin bundling #983

Open
Open

Description

@jslegers

I'm currently using r.js to bundle my project. Everything works fine, except for where plugins are used.

After a few hours of tinkering with my code, I came up with the following workaround :

define("full/path/to/ModuleA" /* 1 */, typeof window !== "undefined" ? [ /* 2 */
  "../DependencyA",
  "../DependencyB"
] : [], function(DependencyA,
                 DependencyB) {

  return {
    load: function(id, amdRequire, callback) {
      if (typeof window !== "undefined") { /* 3 */
        // DO NORMAL PLUGIN STUFF
      } else {
        amdRequire([ /* 4 */
          "full/path/to/DependencyA",
          "full/path/to/DependencyB",
          "full/path/to/whatever/else/should/be/included"
        ], function(A, B, p) {
          callback();
        });
      }
    }
  };
});

Basically, what I do is this :

  1. I define my module as a named module instead of an unnamed module
  2. I don't load any dependencies in my define function if window is not defined
  3. I only do the normal loading behavior inside my load method if window is not defined
  4. if window is not defined inside my load method, I load my dependencies, along with wherever else needs to be loaded there

This seems to do the trick, but I'm not quite satisfied with this solution :

  1. I don't like using named modules
  2. I don't like having to list my dependencies twice
  3. I don't like having to use the full path of my dependencies inside the load method

Is there a way I can avoid having to use named modules, full path dependencies and/or listing my dependencies twice?

Activity

  1. jrburke commented on May 21, 2018

    @jrburke
    Member

    It is a bit hard to follow that the core issue is since it is unclear what loader plugin is being used and how it is failing in the build. Loader plugins can participate in the build, even specifying a separate "plubinBuilder" module to be run just in the builds, but the API it is not the greatest or well explained. It might be worth looking at the text plugin to see how it interacts with the build system.

  2. jslegers commented on May 22, 2018

    @jslegers
    Author

    There are four core issues here :

    1. If I leave out the typeof window !== "undefined" in my define (/* 2 */), the plugin will try to run the dependencies during build time, causing errors to occur from running code on a server that's supposed to be run in a browser.
    2. Adding the typeof window !== "undefined", however, causes r.js not to recognize this code as a module, adding a empty define("full/path/to/ModuleA", [], function () {} module at the end and breaking my bundle when trying to run it in the browser. Using a named module (/* 1 */) fixes this.
    3. The typeof window !== "undefined" in my load method (/* 3 */) is also necessary to prevent code from code running on a server that's supposed to be run in a browser (like doing an Ajax call to check for availability of a file
    4. Adding my dependencies again at /* 4 */ was necessary to ensure they're included after leaving them out in the define. I could not use relative paths here because inside the bundle those paths would be evaluated as relative to the bundle rather than the module.

    Anyway, I just learnt that our Jenkins build breaks because of the changes I made to make these modules work nicely with r.js, so it's not just a cosmetic improvement I'm looking for.

  3. memoryseven commented on Jan 18, 2019

    @memoryseven

    "full/path/to/ModuleA" is an exists module?

  4. jslegers commented on Jan 18, 2019

    @jslegers
    Author

    "full/path/to/ModuleA" is the full path to the module I'm defining here.

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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions