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
Conversation
@@ -122,6 +122,7 @@ const bundle: BundleFunction = async ({ | |||
bundlerWarnings, | |||
inputs, | |||
mainFile: normalizedMainFile, | |||
moduleFormat: 'cjs', |
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.
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 ({ |
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.
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', |
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.
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 ({ |
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.
Moved unchanged from src/runtimes/node/utils/zip.ts
.
}): Promise<NodeBundlerName> => { | ||
const { defaultEsModulesToEsbuild, traceWithNft } = featureFlags | ||
|
||
if (['.mjs', '.ts'].includes(extension)) { |
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.
Note for a future PR: maybe we should support *.mts
too.
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.
Definitely!
|
||
t.true(functionsAreESM.every(Boolean)) | ||
}, | ||
) |
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.
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]) |
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.
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.
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.
Thank you! Done in 7156614.
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.
Looks good! Except a small issue with some tests on Windows.
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:
nodeVersion
configuration property) is greater than or equal to Node 14zisi_pure_esm
feature flag is enabledIn any other scenario, zip-it-and-ship-it continues to transpile ESM and generate CJS functions.