使用grunt在require上设置等待秒数

时间:2013-11-11 19:08:26

标签: javascript requirejs gruntjs require

我正在使用我的应用上的Require遇到加载超时错误。我正在使用grunt和require优化器构建我的require文件。我已经设定了等待时间并且已经看到我当地的超时时间消失了,但它仍然在生产中发生。我在我的grunt文件中设置了我的等待时间,但也尝试了我的主js文件,但是没有看到它们被推送到活动脚本文件的位置。有谁知道我在哪里能找到这个价值?当grunt运行任务时,它不会写入require.js文件,并且当require和optimizer运行时,它不包含在我的main.js文件中。浏览器在哪里获取此值?我在prod上的require.js文件中看到了最初的7秒超时,但找不到我的选项被拾取的位置。

这是我需要的咕噜声任务:

 requirejs: {
        options: {
            baseUrl: ".",
            appDir: "js",
            waitSeconds: 40,
            findNestedDependencies: true,
            mainConfigFile: "js/common.js",
            dir: "../assets/js",
            paths: {
                "rs": "mains/recordsearch"
            },
            optimize: "none",
            // modules to be optimized and bundled
            // "include" and "exclude" can be used here
            // to add or ignore dependencies
            modules: [{
                name: "common"
            }, {
                name: "rs/home"
            }, {
                name: "commons/html5shim"
            }]
        },
        dev: {},
        prod: {
            options: {
                optimize: "uglify"
            }
        }
    },

和我的“主要”需要js页面:

requirejs.config({
  paths: {
    // libraries path
    "jquery": "libs/jquery",
    "jquery-ui": "libs/jquery-ui",
    "modernizr": "libs/modernizr.custom",

    // validation
    "boolean": "validation/custom/boolean",
    "comparefield": "validation/custom/comparefield",
    "expirationdate": "validation/custom/expirationdate",
    "securitycode": "validation/custom/securitycode",
    "fullname": "validation/custom/fullname",
    "zip": "validation/custom/zip",
    "ajaxval": "validation/framework/jquery.unobtrusive-ajax.min",
    "vsdoc": "validation/framework/jquery.validate-vsdoc",
    "validate": "validation/framework/jquery.validate.min",
    "unobtrusive": "validation/framework/jquery.validate.unobtrusive.min",

    // plugins
    "acmodal": "plugins/acmodal",
    "acbutton": "plugins/acbutton",
    "acnav": "plugins/acnav",
    "actooltip": "plugins/actooltip",

    // utils and polyfills
    "bridge": "utils/pluginbridge",
    "object.create": "polyfills/object.create",
    "counter": "utils/counter",

    // funnels
    "rs": "mains/recordsearch"
},
// The shim section allows you to specify 
// dependencies between non AMD compliant files.
shim: {
    "jquery": {
        exports: "$"
    },
    "modernizr": {},
    "acbutton": ["jquery", "object.create", "bridge"],
    "acnav": ["jquery", "object.create", "bridge", "modernizr"],
    "acmodal": ["jquery", "object.create", "bridge", "jquery-ui"],
    "actooltip": ["jquery", "object.create", "bridge", "counter"],
    "ajaxval": ["validate", "unobtrusive", "boolean", "comparefield", "expirationdate", "comparefield", "boolean", "fullname", "zip", "securitycode"]
},
});

require([
"jquery",
"ajaxval",
"actooltip",
"acbutton",
"acnav",
"acmodal",
"commons/errorhandling"
], function () {

//waiting until dom is loaded to load the page modules
$(function () {
    // the start module is defined on the body tag.
    // example: <body data-jspage="rs/main"> or <body data-jspage="rs/main, rs/common">
    var startModule = $("body").attr("data-jspage");
    var siteArea = $("body").attr("data-area");

    if (startModule) {
        require([startModule]);
    }
    if (siteArea === "FE") {
        require(["commons/signin"]);
    }
 });
});

提前感谢您的任何反馈。

1 个答案:

答案 0 :(得分:2)

经过进一步测试,我发现waitSeconds必须进入mainconfigFile,在这种情况下,我在上面发布的“主”js。它从该文件读入页面,该文件正在生产中,而不是gruntfile。如果将waitSeconds选项放入gruntfile中,除非您在节点中运行站点,否则它将无法工作。虽然grunt-require-contrib文档将其作为一种选择,但不确定它在什么情况下有效。我查看了grunt节点包中的r.js脚本,并且我在该文件中没有任何改变会影响到输出文件的任何内容,只会影响优化和捆绑。我通过将waitSeconds值传递给我的错误来运行一些测试,并发现更改mainconfigFile中的值显示在浏览器中,但gruntfile任务中的任何更改都没有做任何事情。希望这有助于其他人。