【问题标题】:Properly loading webshims / modernizr with requirejs?使用 requirejs 正确加载 webshims/modernizr?
【发布时间】:2013-11-19 15:49:32
【问题描述】:

我的 index.html 文件顶部和 main.js 中包含了modernizr / pollyfiller:

require.config({
    paths : {
    'jquery' : 'lib/jquery-1.10.2.min',
    'jqdate' : 'lib/jquery.dateFormat-1.0',
    'webshims' : 'lib/polyfiller'
},
shim : {
    'lib/underscore' : {
        exports : '_'
    },
    'lib/backbone' : {
        deps : ["lib/underscore", "jquery"],
        exports : 'Backbone'
    },
    "modernizr" : {
        deps : ["jquery"],
        exports : "modernizr"
    },
    "webshims" : {
        deps : [ "jquery", "lib/modernizr-custom"],
        exports: "webshims"
    }
}
});
var router, vent;

    require(["jquery", "lib/underscore", "lib/backbone", "app", "lib/modernizr-custom", "webshims"], function($, _, Backbone, Router, modernizr, webshims) {

$(function() {
    $.webshims.setOptions('forms forms-ext', {
        replaceUI : false,
        waitReady : false
    });

    $.webshims.polyfill('forms forms-ext');

    router = new Router();
    vent = _.extend({}, Backbone.Events);
    $.expr.cacheLength = 1;

    Backbone.history.start({

    });
});
});

这通常会加载正常,但是,有时看起来 webshims 在我尝试调用时没有定义:

    $.webshims.setOptions('forms forms-ext', {
        replaceUI : false,
        waitReady : false
    });

    $.webshims.polyfill('forms forms-ext');

我得到错误:TypeError: $.webshims is undefined

有没有更好的加载方式?

编辑 所以,我像你说的那样更新了脚本,并且必须在路径和 shim 定义中大写 Webshims。它加载正常,但现在我收到一个错误:

Uncaught SyntaxError: Unexpected token <

在 Chrome 和

SyntaxError: syntax error


<!DOCTYPE html>

在火狐中

【问题讨论】:

  • 我不清楚为什么你必须大写 webshim。此外,您已编辑问题以显示更改,但 require 调用仍以小写形式在需求数组中显示 webshim。您现在报告的错误看起来像是试图将 HTML 文件加载为 JS 代码。我不清楚这是否与您遇到的 RequireJS 问题有关,或者 问题已经解决,现在您遇到了 another 问题。
  • 使用小写 webshims 时,会发生以下情况:错误:模块加载超时:lib/polyfiller requirejs.org/docs/errors.html#timeout
  • 我建议使用您可以使用的任何调试工具来检查从浏览器到服务器的网络查询。这就是我一直解决超时问题的方法。
  • 我已更新以反映您的建议。我正在使用本地主机并查看 polyfiller.js 加载。但它仍然超时。
  • polyfiller.js 是否真的包含 JS 代码?它是否设置了一个名为webshims 的全局变量?如果不是,那可能是您的问题的根源。

标签: backbone.js requirejs modernizr webshim


【解决方案1】:

更新答案

Alexander Farkas 在评论中指出polyfiller defines itself as "polyfiller" 是这样的:

define('polyfiller', ['jquery'], factory);

所以:

  1. 加载 polyfiller.js 不需要 shim。

  2. polyfiller.js 定义的模块应该总是被称为"polyfiller"。所以必须有一个paths 设置将模块名称polyfiller 映射到polyfiller.js 文件的实际路径。

所以应该修改原始配置以删除"webshims" shim,然后paths 设置"webshims": "lib/polyfiller" 应该变成"polyfiller": "lib/polyfiller" 并且需要调用应该是:

require(["jquery", "lib/underscore", "lib/backbone", "app", "lib/modernizr-custom", "polyfiller"], function($, _, Backbone, Router, modernizr) {

我已经从函数的参数中删除了最后一个变量,因为不需要传递模块值,因为 polyfiller.js 文件将自身注册为 $.webshims

这类似于 jQuery 将自己定义为 "jquery" 的方式(它不需要 shim,并且始终称为 "jquery")。

原答案

更改您的require 调用,以便您需要"webshims" 而不是"lib/polyfiller"

require(["jquery", "lib/underscore", "lib/backbone", "app", "lib/modernizr-custom", "webshims"], ...

您问题中的代码显示您已设置paths 配置选项,以便模块名称"webshims" 解析为"lib/polyfiller",并为其创建了看起来合理的填充程序。但是,当您需要 webshims 模块时,您将其称为 "lib/polyfiller"。 RequireJS 不会做反向解析来确定"lib/polyfiller""webshims"

或者,您可以从 paths 中删除 "webshims" 名称并重命名 shim,以便将其设置为 "lib/polyfiller"。但是,我认为在整个应用程序中通过单字名称引用 3rd 方库而不是为它们提供路径是一种更好的做法。所以“jquery”、“bootstrap”、“underscore”和“webshims”等,而不是“lib/...”。

【讨论】:

  • 看起来错误是由于它试图加载 webshims.js 引起的,并且出现 302 错误。如上所述更改代码后,当我在 require 中引用 webshims 时,它不应该在寻找 lib/polyfiller.js 吗?
  • 它正在寻找不存在的 webshims.js
  • 您将“webshims”更改为“Webshims”无处不在,但在您的require 通话中除外。它仍然在那里读取“webshims”(全部小写)。如果您的配置中有“Webshims”,而您的要求中有“webshims”,那么 RequireJS 将首先查找“webshims”,然后再查找“webshims.js”,因为“Webshims”!=“webshims”。
  • 是的。至少似乎可以解析为正确的文件。您的建议造成了超时。不过谢谢你的帮助。
  • 不知道这是否会改变一些东西,但是 webshims polyfill.js(应该包含的文件)已经将自己注册为 polyfill(参见:github.com/aFarkas/webshim/blob/gh-pages/src/polyfiller.js#L8
猜你喜欢
  • 2014-03-06
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多