2016-01-21 110 views
0

運行我的Grunt任務時出現了一個非常奇怪的問題。 一切都很好,當它突然開始返回該錯誤:Grunt無法解釋的語法錯誤

Loading "gruntfile.js" tasks...ERROR 
SyntaxError: Unexpected token) 

我不記得觸及gruntfile.js代碼,事實上,它並沒有給我一個錯誤行讓我瘋了。

你能否看看我在文件中丟失了什麼? 非常感謝您提前!

var path = require("path"); 

module.exports = function (grunt) 
{ 
    var pluginsToUglify  = ["dist/*.js","!**/VPAIDAdLinear.js","!**/VPAIDPlugins.js","!**/screenad_*","!**/grunt*","!**/*bkup*"], 
     requiresToUglify = [], 
     filesToCopy   = ['app/VPAIDPlugins.js','app/data/*feed.json','app/images/*.gif','app/images/*.png','app/images/*.jpg','app/videos/*.mp4','app/videos/*.webm','!1x1.gif','!**/*bkup.js','!**/*polyfills.js','!**/*screenad_*','!**/*grunt*'], 
     cssToMin   = ["app/styles/VPAIDAdLinear.css","!**/*bkup*"], 
     jsToReplace   = ["./dist/*.js"]; 

    grunt.initConfig({ 
     stylus: { 
      compile: { 
       options: { 
        compress: false, 
        relativeDest: './' 
       }, 
       files: { 
        src: ["**/*.styl"], 
        ext: ".css" 
       } 
      } 
     }, 
     jshint: { 
      files: { 
       src: ['**/*.js', '!**/*bkup.js', '!**/*polyfills.js','!**/*screenad_*','!**/*grunt*'] 
      } 
     }, 
     preprocess: { 
      options: { 
       inline: true, 
       context: { 
        DEV: false 
       } 
      }, 
      js: { 
       src: [ 
        './dist/VPAIDPlugins.js' 
       ] 
      } 
     }, 
     clean: { 
      prebuild: { 
       src: ['./dist'] 
      }, 
      previewer: { 
       src: ['*.*','!package.json','!gruntfile.js','!readme.md'] 
      } 
     }, 
     // make a zipfile 
     compress: { 
      build: { 
       options: { 
        archive: './dist.zip' 
       }, 
       files: [{ 
        src: ['dist/*'], 
        dest: '', 
        filter: 'isFile' 
       } // includes files in path 
       ] 
      } 
     }, 
     watch: { 
      options: { 
       livereload: false 
      }, 
      js: { 
       files: ['**/*.js', '!**/*bkup.js', '!**/*polyfills.js'], 
       tasks: ['jshint'] 
      }, 
      css: { 
       files: ['**/*.styl'], 
       tasks: ['stylus'] 
      } 
     } 
    }); 

    require("matchdep").filterDev("grunt-*").forEach(grunt.loadNpmTasks); 

    function checkPluginsToUglify() 
    {  
     var pluginsFile  = grunt.file.read("app/VPAIDPlugins.js"); 
     var pluginsArray = pluginsFile.replace(/[\S\s]*?(\[[\S\s]*\])[\S\s]*/i,"$1").replace(/(\"[,\n\r\s])/g,".js$1"); 

     pluginsToUglify  = eval(pluginsArray); 
     pluginsToUglify  = pluginsToUglify.map(function(item){ return "app/plugins/"+item; }); 
     jsToReplace   = [].concat(pluginsToUglify); 
    } 

    function checkFilesToCopy() 
    {   
     var pluginFile, 
      requires = "", 
      requiresArray = []; 

     for(var i = 0; i < pluginsToUglify.length; i++) 
     { 
      pluginFile   = grunt.file.read(pluginsToUglify[i]); 
      requires  = eval(pluginFile.replace(/[\S\s]*?\.requires\s*=\s*(\[[\S\s]*?\])[\S\s]*/i,"$1")); 
      requires   = requires.map(function(item) 
              { 
               var path = ""; 
               if(item.indexOf(".js") !== -1) 
               { 
                path = "app/scripts/"+item; 
                requiresToUglify.push(path); 
               } 
               else if (item.indexOf(".css") !== -1) 
                path = "app/styles/"+item; 

               return path; 
              }); 

      requiresArray = requiresArray.concat(requires); 
     } 

     if(requiresArray.length > 0) 
      filesToCopy = filesToCopy.concat(requiresArray); 

     jsToReplace = jsToReplace.concat(filesToCopy.filter(function(item){ return /\.js$/i.test(item); })); 
     cssToMin  = cssToMin.concat(filesToCopy.filter(function(item){ return item.indexOf(".css") !== -1; })); 

     jsToReplace = jsToReplace.filter(function(item, pos) { return jsToReplace.indexOf(item) == pos; }); 

     cssToMin  = cssToMin.filter(function(item, pos) { return cssToMin.indexOf(item) == pos; });   
     filesToCopy = filesToCopy.filter(function(item, pos) { return filesToCopy.indexOf(item) == pos; });   
    } 

    function mergeTasks() 
    { 
     grunt.config.merge({ 
      'string-replace': { 
       build: { 
        files: [ 
         { 
          expand : true, 
          flatten : true, 
          filter : "isFile", 
          src  : jsToReplace, 
          dest : 'dist/' 
         } 
        ], 
        options: { 
         replacements: [ 
          { 
           pattern: /console\.log\(.*?\);?/ig, 
           replacement: '' 
          }, 
          { 
           pattern: /^\s*\/\/\s*$/igm, 
           replacement: '' 
          } 
         ] 
        } 
       } 
      } 
     }); 

     grunt.config.merge({ 
      cssmin: { 
       build: { 
        files: [{ 
         expand : true, 
         flatten : true, 
         filter : "isFile", 
         src  : cssToMin, 
         dest : "dist", 
         ext  : ".css" 
        }] 
       } 
      } 
     }); 

     grunt.config.merge({ 
      copy: { 
       build: { 
        files: [ 
         { 
          expand : true, 
          flatten : true, 
          filter : "isFile", 
          dest : "dist/", 
          src  : filesToCopy 
         } 
        ] 
       }, 
       previewer: { 
        files: [ 
         { 
          expand : true, 
          flatten : true, 
          filter : "isFile", 
          dest : ".", 
          src  : ["dist/*.*","app/data/adParameters.json"] 
         } 
        ] 
       } 
      } 
     }); 

     grunt.config.merge({ 
      uglify: { 
       allPlugins: { 
        files: { 
         "dist/allplugins.min.js"  : pluginsToUglify, 
         "dist/VPAIDAdLinear.min.js" : ["app/VPAIDAdLinear.js"] 
        } 
       }, 
       pluginContainer: { 
        files: { 
         "dist/VPAIDPlugins.js"  : ["dist/VPAIDPlugins.js"] 
        } 
       }, 
       requires: { 
        files: [ 
         { 
          expand : true, 
          flatten : true, 
          filter : "isFile", 
          src  : requiresToUglify, 
          dest : "dist/" 
         } 
        ] 
       } 
      } 
     }); 

     var pluginsToClean = pluginsToUglify.map(function(item){ return path.join("dist", path.basename(item)); }); 
     grunt.config.merge({ 
      clean: { 
       build: { 
        src: ['dist/allplugins.min.js'].concat(pluginsToClean) 
       } 
      } 
     }); 
    } 

    checkPluginsToUglify(); 
    checkFilesToCopy(); 
    mergeTasks(); 

    grunt.registerTask('default', ['watch']); 
    grunt.registerTask('build', ['clean:prebuild', 'clean:previewer', 'copy:build', 'string-replace:build', 'uglify:allPlugins', 'uglify:requires', 'preprocess:js', 'uglify:pluginContainer', 'cssmin:build', 'clean:build', 'compress:build']); 
    grunt.registerTask('buildPreview', ['clean:prebuild', 'clean:previewer', 'copy:build', 'string-replace:build', 'uglify:allPlugins', 'uglify:requires', 'preprocess:js', 'uglify:pluginContainer', 'cssmin:build', 'clean:build', 'copy:previewer']); 
}; 

回答

0

對不起球員, 有什麼錯我grunt.js代碼。 問題是app/VPAIDPlugins.js文件和我用來在文件中查找數組的正則表達式。我已更改該文件,並且RegEx無效,eval返回意外)錯誤。