Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AMD plugin bundling #983

Open
jslegers opened this issue May 18, 2018 · 4 comments
Open

AMD plugin bundling #983

jslegers opened this issue May 18, 2018 · 4 comments

Comments

@jslegers
Copy link

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?

@jrburke
Copy link
Member

jrburke commented May 21, 2018

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.

@jslegers
Copy link
Author

jslegers commented May 22, 2018

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.

@memoryseven
Copy link

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

@jslegers
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants