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 compatibility broken in 1.8.1. #130

Closed
mbostock opened this issue May 4, 2018 · 3 comments
Closed

AMD compatibility broken in 1.8.1. #130

mbostock opened this issue May 4, 2018 · 3 comments

Comments

@mbostock
Copy link

mbostock commented May 4, 2018

Viz.js 1.8.1 adds a new UMD block that 1.8.0 didn’t have:

if (typeof exports === 'object' && typeof module === 'object')
  module.exports = Module;
else if (typeof define === 'function' && define['amd'])
  define([], function() { return Module; });
else if (typeof exports === 'object')
  exports["Module"] = Module;

But Viz.js 1.8.1 retains the global / CommonJS export block from previous releases:

if (typeof module === "object" && module.exports) {
  module.exports = Viz;
} else {
  global.Viz = Viz;
}

As a result, in an AMD environment (such as when using d3-require inside of Observable), loading Viz.js 1.8.1 returns the Module object (created by Emscripten?) rather than the expected Viz object.

Replacing both blocks with something like this should work:

if (typeof exports === 'object' && typeof module !== 'undefined') {
  module.exports = Viz;
} else if (typeof define === 'function' && define.amd) {
  define(function() { return Viz; });
} else {
  global.Viz = Viz;
}

(I often use Rollup to generate UMD bundles from ES modules; see @observablehq/graphviz for an example.)

@mbostock
Copy link
Author

mbostock commented May 4, 2018

I see you’re already using Rollup for 2.0.0, hooray! So feel free to close this if you don’t feel a need to ship a patch release for v1. 😄

mdaines added a commit that referenced this issue May 4, 2018
@mdaines
Copy link
Owner

mdaines commented May 4, 2018

@mbostock I've released 1.8.2, which I think should address this.

I am using Rollup for 2.0.0, but I'm also splitting the Emscripten stuff into a separate file so it can be used as a Web Worker. That doesn't quite work in Observable yet as I just found out. I've created an issue for that here: #131

@mdaines mdaines closed this as completed May 4, 2018
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