vite-plugin-node-polyfills icon indicating copy to clipboard operation
vite-plugin-node-polyfills copied to clipboard

define globalThis if not defined

Open artursapek opened this issue 1 year ago • 4 comments
trafficstars

I'm using this plugin in a project that started throwing errors in some sub-dependency because globalThis wasn't defined yet when this code ran. This one-line change resolves those errors. :-)

artursapek avatar Apr 17 '24 17:04 artursapek

yo @davidmyersdev any feedback?

artursapek avatar May 03 '24 15:05 artursapek

Hey @artursapek. 👋 I just ran the tests, and it looks like some are failing and need to be updated. Would you mind updating those? I can merge once CI is passing.

davidmyersdev avatar May 09 '24 22:05 davidmyersdev

Screenshot 2024-05-22 at 4 57 15 PM

This only happens in HMR mode on vite dev. In addition to globalThis error @artursapek mentioned, I also get this error after setting globalThis to {}.

baoskee avatar May 22 '24 23:05 baoskee

@artursapek you need to update the test files to pass the tests;

Test files:


Add this line for all tests

globalThis = globalThis || window

Your branch

expect(result?.code).toEqual(formatWhitespace(`
  import __buffer_polyfill from "/shims/buffer/dist/index.js"
  import __global_polyfill from "/shims/global/dist/index.js"
  import __process_polyfill from "/shims/process/dist/index.js"
  globalThis = globalThis || window
  globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
  globalThis.global = globalThis.global || __global_polyfill
  globalThis.process = globalThis.process || __process_polyfill

  Buffer.from("test");
`))

@artursapek also, please update your branch, as there are already many changes in the tests in the main branch that may affect the final result.


Main branch

unixhelloworld avatar Jul 03 '24 08:07 unixhelloworld