Snap.svg icon indicating copy to clipboard operation
Snap.svg copied to clipboard

Error loading Snap with webpack

Open zuotian opened this issue 10 years ago • 47 comments

When loading Snap (from the dev branch) with webpack.

var Snap = require('snapsvg');

browser throw error Uncaught TypeError: Cannot read property 'on' of undefined

from line 1625 in svg.js

...
// default
eve.on("snap.util.getattr", function () {
    var att = eve.nt();
...

zuotian avatar Jan 06 '15 13:01 zuotian

Same problem for me.

tykko87 avatar Jan 13 '15 17:01 tykko87

Same problem here :cry:

clm-a avatar Feb 23 '15 14:02 clm-a

Is this related to https://github.com/adobe-webplatform/Snap.svg/issues/338 maybe ?

ibrierley avatar Feb 23 '15 14:02 ibrierley

@ibrierley I checked out back to commit No.#c0b0b5805ecb231 and just felt on the issue you've referenced !

clm-a avatar Feb 23 '15 15:02 clm-a

Same here

frank-weindel avatar Mar 06 '15 14:03 frank-weindel

Same!

andrew-minted avatar Mar 11 '15 22:03 andrew-minted

I've found a fairly simple way of pull in Snap with webpack using legacy-loader. Unfortunately Snap uses eval() in certain places (transform() is one area I know for sure) and so Snap is REQUIRED to be on the window.

var Snap = require('legacy-loader?exports=Snap!Snap.svg');
window.Snap = Snap; // transform REQUIRES Snap on the window because it uses eval

If you want to also get access to other resources that Snap exports (like mina) you can do something like this instead:

var SnapLib = require('legacy-loader!Snap.svg');
var Snap = SnapLib.Snap;
var mina = SnapLib.mina;
window.Snap = Snap; // transform REQUIRES Snap on the window because it uses eval

A bit involved I know. ES6 destructuring could be used to make it a bit more pleasant. Hope this helps.

frank-weindel avatar Mar 14 '15 19:03 frank-weindel

@frank-weindel Could you, please, point where “Snap uses eval() in certain places (transform() is one area I know for sure)”?

DmitryBaranovskiy avatar Mar 16 '15 11:03 DmitryBaranovskiy

@DmitryBaranovskiy I think this may have been a webpack issue since I can't reproduce it anymore and we were using dev-tool: eval before. Apologies.

frank-weindel avatar Apr 03 '15 15:04 frank-weindel

I retract my original comment as legacy-loader has some major issues in Firefox.

frank-weindel avatar Apr 03 '15 15:04 frank-weindel

But still do we have a way to load Snap with webpack, since it doesn't uses eval()?

kapooostin avatar Apr 15 '15 04:04 kapooostin

Here is another recipe

var Snap = require( "imports-loader?this=>window,fix=>module.exports=0!snapsvg/dist/snap.svg.js" );

kapooostin avatar Apr 17 '15 18:04 kapooostin

I was also hitting some issues using Webpack.

I suggest everybody that for the time being they follow @kapooostin’s suggestion. Works like a charm. Thanks!

alfonsomunozpomer avatar Aug 20 '15 11:08 alfonsomunozpomer

using this in react like

import Snap from 'imports-loader?this=>window,fix=>module.exports=0!snapsvg/dist/snap.svg.js';

I just added imports-loader to package.json and it works like a charm :+1:

hybrisCole avatar Aug 24 '15 01:08 hybrisCole

Thanks everybody. I had some troubles moving this to the webpack config because I am still earning my webpack black belt. So I thought I would drop here what I did end up getting to work:

            {
                test: require.resolve('snapsvg'),
                loader: 'imports-loader?this=>window,fix=>module.exports=0'
            },

NicholasTuck avatar Sep 25 '15 16:09 NicholasTuck

@NicholasTuck awesome, totally worked. :)

dvdplm avatar Oct 07 '15 07:10 dvdplm

Sorry, but still all this things with legacy-loader or with imports-loader doesn`t work Still have "Cannot read property 'on' of undefined" as in first message

In the "webpack" case of course

yazonnile avatar Oct 23 '15 21:10 yazonnile

I'm using webpack to load Snap.svg for React.js application. I'm trying with the several ways above on imports-loader, what I get in Snap are just the file name, any clue? (It says Snap is not a function`)

harryworld avatar Oct 28 '15 12:10 harryworld

I found the answer not long afterward. It was because the mix plugins with file-loader I end up modifying wepack.config.js, excluding .svg.js pattern from file-loader, and then use a combination of @hybrisCole and @NicholasTuck

harryworld avatar Oct 28 '15 13:10 harryworld

@harryworld thx. Will try!

yazonnile avatar Oct 28 '15 13:10 yazonnile

Would it be possible to publish a snap.svg-commonjs.js that left the require statements for eve intact that webpack works with out of the box? Is that all that's needed?

geelen avatar Jan 16 '16 06:01 geelen

Another solution

npm install snapsvg

And use this loader config:

      {
        test: require.resolve('snapsvg'),
        loader: 'imports-loader?this=>window,fix=>module.exports=0'
      }

Currently the npm version is still 0.4.0. Can it be bumped to 0.4.1 please? Seems it was published 11 months ago?

0x80 avatar Mar 05 '16 17:03 0x80

Any plans on supporting webpack sans workaround?

bmcorser avatar Aug 21 '16 20:08 bmcorser

Should we build and publish the 0.4.1 version? I would like to use the latests one via NPM and WebPack.

ova2 avatar Sep 04 '16 11:09 ova2

I published snapsvg-cjs so you can npm install snapsvg-cjs for use with webpack.

Once this project works out of the box with webpack I'll deprecate my snapsvg-cjs

geelen avatar Sep 05 '16 22:09 geelen

Does anyone here have tried configuring Snap on a React Project?

mjisican avatar Oct 18 '16 12:10 mjisican

@mjisican,

In Expression Atlas we use Snap and React. Have a look at a component that displays a SVG anatomogram here: https://github.com/gxa/anatomogram

alfonsomunozpomer avatar Oct 18 '16 13:10 alfonsomunozpomer

Thanks @alfonsomunozpomer :D been doing some experiments on react on my free time, this would help me... thank you very much :D

mjisican avatar Oct 19 '16 03:10 mjisican

@NicholasTuck it works for me ,thanks

vellinchou avatar Nov 03 '16 08:11 vellinchou

Any word on when this will be done? is the snapsvg-cjs still the best option? Also, snapsvg-cjs doesn't work for me.

hipkiss91 avatar Jan 30 '17 16:01 hipkiss91