Skip to content
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

feat: add ESM support when bundling with NFT #759

Merged
merged 5 commits into from Oct 22, 2021
Merged

feat: add ESM support when bundling with NFT #759

merged 5 commits into from Oct 22, 2021

Conversation

eduardoboucas
Copy link
Member

@eduardoboucas eduardoboucas commented Oct 21, 2021

- Summary

Adds support for ESM functions when bundling with NFT. We look for ESM files in the NFT cache and, for each one that is found, we use esbuild to transpile them down to CJS. We then replace the original files with the transpiled ones in the archive.

- Test plan

Included the NFT variation in 3 tests.

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

0cd8d5da58bb8d2f482eec89f968ebe6

@eduardoboucas eduardoboucas added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Oct 21, 2021
Copy link
Contributor

@netlify-team-account-1 netlify-team-account-1 left a comment

Choose a reason for hiding this comment

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

approving since tests are green, and it looks fine. One thing I don't understand, maybe you can give some insight:

Once ESM files are transpiled, what happens to them? I understand that each ESM file gets a CJS equivalent at some tmp destination. I'd assume that these tmp files are used to replace the ESM ones at some point, but I can't find where that happens 🤔

src/runtimes/node/bundlers/nft/types.ts Outdated Show resolved Hide resolved
@eduardoboucas
Copy link
Member Author

Once ESM files are transpiled, what happens to them? I understand that each ESM file gets a CJS equivalent at some tmp destination. I'd assume that these tmp files are used to replace the ESM ones at some point, but I can't find where that happens 🤔

You're absolutely correct. I think the part you're missing happens in a Map called aliases (which, as most things, could have a better name), which maps paths that should be in the archive to the paths they should take inside the zip.

Example:

{
  "/tmp/function-1-transpiled.js" => "/netlify/functions/function-1.js"
}

With this aliases object, when we find /tmp/function-1-transpiled.js in the list of files to include, we rename it to /netlify/functions/function-1.js, which is the expected path of the original, untranspiled file.

Each bundler is responsible for creating this aliases file, which is then used at the archive creation time.

Does that help?

@netlify-team-account-1
Copy link
Contributor

yes, that helped! thank you :)

@kodiakhq kodiakhq bot merged commit b63ff7e into main Oct 22, 2021
@kodiakhq kodiakhq bot deleted the feat/nft-esm branch October 22, 2021 09:08
@github-actions
Copy link
Contributor

⏱ Benchmark results

largeDepsEsbuild: 8.4s

^   8.4s  
│   ┌──┐  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
└───┴──┴──>
     T    
Legend
  • T (current commit): 8.4s

largeDepsNft: 53.8s

^  53.8s  
│   ┌──┐  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
└───┴──┴──>
     T    
Legend
  • T (current commit): 53.8s

largeDepsZisi: 1m 7.5s

^ 1m 7.5s 
│   ┌──┐  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
│   |▒▒|  
└───┴──┴──>
     T    
Legend
  • T (current commit): 1m 7.5s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants