Skip to content

andy2ndlife/grunt-variablize

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

grunt-variablize Build Status

Turn json file into js object.

Overview

If you used to

var socket = io.connect('http://localhost:5000'); // dev environment

and

var socket = io.connect('http://172.18.3.24:5033'); // prod environment

well... you deserve to have

// resources/config.json
{
  "dev": {
    "api": "http://localhost:5000"
  },
  "prod": {
    "api": "http://172.18.3.24:5033"
  }
}

and use it like that

var socket = io.connect(Config.api); // dev or prod - who cares

Getting Started

This plugin requires Grunt ~0.4.2

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-variablize --save-dev

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

grunt.loadNpmTasks('grunt-variablize');

The "variablize" task

Overview

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

grunt.initConfig({
  variablize: {
    your_target: {
      // Target-specific options go here.
    }
  }
});

Options

input

Type: String

Path to json file.

output

Type: String

Path to output js file.

variable

Type: String

Variable name to use.

property

Type: String

Json property name which should be variablized.

Usage Examples

Basic Usage

In this example, the whole resources/Config.json file is used to create dist/Config.js.

// resources/Config.json
{
  "foo": "bar"
}
// dist/Config.js
var Config = {"foo":"bar"};
grunt.initConfig({
  variablize: {
    basic: {
      input: "resources/Config.json",
      output: "dist/Config.js",
      variable: "Config"
    }
  }
});

Use Property Name

In this example, property is used to filter json file.

// input file - resources/Config.json
{
  "dev": {
    "serviceUrl": "http://dev.my.service/"
  },
  "test": {
    "serviceUrl": "http://test.my.service/"
  },
  "prod": {
    "serviceUrl": "http://prod.my.service/"
  }
}
// output file - dist/Config.js
var Config = {"serviceUrl":"http://prod.my.service/"};
grunt.initConfig({
  variablize: {
    prod: {
      input: "resources/Config.json",
      output: "dist/Config.js",
      variable: "Config",
      property: "prod"
    }
  }
});

About

Provide json file as js variable

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published