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

@signalchain/rollup-plugin-env-gen

v0.6.0

Published

Config based generation of a `.env` and public variables file. Manage your environment secrets and public application variables from a single JavaScript file.

Downloads

45

Readme

@signalchain/rollup-plugin-env-gen

Config based generation of a .env and public variables file. Manage your environment secrets and public application variables from a single JavaScript file.

Works with Rollup and Vite.

Install

npm i -D @signalchain/rollup-plugin-env-gen

Sample Env

Optionally generate a modified version of the source file and produce a .sample file. (This is the default, but can be turned off by setting samplePath to false.) The sample file is identical to the input file with the exception that variables and secrets are converted to the zero value of their type.

For example:

  • "Hello world!" ➡ ""
  • 12345 ➡ 0
  • true ➡ false

Public Vars

Public variables, such as those used in client side applications, will be generated only if the exported object has a publicVars field. There is a clear boundary at build time between publicVars and all other fields. That said, if you import secrets into client side code, they are visible to anyone who wants to poke at the source. So only include variables that are meant to be public in the publicVars object.

Runtime Template Variables

For runtime template strings in Public Env, wrap your template string like this ➡ "%YOUR_TEMPLATE_STRING%". Wrapping is required to keep the template string from being evaluated at build time. This feature only makes sense for public vars, so any template string outside of the publicVars object will be evaluated at build time.

Use

export type Options = {
	mode?: string // process.env.NODE_ENV
	inputPath?: string // [PROJECT_ROOT]/.env.js
	envPath?: string // [PROJECT_ROOT]/.env
	samplePath?: string | boolean // [PROJECT_ROOT]/.env.sample.js
	publicPath?: string // [PROJECT_ROOT]/src/publicVars.js
	watch?: boolean // true (WIP)
}

// rollup.config.js || vite.config.js
plugins: [envGen(Options)]

The inputPath defaults to [PROJECT_ROOT]/.env.js. That file exports a default object with a set of keys that will be used to match the mode option on build. If the publicVars key is in the exported object from the inputPath, it will generate a publicVars.js file at the publicPath. (Defaults to [PROJECT_ROOT]/src/publicVars.js.)

Example

// .env.js

const shared = {
	BOTH: true,
}

const development = {
	...shared,
	TEST: true,
}

const production = {
	...shared,
	TEST: false,
}

const sharedPublic = {
	PUBLIC: true,
}

const publicVars = {
	development: {
		...sharedPublic,
		TEMPLATED_TEMPLATE: "%`${window.location.host}`%",
	},
	production: {
		...sharedPublic,
	},
}

export default {
	development,
	production,
	publicVars,
}

.gitignore

If you don't want to commit your secrets to git, add the following to your .gitignore

# environment variables files
.env
.env*
!.env.sample.js

This says, "don't commit .env or .env<any other text>, but do commit .env.sample.js"