-
Notifications
You must be signed in to change notification settings - Fork 35
feat: add support for TypeScript to NFT #1515
Conversation
⏱ Benchmark resultsComparing with f345c7c largeDepsEsbuild: 2.7s⬇️ 25.29% decrease vs. f345c7c
Legend
largeDepsNft: 8.7s⬇️ 28.84% decrease vs. f345c7c
Legend
largeDepsZisi: 16.6s⬇️ 29.44% decrease vs. f345c7c
Legend
|
@@ -109,6 +118,19 @@ const traceFilesAndTranspile = async function ({ | |||
ignore: getIgnoreFunction(config), | |||
readFile: async (path: string) => { | |||
try { | |||
if (extname(path) === '.ts') { |
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.
whats about .mts
or .cts
if working in ESM or commonjs scopes?
Maybe some \.(c|m)?ts$
regex?
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.
👍🏻 I'll do this as a follow-up.
@@ -109,6 +118,19 @@ const traceFilesAndTranspile = async function ({ | |||
ignore: getIgnoreFunction(config), | |||
readFile: async (path: string) => { | |||
try { | |||
if (extname(path) === '.ts') { | |||
const transpiledSource = await transpile({ config, name, path }) |
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.
I assume config is the tsconfig?
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.
No, config
is the config block for the function that comes from both netlify.toml
and the in-source configuration.
return { | ||
aliases: tsAliases, | ||
mainFile: getPathWithExtension(mainFile, '.js'), | ||
moduleFormat: MODULE_FORMAT.ESM, |
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.
is only esm supported? or shouldn't this be read from the tsconfig settings?
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.
We're making the v2 API ESM only, so I think hardcoding that setting here is fine.
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.
Although, it might still be useful for people wanting to use NFT with TypeScript on v1 functions, for some reason. I'll create an issue to do this as a follow-up.
@@ -34,6 +36,8 @@ const bundle: BundleFunction = async ({ | |||
includedFilesBasePath || basePath, | |||
) | |||
const { | |||
aliases, | |||
mainFile: normalizedMainFile, |
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.
can you elaborate on what normalizedMainFile
is? Is it a normalized path to the bundle entry file?
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.
The mainFile
property refers to both the path of a function's entry file, as found in a Netlify project, and to the path of the entry file inside the generated bundle.
Most times this file name is the same, because we find function.js
and generate function.js
. But when transpiling, we need to rewrite function.ts
as function.js
, and that's what normalizedMainFile
refers to.
Although we use it in a bunch of places, I realise it's not a great name, so perhaps we could make that a bit clearer. Do you have any suggestions?
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.
LGTM, just some comments out of curiosity but nothing major or blocking
Summary
Currently, TypeScript files are only supported when the bundler is set to esbuild. This PR adds support for TypeScript when using NFT as the bundler.
With that, we also change the upcoming functions API v2 to use NFT by default, instead of esbuild (2779f30).