Skip to content

Extending environment variables to get build format during build #6954

@abarke

Description

@abarke

Clear and concise description of the problem

I would like to know what build.lib.format is at build time.

Use case: usage information (we log which build our clients are using at runtime).

This would also be useful for anyone who would like to change some values for different builds e.g. es, cjs and esm.

Suggested solution

Ideally there should be an environment variable set in import.meta.env for the build format e.g.

{ 
  "BASE_URL": "/", 
  "MODE": "production", 
  "DEV": false, 
  "PROD": true, 
  "BUILD": "umd" // <----- Additional environment variable for library format
}

Alternative

Current workaround: Assuming I define process.env.VITE_LIB_TYPE=umd in vite.config.js I can use import.meta.env in the code and at build it will be replaced.

// app.js

function buildType() {
  return import.meta.env.VITE_LIB_TYPE || "empty"
}

// dist/app.js

function buildType() {
  return "umd"
}

However this solution means I need to have two vite.config.js files. One for each build format e.g. es and umd.

Or is there a way to define an array of configs? Or perhaps be able to add custom environment variables for each build type?

Additional context

Solves #5975

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions