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

Error importing into gatsby

Open tractorcow opened this issue 3 years ago • 1 comments

When embedding this component into gatsby, we have an error on window definition.

https://github.com/farminf/pannellum-react/blob/master/src/pannellum/js/libpannellum.js#L24

This triggers the below

WebpackError: ReferenceError: window is not defined

As we only render the component on client side, it's not necessary that we actually render on the serverside, only that the module itself doesn't crash compilation.

We can probably work around by using a conditional import on the panellum-react module, but ideally the window.libpannellum assignment would be wrapped in an if (typeof window !== 'undefined')

tractorcow avatar Jun 13 '22 22:06 tractorcow

FYI this is my temporary workaround.

import type { PannellumVideo as PannellumVideoType } from "pannellum-react/lib/index"
let PannellumVideo: typeof PannellumVideoType
if (typeof window === "undefined") {
  PannellumVideo = () => <div />
} else {
  // eslint-disable-next-line @typescript-eslint/no-var-requires
  const imported = require("pannellum-react")
  PannellumVideo = imported.PannellumVideo
}

export { PannellumVideo }

tractorcow avatar Jun 13 '22 23:06 tractorcow