Prozi / pixi-shim

PIXI.js Back-End "shim". For mocking Canvas in Node.js with ❤️
https://pietal.dev
46 stars 11 forks source link

Window is not defined, in Webpack 5 bundle webworker #17

Closed kevzettler closed 3 years ago

kevzettler commented 3 years ago

I'm trying to use this in a webpack 5 bundle for a webworker. I get an error that window is not defined from pixi-shim/polyfills/window.js.

window.js:10 Uncaught (in promise) ReferenceError: window is not defined
    at Object../node_modules/pixi-shim/polyfills/window.js (window.js:10)
    at __webpack_require__ (bootstrap:21)
    at fn (hot module replacement:61)
    at Object../node_modules/pixi-shim/index.js (index.js:4)
    at __webpack_require__ (bootstrap:21)
    at fn (hot module replacement:61)
    at Module../src/RenderStore.ts (Projectile.ts:34)
    at __webpack_require__ (bootstrap:21)
    at fn (hot module replacement:61)
    at Module../src/ClientStore.ts (Camera.ts:7)

I'm totally confused because I thought this module was meant for headless environments like Node why is it looking for a window?

Prozi commented 3 years ago

@kevzettler yes it's for node, why are you using webpack for a node module?

you propably should be using just pixi.js if with webpack

kevzettler commented 3 years ago

@Prozi its not node. Its a webworker, which is browser environment.

Prozi commented 3 years ago

so the webworker has window is undefined hmm can you provide me with minimal reproduction of the worker code and how you try to run it? minimal repo would be awesome and I'll try to help @kevzettler

kevzettler commented 3 years ago

@Prozi find a minimal repo at https://github.com/kevzettler/pixi-webpack-worker

It is using default pixi but you can easily npm install pixi-shim and drop in.

Prozi commented 3 years ago

@kevzettler thanks for quick reply

so I managed to clone and reproduce the bugs

then I did some tweaking of pixi-shim

and with version 2.3.1 it works:

import PIXI from 'pixi-shim';

self.addEventListener('message', (message) => {
  const application = new PIXI.Application();

  console.log({ application, message });
});

also npm i pixi-shim --save

good luck!

Prozi commented 3 years ago

Closing considering it worked with 2.3.1