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

feat: add pure ESM support to esbuild and default bundlers #1018

Merged
merged 6 commits into from
Feb 15, 2022

Conversation

eduardoboucas
Copy link
Member

Summary

This is a follow-up to #1004, adding support for pure ESM functions to:

  • The default bundler, using NFT to trace dependencies when an ESM file with a .js extension is found (a468380)
  • esbuild (8e0c725)

@eduardoboucas eduardoboucas added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Feb 14, 2022
parseWithEsbuild: false,
traceWithNft: false,
zisi_detect_esm: false,
Copy link
Member Author

Choose a reason for hiding this comment

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

This determines whether we'll look at a .js file and figure out whether it's CJS or ESM.

@@ -84,11 +87,21 @@ export const bundleJsFile = async function ({
// URLs, not paths, so even on Windows they should use forward slashes.
const sourceRoot = targetDirectory.replace(/\\/g, '/')

// Configuring the output format of esbuild. The `includedFiles` array we get
Copy link
Member Author

Choose a reason for hiding this comment

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

We were not including a package.json when bundling with esbuild. This becomes a problem with ESM functions, since an ESM file and a lack of a package.json with {"type": "module"} would lead to a CJS function with ESM syntax.

@Skn0tt
Copy link
Member

Skn0tt commented Feb 15, 2022

added a minor refactoring with TypeScript syntax. was hard to capture in a GitHub suggestion, so committed instead - let me know if you disagree :) 0e95b0f

featureFlags: FeatureFlags,
configVersion?: string,
): Promise<{ includedFiles: string[]; moduleFormat: ModuleFormat }> => {
const packageJsonFile = await getClosestPackageJson(srcDir)
Copy link
Member

@Skn0tt Skn0tt Feb 15, 2022

Choose a reason for hiding this comment

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

I'm not sure why we introduce + use getClosestPackageJson. srcDir would be smth like projectDir/netlify/functions/my-function.ts, right? The difference between getClosesPackageJson and getPackageJson is that one has an optimisation to not read further than the nearest node_modules folder, which would only ever apply if we searched the package.json for a NPM dependency. This isn't the case here - what am I missing?

Copy link
Member Author

Choose a reason for hiding this comment

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

getClosestPackageJson returns the contents of the package.json and its path, whereas getPackageJson returns just the contents. I agree that we should consolidate these, but I'd prefer to do that as a separate PR to reduce the amount of changes. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

oh, somehow missed this reply. agree! I'll work on that.

@eduardoboucas
Copy link
Member Author

added a minor refactoring with TypeScript syntax. was hard to capture in a GitHub suggestion, so committed instead - let me know if you disagree :) 0e95b0f

I initially had that and was torn on which one was more readable. If you prefer that approach, I'm more than happy to use it!

@kodiakhq kodiakhq bot merged commit 809b206 into main Feb 15, 2022
@kodiakhq kodiakhq bot deleted the feat/esm-functions branch February 15, 2022 14:30
Skn0tt added a commit to netlify/build that referenced this pull request May 21, 2024
…ip-it-and-ship-it#1018)

* feat: add ESM support to default bundler

* feat: add ESM support to esbuild

* chore: fix tests

* refactor: make return type explicit in signature

(let's get rid of those "as" conversions)

Co-authored-by: Simon Knott <info@simonknott.de>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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

3 participants