Repository: netlify/zip-it-and-ship-it (js)

Issue:
Error occurs when plugin uses listAll to get list of modules from typescript file
<!--
If you are reporting a new issue, make sure that we do not have any duplicates
already open. You can ensure this by searching the issue list for this
repository. If there is a duplicate, please close your issue and add a comment
to the existing issue instead.

If you suspect your issue is a bug, please edit your issue description to
include the BUG REPORT INFORMATION shown below. If you fail to provide this
information within 7 days, we cannot debug your issue and will close it. We
will, however, reopen it if you later provide the information.

---------------------------------------------------
BUG REPORT INFORMATION
---------------------------------------------------
Use the commands below to provide key information from your environment:
You do NOT have to include this information if this is a FEATURE REQUEST
-->

**- Do you want to request a _feature_ or report a _bug_?**
Bug

**- What is the current behavior?**
Error: Could not list Netlify Functions files

I am using  the `netlify-plugin-inline-functions-env` plugin which [calls `functions.listAll`](https://github.com/bencao/netlify-plugin-inline-functions-env/blob/c3866ce029bb93b35ed3000e554be8c85be8892a/index.js#L36) that's passed in by the netlify builder. The function call produces the above-mentioned error when a `ts` function file is importing another `ts` file.
```
import example from '../utils/example'; // importing from `../utils/example.ts` produces the error
```

I did some code tracing and believe this is the offending line: https://github.com/netlify/zip-it-and-ship-it/blob/57e57ab3397b6e4eeac5f787537dfdfdf31c3c72/src/runtimes/node/src_files.js#L47
(If I comment out that line the build succeeds locally)
Not quite sure why it is defaulting to the legacy bundler, I believe the default bundler is supposed to be esBuild now?

**- If the current behavior is a bug, please provide the steps to reproduce.**
1. Add `netlify-plugin-inline-functions-env` plugin for netlify builds
2.  Write a function in `.ts` e.g. `main.ts`
3. Write another helper module e.g. `../utils/example.ts`
4. Import module within `main.ts` e.g. `import example from '../utils/example.ts'`
5. run `netlify build`

This will trigger an error during the inlining because it uses the `listAll` function injected from within this repo.

**- What is the expected behavior?**
Build successfully completes.

**- Please mention your node.js, and operating system version.**
node v12.16.1
MacOS v11.3.1

Pull request:
fix: add default bundler to listFunctions and listFunctionsFiles calls
Ensures that `listFunctions()` and `listFunctionsFiles()` use the right default bundler based on the extension — e.g. TypeScript files will use esbuild even if not explicitly set in the config. This is in line with the `zipFunction()` and `zipFunctions()` methods.

Closes #512.

Patch:
diff --git a/src/main.js b/src/main.js
index 71db227..95e2ead 100644
--- a/src/main.js
+++ b/src/main.js
@@ -53,7 +53,17 @@ const getListedFunctionFiles = async function (
   return srcFiles.map((srcFile) => ({ srcFile, name, mainFile, runtime: runtime.name, extension: extname(srcFile) }))
 }
 
-const getSrcFiles = function ({ config, runtime, stat, mainFile, extension, srcPath, srcDir, pluginsModulesPath }) {
+const getSrcFiles = function ({
+  bundler,
+  config,
+  runtime,
+  stat,
+  mainFile,
+  extension,
+  srcPath,
+  srcDir,
+  pluginsModulesPath,
+}) {
   const { getSrcFiles: getRuntimeSrcFiles } = runtime
 
   if (extension === '.zip' || typeof getRuntimeSrcFiles !== 'function') {
@@ -61,6 +71,7 @@ const getSrcFiles = function ({ config, runtime, stat, mainFile, extension, srcP
   }
 
   return getRuntimeSrcFiles({
+    bundler,
     config,
     extension,
     srcPath,
diff --git a/src/runtimes/node/index.js b/src/runtimes/node/index.js
index 8aa0cc7..547f04e 100644
--- a/src/runtimes/node/index.js
+++ b/src/runtimes/node/index.js
@@ -13,6 +13,14 @@ const { zipZisi } = require('./zip_zisi')
 // exception of TypeScript files, for which the only option is esbuild.
 const getDefaultBundler = ({ extension }) => (extension === '.ts' ? JS_BUNDLER_ESBUILD : JS_BUNDLER_ZISI)
 
+// A proxy for the `getSrcFiles` function which adds a default `bundler` using
+// the `getDefaultBundler` function.
+const getSrcFilesWithBundler = (parameters) => {
+  const bundler = parameters.config.nodeBundler || getDefaultBundler({ extension: parameters.extension })
+
+  return getSrcFiles({ ...parameters, bundler })
+}
+
 const zipFunction = async function ({
   archiveFormat,
   basePath,
@@ -90,4 +98,9 @@ const zipWithFunctionWithFallback = async ({ config = {}, ...parameters }) => {
   }
 }
 
-module.exports = { findFunctionsInPaths, getSrcFiles, name: RUNTIME_JS, zipFunction: zipWithFunctionWithFallback }
+module.exports = {
+  findFunctionsInPaths,
+  getSrcFiles: getSrcFilesWithBundler,
+  name: RUNTIME_JS,
+  zipFunction: zipWithFunctionWithFallback,
+}
diff --git a/src/runtimes/node/src_files.js b/src/runtimes/node/src_files.js
index 2c43375..be259b3 100644
--- a/src/runtimes/node/src_files.js
+++ b/src/runtimes/node/src_files.js
@@ -44,7 +44,6 @@ const getPathsOfIncludedFiles = async (includedFiles, basePath) => {
 const getSrcFiles = async function ({ config, ...parameters }) {
   const { paths } = await getSrcFilesAndExternalModules({
     ...parameters,
-    bundler: config.nodeBundler || JS_BUNDLER_ZISI,
     externalNodeModules: config.externalNodeModules,
   })
 


Test patch:
diff --git a/tests/fixtures/list/five/index.ts b/tests/fixtures/list/five/index.ts
new file mode 100644
index 0000000..85df844
--- /dev/null
+++ b/tests/fixtures/list/five/index.ts
@@ -0,0 +1,5 @@
+import { type } from './util'
+
+const obj = { type }
+
+export default obj
diff --git a/tests/fixtures/list/five/util.ts b/tests/fixtures/list/five/util.ts
new file mode 100644
index 0000000..f271961
--- /dev/null
+++ b/tests/fixtures/list/five/util.ts
@@ -0,0 +1,5 @@
+type MyType = string
+
+const type: MyType = '❤️ TypeScript'
+
+export { type }
diff --git a/tests/main.js b/tests/main.js
index 600df40..7857492 100644
--- a/tests/main.js
+++ b/tests/main.js
@@ -515,6 +515,7 @@ test('Can list function main files with listFunctions()', async (t) => {
     [
       { name: 'test', mainFile: 'test.zip', runtime: 'js', extension: '.zip' },
       { name: 'test', mainFile: 'test.js', runtime: 'js', extension: '.js' },
+      { name: 'five', mainFile: 'five/index.ts', runtime: 'js', extension: '.ts' },
       { name: 'four', mainFile: 'four.js/four.js.js', runtime: 'js', extension: '.js' },
       { name: 'one', mainFile: 'one/index.js', runtime: 'js', extension: '.js' },
       { name: 'two', mainFile: 'two/two.js', runtime: 'js', extension: '.js' },
@@ -534,6 +535,7 @@ testBundlers(
       [
         { name: 'test', mainFile: 'test.zip', runtime: 'js', extension: '.zip', srcFile: 'test.zip' },
         { name: 'test', mainFile: 'test.js', runtime: 'js', extension: '.js', srcFile: 'test.js' },
+        { name: 'five', mainFile: 'five/index.ts', runtime: 'js', extension: '.ts', srcFile: 'five/index.ts' },
         {
           name: 'four',
           mainFile: 'four.js/four.js.js',