javascriptnode-modulesvite

Override `this` to `window` for an import in vite


I'm using an old version of a dependency that uses this to refer to window. It attaches a variable to the window like this: this.variable = variable
Running yarn vite, I get this in the browser:

Uncaught TypeError: Cannot set properties of undefined (setting 'variable')

Is there a way in Vite for this to be set to window so I can use this kind of dependency in my project?


Solution

  • Rollup's moduleContext option can be used here.

    The option build.rollupOptions.moduleContext can be used to set the context to window for a specific module.