gulp-ng-constant icon indicating copy to clipboard operation
gulp-ng-constant copied to clipboard

Group under one single constant name.

Open alajmo opened this issue 9 years ago • 6 comments
trafficstars

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).

alajmo avatar Jul 27 '16 21:07 alajmo

Have you tried something like this?

{ "CONFIG": { "person": "samir", "age": 99 } }

guzart avatar Aug 09 '16 01:08 guzart

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.

alajmo avatar Aug 09 '16 11:08 alajmo

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

guzart avatar Aug 16 '16 00:08 guzart

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.

alajmo avatar Aug 16 '16 08:08 alajmo

+1

regmish avatar Jan 06 '17 16:01 regmish

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.

ViieeS avatar Nov 17 '17 20:11 ViieeS