grunt-ng-constant不生成配置脚本

时间:2015-11-10 23:37:38

标签: javascript angularjs node.js gruntjs environment-variables

我尝试在AngularJS中使用环境变量来执行特定于环境的配置。我使用的是使用Grunt的Yeoman工作流程,grunt-ng-constant插件据称可以帮助进行特定于环境的配置。在这个tutorial之后,我相应地设置了我的Gruntfile,但是当我在控制台中运行grunt serve时,config.js没有被写入/app/scripts/。没有config.js,我无法将环境变量注入角度应用程序。

这是我的Gruntfile

的片段
module.exports = function (grunt) {

  // Time how long tasks take. Can help when optimizing build times
  require('time-grunt')(grunt);

  // Automatically load required Grunt tasks
  require('jit-grunt')(grunt, {
    useminPrepare: 'grunt-usemin',
    ngtemplates: 'grunt-angular-templates',
    cdnify: 'grunt-google-cdn'
  });

  // Configurable paths for the application
  var appConfig = {
    app: require('./bower.json').appPath || 'app',
    dist: '../server/dist'
  };

  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-ng-constant');

  // Define the configuration for all the tasks
  grunt.initConfig({

    // Project settings
    yeoman: appConfig,

    ngconstant: {
      // options for all environments
      options: {
        space: '  ',
        wrap: '"use strict";\n\n {%= __ngModule %}',
        name: 'config'
      },

      // Development/Testing environment
      development: {
        options: {
          dest: '<%= yeoman.app %>/scripts/config.js'
        },
        constants: {
          ENV: {
            name: 'development',
            apiEndpoint: 'http://localhost:3000'
          }
        }
      },

      // Production environment
      production: {
        options: {
          dest: '<%= yeoman.dist %>/scripts/config.js'
        },
        constants: {
          ENV: {
            name: 'production',
            apiEndpoint: 'http://productionUrl'
          }
        }
      }
    },

    ...

    grunt.registerTask('serve', 'Compile then start a connect web server',          function (target) {
        if (target === 'dist') {
          return grunt.task.run(['build', 'connect:dist:keepalive']);
        }

        grunt.task.run([
          'clean:server',
          'wiredep',
          'concurrent:server',
          'autoprefixer:server',
          'connect:livereload',
          'watch',
          'ngconstant:development'
        ]);
    });

    ...

与Gruntfile在同一目录(/.sass-cache)中生成的内容为/.tmpclient

我的应用文件结构:

enter image description here

1 个答案:

答案 0 :(得分:1)

ngconstant任务未被调用,因为它位于watch任务之后。修改运行块,使'ngconstant:development'行位于'autoprefixer:server',旁边,因此它位于connect&amp;看任务。不要忘记添加逗号!

    grunt.task.run([
      'clean:server',
      'wiredep',
      'concurrent:server',
      'autoprefixer:server',
      'ngconstant:development',
      'connect:livereload',
      'watch'
    ]);

此外,bower.json文件中的应用程序路径可能有误。确保通过更改appConfig来修改应用的路径,使其如下所示:

var appConfig = {
  app: 'app',
  dist: '../server/dist'
}