当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

gulp-ng-config

授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 陈胤
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

gulp-ng-config

NPM

It's often useful to generate a file of constants, usually as environment variables, for your Angular apps.This Gulp plugin will allow you to provide an object of properties and will generate an Angular module of constants.

To Install:

npm install gulp-ng-config

How it works

It's pretty simple:gulpNgConfig(moduleName)

Example Usage

We start with our task. Our source file is a JSON file containing our configuration. We will pipe this through gulpNgConfig and out will come an angular module of constants.

var gulp = require('gulp');
var gulpNgConfig = require('gulp-ng-config');

gulp.task('test', function () {
  gulp.src('configFile.json')
  .pipe(gulpNgConfig('myApp.config'))
  .pipe(gulp.dest('.'))
});

Assume that configFile.json contains:

{
  "string": "my string",
  "integer": 12345,
  "object": {"one": 2, "three": ["four"]},
  "array": ["one", 2, {"three": "four"}, [5, "six"]]
}

Running gulp test will take configFile.json and produce configFile.js with the following content:

angular.module('myApp.config', [])
.constant('string', "my string")
.constant('integer', 12345)
.constant('object', {"one":2,"three":["four"]})
.constant('array', ["one",2,{"three":"four"},[5,"six"]]);

We now can include this configuration module in our main app and access the constants

angular.module('myApp', ['myApp.config']).run(function (string) {
  console.log("The string constant!", string) // outputs "my string"
});

Configuration

Currently there are a few configurable options to control the output of your configuration file:

options.environment

Type: String Optional

If your configuration contains multiple environments, you can supply the key you want the plugin to load from your configuration file.

Example config.json file with multiple environments:

{
  "local": {
    "EnvironmentConfig": {
      "api": "http://localhost/"
    }
  },
  "production": {
    "EnvironmentConfig": {
      "api": "https://api.production.com/"
    }
  }
}

Usage of the plugin:

gulpNgConfig('myApp.config', {
  environment: 'production'
})

Expected output:

angular.module('myApp.config', [])
.constant('EnvironmentConfig', {"api": "https://api.production.com/"});

Nested Environment

If the configuration is nested it can be accessed by the namespace, for example

{
  "version": "0.1.0",
  "env": {
    "local": {
      "EnvironmentConfig": {
        "api": "http://localhost/"
      }
    },
    "production": {
      "EnvironmentConfig": {
        "api": "https://api.production.com/"
      }
    }
  }
}

Usage of the plugin:

gulpNgConfig('myApp.config', {
  environment: 'env.production'
})

Expected output:

angular.module('myApp.config', [])
.constant('EnvironmentConfig', {"api": "https://api.production.com/"});

Multiple Environment keys

Multiple environment keys can be supplied in an array, for example for global and environmental constants

{
  "global": {
    "version": "0.1.0"
   },
  "env": {
    "local": {
      "EnvironmentConfig": {
        "api": "http://localhost/"
      }
    },
    "production": {
      "EnvironmentConfig": {
        "api": "https://api.production.com/"
      }
    }
  }
}

Usage of the plugin:

gulpNgConfig('myApp.config', {
  environment: ['env.production', 'global']
})

Expected output:

angular.module('myApp.config', [])
.constant('EnvironmentConfig', {"api": "https://api.production.com/"});
.constant('version', '0.1.0');

options.constants

Type: Object Optional

You can also override properties from your json file or add more by including them in the gulp tasks:

gulpNgConfig('myApp.config', {
  constants: {
    string: 'overridden',
    random: 'value'
  }
});

Generating configFile.js

angular.module('myApp.config', [])
.constant('string', "overridden")
.constant('integer', 12345)
.constant('object', {"one":2,"three":["four"]})
.constant('array', ["one",2,{"three":"four"},[5,"six"]])
.constant('random', "value");

options.type

Type: String Default value: 'constant' Optional

This allows configuring the type of service that is created -- a constant or a value. By default, a constant is created, but a value can be overridden. Possible types:

  • 'constant'
  • 'value'
gulpNgConfig('myApp.config', {
  type: 'value'
});

