使用AngularJS进行调试或释放模式

时间:2014-09-12 16:44:33

标签: angularjs gruntjs

我有一个Angular应用程序,我正在使用Grunt,我使用gruntfile进行min,uglify和concat,在我的main.html中引用我的新.js文件(使用Grunt创建)后,一切都工作正常发布我的项目。但是,当我想进行一些更改并希望在调试模式下运行我的应用程序时,我必须在main.html中再次更改我的.js引用。有没有办法检查我的应用程序在什么模式下运行,所以我可以加载特定的.js文件? 我已经在ASP.net中看到了如何实现这一点,但我怎么能在我的情况下解决它呢?

以下是我的gruntfile.js

'use strict';

module.exports = function (grunt) {

grunt.initConfig({
  clean: {
    options: {force: true},
    all: {
        src: ['../Analaysis.UI/app_built/**/*.*']
    }
  },

  ngmin: {
    all: {
        files: [
            {
                expand: true,
                cwd: '../Analaysis.UI',
                src: ['**/*.js'
                    , '!**/Scripts/*'
                    , '!**/Scripts/**/*'
                    , '!**/obj/*'
                    , '!**/obj/**/*'
                    , '!**/src/**/**/*'],
                dest: '../Analaysis.UI/app_built/',
                ext: '.js'
            }
        ]
      }
  },

  uglify: {
    all: {
        files: [
            {
                expand: true,
                cwd: '../Analaysis.UI/app_built',
                src: ['**/*.js'],
                dest: '../Analaysis.UI/app_built/',
                ext: '.min.js'
            }
        ]
      }
  },

  concat: {
        js: {
            options: {
                separator: ';'
            },

                src: ['../Analaysis.UI/app_built/Global/global.min.js'
                    ,'../Analaysis.UI/app_built/Global/config.min.js'
                    ,'../Analaysis.UI/app_built/Global/GlobalService.min.js'
                    ,'../Analaysis.UI/app_built/Login/LoginController.min.js'
                    ,'../Analaysis.UI/app_built/Login/LoginService.min.js'],
                dest: '../Analaysis.UI/app_built/app_built_login.js'

     }
  }
 });

 grunt.registerTask("default", ['build']);
 grunt.registerTask('build', ['clean', 'ngmin', 'uglify', 'concat']);

 grunt.loadNpmTasks('grunt-contrib-clean'); 
 grunt.loadNpmTasks('grunt-ngmin');
 grunt.loadNpmTasks('grunt-contrib-uglify');
 grunt.loadNpmTasks('grunt-contrib-concat');
};

1 个答案:

答案 0 :(得分:0)

对于每种类型的构建,您都需要不同的配置。

检查以下代码。

'use strict';

module.exports = function (grunt) {

 grunt.initConfig({
    uglify: {
        all: {
            files: [
                {
                    expand: true,
                    cwd: '../Analaysis.UI/app_built',
                    src: ['**/*.js'],
                    dest: '../Analaysis.UI/app_built/',
                    ext: '.min.js'
        }
    ]
        },
        prod: {
            files: [
                {
                    expand: true,
                    cwd: '../Analaysis.UI/app_built',
                    src: ['**/*.js'],
                    dest: 'prod/Analaysis.UI/app_built/',//You can have different path according to build type
                    ext: '.min.js'
        }
    ]
        }

    },

    concat: {
        js: {
            options: {
                separator: ';'
            },

            src: ['../Analaysis.UI/app_built/Global/global.min.js'
                , '../Analaysis.UI/app_built/Global/config.min.js'
                , '../Analaysis.UI/app_built/Global/GlobalService.min.js'
                , '../Analaysis.UI/app_built/Login/LoginController.min.js'
                , '../Analaysis.UI/app_built/Login/LoginService.min.js'],
            dest: '../Analaysis.UI/app_built/app_built_login.js'

        },
        prod: {
            options: {
                separator: ';'
            },

            src: ['../Analaysis.UI/app_built/Global/global.min.js'
                , '../Analaysis.UI/app_built/Global/config.min.js'
                , '../Analaysis.UI/app_built/Global/GlobalService.min.js'
                , '../Analaysis.UI/app_built/Login/LoginController.min.js'
                , '../Analaysis.UI/app_built/Login/LoginService.min.js'],
            dest: 'production/Analaysis.UI/app_built/app_built_login.js'

        }
    }
 });

grunt.registerTask("default", ['build']);
grunt.registerTask('build', ['uglify', 'concat']);

//Task according to build. run "grunt debug" for debug build
grunt.registerTask('debug', ['uglify:all', 'concat:js']);
grunt.registerTask('prod', ['uglify:prod', 'concat:prod']);

grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-concat');
};