npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

plugin-vite-check-env

v1.0.7

Published

A Vite plugin for checking and validating environment variables in your Vite project.

Downloads

9

Readme

plugin-vite-check-env

A Vite plugin for checking and validating environment variables in your Vite project.

Installation

To install the plugin, use npm or yarn:

npm install plugin-vite-check-env --save-dev

or

yarn add plugin-vite-check-env --dev

Usage

In your vite.config.js or vite.config.ts, import and configure the plugin:

import { defineConfig } from 'vite';
import CheckEnv from 'plugin-vite-check-env';

export default defineConfig({
  plugins: [
    // ...other plugins
    CheckEnv({
      VITE_DOMAIN_API: undefined, // Required variable with no default value
      VITE_TIMEOUT_REDIRECT: 1000, // Optional variable with a default value
      PORT: { type: 'custom', value: 3000 }, // Custom environment variable with default value
      HOST: { type: 'custom', required: true }, // Required custom variable
    }),
  ],
});

Options Format

You can define environment variables in two ways:

  • As a simple key-value pair:
    • If a variable is marked as undefined, it is considered required but without a default value.
    • If a value is provided, it will be used as the default if the variable is not defined.
  • Using an object description: You can define environment variables using a more detailed object structure if needed:
export interface IOptions {
  [index: string]: {
    type: 'custom' | 'vite', // Where the plugin should look for the variable (process.env or import.meta.env)
    value?: string, // Optional default value if the variable is not set
    required?: boolean, // If true, the variable must be defined
  }
}

Example

Simple Definition

CheckEbv({
  VITE_DOMAIN_API: undefined, // This variable is required with no default value
  VITE_TIMEOUT_REDIRECT: 1000, // Optional variable with a default of 1000
});

Object Definition

CheckEbv({
  PORT: { type: 'custom', value: '3000' }, // Custom variable with a default value
  HOST: { type: 'custom', required: true }, // Custom variable that is required
});

Explanation

  • type: Specifies where the plugin should retrieve the environment variable from.
    • vite: Looks for the variable in import.meta.env.
    • custom: Looks for the variable in process.env (you might load these with tools like dotenv).
  • value: Provides a default value for the variable if it is not set. Note: This cannot be used together with required.
  • required: Ensures that the environment variable is defined. If it's not, the plugin will throw an exception. Note: This cannot be used together with value.

Handling Missing Variables

If a required environment variable is not found, the plugin will throw an error and stop the build process. This ensures that the necessary environment variables are always present when running your Vite project.

Use Cases

  • Development: Ensure that critical environment variables like API URLs or authentication tokens are present and configured correctly.
  • Production: Set up default values for non-critical variables, while ensuring that required variables are always set.

License

ISC