Gulp plugin for dynamic generation of angular constant modules.
npm install gulp-ng-constantgulp-ng-constant
================

| Package | gulp-ng-constant |
| Description | Plugin for dynamic generation of angular constant modules. Based of grunt-ng-constant |
| Node Version | >= 0.10 |
1. Usage
* Configuration in gulpfile.js
* Configuration in config.json
2. Options
* name
* stream
* constants
* deps
* wrap
* space
* template
* templatePath
3. Examples
* Multiple Environments
* Stream
* YAML
4. Special Thanks
_gulpfile.js_
``javascript
var ngConstant = require('gulp-ng-constant');
gulp.task('config', function () {
gulp.src('app/config.json')
.pipe(ngConstant({
name: 'my.module.config',
deps: ['ngAnimate'],
constants: { myPropCnt: 'hola!' },
wrap: 'amd',
}))
// Writes config.js to dist/ folder
.pipe(gulp.dest('dist'));
});
`
_app/config.json_
`json`
{
"myFirstCnt": true,
"mySecondCnt": { "hello": "world" }
}
_dist/config.js_ _(output)_
`javascript`
define(["require", "exports"], function(require, exports) {
return angular.module("my.module.config", ["ngAnimate"])
.constant("myFirstCnt", true)
.constant("mySecondCnt", { "hello": "world" })
.constant("myPropCnt", "hola!");
});
_gulpfile.js_
`javascript
var ngConstant = require('gulp-ng-constant');
gulp.task('config', function () {
gulp.src('app/config.json')
.pipe(ngConstant())
// Writes config.js to dist/ folder
.pipe(gulp.dest('dist'));
});
`
_app/config.json_
`json`
{
"name": "my.module.config",
"deps": ["ngAnimate"],
"wrap": "commonjs",
"constants": {
"myFirstCnt": true,
"mySecondCnt": { "hello": "world" }
}
}
_dist/config.js_ _(output)_
`javascript`
module.exports = angular.module("my.module.config", ["ngAnimate"])
.constant("myFirstCnt", true)
.constant("mySecondCnt", { "hello": "world" })
.constant("myPropCnt", "hola!");
#### options.name
Type: string filename
Default: or "ngConstants" json.name
Overrides:
_optional_
The module name.
This property will override any name property defined in the input json file. The default name when used as a tranform stream (i.e. regular plugin) is the passed file name. When options.stream is true the default name is "ngConstants".
#### options.stream
Type: boolean false
Default:
_optional_
If true it returns a new gulp stream, which can then be piped other gulp plugins
(Example).
#### options.constants
Type: Object | string undefined
Default: json.constants
Exends/Overrides:
Constants to defined in the module.
Can be a JSON string or an Object.json
This property extends the one defined in the input file. If there arejson
properties with the same name, this properties will override the ones from the
input file.
#### options.merge
Type: boolean false
Default:
_optional_
This applies to constants of the Object type.
If true the constants of type Object from the input file and the constants
from the configuration will be merged.
#### options.deps
Type: array []
Default: json.deps
Overrides:
_optional_
An array that specifies the default dependencies a module should have. To add the constants to an existing module, you can set it to false.deps
This property will override any property defined in the input json file.
#### options.wrap
Type: boolean|string false
Default: ['amd', 'commonjs']
Available:
_optional_
A boolean to active or deactive the automatic wrapping.
A string who will wrap the result of file, use the
<%= __ngModule %> variable to indicate where to put the generated
module content.
A string with 'amd' that wraps the module as an AMD module,
compatible with RequireJS
#### options.space
Type: string '\t'
Default:
_optional_
A string that defines how the JSON.stringify method will prettify your code.
#### options.template
Type: string
Default: _content of tpls/constant.tpl.ejs_
_optional_
EJS template to apply when creating the output configuration file. The following variables
are passed to the template during render:
* moduleName: the module name (string)deps
* : the module dependencies (array)constants
* : the module constants (array)constantObj
* where a is an object with a name and a value, both strings.
#### options.templatePath
Type: string 'tpls/constant.tpl.ejs'
Default:
_optional_
Location of a custom template file for creating the output configuration file. Defaults to the provided constants template file if none provided.
_config.json_
`json`
{
"development": { "greeting": "Sup!" },
"production": { "greeting": "Hello" }
}
_gulpfile.js_
`javascript
var gulp = require('gulp');
var ngConstant = require('gulp-ng-constant');
gulp.task('constants', function () {
var myConfig = require('./config.json');
var envConfig = myConfig[process.env];
return ngConstant({
constants: envConfig,
stream: true
})
.pipe(gulp.dest('dist'));
});
`
`javascript
var gulp = require('gulp');
var ngConstant = require('gulp-ng-constant');
var uglify = require('gulp-uglify');
gulp.task('constants', function () {
var constants = { hello: 'world' };
return ngConstant({
constants: constants,
stream: true
})
.pipe(uglify())
.pipe(gulp.dest('dist'));
});
`
Just write your configuration in a YAML file and pipe it to the plugin.
_config.yml_
`yaml`
greeting: Merry Christmas!
seasons:
- Winter
- Spring
- Summer
- Fall
_gulpfile.js_
`javascript
var gulp = require('gulp');
var ngConstant = require('gulp-ng-constant');
gulp.task('constants', function () {
gulp.src('app/config.yml')
.pipe(ngConstant())
.pipe(gulp.dest('dist'));
});
``
@alexeygolev, @sabudaye, @ojacquemart, @lukehorvat, @rimian, @andidev, @dotDeeka, @LoicMahieu