All Projects → werk85 → Grunt Ng Constant

werk85 / Grunt Ng Constant

Licence: mit
Plugin for dynamic generation of angular constant and value definition modules.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Grunt Ng Constant

Bolt
⚡ is a fast grunt based, data driven, static site seed project, for rapid web development of PWA's or JAMstack projects
Stars: ✭ 30 (-88.59%)
Mutual labels:  grunt
n-asset-macro
Asset macro for Latte and Nette Framework for assets cache busting.
Stars: ✭ 14 (-94.68%)
Mutual labels:  grunt
grunt-retire
Grunt plugin for retire.
Stars: ✭ 89 (-66.16%)
Mutual labels:  grunt
generator-veams
Scaffold modern frontend web apps or web pages with a static site generator (Assemble or Mangony), Grunt and/or Gulp, Sass and Bower. Use modern frameworks like Bourbon, Bootstrap or Foundation and structure your JavaScript with ES Harmony support.
Stars: ✭ 45 (-82.89%)
Mutual labels:  grunt
Resume
Fully Responsive HTML/SCSS Resume Template + Matching Sketch File
Stars: ✭ 62 (-76.43%)
Mutual labels:  grunt
microframeworks-showcase
A simple grocery list webapplication implemented with the Microframeworks Spark Java, Jodd, Ninja, Javalite, Pippo and Ratpack
Stars: ✭ 29 (-88.97%)
Mutual labels:  ng
ng2-go-top-button
A simple customizable go-top-button component for Angular projects.
Stars: ✭ 18 (-93.16%)
Mutual labels:  ng
Grunt Filerev
[DEPRECATED] File revving
Stars: ✭ 261 (-0.76%)
Mutual labels:  grunt
Research
Repo for my blog & technology research
Stars: ✭ 43 (-83.65%)
Mutual labels:  grunt
Angular
web项目Demo,前端采用Angular+Grunt+bower+Requirejs+Bootstrap,后台使用SpringMVC框架
Stars: ✭ 16 (-93.92%)
Mutual labels:  grunt
grunt-assemble-i18n
Assemble middleware for adding i18n support to projects.
Stars: ✭ 25 (-90.49%)
Mutual labels:  grunt
grunt-xmlpoke
Updates values in XML files based on XPath queries. Similar to the `xmlpoke` task in NAnt.
Stars: ✭ 13 (-95.06%)
Mutual labels:  grunt
wp-mail-catcher
A fast, lightweight plugin that saves emails sent by your WordPress website.
Stars: ✭ 16 (-93.92%)
Mutual labels:  grunt
docker-npm
npm, yarn, node, npx, bower, grunt, gulp, generate-md - build and dev tools.
Stars: ✭ 53 (-79.85%)
Mutual labels:  grunt
grunt-tv4
Use Grunt and Tiny Validator tv4 to validate files against json-schema draft v4
Stars: ✭ 13 (-95.06%)
Mutual labels:  grunt
viaduct
CMS for via. Moved to https://gitlab.com/studieverenigingvia/viaduct
Stars: ✭ 16 (-93.92%)
Mutual labels:  grunt
grunt-search
Grunt plugin that searches a list of files and logs all findings in various formats.
Stars: ✭ 15 (-94.3%)
Mutual labels:  grunt
Grunt Cache Bust
Cache bust static assets using content hashing
Stars: ✭ 262 (-0.38%)
Mutual labels:  grunt
Ngx Md
Angular(ngx) directive for parsing markdown content in your web application.
Stars: ✭ 258 (-1.9%)
Mutual labels:  ng
metadatamanagement
Metadatamanagement (MDM) - Data Search for Higher Education Research and Science Studies
Stars: ✭ 21 (-92.02%)
Mutual labels:  grunt

grunt-ng-constant CircleCI

Plugin for dynamic generation of angular constant and value modules.

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-ng-constant --save-dev

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

grunt.loadNpmTasks('grunt-ng-constant');

The "ngconstant" task

Overview

The following shows a simple configuration for generating a config module with one constant and one value that contain your package information and debug config.

grunt.initConfig({
  ngconstant: {
    options: {
      name: 'config',
      dest: 'config.js',
      constants: {
        package: grunt.file.readJSON('package.json')
      },
      values: {
        debug: true
      }
    },
    build: {
    }
  },
})

For the target build the resulting file config.js looks like this:

angular.module('config', [])

.constant('package', {
  "version": "0.0.1",
  ...
})

.value("debug", true)

;

Options

name

Type: String

Required

The name of the constant module used in your angular project.

dest

Type: String

Required

The path where the generated constant module should be saved.

deps

Type: Array or Boolean

Default value: []

Optional

An array that specifies the default dependencies a module should have. When your module should not have any modules, so you can append the constants to an already existing one, you can set deps to false.

constants

Type: Object, String, Function

Default value: {}

Optional

If it is an object it gets automatically merged in all target constants definitions. This option should be used when you need a global constants definition for all your targets.

values

Type: Object, String, Function

Default value: {}

Optional

If it is an object it gets automatically merged in all target values definitions. This option should be used when you need a global values definition for all your targets.

wrap

Type: String or Boolean

Default value: false

Optional

A boolean to activate or deactivate the automatic wrapping. A string which will wrap the result of file, use the {%= __ngModule %} variable to indicate where to put the generated module content. See the "Custom Wrap Option" section for further informations.

serializer

Type: String|Function

Default value: jju

Optional

Available options:

  • jju (Default) Uses the jju stringify method.
  • json Uses JSON.stringify for serialization.
  • tosource Use the node-tosource module.

If you want to define your own serializer use function(obj, serializerOptions, options) { return /* your serialized string */ }. this will be set to the plugin context.

serializerOptions

Type: Object

