egoist / tsup

The simplest and fastest way to bundle your TypeScript libraries.
https://tsup.egoist.dev
MIT License
9.01k stars 217 forks source link

Embed Environment Variables into the build #1030

Closed alexandredev3 closed 6 months ago

alexandredev3 commented 11 months ago

Currently, the only way to tsup statically embed environment variables into the build is by adding them in the config file or using the --env cli flag:

import { defineConfig } from 'tsup'

export default defineConfig({
  entry: ['src/index.ts'],
  env: {
    SUPABASE_PUBLIC_KEY: "blahblah"
  }
})
$ tsup src/index.ts --env.SUPABASE_PUBLIC_KEY blahblah

However, this process can become cumbersome, especially when dealing with multiple environment variables. What if we could simplify it by adding support for .env files similar to vitejs? This would allow users to simply pass the path of their .env file:

import { defineConfig } from 'tsup'

export default defineConfig({
  entry: ['src/index.ts'],
  env: ".env" // .env.local, .env.development, .env.production ...
})

or

$ tsup src/index.ts --env .env

Upvote & Fund

Fund with Polar

branchard commented 7 months ago

You can just do it by hand:

import {defineConfig} from 'tsup';
import dotenv from 'dotenv';

export default defineConfig({
    entry: ['src/index.ts'],
    env: dotenv.config().parsed,
});
alexandredev3 commented 6 months ago

You can just do it by hand:

import {defineConfig} from 'tsup';
import dotenv from 'dotenv';

export default defineConfig({
    entry: ['src/index.ts'],
    env: dotenv.config().parsed,
});

Yep, I ended up doing it that way

  import { defineConfig } from 'tsup';
  import "@dotenvx/dotenvx";

  export default defineConfig({
    clean: true,
    target: 'es2020',
    dts: true,
    env: {
      SUPABASE_CLIENT_URL: process.env.SUPABASE_CLIENT_URL!,
      SUPABASE_CLIENT_PUBLIC_KEY: process.env.SUPABASE_CLIENT_PUBLIC_KEY!,
    },
    format: [
      'cjs',
      'esm'
    ]
  });
$ npm run dotenvx run --env-file=.env.local -- tsup ./**/*.ts --watch

Thank you though! 🚀