question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Native ES6 modules vs. the bundle

See original GitHub issue

When plotly.js is loaded in Chrome by the native ES6 import, the loading crashes with Cannot read property 'document' of undefined at line 18178, i.e. at var d3_document = this.document;

The problem is that the bundler assumes that when a function is executed without any this, the this will automatically be set to window. This is not the case with ES6 modules, where this will be undefined.

I fixed it by changing line 27724 from }(); to }.apply(self); but this is just a hack 😃

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:21
  • Comments:41 (7 by maintainers)

github_iconTop GitHub Comments

15reactions
michielkikkertcommented, Dec 4, 2019

Is there a ‘real’ solutions for this available? I’d like to use Plotly for an Angular 8 project - but reverting to es5 is not really an option (it will break for Angular 9 I think). I was so hoping to use plotly as a Highchart replacement - but no dice I’m afraid.

13reactions
oldrich-scommented, Aug 1, 2019

@jmsuresh In this file:

https://cdnjs.cloudflare.com/ajax/libs/plotly.js/1.49.1/plotly.js

search for define(d3). You will find:

if (typeof define === "function" && define.amd) this.d3 = d3, define(d3); else if (typeof module === "object" && module.exports) module.exports = d3; else this.d3 = d3;
}();

Change }(); to }.apply(self);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Native, Unbundled ES6 Modules With a Simple Build ...
Native module loading​​ According to Google Chrome's Platform Status, less then 4% of page loads use it! Most resources I've found use bundlers...
Read more >
ES6 browser support: is it time to rethink bundling? - Contentful
The new setup using native supported ES6 modules. Now that we have the “traditional bundle” for all the browsers that don't support ES6...
Read more >
Using Native JavaScript Modules in Production Today
In the rest of this post I'm going to show you how I bundle to modules (including the use of dynamic import and...
Read more >
Demystifying Code Bundling with JavaScript Modules
Dependencies are modules, too, which means they no longer need to be loaded individually before the main file can load. The bundle is...
Read more >
Webpack vs ES6 modules - Stack Overflow
Also, Webpack version 5 appeared which provides module federation feature where you can use part of code from one bundle into another. which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found