plotly.js icon indicating copy to clipboard operation
plotly.js copied to clipboard

Native ES6 modules vs. the bundle

Open dk-teknologisk-osv opened this issue 7 years ago • 41 comments

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 :)

dk-teknologisk-osv avatar Feb 06 '19 09:02 dk-teknologisk-osv

Thanks for writing in.

When plotly.js is loaded in Chrome by the native ES6 import,

Can you share a reproducible example of how you're going about doing this. Thank you!

etpinard avatar Feb 06 '19 14:02 etpinard

Try:

<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
</head>
<body>
<script type="module" src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</body>
</html>

or alternatively:

<script type="module">
    import 'https://cdn.plot.ly/plotly-latest.min.js'
</script>

dk-teknologisk-osv avatar Feb 07 '19 07:02 dk-teknologisk-osv

In a codepen: https://codepen.io/alexcjohnson/pen/aXEbOa?editors=1011

That particular error is with d3v3 - seems like v4 fixes it so we should get it when we leapfrog to v5. Will be interesting to see if there's anything else in our code that uses this to mean the window (or anything else that's incompatible with ES6 modules... I'm not very familiar) but it doesn't seem like there's much point trying before the d3 upgrade.

alexcjohnson avatar Feb 07 '19 14:02 alexcjohnson

@oldrich-svec

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

Can you please provide which particular line had to be changed. I am stuck with this for the past two days (tried many things with babel options - nothing worked)

jmsuresh avatar May 06 '19 08:05 jmsuresh

Any fix for this?

leolorenzoluis avatar Jun 02 '19 03:06 leolorenzoluis

Fix for version 1.48.2; the change is at line number 32481 '/node_modules/plotly.js/dist/plotly.js'

LRagji avatar Jun 14 '19 06:06 LRagji

Anybody know if there is a issue for this filed in the d3 project?

wendelstam avatar Jul 22 '19 11:07 wendelstam

@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);

oldrich-s avatar Aug 01 '19 06:08 oldrich-s

From https://github.com/plotly/plotly.js/issues/4130 @Arjun-Shinojiya wrote:

I am trying to integrate plotly.js in my Angular 8 project. I successfully integrated it and tested it in local ,that time it is working fine without any error. But when I check it in live server ,that time I am getting cannot read property 'document ' of undefined error.. I also similar issue blog in this repository.`.I wanted to try this hack changing line 27724 from }(); to }.apply(self); but this is just a hack :) But I dont know in which module file I can get this line.Currently none of the plotly.js file have 27724 lines

Maybe someone here can help.

etpinard avatar Aug 15 '19 15:08 etpinard

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 :)

@oldrich-svec can you please tell the exact location of the file which you mentioned in this questions last line? .I am facing the same issue.

Arjun-Shinojiya avatar Aug 15 '19 16:08 Arjun-Shinojiya

see https://github.com/plotly/plotly.js/issues/3518#issuecomment-517138019

oldrich-s avatar Aug 15 '19 16:08 oldrich-s

see #3518 (comment)

So I should use plotly.js via CDN in angular 8 right?

Arjun-Shinojiya avatar Aug 15 '19 16:08 Arjun-Shinojiya

@Arjun-Shinojiya it is up to you how you use plotly.js ;). The comment just shows you what line you want to edit.

oldrich-s avatar Aug 16 '19 04:08 oldrich-s

Is editing the line of code the only solution? What happens when I want to upgrade to a new version of plotly? I have to edit it every time?

TravisLRiffle avatar Aug 27 '19 21:08 TravisLRiffle

@TravisLRiffle yes you have to reapply your hack every time! For me, it is not a solution for enterprise applications.

meriturva avatar Aug 28 '19 06:08 meriturva

After fixing this, I got a d3_xhr-not found-error although it's defined

maxmeyer avatar Sep 21 '19 20:09 maxmeyer

Alternative workaround for Angular 8 upgraders: For those of us coming here having encountered this issue when upgrading to Angular 8, an alternative workaround solution to editing the plotly.js file is to not use the new "Differential Loading by Default" as mentioned in this comment on angular-plotly.js: https://github.com/plotly/angular-plotly.js/issues/75#issuecomment-526871836

I could solve the issue forcing the typescript to be generated as "target": "es5" making it not use the

Owner


Other Repo Issues