micku7zu / vanilla-tilt.js

A smooth 3D tilt javascript library.
https://micku7zu.github.io/vanilla-tilt.js/index.html
MIT License
3.86k stars 174 forks source link

Module doesn't export init function #15

Closed bastilimbach closed 7 years ago

bastilimbach commented 7 years ago

I tried to implement vanilla-tilt using require("vanilla-tilt"); but failed, because the init function isn't exported. It could be completely my fault and if I'm wrong, we should update the README.md instructions with a working example. Basically, I tried the following:

var tiltjs = require("vanilla-tilt");
tiltjs.init(document.querySelector(".postImgContainer"), {
    max: 25,
    speed: 400
});

Which results in an tiltjs.init is not a function error. When i try this:

var tiltjs = require("vanilla-tilt");
VanillaTilt.init(document.querySelector(".postImgContainer"), {
    max: 25,
    speed: 400
});

It works, but just because VanillaTilt is globally available.

micku7zu commented 7 years ago

Can you please run console.log(tiltjs) to see what is the actual return of that require?

I did try npm install vanilla-tilt and then in NodeJS i tried to

let vanillaTilt = require('vanilla-tilt'); console.log(vanillaTilt.init);

and the output is:

[Function: init]

Try to console.log the tiltjs object returned from require :smile:

micku7zu commented 7 years ago

Or tell me how to reproduce your problem, some simple way

bastilimbach commented 7 years ago

Mh, this is pretty wired. I get the output undefined but when I use tiltjs.default.init(....) it works.

bastilimbach commented 7 years ago

I did a fresh install of vanilla-tilt and copied your example. Still get undefined.

bastilimbach commented 7 years ago

I think this happens because I'm not using ES5. ES5 puts the .default. automatically between these functions. Could this be possible?

micku7zu commented 7 years ago

I really don't know how all this imports works. But you can read here http://www.2ality.com/2014/09/es6-modules-final.html

I think you need to read at 3.2 Default exports (one per module)

bastilimbach commented 7 years ago

@micku7zu I think I found the issue. Module bundlers are using the module variable, which points to the ES5 script. Check out the PR.

MarcoMedrano commented 7 years ago

I think I have same issue, I tried to use this in IE11 (no classes) then I tried to import the babel version. import VanillaTilt from 'vanilla-tilt/dist/vanilla-tilt.babel';

But i got similar error: _vanillaTilt2.default.init is not a function

Any solution or workaround is appreciated !!

bastilimbach commented 7 years ago

@marcomedrano you can use my fork for now. Just run npm install bastilimbach/vanilla-tilt.js#fix/issue-15

But for the future, I think we need to rethink the directory structure and build process of vanilla-tilt.

MarcoMedrano commented 7 years ago

Yeah that worked even I did not had to specify babel version: import VanillaTilt from 'vanilla-tilt';

Thank you!

micku7zu commented 7 years ago

I merged the pull request. Thanks 👍