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

grunt-envpreprocess

v0.1.4

Published

A task for replacing environment-specific variables in a specified file or directory. Environment variables are specified in an external .json file.

Downloads

284

Readme

envpreprocess

preprocess environment variables

Getting Started

This plugin requires Grunt ~0.4.5

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-envpreprocess --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTask('grunt-envpreprocess');

The grunt-envpreprocess task

Text search and replacement in files is powered by the core of grunt-text-replace.

Overview

In your project's Gruntfile, add a section named envpreprocess to the data object passed into grunt.initConfig().

grunt.initConfig({
   envpreprocess:{
        dev:{
            files:{
                src:  'config/env.json'
            },
            options:{
                replacePath: ['dev_build/**/*.*'],
                environment: 'dev'
            }
        },
        prod:{
            files:{
                src:  'config/env.json'
            },
            options:{
                replacePath: ['dist_build/**/*.*'],
                environment: 'prod'
            }
        }
    }
});

(note that if you don't specify dev.options.environment, it will use the target, which in that case is dev)

When the task is run, you'll see a log output like this

Running "envpreprocess:prod" (envpreprocess) task
Reading ENV variables from config/env.json
Replaced all ENV variables in prod_build/**/*.*

Options

options.replacePath

Type: String

The path of files to have ENV variables replaced with the preprocessor.

options.environment

Type: String Default value: dev

Which environment to use when retrieving ENV variables from the .json file. If given "*", any environment that isn't specified will use that value.

Example Usage

Environment variables are defined in the config file located at the specified 'files.src' file. For each variable you must define a value for each environment, or use "*" to apply it to any environments that don't have a value specified. This is an example of such file

{
	"APP_NAME": {
		"dev": "AppDev",
		"prod": "AppProd"
	},
	"APP_VERSION": {
		"*": "0.1.0"
	},
	"API_BASE": {
		"dev": "http://localhost:8000",
		"prod": "https://www.mysite.com/api"
	}
}

Then in a file you want an ENV variable to be replaced, use something like this

    <head>
        <title>ENV.APP_NAME</title>
    </head>
    <script src="ENV.API_BASE/user/create">
    var version = "ENV.APP_VERSION";
    alert(version);

If you run 'grunt envpreprocess' with options.environment="dev", it will produce

    <head>
        <title>AppDev</title>
    </head>
    <script src="http://localhost:8000/user/create">
    var version = "0.1.0";
    alert(version);

Similarly, if you run 'grunt envpreprocess' with options.environment="prod", it will produce

    <head>
        <title>AppProd</title>
    </head>
    <script src="https://www.mysite.com/api/user/create">
    var version = "0.1.0";
    alert(version);

Demo

You can run a simple demo by cloning this repo and running grunt. In the demo/ directory there is test.html and demo/config/env.json to demonstrate how to define environment variables and use them in HTML/JS. To run the demo, run npm install && npm run demo. This will copy test.html to demo/build_output/ and then envpreprocess will be run to replace the environment variables inside the build output file.


Note that this is going to modify the files that you point it to. This means that you shouldn't point this task at your actual source files, but at a separate build folder containing a copy of the source.

Release History

0.1.0 - Initial release. Functionality is working. Not tested.

0.1.1 - added demo

0.1.2 - accepted PR from nicolasbd to use target as environment if not specified in options

0.1.3 - fix README format