jlmakes / scrollreveal

Animate elements as they scroll into view.
https://scrollrevealjs.org/
22.39k stars 2.26k forks source link

ScrollReveal is not defined with webpack [solved v3.0.5] #209

Closed marciomunhoz closed 8 years ago

marciomunhoz commented 8 years ago

When using ScrollReveal with webpack, the object ScrollReveal is not defined. Have someone experienced the same issue?

Thanks!

volnei commented 8 years ago

+1 with last version

tamamadesu commented 8 years ago

same question!

tforssander commented 8 years ago

I noticed a similar error when updating to the 3.0.3 release and noticed that I needed to change from window.sr = new scrollReveal(config); to window.sr = ScrollReveal(config); (notice the uppercased S).

volnei commented 8 years ago

I have tried with all possible cases with no success. The error appears when lib is loaded.

volnei commented 8 years ago

The error occurs on return ScrollReveal; at line 473 of file https://github.com/jlmakes/scrollreveal.js/blob/master/dist/scrollreveal.js

I think is caused by a package tooling used to build the dist. Maybe this is incompatible with webpack? I think this is generated by gulp on config umd({ namespace: 'ScrollReveal', exports: 'ScrollReveal' }), but i'm not familiar with gulp... =/

volnei commented 8 years ago

@jlmakes it works by using the scrollreveal.js (before wrap) but need a change in the code at line 456

-  var _requestAnimationFrame = this.requestAnimationFrame       ||
-                               this.webkitRequestAnimationFrame ||
-                               this.mozRequestAnimationFrame;
+  var _requestAnimationFrame = window.requestAnimationFrame       ||
+                               window.webkitRequestAnimationFrame ||
+                               window.mozRequestAnimationFrame;

If you are interested to investigate, I just create a repo with an example app to simulate the problem.

git clone https://github.com/volnei/webpack-scrollreveal.git
npm install
npm run build

And open the index.html file after making the changes above.

Hope this helps!

jlmakes commented 8 years ago

Thanks @volnei I will explicitly reference window in the next release. Still working on a solution… :bow:

jlmakes commented 8 years ago

@volnei First, I believe I’ve confirmed this part of your index.js needs to be changed:

- import {ScrollReveal, Tools} from 'scrollreveal'
+ import ScrollReveal from 'scrollreveal'

window.sr = ScrollReveal()
window.sr.reveal('.box')

Secondly, update ScrollReveal from v3.0.3 to v3.0.5:

npm update scrollreveal

Afterwards… webpack-sample

@volnei @marciomunhoz @tamamadesu @tforssander I published v3.0.5 with the necessary changes, and have confirmed it is working with Webpack! :bow:

volnei commented 8 years ago

Thank you a lot @jlmakes. Now it works fine :smile:

yarhtut commented 7 years ago

👍 Thanks, @jlmakes

birksy89 commented 6 years ago

For me this worked:

import ScrollReveal from 'scrollreveal'

Whereas before I tried:

import 'scrollreveal'
luciancic commented 6 years ago

Tried with the latest version (3.3.6) and did not work with webpack, but when I reverted to scrollreveal@3.0.5 it worked just fine.

jlmakes commented 6 years ago

Can you do me a favor and test with scrollreveal@beta please @lucian95?

luciancic commented 6 years ago

Sure thing. It did load from webpack but it only contained an __esModule and default property. Is that the object you intended to return with require('scrollreveal')? Seems off.

scrollreveal beta

jlmakes commented 6 years ago

That looks like the result of a the Babel ES2015 to Common JS Plugin @lucian95.

I'm not sure, but seems like if you're going to consume the default export of a ES2015 module with Babel v6+, you'd need to do this:

const ScrollReveal = require('scrollreveal').default

Also mentioned in the Babel documentation, is a plugin that automatically unboxes the default property value as Node's module.exports, called babel-plugin-add-module-exports... which as I understand should allow you to write:

const ScrollReveal = require('scrollreveal')
luciancic commented 6 years ago

In that case yes, doing require('scrollreveal').default().reveal(...) works properly. I tried it again just now.