【问题标题】:AngularJS + Grunt ngAnnotate and single quoteAngularJS + Grunt ngAnnotate 和单引号
【发布时间】:2018-05-23 02:37:28
【问题描述】:

我正在尝试缩小我的应用程序,但无法通过 Grunt ngAnnotate 因为它引发错误

错误:由于解析错误,无法处理源代码 解析正则表达式时出错:正则表达式无效:/[A-Za-z0-9!#_\?:&.-']/:字符类中的范围乱序(3:22)

在一个看起来像的指令上

myApp.directive('allowedChars', function() {

  var EMAIL_REGEXP = /[A-Za-z0-9!#_\?:&\.- ']/;


  return {
    require: 'ngModel',
    restrict: '',
    link: function(scope, elm, attrs, ctrl) {
      // only apply the validator if ngModel is present and Angular has added the allowed characters validator
      if (ctrl && ctrl.$validators.chars) {

        // this will overwrite the default Angular email validator
        ctrl.$validators.email = function(modelValue) {
          return ctrl.$isEmpty(modelValue) || EMAIL_REGEXP.test(modelValue);
        };
      }
    }
  };
});

Gruntfile.js 看起来像

module.exports = function(grunt) {
    //grunt wrapper function 
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        ngAnnotate: {
            options: {
                singleQuotes: true
            },
            app: {
          files: [
            {
              expand: true,     // Enable dynamic expansion.
              cwd: './app/js/controllers/',      // Src matches are relative to this path.
              src: ['**/*.js'], // Actual pattern(s) to match.
              dest: './app/js/build/controllers',   // Destination path prefix.
              ext: '.min.js',   // Dest filepaths will have this extension.
              extDot: 'first'   // Extensions in filenames begin after the first dot
            },
            {
              expand: true,     // Enable dynamic expansion.
              cwd: './app/js/services/',      // Src matches are relative to this path.
              src: ['**/*.js'], // Actual pattern(s) to match.
              dest: './app/js/build/services',   // Destination path prefix.
              ext: '.min.js',   // Dest filepaths will have this extension.
              extDot: 'first'   // Extensions in filenames begin after the first dot
            },  
            {
              expand: true,     // Enable dynamic expansion.
              cwd: './app/js/directives/',      // Src matches are relative to this path.
              src: ['**/*.js'], // Actual pattern(s) to match.
              dest: './app/js/build/directives',   // Destination path prefix.
              ext: '.min.js',   // Dest filepaths will have this extension.
              extDot: 'first'   // Extensions in filenames begin after the first dot
            },                              
          ],
            }
        },
    });

    //load grunt tasks
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-ng-annotate'); 

    //register grunt default task
    grunt.registerTask('default', ['ngAnnotate', 'concat', 'uglify']);    
}

【问题讨论】:

    标签: angularjs gruntjs grunt-contrib-concat


    【解决方案1】:

    正如错误所示,您的正则表达式无效。尝试转义一些特殊字符。像这样:

    var EMAIL_REGEXP = /[A-Za-z0-9\!\#\_\?\:\&\.\- \']/;
    

    这应该允许ngAnnotate 编译。

    【讨论】:

      猜你喜欢
      • 2019-02-11
      • 2014-05-22
      • 2023-03-22
      • 2017-09-30
      • 2015-08-04
      • 2013-12-14
      • 2013-02-16
      • 2013-08-25
      相关资源
      最近更新 更多