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

vite-plugin-meta-env

v1.0.2

Published

a vite plugin, define dynamic env variables in import.meta.env

Downloads

3,240

Readme

vite-plugin-meta-env

English | 简体中文

commitizen Wei Design

define dynamic env variables in import.meta.env

1、Intro

This plugin is used in vite , Expose dynamic or without prefix environment variables in the project

Usage

  • a、dynamicenvironment variables

  • b、without prefixenvironment variables


In vite projects, the environment variable is usually exposed which is starting with envPrefix The default is VITE_,

such as: VITE_API_URL, VITE_APP_NAME and so on.

But sometimes you need to use some dynamic environment variables and variables without prefix such as: APP_VERSION, APP_BUILD_TIME and so on.

This plugin is born to solve this problem.

Here we use the config and define configuration options unique to vite to complete this function

2、Usage

Install

pnpm add vite-plugin-meta-env -D

Config

VitePluginMetaEnv receives two parameters:

/**
 * Use the define option to expose a variable without a prefix
 * @param {EnvVars} Vars environment variable object
 * @param defineOn Variable Definition Location
 */
  • The first parameter: environment variable object, key is the variable name, value is the variable value.
  • The second parameter: variable definition location, optional import.meta.env or process.env
// vite.config.js

import { defineConfig } from 'vite'

// import plugin
import VitePluginMetaEnv from 'vite-plugin-meta-env'

export default () => {
    // environment variables, object structure
    const metaEnv = {
        APP_VERSION: '1.0.0'
    }
    return defineConfig({
        // ...
        plugins: [
            // use plugin
            VitePluginMetaEnv(metaEnv, 'import.meta.env'),
            // VitePluginMetaEnv(metaEnv, 'process.env'),
        ]
    })
}

in the project, you can access the environment variables we defined through import.meta.env.APP_VERSION

demo

preview

3、warning

TypeScript tips

to ensure type checking and code hints, please add type declarations in env.d.ts or vite-env.d.ts

// env.d.ts
/// <reference types="vite/client" />
interface ImportMetaEnv {
    readonly BASE_URL: string // Built-in variable
    readonly MODE: string // Built-in variable
    readonly APP_VERSION: string
    // more...
}

interface ImportMeta {
    readonly env: ImportMetaEnv
}

author