-
Notifications
You must be signed in to change notification settings - Fork 35
fix: make esbuild work with ESM and require calls #1257
Conversation
⏱ Benchmark resultsComparing with 5a7562a largeDepsEsbuild: 2.2s⬇️ 2.03% decrease vs. 5a7562a
LegendlargeDepsNft: 10.2s⬇️ 7.59% decrease vs. 5a7562a
LegendlargeDepsZisi: 15.3s⬇️ 5.72% decrease vs. 5a7562a
|
45fe0a8
to
c596234
Compare
import { createRequire as ___nfyCreateRequire } from 'module'; | ||
import { fileURLToPath as ___nfyFileURLToPath } from "url"; | ||
import { dirname as ___nfyPathDirname } from "path"; | ||
let __filename = ___nfyFileURLToPath(import.meta.url); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It just occurred to me that if the user code tries to declare these variables, we'll get an error. This only happens if the declaration happens in the same scope, though. Have you checked a sample bundle to see if this situation could happen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Esbuild is thankfully clever enough to rename variables
import renderNextPage from './renderNextPage'
const __dirname = 1
export const handler = async (event, context, callback) => {
...
turns into
var import_renderNextPage = __toESM(require_renderNextPage(), 1);
var __dirname2 = 1;
var handler = async (event, context, callback) => {
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But nevertheless i will put it behind FF
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨
Summary
This adds a banner to esbuild if the output format is set to ESM. This banner makes require() calls work again.
This can happen when ezbuild produces ESM and some of the ESM modules import CJS modules which use
require
.For us to review and ship your PR efficiently, please perform the following steps:
This ensures we can discuss the changes and get feedback from everyone that should be involved. If you`re fixing
a typo or something that`s on fire 🔥 (e.g. incident related), you can skip this step.
passes our tests.
A picture of a cute animal (not mandatory, but encouraged)