Default value: {indent: '', no_trailing_comma: true}

Optional

Use this option for setting specific options for the given serializer. The default config configures the jju stringify method. See the documentation for more information of possible options.

template

Type: String

Default value: grunt.file.read('constant.tpl.ejs')

Optional

Custom template for creating the output constants file. Defaults to the default constants template file if none provided.

delimiters

Type: String

Default value: 'ngconstant' which sets the template delimiters to {% and %}. Make sure that you do not use the same delimiters as your grunt configuration or you get an unwanted behavior.

Optional

configMergeCustomizer

Type: Function

Default value: _.noop

Optional

Allows to customize the merge of global and target configuration. The function has the signature (key, objValue, srcValue, key, object, source, stack). The key parameter can be 'constants' or 'values'. The rest of the parameters is identical to _.mergeWith.

Usage Examples

Default Options

In this example I convert the package.json information to an angular module. So I am able to display such things as the current version of the application in the app.

grunt.initConfig({
  ngconstant: {
    dist: {
      options: {
        dest: 'dist/constants.js',
        name: 'constants',
      },
      constants: {
        package: grunt.file.readJSON('package.json')
      },
      values: {
        debug: true
      }
    }
  },
})

Note: In most cases for all following examples the applied functionality on constants can also be achieved with the values parameter.

Custom Options

In this example we set custom configurations for the space and deps parameters. So we create a module that has dep1 and dep2 as dependencies and defines two different constants constants1 and constants2 with custom values. The space parameter is set to a .

grunt.initConfig({
  ngconstant: {
    options: {
      space: ' ',
      deps: ['dep1', 'dep2'],
      dest: 'dist/module.js',
      name: 'someModule'
    },
    dist: {
      constants: {
        'constant1': {
          'key1': 'value1',
          'key2': 42
        },
        'constant2': 'value2'
      }
    }
  },
})

The resulting module looks like the following:

angular.module("someModule", ["dep1", "dep2"])

.constant("constant1", {
  "key1": "value1",
  "key2": 42
})

.constant("constant2", "value2")

;

You can also load the constants definition directly from a file:

grunt.initConfig({
  ngconstant: {
    options: {
      dest: 'dist/module.js',
      name: 'someModule'
    },
    dist: {
      constants: 'constants.json'
    }
  },
})

Or if you want to calculate the constants value at runtime you can create a lazy evaluated method which should be used if you generate your json file during the build process.

grunt.initConfig({
  ngconstant: {
    options: {
      dest: 'dist/module.js',
      name: 'someModule'
    },
    dist: {
      constants: function () {
        return {
          lazyConfig: grunt.file.readJSON('build/lazy-config.json')
        };
      }
    }
  },
})

Wrap Option

The wrap option allows you to encapsulate the module in a closure. Simply set wrap to true.

grunt.initConfig({
  ngconstant: {
    options: {
      dest: 'tmp/wrap_options.js',
      name: 'module2',
      wrap: true
    },
    dist: {
      constants: {
        'constant1': {
          key1: 123,
          key2: 'value2',
          foobar: false
        }
      }
      
    },
    nowrap: { 
      options: {
        wrap: false // Disable wrapping for the 'nowrap' target
      },
      constants: {
        ...
      }
    }
  },
})

The resulting module looks like:

(function(angular, undefined) {
   angular.module("module2", ["test"])

.constant("constant1", {
  "key1": 123,
  "key2": "value2",
  "foobar": false
})

; 
})(angular);

Custom Wrap Option

If you want to use another wrapping you can use a string as wrap option, which is interpolated by the plugin. Use the __ngModule variable as placeholder for the generated module.

Here a RequireJS example:

grunt.initConfig({
  ngconstant: {
    options: {
      dest: 'tmp/wrap_options.js',
      name: 'module2',
      wrap: 'define(["angular", "ngResource", "ngCookies"], function() { \n return {%= __ngModule %} \n\n});',
    },
    dist: {
      constants: {
        'constant1': {
          key1: 123,
          key2: 'value2',
          foobar: false
        }
      }
    }
  },
})

The resulting module looks like the following:

define(["angular", "ngResource", "ngCookies"], function() { 
 return angular.module("module2", ["test"])

.constant("constant1", {
  "key1": 123,
  "key2": "value2",
  "foobar": false
})

; 

});

Note: For longer wrapping templates it is recommended to use grunt.file.read('customer-wrap.tpl.ejs').

Global Constants option

If you need the same configuration for all your targets you can use the constants option to automatically merge your per target configuration into the global one. If you don't want to merge, you can use the per target constants option to override everything.

grunt.initConfig({
  ngconstant: {
    options: {
      name: 'config',
      dest: 'config.js',
      constants: {
        title: 'grunt-ng-constant',
        debug: true
      }
    },
    dev: {
      constants: {
        title: 'grunt-ng-constant-beta'
      }
    },
    prod: {
      constants: {
        debug: false
      }
    },
    override_global: {
      options: {
        constants: { // This does not merge it overrides
          ...
        }
      }
    }
  }
});

Which results in the following constants objects.

For the target dev:

angular.module('config', [])

.constant('title', 'grunt-ng-constant-beta')

.constant('debug', true)

;

For the target prod:

angular.module('config', [])

.constant('title', 'grunt-ng-constant')

.constant('debug', false)

;

FAQ

How can I change the style of the generated code?

If the code looks to ugly for you. You can use grunt-jsbeautifyer.

How can I create multiple modules?

Create a custom target for each module and set the dest, name, constants and values parameter for each one.

How can I create a CoffeeScript version of the module

Till v1.0.0 this was supported natively by the plugin. Now you have to use the grunt-js2coffee plugin.

Hey I like this project how can I help?

Report bugs, propose new features or simply star the project that shows me that are people are interested in this project.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].