react-spring icon indicating copy to clipboard operation
react-spring copied to clipboard

react-spring v6 importing parallax not working

Open iammarkps opened this issue 8 years ago • 52 comments

Unexpected identifier

/Users/iammark/Projects/openhouse/node_modules/react-spring/dist/addons.js:1
(function (exports, require, module, __filename, __dirname) { import _extends from '@babel/runtime/helpers/esm/extends';
                                                                     ^^^^^^^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Object.runInThisContext (vm.js:303:10)
    at Module._compile (internal/modules/cjs/loader.js:657:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.react-spring/dist/addons (/Users/iammark/Projects/openhouse/.next/server/static/development/pages/index.js:2031:18)
    at __webpack_require__ (/Users/iammark/Projects/openhouse/.next/server/static/development/pages/index.js:23:31)
    at Module../pages/index.js (/Users/iammark/Projects/openhouse/.next/server/static/development/pages/index.js:1785:82)
    at __webpack_require__ (/Users/iammark/Projects/openhouse/.next/server/static/development/pages/index.js:23:31)
    at Object.3 (/Users/iammark/Projects/openhouse/.next/server/static/development/pages/index.js:1964:18)
    at __webpack_require__ (/Users/iammark/Projects/openhouse/.next/server/static/development/pages/index.js:23:31)

When I tried to import parallax from 'react-spring/dist/addons' it's throwing me this error. This is my import code

import { ParallaxLayer } from 'react-spring/dist/addons'
import { Spring } from 'react-spring'

iammarkps avatar Oct 25 '18 18:10 iammarkps

Seems fine here: https://codesandbox.io/s/vyvl2pwnm0?from-embed

I think it's got something to do with your buildsystem. Like if it cannot find babel. I do not include babel, just declare it as peer-dep. To be sure, can you try: import {.....} from 'react-spring/dist/addons.cjs' ? This would pull in the common js build

drcmda avatar Oct 26 '18 09:10 drcmda

I think the problem is with NextJS

iammarkps avatar Oct 26 '18 10:10 iammarkps

The old error is gone but now its throw this error instead.

global.requestAnimationFrame is not a function

TypeError: global.requestAnimationFrame is not a function
    at requestFrame (/Users/iammark/Projects/openhouse/node_modules/react-spring/dist/web.cjs.js:18:17)
    at Controller.start (/Users/iammark/Projects/openhouse/node_modules/react-spring/dist/web.cjs.js:1542:18)
    at Controller.update (/Users/iammark/Projects/openhouse/node_modules/react-spring/dist/web.cjs.js:1530:47)
    at new Controller (/Users/iammark/Projects/openhouse/node_modules/react-spring/dist/web.cjs.js:1335:10)
    at new Parallax (/Users/iammark/Projects/openhouse/node_modules/react-spring/dist/addons.cjs.js:218:25)
    at processChild (/Users/iammark/Projects/openhouse/node_modules/react-dom/cjs/react-dom-server.node.development.js:2435:14)
    at resolve (/Users/iammark/Projects/openhouse/node_modules/react-dom/cjs/react-dom-server.node.development.js:2401:5)
    at ReactDOMServerRenderer.render (/Users/iammark/Projects/openhouse/node_modules/react-dom/cjs/react-dom-server.node.development.js:2728:22)
    at ReactDOMServerRenderer.read (/Users/iammark/Projects/openhouse/node_modules/react-dom/cjs/react-dom-server.node.development.js:2699:25)
    at renderToString (/Users/iammark/Projects/openhouse/node_modules/react-dom/cjs/react-dom-server.node.development.js:3113:25)
    at renderPage (/Users/iammark/Projects/openhouse/node_modules/next/dist/server/render.js:319:26)
    at Function.getInitialProps (/Users/iammark/Projects/openhouse/.next/server/static/development/pages/_document.js:193:18)
    at _callee$ (/Users/iammark/Projects/openhouse/node_modules/next/dist/lib/utils.js:86:30)
    at tryCatch (/Users/iammark/Projects/openhouse/node_modules/regenerator-runtime/runtime.js:62:40)
    at Generator.invoke [as _invoke] (/Users/iammark/Projects/openhouse/node_modules/regenerator-runtime/runtime.js:288:22)
    at Generator.prototype.(anonymous function) [as next] (/Users/iammark/Projects/openhouse/node_modules/regenerator-runtime/runtime.js:114:21)

iammarkps avatar Oct 26 '18 11:10 iammarkps

Really weird, could you share your repo somehow? It looks to me as if it doesn't pick up the correct entry point (which for the web is src/targets/web/index.js) -- in there the globals are set. Missing globals is almost certainly a weird entry. Or maybe tree-shaking is messing with it. In my projects i'm using straight webpack, and codesandbox, they all seem fine. It must be something peculiar in the way you fetch this lib.

drcmda avatar Oct 26 '18 12:10 drcmda

@drcmda https://github.com/iammarkps/openhouse This is my repo

iammarkps avatar Oct 26 '18 13:10 iammarkps

This is so weird, i tried to pluck out everything, just pasting a parallax example into index.js and it worked, the moment i do something else it falls appart. It says it can't find index.js:2429 GET http://localhost:3000/Users/drcmda/dev/openhouse/node_modules/react-spring/dist/addons.js 404 (Not Found)

addons.js is right there node_modules

Then i used the .cjs export, now it complains about global.raf not find, which is insane. Can it maybe be that next is using outdated babel or webpack inside?

@TrySound Do you have any idea what this could be? Did this ever happen anywhere else?

Maybe making a very reduced example and adding an issue in next.js's tracker would be the way to go.

drcmda avatar Oct 26 '18 13:10 drcmda

NextJS is using babel 7

iammarkps avatar Oct 26 '18 13:10 iammarkps

@drcmda btw nextjs use file-system routing I think 'http://localhost:3000/Users/drcmda/dev/openhouse/node_modules/react-spring/dist/addons.js' won't work.

iammarkps avatar Oct 26 '18 13:10 iammarkps

but that was a standard import { ... } from 'react-spring/dist/addons' and the console said 404. Have to admit i'm stumped by this.

drcmda avatar Oct 26 '18 14:10 drcmda

Oh I thought you use it in browser sorry

iammarkps avatar Oct 26 '18 14:10 iammarkps

I'm also experiencing the same issue with Gatsby: https://github.com/LekoArts/gatsby-starter-portfolio-cara/pull/8

You also can see the Netlify build there.

LekoArts avatar Oct 31 '18 16:10 LekoArts

Could you check if "global" is defined at all? It would be weird if that's not the case in a bundled environment. And if you go:

global = { requestAnimationFrame: fn => requestAnimationFrame(fn), cancelAnimationFrame: fn => cancelAnimationFrame(fn) }

Before the other stuff initializes, does that fix it?

Would love to know the root of it ... it's such a strange issue ...

drcmda avatar Oct 31 '18 17:10 drcmda

Could you check if "global" is defined at all? It would be weird if that's not the case in a bundled environment. And if you go:

Doing a console.log(global) gave me a result with a bunch of key value pairs in it.

global = { requestAnimationFrame: fn => requestAnimationFrame(fn), cancelAnimationFrame: fn => cancelAnimationFrame(fn) }

Putting it where? Placing it in my index.jsx file it tells me that requestAnimationFrame is not defined.

You can try it yourself with the repo though.

LekoArts avatar Nov 01 '18 11:11 LekoArts

@iammarkps @LekoArts could you try [email protected]? I am exposing raf and caf as window. ... now. Seems weird but if it fixes it then i'll release a patch later on.

drcmda avatar Nov 01 '18 15:11 drcmda

I updated, yeah. But window is problematic, too: https://www.gatsbyjs.org/docs/debugging-html-builds/

I got a "window is not defined" error. It would be best to add this check to react-spring, not to my code: https://github.com/LekoArts/gatsby-starter-portfolio-cara/blob/update-spring/src/pages/index.jsx#L17-L22

Because then emotion complains about forwardProp and other stuff.

LekoArts avatar Nov 01 '18 17:11 LekoArts

Could this all be a symptom of these build tools? I haven't touched global.requestAnimationFrame or window.requestAnimationFrame and 5.x uses the same format. Both global/window should be fairly common. I think it would be best if next/gatsby sort this out, i am not sure at this point if there's something i can fix otherwise. I don't require or import Parallax anywhere, they are separate packages with standard esm/cjs exports. Wouldn't know where to put this window !== undefined stuff (and why).

drcmda avatar Nov 01 '18 18:11 drcmda

Thanks to @pieh I got it working: https://github.com/LekoArts/gatsby-starter-portfolio-cara/pull/8/commits/eb5037ac39809b2eba5e280c2df93a0d5d25e4c2#diff-9d1d0e7c9aa0b75d4b0245b0e93b4163

With the normal build. So it's definitely something on Gatsby's end (however you could also make react-spring SSR ready but one can't expect/demand that to happen).

LekoArts avatar Nov 02 '18 13:11 LekoArts

Are there any steps i would need to take in order to support SSR? I wasn't aware it would cause problems.

drcmda avatar Nov 02 '18 14:11 drcmda

I don't know specifics of Your library, but for component to be "SSR ready" you would want to access browser specific APIs (i.e. requestAnimationFrame) not in component constructor, componentWillMount or render lifecycle methods - componentDidMount is good place for that. Or if you can't refactor it to not call them there you would need guards like typeof window !== `undefined` before using them.

We potentially could create mocks in gatsby so calling browser APIs in SSR doesn't throw errors but is no-op, but this might cause weird side effects, so I feel it's better if we don't do that - it's easier to debug something if it crash with error than if it fails silently

pieh avatar Nov 02 '18 15:11 pieh

@pieh I could make them no-ops if global isn't found.

@LekoArts Could you try 6.1.7-beta.1 without the window-checks you have merged? Does it work like that?

drcmda avatar Nov 02 '18 16:11 drcmda