Grunt多次汇编JS文件

时间:2016-05-24 12:07:37

标签: javascript node.js gruntjs grunt-contrib-concat

我正在Node.js网站上工作,我正在使用Grunt来连接和缩小我的CSS和JS文件。但是,在运行grunt命令后,我收到错误消息:

fullPage: Fullpage.js can only be initialized once and you are doing it multiple times!

这是我的咕噜文件:

/*global module */
module.exports = function (grunt) {
    "use strict";
    grunt.initConfig({
        // read in the project settings from the package.json file into the pkg property
        pkg: grunt.file.readJSON("package.json"),

        // Install only the bower packages that we need
        bower: {
            install: {
                options: {
                    "targetDir": "./public/lib",
                    "copy": true,
                    "cleanup": true,
                    "install": true
                }
            }
        },

        concat: {
            css: {
                src: ["public/lib/css/**/*.css", "public/css/cts.css"],
                dest: "public/lib/dist/main.css"
            },
            js: {
                src: ["public/lib/**/jquery.js", "public/lib/**/*.js", "public/js/cts.js"],
                dest: "public/lib/dist/main.js"
            }
        },

        cssmin: {
            target: {
                files: {
                    "public/lib/dist/main.min.css": "public/lib/dist/main.css"
                }
            }
        },

        uglify : {
            js: {
                files: {
                    "public/lib/dist/main.min.js": "public/lib/dist/main.js"
                }
            }
        },

        copy: {
            files: {
                expand: true,
                flatten: true,
                src: ["public/lib/fonts/**/*"],
                dest: "public/lib/fonts/",
                filter: "isFile"
            }
        }
    });

    // Add all plugins that your project needs here
    grunt.loadNpmTasks("grunt-bower-task");
    grunt.loadNpmTasks("grunt-contrib-concat");
    grunt.loadNpmTasks("grunt-contrib-copy");
    grunt.loadNpmTasks("grunt-contrib-cssmin");
    grunt.loadNpmTasks("grunt-contrib-uglify");
    grunt.loadNpmTasks("grunt-contrib-watch");

    // this would be run by typing "grunt test" on the command line
    // the array should contains the names of the tasks to run
    grunt.registerTask("test", []);

    // define the default task that can be run just by typing "grunt" on the command line
    // the array should contains the names of the tasks to run
    grunt.registerTask("default", [ "bower", "concat", "cssmin", "uglify", "copy"]);
    grunt.registerInitTask("install", ["bower"]);
};

如果有什么我认为jQuery会被连接多次,但事实并非如此。有什么建议我可能做错了吗?

编辑:这是我升级后的grunt文件,其中包含concat.src中列出的所有第三方库。

/// <binding BeforeBuild='default' />
/*global module */
module.exports = function (grunt) {
    "use strict";
    grunt.initConfig({
        // read in the project settings from the package.json file into the pkg property
        pkg: grunt.file.readJSON("package.json"),

        // Install only the bower packages that we need
        bower: {
            install: {
                options: {
                    "targetDir": "./public/lib",
                    "copy": true,
                    "cleanup": true,
                    "install": true
                }
            }
        },

        concat: {
            css: {
                src: ["public/lib/css/**/*.css", "public/css/cts.css"],
                dest: "public/lib/dist/main.css"
            },
            js: {
                src: [
                    "public/lib/js/jquery/jquery.js",
                    "public/lib/js/bootstrap/bootstrap.js",
                    "public/lib/js/fullpage.js/jquery.fullpage.js",
                    "public/lib/js/jquery-easing-original/jquery.easing.js",
                    "public/lib/js/slimscroll/jquery.slimscroll.js",
                    "public/lib/js/wow/wow.js",
                    "public/js/cts.js"
                    ],
                dest: "public/lib/dist/main.js"
            }
        },

        cssmin: {
            target: {
                files: {
                    "public/lib/dist/main.min.css": "public/lib/dist/main.css"
                }
            }
        },

        uglify : {
            js: {
                files: {
                    "public/lib/dist/main.min.js": "public/lib/dist/main.js"
                }
            }
        },

        copy: {
            files: {
                expand: true,
                flatten: true,
                src: ["public/lib/fonts/**/*"],
                dest: "public/lib/fonts/",
                filter: "isFile"
            }
        }
    });

    // Add all plugins that your project needs here
    grunt.loadNpmTasks("grunt-bower-task");
    grunt.loadNpmTasks("grunt-contrib-concat");
    grunt.loadNpmTasks("grunt-contrib-copy");
    grunt.loadNpmTasks("grunt-contrib-cssmin");
    grunt.loadNpmTasks("grunt-contrib-uglify");
    grunt.loadNpmTasks("grunt-contrib-watch");

    // this would be run by typing "grunt test" on the command line
    // the array should contains the names of the tasks to run
    grunt.registerTask("test", []);

    // define the default task that can be run just by typing "grunt" on the command line
    // the array should contains the names of the tasks to run
    grunt.registerTask("default", [ "bower", "concat", "cssmin", "uglify", "copy"]);
    grunt.registerTask("combine", [ "concat", "cssmin", "uglify", "copy"]);
    grunt.registerInitTask("install", ["bower"]);
};

1 个答案:

答案 0 :(得分:1)

您的问题似乎出现在django.contrib.sites

DoesNotExist at /accounts/login/
Site matching query does not exist.
Request Method: GET
Request URL:    http://127.0.0.1:8000/accounts/login/
Django Version: 1.9
Exception Type: DoesNotExist
Exception Value:    
Site matching query does not exist.
Exception Location: c:\Python27\lib\site-packages\django\db\models\query.py in get, line 387
Python Executable:  c:\Python27\python.exe
Python Version: 2.7.11
Python Path:    
['E:\\myproject\\mysite',
 'C:\\Windows\\SYSTEM32\\python27.zip',
 'c:\\Python27\\DLLs',
 'c:\\Python27\\lib',
 'c:\\Python27\\lib\\plat-win',
 'c:\\Python27\\lib\\lib-tk',
 'c:\\Python27',
 'c:\\Python27\\lib\\site-packages']
Server time:    Tue, 24 May 2016 13:26:56 +0000

这将会多次添加您的文件,因为在src中提到的路径中可能存在一些常见的文件。

您应该将所有供应商文件(例如jquery)移出公共目录,然后放入另一个供应商文件,例如供应商。

你的src应该看起来像

concate.js.src

如您所见,这两条路径中没有共同文件。

另外,最好将第三方代码作为兄弟文件夹放在app目录之外,而不是在其中。

修改

啊!我看到你的问题是什么。您希望在其他供应商文件中首先使用jquery。

src: ["public/lib/**/jquery.js", "public/lib/**/*.js", "public/js/cts.js"] src: ["vendor/**/*.js", "public/**/*.js"] 可能会导致文件被添加两次。

试试这个

public/lib/**/jquery.js

首先放置jquery的完整路径public/lib/**/*.js,然后src: ["public/lib/jquery/jquery.js", "public/lib/**/*.js", "!public/lib/jquery/jquery.js", public/js/cts.js"] 应该阻止jquery作为public/lib/jquery/jquery.js的一部分再次添加

从这里获得上述模式http://gruntjs.com/configuring-tasks#globbing-patterns

如果这仍然不起作用,那么另一个选项是单独添加src数组中的所有路径。如果你有一个requirejs配置只是从那里复制路径,因为jquery可能不是你将来面临的唯一依赖问题。