This will produce configFile.js with a value service.

angular.module('myApp.config', [])
.value('..', '..');

options.createModule

Type: Boolean Default value: true Optional

By default, a new module is created with the name supplied. You can access an existing module, rather than creating one, by setting createModule to false.

gulpNgConfig('myApp.config', {
  createModule: false
});

This will produce configFile.js with an existing angular module

angular.module('myApp.config')
.constant('..', '..');

options.wrap

Type: Boolean or String Default value: false Optional

Presets:

  • ES6
  • ES2015

Wrap the configuration module in an IIFE or your own wrapper.

gulpNgConfig('myApp.config', {
  wrap: true
})

Will produce an IIFE wrapper for your configuration module:

(function () {
  return angular.module('myApp.config') // [] has been removed
  .constant('..', '..');
})();

You can provide a custom wrapper. Provide any string you want, just make sure to include <%= module %> for where you want to embed the angular module.

gulpNgConfig('myApp.config', {
  wrap: 'define(["angular"], function () {\n return <%= module %> \n});'
});

The reuslting file will contain:

define(["angular"], function () {
 return angular.module('myApp.config', [])
.constant('..', '..');
});

options.parser

Type: String Default value: 'json' Optional

By default, json file is used to generate the module. You can provide yml file to generate the module. Just set parser to 'yml' or 'yaml'. If your file type is yml and you have not defined parser, your file will still be parsed and js be generated correctly.For example, you have a config.yml file,

string: my string
integer: 12345
object:
  one: 2
  three:
    - four
gulp.src("config.yml")
.pipe(gulpNgConfig('myApp.config', {
  parser: 'yml'
}));

Generating,

angular.module('myApp.config', [])
.constant('string', "my string")
.constant('integer', 12345)
.constant('object', {"one":2,"three":["four"]});

options.pretty

Type: Number|Boolean Default value: false Optional

This allows JSON.stringify to produce a pretty formatted output string.

gulp.src('config.json')
.pipe(gulpNgConfig('myApp.config', {
  pretty: true // or 2, 4, etc -- all representing the number of spaces to indent
}));

Will output a formatted JSON object in the constants, instead of inline.

angular.module("gulp-ng-config", [])
.constant("one", {
  "two": "three"
});

options.keys

Type: Array Optional

If you only want some of the keys from the object imported, you can supply the keys you want the plugin to load.

Example config.json file with unwanted keys:

{
  "version": "0.0.1",
  "wanted key": "wanted value",
  "unwanted key": "unwanted value"
}

Usage of the plugin:

gulpNgConfig("myApp.config", {
  keys: ["version", "wanted key"]
})

Expected output:

angular.module("myApp.config", [])
.constant("version", "0.0.1")
.constant("wanted key", "wanted value");

options.templateFilePath

Type: String Optional

This allows the developer to provide a custom output template.

Sample template:angularConfigTemplate.html

var foo = 'bar';

angular.module("<%= moduleName %>"<% if (createModule) { %>, []<% } %>)<% _.forEach(constants, function (constant) { %>
.<%= type %>("<%= constant.name %>", <%= constant.value %>)<% }); %>;

Configuration:

{
  "Foo": "bar"
}

Gulp task:

gulp.src('config.json')
.pipe(gulpNgConfig('myApp.config', {
  templateFilePath: path.normalize(path.join(__dirname, 'templateFilePath.html'))
}));

Sample output:

var foo = 'bar';

angular.module('myApp.config', [])
.constant('Foo', 'bar');

Additional Usages

Without a json/yaml file on disk

Use buffer-to-vinyl to create and stream a vinyl file into gulp-ng-config. Now config values can come from environment variables, command-line arguments or anywhere else.

var b2v = require('buffer-to-vinyl');
var gulpNgConfig = require('gulp-ng-config');

gulp.task('make-config', function() {
  var json = JSON.stringify({
    // your config here
  });

  return b2v.stream(new Buffer(json), 'config.js')
    .pipe(gulpNgConfig('myApp.config'))
    .pipe(gulp.dest('build'));
});

ES6/ES2015

An ES6/ES2015 template can be generated by passing wrap: true as a configuration to the plugin

Contributing

Contributions, issues, suggestions, and all other remarks are welcomed. To run locally just fork & clone the project and run npm install. Before submitting a Pull Request, make sure that your changes pass gulp test, and if you are introducing or changing a feature, that you add/update any tests involved.

  • gulp-ng-config It’s often useful to generate a file of constants, usually as environment variables, for your Angular apps. This Gulp plugin will allow you to provide an object of properties and will g

  • 单独执行webpack和使用gulp-webpack打包的文件结果不一样。 遇到的问题: 我用gulp-webpack工具来执行webpack任务。发现打包后的文件中的 “import $ from 'jquery'”都还保留着(css文件已经被编译进去了),文件也很小,只有几百行,然而我单独在终端执行“webpack”,文件是可以被正确编译的,有上万行。 请问大家,这是怎么回事?怎么解决?谢谢~

  • 第1步:安装Node 首先,最基本也最重要的是,我们需要搭建node环境。访问 nodejs.org,然后点击大大的绿色的 install 按钮,下载完成后直接运行程序,就一切准备就绪。npm会随着安装包一起安装,稍后会用到它。 为了确保Node已经正确安装,我们执行几个简单的命令。 node -v npm -v 如果这两行命令没有得到返回,可能node就没有安装正确,进行重装。 第2步:安装gu

  • Angular相关 这个部分介绍与Angular相关的一些插件。 gulp-angular-templatecache Angular自带的$templateCache服务可以把Angular中用到的所有模板缓存下来,而这个插件的功能就是直接将指定的HTML模板文件以JS字符串的形式注册在$tempalteCache服务中,这样所有的模板就会随JS文件直接一次性下载下来。这个插件使用起来也非常简单

  • >npm install -g gulp  #全局安装 >npm install gulp (--save-dev)   #每个项目中也要单独安装一次,且添加到package.json中 #####1. 核心API gulp.task(name[, deps], fn)   gulp.src(globs[, options]) 可以没有返回,[node-glob语法](https://github

 相关资料
  • 这篇快速上手指南将教你如何使用Gulp构建TypeScript,和如何在Gulp管道里添加Browserify,uglify或Watchify。 本指南还会展示如何使用Babelify来添加Babel的功能。 这里假设你已经在使用Node.js和npm了。 我们首先创建一个新目录。 命名为proj,也可以使用任何你喜欢的名字。 mkdir proj cd proj 我们将以下面的结构开始我们的工程

  • 更改历史 * 2017-11-12 杨海月 增加xxx内容,更改xxx内容,删除xxx内容 * 2017-11-01 胡小根 初始化文档 第一章 历史、现状及发展 1.1 gulp历史 gulp是前端开发过程中一种基于流的 代码构建工具 ,是自动化项目的构建利器;它不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成;使用它,不仅可

  • 问题内容: 我想遍历一个对象,并在每次迭代时将文件路径数组传递给gulp.src,然后对这些文件进行一些处理。下面的代码仅用于说明目的,因为return语句会在第一次通过时终止循环,因此实际上将无法工作。 这是基本思想。有关如何执行此操作的任何想法? 问题答案: 我能够使用合并流实现这一目标。如果有人感兴趣,这里是代码。这个想法是在循环内创建一个流数组,并在完成迭代后合并它们:

  • 我安装了gulp,但我不能使用“gulp”命令,因为它会给我“-bash:gulp:command not found”错误。当我使用“NPX GULP”,然后它的工作,但我不知道为什么。

  • gulp-concat:文件合并 gulp-connect:在本地开启一个websocket服务,使用liveReload实现实时更新 gulp-watch:监听文件的变化,配合connect实现服务自动刷新 gulp-plumber:实时更新错误不会导致终端gulp运行开启的服务断开 gulp-livereload:即时重整 gulp-clean:清理档案 gulp-load-plugins:自

  • gulp-load-plugins Automatically load any gulp plugins in your package.json Install $ npm install --save-dev gulp-load-plugins Given a package.json file that has some dependencies within: { "depen