extism / js-pdk

Write Extism plugins in JavaScript & TypeScript
52 stars 16 forks source link

Change prelude format to iife #75

Closed Phault closed 4 months ago

Phault commented 4 months ago

I ran into a headscratcher where my plugin worked without esbuild minification, but not when it was enabled. It would crash hard complaining something wasn't a function and sometimes it wasn't a constructor instead.

After some hours of debugging, it turns out the minification would cause a minified runtime global variable to be redeclared, so when the runtime tried to use its variable it would explode. Note while I say global, I mean all the variables meant to be locally scoped as well, as when the format is cjs, esbuild assume the code will run in its own scope so it won't isolate top-level variables.

It isn't guaranteed to always cause issues, as usually shadowing a variable isn't a problem, but I'm guessing function hoisting complicated it in my case.

There should be no downside to this change, as it has no exports anyway and all the intentional globals are declared by modifying globalThis already.


Oh and disabled the sourcemap too, because it's completely unused anyway.