build icon indicating copy to clipboard operation
build copied to clipboard

Better handling of already-transpiled TypeScript functions

Open ehmicky opened this issue 4 years ago • 5 comments

When a TypeScript function has been transpiled before zip-it-and-ship-it starts, it will be present in the functions directory both as a .ts and a .js file. The current behavior seems to be:

  • Both the .ts and .js files are being bundled, separately.
  • In production, the .js function is being served and the .ts one is being ignored.
  • listFunctions() and listFunctionsFiles() return both the .ts and .js files.

Instead, we should ignore the .ts file when a sibling file with the same filename but a .js extension exists.

ehmicky avatar Sep 01 '21 15:09 ehmicky

  • Both the .ts and .js files are being bundled, separately.

We keep a list of allowed extensions, sorted by priority (https://github.com/netlify/zip-it-and-ship-it/blob/main/src/runtimes/node/finder.js#L9-L21). When you have foo.js and foo.ts, we only create one foo.zip with the contents of the JavaScript function.

Can you clarify what you mean when you say that both files are bundled? Do you have a repro case?

eduardoboucas avatar Sep 13 '21 10:09 eduardoboucas

This specific commit is an example of a site using both a *.js function and a *.ts function.

Both functions are being bundled:

Packaging Functions from netlify/functions directory:
 - typescript.ts
 - typescript.js

As you mention, although both are being bundled, in production, the *.js file is being served, not the *.ts file.

Finally, when running listFunctions() or listFunctionsFiles(), both files are returned.

ehmicky avatar Sep 14 '21 15:09 ehmicky

We generate that message using listFunctions (see https://github.com/netlify/build/blob/main/packages/build/src/plugins_core/functions/utils.js#L13-L20). So what you're seeing doesn't mean that both functions are being bundled, it just means that we incorrectly list them both as a result of listFunctions's behaviour.

I think we can fix this simply by removing duplicates in the list before printing them. Because we get all functions, we could even go a step further and say that typescript.ts was ignored because it was overridden by typescript.js, like we do with environment variables from different sources in the CLI.

eduardoboucas avatar Sep 14 '21 16:09 eduardoboucas

This makes sense!

ehmicky avatar Sep 14 '21 16:09 ehmicky

Transferred to netlify/build.

eduardoboucas avatar Nov 01 '21 11:11 eduardoboucas

This issue has been automatically marked as stale because it has not had activity in 1 year. It will be closed in 14 days if no further activity occurs. Thanks!

github-actions[bot] avatar Nov 02 '22 14:11 github-actions[bot]

This issue was closed because it had no activity for over 1 year.

github-actions[bot] avatar Nov 17 '22 14:11 github-actions[bot]