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: generate pure ESM functions for NFT #1004

Merged
merged 6 commits into from Feb 8, 2022
Merged

Conversation

eduardoboucas
Copy link
Member

@eduardoboucas eduardoboucas commented Feb 7, 2022

Summary

As of last month, AWS Lambda supports native ESM functions. Currently, we're unable to take advantage of this functionality since zip-it-and-ship-it always generates CJS functions.

This PR starts changing that by generating pure ESM functions when all of the following conditions are met:

  • The bundler is NFT
  • The Node.js version (supplied via the nodeVersion configuration property) is greater than or equal to Node 14
  • The zisi_pure_esm feature flag is enabled

In any other scenario, zip-it-and-ship-it continues to transpile ESM and generate CJS functions.

@eduardoboucas eduardoboucas added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Feb 7, 2022
@@ -122,6 +122,7 @@ const bundle: BundleFunction = async ({
bundlerWarnings,
inputs,
mainFile: normalizedMainFile,
moduleFormat: 'cjs',
Copy link
Member Author

Choose a reason for hiding this comment

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

To be addressed in a follow-up PR.

export { getBundler }
// We use ZISI as the default bundler, except for certain extensions, for which
// esbuild is the only option.
const getDefaultBundler = async ({
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 function was moved from src/runtimes/node/index.ts. The code was not changed.

@@ -42,6 +42,7 @@ const bundle: BundleFunction = async ({
basePath: getBasePath(dirnames),
inputs: srcFiles,
mainFile,
moduleFormat: 'cjs',
Copy link
Member Author

Choose a reason for hiding this comment

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

To be addressed in a follow-up PR.

// `adm-zip` and `require()` expect Unix paths.
// We remove the common path prefix.
// With files on different Windows drives, we remove the drive letter.
const normalizeFilePath = function ({
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved unchanged from src/runtimes/node/utils/zip.ts.

@ehmicky ehmicky self-requested a review February 7, 2022 18:52
}): Promise<NodeBundlerName> => {
const { defaultEsModulesToEsbuild, traceWithNft } = featureFlags

if (['.mjs', '.ts'].includes(extension)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Note for a future PR: maybe we should support *.mts too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Definitely!


t.true(functionsAreESM.every(Boolean))
},
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice tests!

tests/main.js Outdated
await unzipFiles(files, (path) => `${path}/../${basename(path)}_out`)

const functionPaths = [join(tmpDir, 'func1.zip_out', 'func1.js'), join(tmpDir, 'func2.zip_out', 'func2.js')]
const func1 = await import(functionPaths[0])
Copy link
Contributor

@ehmicky ehmicky Feb 7, 2022

Choose a reason for hiding this comment

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

pathToFileURL() must be used.

Maybe you can cherry pick the following test helper from another pending PR? https://github.com/netlify/zip-it-and-ship-it/pull/980/files#diff-91254b3a8e85bba2e8bad93724729fcd3531241f5b57196acc1e49db3e856a49R113

This is because import() argument is a URI, not a file path (unlike require()).
On Windows, absolute file paths like C:\... are not valid URLs. Also, URIs are percent-encoded, so this would also fail for file paths with characters which would normally be percent-encoded.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you! Done in 7156614.

tests/main.js Outdated Show resolved Hide resolved
Copy link
Contributor

@ehmicky ehmicky left a comment

Choose a reason for hiding this comment

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

Looks good! Except a small issue with some tests on Windows.

@kodiakhq kodiakhq bot merged commit cac2058 into main Feb 8, 2022
@kodiakhq kodiakhq bot deleted the feat/esm-entrypoint branch February 8, 2022 09:35
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