chrisdickinson / raf

requestAnimationFrame polyfill library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GraalVM support? ReferenceError: global is not defined

helllamer opened this issue · comments

Just a small feature request:
Add support for undefined global namespace, if js-runtime environment is not a browser and not a nodejs.

TL;DR

// webpackBootstrap
require("raf");
org.graalvm.polyglot.PolyglotException: ReferenceError: global is not defined
	at <js>.:program(webpack://ScalaJSBundlerLibrary/./node_modules/raf/index.js?:2)

Workaround

var global = {};
// run webpack stuff:
require("raf");
// webpack import ok

Full GraalVM backtrace

Mostly useless.

EXPAND BACKTRACES
Exception in thread "ScalaGraal-pool-1-thread-1" japgolly.scalagraal.ExprError$EvalError: ReferenceError: global is not defined
	at japgolly.scalagraal.ExprError$InEval$.unapply(ExprError.scala:23)
	at japgolly.scalagraal.Expr$.$anonfun$apply$2(Expr.scala:168)
	at japgolly.scalagraal.Expr.$anonfun$$greater$greater$1(Expr.scala:144)
	at japgolly.scalagraal.Expr.evalOrThrow(Expr.scala:25)
	at japgolly.scalagraal.GraalContext$Builder.evalOrThrow$1(GraalContext.scala:131)
	at japgolly.scalagraal.GraalContext$Builder.$anonfun$build$3(GraalContext.scala:134)
	at japgolly.scalagraal.GraalContext$Builder.$anonfun$build$3$adapted(GraalContext.scala:134)
	at scala.Option.foreach(Option.scala:437)
	at japgolly.scalagraal.GraalContext$Builder.build(GraalContext.scala:134)
	at japgolly.scalagraal.GraalContextPool$Builder$Fixed$Step2A.$anonfun$build$1(GraalContextPool.scala:84)
	at scala.Function1.$anonfun$andThen$1(Function1.scala:85)
	at japgolly.scalagraal.GraalContextPool$Builder$Fixed$Step2B.$anonfun$build$2(GraalContextPool.scala:96)
	at japgolly.scalagraal.GraalContextPool$DefaultContextThread$$anon$2.run(GraalContextPool.scala:164)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: org.graalvm.polyglot.PolyglotException: ReferenceError: global is not defined
	at <js>.:program(webpack://ScalaJSBundlerLibrary/./node_modules/raf/index.js?:2)
	at <js>../node_modules/raf/index.js(showcasessr-fastopt-library.js:4941)
	at <js>.__webpack_require__(showcasessr-fastopt-library.js:5808)
	at <js>.:program(webpack://ScalaJSBundlerLibrary/./node_modules/react-motion/lib/Motion.js?:31)
	at <js>../node_modules/react-motion/lib/Motion.js(showcasessr-fastopt-library.js:5050)
	at <js>.__webpack_require__(showcasessr-fastopt-library.js:5808)
	at <js>.:program(webpack://ScalaJSBundlerLibrary/./node_modules/react-motion/lib/react-motion.js?:7)
	at <js>../node_modules/react-motion/lib/react-motion.js(showcasessr-fastopt-library.js:5116)
	at <js>.__webpack_require__(showcasessr-fastopt-library.js:5808)
	at <js>.:anonymous(webpack://ScalaJSBundlerLibrary/./node_modules/react-stonecutter/lib/react-stonecutter.js?:1)
	at org.graalvm.sdk/org.graalvm.polyglot.Context.eval(Context.java:379)
	... 13 more
Exception in thread "ScalaGraal-pool-1-thread-2" japgolly.scalagraal.ExprError$EvalError: ReferenceError: global is not defined
	at japgolly.scalagraal.ExprError$InEval$.unapply(ExprError.scala:23)
	at japgolly.scalagraal.Expr$.$anonfun$apply$2(Expr.scala:168)
	at japgolly.scalagraal.Expr.$anonfun$$greater$greater$1(Expr.scala:144)
	at japgolly.scalagraal.Expr.evalOrThrow(Expr.scala:25)
	at japgolly.scalagraal.GraalContext$Builder.evalOrThrow$1(GraalContext.scala:131)
	at japgolly.scalagraal.GraalContext$Builder.$anonfun$build$3(GraalContext.scala:134)
	at japgolly.scalagraal.GraalContext$Builder.$anonfun$build$3$adapted(GraalContext.scala:134)
	at scala.Option.foreach(Option.scala:437)
	at japgolly.scalagraal.GraalContext$Builder.build(GraalContext.scala:134)
	at japgolly.scalagraal.GraalContextPool$Builder$Fixed$Step2A.$anonfun$build$1(GraalContextPool.scala:84)
	at scala.Function1.$anonfun$andThen$1(Function1.scala:85)
	at japgolly.scalagraal.GraalContextPool$Builder$Fixed$Step2B.$anonfun$build$2(GraalContextPool.scala:96)
	at japgolly.scalagraal.GraalContextPool$DefaultContextThread$$anon$2.run(GraalContextPool.scala:164)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: org.graalvm.polyglot.PolyglotException: ReferenceError: global is not defined
	at <js>.:program(webpack://ScalaJSBundlerLibrary/./node_modules/raf/index.js?:2)
	at <js>../node_modules/raf/index.js(showcasessr-fastopt-library.js:4941)
	at <js>.__webpack_require__(showcasessr-fastopt-library.js:5808)
	at <js>.:program(webpack://ScalaJSBundlerLibrary/./node_modules/react-motion/lib/Motion.js?:31)
	at <js>../node_modules/react-motion/lib/Motion.js(showcasessr-fastopt-library.js:5050)
	at <js>.__webpack_require__(showcasessr-fastopt-library.js:5808)
	at <js>.:program(webpack://ScalaJSBundlerLibrary/./node_modules/react-motion/lib/react-motion.js?:7)
	at <js>../node_modules/react-motion/lib/react-motion.js(showcasessr-fastopt-library.js:5116)
	at <js>.__webpack_require__(showcasessr-fastopt-library.js:5808)
	at <js>.:anonymous(webpack://ScalaJSBundlerLibrary/./node_modules/react-stonecutter/lib/react-stonecutter.js?:1)
	at org.graalvm.sdk/org.graalvm.polyglot.Context.eval(Context.java:379)
	... 13 more

How does graal have require but not node’s other globals?

I'm not a specialist in js/require/webpack.
Just trying to run webpack-bundled js-file for SSR via GraalVM polyglot
All else dependencies (like react, etc) don't throw such exceptions.

GraalVM officially have several (or more?) options for JS execution:

Webpack 5 is broken, and doesn’t polyfill node globals by default. What you’ll want to do, instead of setting global to an empty object, is replace all instances of a global global in the bundle with globalThis (assuming graal supports that).

Thanks. I need some time to understand and apply your information to make further steps.
This issue should be closed, by now?

We have to hack around this. Any update? Building web workers with Vite and global is undefined.

@jbeuckm if vite won't polyfill it properly by default, then you'd have to either configure it to do so, or switch to a working bundler.