New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

gulp-env-loader

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gulp-env-loader

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

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
177
increased by12.74%
Maintainers
1
Weekly downloads
 
Created
Source

gulp-env-loader

[ English | 中文 ]

A gulp plugin for loading environment variables and replacing them in file contents.
It can load environment variables from a specified configuration file or from the default .env file.

It uses dotenv to load additional environment variables from the following files in your environment directory, and it also statically replaces environment variables that appear in the file.

.env                # loaded in all cases
.env.local          # loaded in all cases, but ignored by git
.env.[mode]         # only loaded in specified mode
.env.[mode].local   # only loaded in specified mode, but ignored by git

ignores .*.local, so you also need to add it to your project's .gitignore file:

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

Install

npm install -D gulp-env-loader

Node.js > 12

Usage

  1. Create a .env file in the root directory of your project, or create different .env files for different environments, such as .env.development, .env.production, etc.
# .env configuration
APP_MODE="development"
APP_API_URL="http://test-api.com"
# .env.production configuration
APP_MODE="production"
APP_API_URL="https://api.com"
  1. Create gulpfile.js
const gulp = require('gulp')
const envInject = require('gulp-env-loader')() //!recommended to put at the beginning and execute immediately

// Output the configured environment variables
console.log('env', envInject.env)

gulp.task('build', function() {
  return gulp.src('./src/*.js', { sourcemaps: true })
    .pipe(envInject())
    .pipe(gulp.dest('./dist', { sourcemaps: '.' }))
})
  1. You can add the runtime parameter mode at runtime, which will automatically load the corresponding environment variable configuration file.
gulp build --mode=production
  1. Output results

Source file: ./src/api.js

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

Output file: ./dist/api.js

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

API

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

config

An optional configuration object or configuration file path.
If it is a string, it represents the configuration file path. If it is an object, it can contain the following properties:

  • path - (string) Configuration file path, default is .env
  • mode - (string) Environment mode name.
  • modekey - (string) Environment mode key name, default is mode
  • ignoreProcessEnv - (boolean) Turn off writing to process.env

Return value

envInject([option])

Creates a through2 stream for replacing environment variables in file contents.

  • isVar - (boolean) Replaces environment variables with their corresponding string representations (Single quotation marks). Default is true.
  • env - (object) Additional Environment Variables.

Thanks

Keywords

FAQs

Package last updated on 26 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc