-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix a bug in __require #1604
Fix a bug in __require #1604
Conversation
This reverts commit 1dea2db, which introduced an optimization based on the invalid assumption that the `require` function will always be either already defined when `__require` is defined, or never.
This was an "optimization" that was introduced without any comment on the actual performance gains. Since |
For more context, when SvelteKit is producing a production build to run on a Node environment, it uses esbuild for the final bundling of the server-side app. If any of the bundled dependencies from To get around this, SvelteKit inserts code to define a global The quickest way back to a working state would indeed probably be to revert this optimization in |
#946 appears to be the issue to track the underlying thing that resulted in SvelteKit needing this |
I should also note that the mechanism SvelteKit is using to define these globals has also since changed since my PR I linked above. As of sveltejs/kit#1822 it is using the official 'inject' feature of esbuild, and it is this that is apparently not defining it in a way or in a place that the |
A quick solution would be not generating this require shim when bundling cjs dependencies in esm environment. Currently this shim just act as a side effect. The best solution would be esbuild finding a way to compile cjs code into esm without |
This approach won't work. Just reverting the change will re-break #1579 which was trying to use I suppose a solution that will work for both edge cases might be a |
Closing this in favor of the |
@evanw Hi, thank you for looking into this. Just be curious, is it possible to omit this // a.js, should be esm
import "./b"
// b.js, should be cjs
module.exports = 1
// esbuild a.js --bundle
var __require = ... // ← it is not in use, actually
var require_b = __commonJS({ ... })
var import_b = __toModule(require_b()); I don't know the implementation, but I guess a The very first implementation of Another possible solution would be not generating this shim at all, since you can always get an error when accessing a nonexist variable/function. |
This reverts commit 1dea2db,
which introduced an optimization based on the invalid assumption that the
require
function will always be either already defined when__require
is defined, or never.Fixes sveltejs/kit#2400