2

I'm trying to use two libraries:

  • D3 (a Javascript graphics library)
  • NVD3 (a graph-specific D3 extension)

I'm also trying to use the Require Optimizer, and that's where things get rough. D3 is AMD-compliant, so if it detects that you're using Require it will define a module and NOT export a global d3 variable. NVD3 has no AMD support, but Require has "shims" for that case.

However, if I "shim" NVD3 and give that shim a dependency of D3, it doesn't work, because NVD3 expects there to be a global d3 variable, which D3 won't make in a Require environment. So, to get around this issue I made a new module (d3Shim), which simply requires D3, then registers it as a global variable:

define(['d3'], function(d3) {
    return window.d3 = d3;
});

I made NVD3 depend on d3Shim, and then everything worked ... in normal Require-land. When I tried to use the require-optimizer to merge everything together in a single file I found NVD3 started breaking again, because of a lack of a d3 variable.

It turns out the optimizer does this for shims:

*shimmed code here*
define(*shim path*, [], function(){});

So, it doesn't matter what dependencies I have, the NVD3 code will run before the d3shim module can do its magic.

My question is, is there any way I can use these two libraries with the Require Optimizer, without having to alter either file? If necessary I can always edit NVD3's code to make it an AMD module, but for obvious reasons it's bad practice to edit 3rd party libraries, so I'd prefer a non-editing solution.

machineghost
  • 33,529
  • 30
  • 159
  • 234

0 Answers0