guzart / gulp-ng-constant

Gulp plugin for dynamic generation of angular constant modules.
MIT License
109 stars 26 forks source link

Group under one single constant name. #38

Open alajmo opened 8 years ago

alajmo commented 8 years ago

Hi,

I didn't find the possibility to group under one single constant name.

Example

Given an object like:

{ "person": "samir", "age": 99 } I wanted to get this:

angular.module("conf", []) .constant("CONFIG", { "person": "samir", "age": 99 }); but instead got this:

angular.module("conf", []) .constant("person", "samir") .constant"age", 99); Using an optional parameter we can simply group the whole object under a user defined constant.

Let me know if you think this feature makes sense and I'll write the tests and update README to include an example (I just added the option explanation for now).

guzart commented 8 years ago

Have you tried something like this?

{ "CONFIG": { "person": "samir", "age": 99 } }
alajmo commented 8 years ago

I shouldn't need to change my json file to get the wanted results, but yes it works. Often times settings files don't contain a root key name like:

{
    "CONFIG": {
       "stuff": "stuff..."
    }
}

It just adds an unnecessary nesting level and forces the user to adapt his json files to third party modules which I think should be avoided, especially when there is an easy solution.

guzart commented 8 years ago

It's true that you might not want to have that complexity in the configuration file. What sort of option would you suggest to add to the plugin? Could show a snippet on how you would like to use the option? Thanks

alajmo commented 8 years ago

Well I did create a pull request for a working solution, https://github.com/guzart/gulp-ng-constant/pull/35. The functionality is already in your code, we just wrap it under a new user entered field, so it would look something like:

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',
      constantName: 'CONFIG'
    }))
    // Writes config.js to dist/ folder
    .pipe(gulp.dest('dist'));
});

Could name constantName to whatever sounds right, groupUnder, name, key etc.

regmish commented 7 years ago

+1

ViieeS commented 6 years ago

I found another solution using jeditor plugin:

var jeditor = require("gulp-json-editor");
var ngConstant = require('gulp-ng-constant');

gulp.src('app/config.json')
        .pipe(jeditor(function (json) {
            return {"Constants": json};
        }, {beautify: false}))
        .pipe(ngConstant({
            name: "my.module.constants",
            wrap: "commonjs"
        }))
        .pipe(gulp.dest('dist'));

There is one restriction, you can't use constants (in lowercase) as constant object name because it will be unwrap by ngConstant.