Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

fix: make esbuild work with ESM and require calls #1257

Merged
merged 3 commits into from
Nov 4, 2022
Merged

fix: make esbuild work with ESM and require calls #1257

merged 3 commits into from
Nov 4, 2022

Conversation

danez
Copy link
Contributor

@danez danez commented Nov 3, 2022

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:

  • Open a bug/issue before writing your code 🧑‍💻.
    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.
  • Read the contribution guidelines 📖. This ensures your code follows our style guide and
    passes our tests.
  • Update or add tests (if any source code was changed or added) 🧪
  • Update or add documentation (if features were changed or added) 📝
  • Make sure the status checks below are successful ✅

A picture of a cute animal (not mandatory, but encouraged)

@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2022

⏱ Benchmark results

Comparing with 5a7562a

largeDepsEsbuild: 2.2s

⬇️ 2.03% decrease vs. 5a7562a

^           2.6s                                          
│           ┌──┐            2.5s                          
│           |  |            ┌──┐                          
│ ──────────┼──┼────2.1s────┼──┼────2.1s────2.2s────2.2s──
│   2.1s    |  |    ┌──┐    |  |    ┌──┐    ┌──┐    ┌──┐  
│   ┌──┐    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

largeDepsNft: 10.2s

⬇️ 7.59% decrease vs. 5a7562a

^          14.2s                                          
│           ┌──┐                                          
│           |  |           12.4s                          
│           |  |            ┌──┐                          
│           |  |            |  |            11s           
│ ──────────┼──┼────────────┼──┼────────────┌──┐──────────
│           |  |    9.8s    |  |   10.1s    |  |   10.2s  
│   9.4s    |  |    ┌──┐    |  |    ┌──┐    |  |    ┌──┐  
│   ┌──┐    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

largeDepsZisi: 15.3s

⬇️ 5.72% decrease vs. 5a7562a

^          19.7s                                          
│           ┌──┐           18.6s                          
│           |  |            ┌──┐                          
│           |  |            |  |                          
│ ──────────┼──┼────────────┼──┼───────────16.1s───15.3s──
│  14.5s    |  |   15.1s    |  |   15.1s    ┌──┐    ┌──┐  
│   ┌──┐    |  |    ┌──┐    |  |    ┌──┐    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

@danez danez force-pushed the esm-esbuild branch 2 times, most recently from 45fe0a8 to c596234 Compare November 3, 2022 13:55
tests/main.test.ts Outdated Show resolved Hide resolved
tests/main.test.ts Outdated Show resolved Hide resolved
import { createRequire as ___nfyCreateRequire } from 'module';
import { fileURLToPath as ___nfyFileURLToPath } from "url";
import { dirname as ___nfyPathDirname } from "path";
let __filename = ___nfyFileURLToPath(import.meta.url);
Copy link
Member

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?

Copy link
Contributor Author

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) => {
...

Copy link
Contributor Author

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

@danez danez self-assigned this Nov 3, 2022
Copy link
Member

@eduardoboucas eduardoboucas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants