在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):ajwhite/gulp-ng-config开源软件地址(OpenSource Url):https://github.com/ajwhite/gulp-ng-config开源编程语言(OpenSource Language):JavaScript 98.7%开源软件介绍(OpenSource Introduction):gulp-ng-configIt'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:
How it worksIt's pretty simple:
Example UsageWe start with our task. Our source file is a JSON file containing our configuration. We will pipe this through 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 {
"string": "my string",
"integer": 12345,
"object": {"one": 2, "three": ["four"]},
"array": ["one", 2, {"three": "four"}, [5, "six"]]
} Running 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"
}); ConfigurationCurrently there are a few configurable options to control the output of your configuration file:
options.environmentType: If your configuration contains multiple environments, you can supply the key you want the plugin to load from your configuration file. Example {
"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 EnvironmentIf 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 keysMultiple 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.constantsType: 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 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.typeType: This allows configuring the type of service that is created -- a
gulpNgConfig('myApp.config', {
type: 'value'
}); This will produce angular.module('myApp.config', [])
.value('..', '..'); options.createModuleType: By default, a new module is created with the name supplied. You can access an existing module, rather than creating one, by setting gulpNgConfig('myApp.config', {
createModule: false
}); This will produce angular.module('myApp.config')
.constant('..', '..'); options.wrapType: Presets:
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 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.parserType: By default, json file is used to generate the module. You can provide yml file to generate the module. Just set 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.prettyType: This allows 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 angular.module("gulp-ng-config", [])
.constant("one", {
"two": "three"
}); options.keysType: If you only want some of the keys from the object imported, you can supply the keys you want the plugin to load. Example {
"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.templateFilePathType: This allows the developer to provide a custom output template. Sample template:
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 UsagesWithout a json/yaml file on diskUse 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/ES2015An ES6/ES2015 template can be generated by passing ContributingContributions, issues, suggestions, and all other remarks are welcomed. To run locally just fork & clone the project and run |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论