Invite-Only Early Access — Think Throo GitHub App is currently invite-only. Request access here.
2025June

ensureArray function in Tsup source code.

In this article, we will review ensureArray function in Tsup source code.

We will look at:

  1. ensureArray function definition.

  2. Where is ensureArray function invoked?

ensureArray function definition

You will find the below code in cli-main.ts in Tsup source code at line 7

function ensureArray(input: string): string[] {
  return Array.isArray(input) ? input : input.split(',')
}

This function returns input as an array of strings. For that, it first checks if the input is an array using Array.isArray method. If that is true, then the input is returned since it is an array otherwise the input is split based on comma.

Where is ensureArray function invoked?

Overall, ensureArray is used in four places in cli-main.ts.

Example 1: 

At line 112, you will find this below code

const format = ensureArray(flags.format) as Format[]
options.format = format

Example 2:

you will find this below code, at line 116

if (flags.external) {
  const external = ensureArray(flags.external)
  options.external = external
}

Example 3:

you will find this below code, at line 137

if (flags.inject) {
  const inject = ensureArray(flags.inject)
  options.inject = inject
}

Example 4:

you will find this below code at line 144

if (flags.loader) {
  const loader = ensureArray(flags.loader)
  options.loader = loader.reduce((result, item) => {
    const parts = item.split('=')
    return {
      ...result,
      [parts[0]]: parts[1],
    }
  }, {})
}

About me:

Hey, my name is Ramu Narasinga. Email: ramu.narasinga@gmail.com

Tired of AI-generated code that works but nobody understands?

I spent 3+ years studying OSS codebases and wrote 350+ articles on what makes them production-grade. I built an open source tool that reviews your PR against your existing codebase patterns.

Your codebase. Your patterns. Enforced.

Get started for free —thinkthroo.com

References:

  1. https://github.com/egoist/tsup/blob/main/src/cli-main.ts#L7

  2. https://github.com/egoist/tsup/blob/main/src/cli-main.ts#L112