Grunt无法解释的语法错误

时间:2016-01-21 09:30:45

标签: javascript syntax gruntjs task

我在运行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']);
};

1 个答案:

答案 0 :(得分:0)

对不起伙计们, 我的grunt.js代码没有任何问题。 问题是app/VPAIDPlugins.js文件和我用来在文件中查找数组的正则表达式。我已经更改了该文件,RegEx无效,eval返回意外的错误。