generator-gulp-angular
does not yet have a way to build for different environments.
One way to do this is with gulp-ng-constant:
This reads in the configuration in either config.json
or configDev.json
, based on the NODE_ENV
environment variable and generates an Angular module with the contents to be included in your application.
gulp.task('config', function () {
var configPath = 'config.json';
if (process.env.NODE_ENV === 'dev') {
configPath = 'configDev.json';
}
return gulp.src(configPath)
.pipe($.ngConstant())
.pipe(gulp.dest('src/app/'));
});
configDev.json
{
"name": "myApp.config",
"constants": {
"config": {
"apiBase": "https://localhost/v1"
}
}
}
config.json
{
"name": "myApp.config",
"constants": {
"config": {
"apiBase": "https://api.example.com/v1"
}
}
}
module.exports = function(options) {
gulp.task('scripts', ['config'], function () {
return gulp.src(options.src + '/app/**/*.js')
.pipe($.eslint())
.pipe($.eslint.format())
.pipe(browserSync.stream())
.pipe($.size());
});
};
angular.module('myApp', ['myApp.config'])
.service('apiService', ['config', function (config) {
this.base = config.apiBase;
});
$ NODE_ENV=dev gulp serve
$ NODE_ENV=dev gulp