Search code examples
angularjsgruntjsgrunt-contrib-concat

AngularJS + Grunt ngAnnotate and single quote


I'm trying to minify my app but can't go over Grunt ngAnnotate as it throws an error

error: couldn't process source due to parse error Error parsing regular expression: Invalid regular expression: /[A-Za-z0-9!#_\?:&.- ']/: Range out of order in character class (3:22)

on a directive which looks like

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 looks like

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']);    
}

Solution

  • As the error suggests, your regular expression is invalid. Try escaping some of the special characters. Like this:

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

    That should allow ngAnnotate to compile.