美文网首页
如何根据不同环境进行编译

如何根据不同环境进行编译

作者: Transnet2014 | 来源:发表于2017-04-19 18:52 被阅读9次

返回导航

#460 #650

generator-gulp-angular does not yet have a way to build for different environments.

One way to do this is with gulp-ng-constant:

1) Create a new gulp task called config.

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"
    }
  }
}

2) Add the config gulp task as a dependency of scripts.

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());
  });
};

3) Use the generated module in your app.

angular.module('myApp', ['myApp.config'])
  .service('apiService', ['config', function (config) {
    this.base = config.apiBase;
  });

4) Run gulp with environment variable to build for the dev environment.

$ NODE_ENV=dev gulp serve
$ NODE_ENV=dev gulp

返回导航

相关文章

网友评论

      本文标题:如何根据不同环境进行编译

      本文链接:https://www.haomeiwen.com/subject/kvphzttx.html