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

gulp-env-loader

v1.3.0

Published

A gulp plugin for loading environment variables and replacing them in the contents of files.

Downloads

545

Readme

gulp-env-loader

[ English | 中文 ]

一个用于加载环境变量并替换文件内容中的环境变量的 gulp 插件。
它可以从指定的配置文件中加载环境变量,也可以从默认的 .env 文件中加载环境变量。

它使用 dotenv 从你的 环境目录 中的下列文件加载额外的环境变量,并且它还将静态替换出现在文件内的环境变量。

.env                # 所有情况下都会加载
.env.local          # 所有情况下都会加载,但会被 git 忽略
.env.[mode]         # 只在指定模式下加载
.env.[mode].local   # 只在指定模式下加载,但会被 git 忽略

git忽略 .*.local,还需要在项目的 .gitignore 文件中添加

# local env files
.env.local
.env.*.local

Install

npm install -D gulp-env-loader

Node.js > 14

Usage

  1. 在项目根目录下创建 .env 文件,也可以根据不同的环境创建不同的 .env 文件,比如 .env.development, .env.production 等。
# .env 文件配置
APP_MODE="development"
APP_API_URL="http://test-api.com"
# .env.production 文件配置
APP_MODE="production"
APP_API_URL="https://api.com"
  1. 创建 gulpfile.js 文件
const gulp = require('gulp')
const envInject = require('gulp-env-loader')() //!建议放在前面,并立即执行

// 输出配置的环境变量
console.log('env', envInject.env)

gulp.task('build', function() {
  return gulp.src('./src/*.js', { sourcemaps: true })
    .pipe(envInject())
    .pipe(gulp.dest('./dist', { sourcemaps: '.' }))
})
  1. 运行时可以加上 环境模式参数mode, 这样会自动加载对应的环境变量配置文件。
gulp build --mode=production
  1. 输出结果

源文件 ./src/api.js

export function userLogin(params) {
  return http.post(`${process.env.APP_API_URL}/user/login`, params)
}

输出文件 ./dist/api.js

export function userLogin(params) {
  return http.post(`https://api.com/user/login`, params)
}

API

require('gulp-env-loader')([config])

参数 config

可选的配置对象或配置文件路径。
如果是字符串,则表示配置文件路径。如果是对象,则可以包含以下属性:

  • path - (string) 配置文件路径, 默认为 .env
  • mode - (string) 环境模式名称。
  • modekey - (string) 环境模式键名, 默认为 mode
    • ignoreProcessEnv - (boolean) 是否写入 process.env

返回值

envInject([option])

创建一个 through2 流,用于替换文件内容中的环境变量。

  • isVar - (boolean) 将环境变量替换为对应值的字符串表示 (加单引号)。默认为 true
  • env - (object) 额外的环境变量对象

Thanks