javascript - 使用 requirejs 和 r.js 优化器时无法加载 jQuery 插件

标签 javascript coffeescript requirejs amd

我的 requirejs 优化器有点问题。运行优化器后,我在构建/编译文件中收到一些错误消息。在没有优化步骤的情况下运行我的 Web 应用程序时,我没有任何错误。

这是我的 client.js 文件(包含配置)(coffeescript)

requirejs.config
  baseUrl: '/source/'
  paths:
    text:                 'lib/text'
    io:                   'lib/socket.io'
    underscore:           'lib/underscore'
    backbone:             'lib/backbone'
    jquery:               'lib/jquery'
#    almond:               'lib/almond'
    bootstrap:            'lib/bootstrap'
    bootstrapFileUpload:  'lib/bootstrap-fileupload'
    jqueryUniform:        'lib/jquery.uniform'
    jqueryBrowser:        'lib/jquery.browser'
    datatables:           'lib/jquery.dataTables'
    datatables_bootstrap: 'lib/DT_bootstrap'
  shim:
    io:
      exports: 'io'
    jquery:
      exports: 'jQuery'
    jqueryBrowser:
      deps:    ['jquery']
    jqueryUniform:
      deps:    ['jqueryBrowser', 'jquery']
    underscore:
      exports: '_'
    backbone:
      deps:    ['underscore', 'jquery']
      exports: 'Backbone'
    datatables_bootstrap:
      deps:    ['jquery', 'datatables']
    datatables:
      deps:    ['jquery']


require ['routers/router', 'backbone'], (Router, Backbone) ->
  MainRouter = new Router()
  Backbone.history.start()

这是我的优化器配置。在需要“requirejs”作为模块后,我从 nodejs 运行优化器。

  config =
    baseUrl: __dirname + '/../client/source'
    name:    'lib/almond'
    include: './client'
    optimize: 'none'
    out:     __dirname + '/../client/' + hash + '.js'
    paths:
      text:                 'lib/text'
      io:                   'lib/socket.io'
      underscore:           'lib/underscore'
      backbone:             'lib/backbone'
      jquery:               'lib/jquery'
      bootstrap:            'lib/bootstrap'
      bootstrapFileUpload:  'lib/bootstrap-fileupload'
      jqueryUniform:        'lib/jquery.uniform'
      jqueryBrowser:        'lib/jquery.browser'
      datatables:           'lib/jquery.dataTables'
      datatables_bootstrap: 'lib/DT_bootstrap'
    shim:
      bootstrap:
        exports: 'bootstrap'
      bootstrapFileUpload:
        exports: 'bootstrapUpload'
      io:
        exports: 'io'
      jquery:
        exports: 'jQuery'
      jqueryBrowser:
        deps:    ['jquery']
      jqueryUniform:
        deps:    ['jqueryBrowser', 'jquery']
      underscore:
        exports: '_'
      backbone:
        deps:    ['underscore', 'jquery']
        exports: 'Backbone'
      datatables:
        deps:    ['jquery']
      datatables_bootstrap:
        deps:    ['jquery', 'datatables']



  requirejs.optimize config, (buildResponse) ->
    js = true
    if js && css
      require './server'
  , (err) ->
    console.log 'requirejs err'
    console.log err

我在 chrome 中看到的具体错误是: “未捕获的类型错误:无法读取未定义的属性‘默认值’”

与此片段相关的内容:

/* Set the defaults for DataTables initialisation */
$.extend( true, $.fn.dataTable.defaults, {

知道可能出了什么问题吗?谢谢!

最佳答案

我遇到了同样的问题。我认为发生此错误的原因是因为 DT_bootstrap.js 不是 AMD 模块,而它取决于一个的副作用。在这种情况下 jquery.dataTables.js

当 RequireJS 优化器将您引用的所有模块组合到一个大的 JS 文件中时,原始 DT_bootstrap.js 位于它的中间某处,jquery.dataTables.js< 之后的某处。问题是 DT_bootstrap.js 在加载组合的 js 文件时立即被评估。它希望在遇到以下行时定义 $.fn.dataTable:

$.extend( true, $.fn.dataTable.defaults, {

由于 jquery.dataTables.js 是一个 AMD 模块,它已被编译但尚未评估。只有在需要它作为依赖项的后续代码中才会对其进行评估,然后才会定义 $.fn.dataTable

我通过在 AMD 模块定义中包装“DT_bootstrap.js”来解决这个问题,就像这里所做的那样:https://github.com/amdjs/backbone/blob/master/backbone.js#L8-L24

例如:

(function(root, factory) {
  // Set up DT_bootstrap appropriately for the environment.
  if (typeof define === 'function' && define.amd) {
    // AMD
    define(['jquery', 'datatables', 'bootstrap'], function($) {
      factory($);
    });
  } else {
    // Browser globals
    factory(root.jQuery);
  }
}(this, function($) {
    // <--- original DT_bootstrap.js goes here 
}));

它为我解决了这个问题。

关于javascript - 使用 requirejs 和 r.js 优化器时无法加载 jQuery 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15371233/

相关文章:

javascript - 如何阻止 "tel:number"链接在桌面/平板电脑上工作

javascript - React setState 不起作用,它的回调也不起作用

javascript - 如何通过 coffeescript 获取字符串的最后一个字符?

javascript - 如何从 react 按钮 onClick 获取值

javascript - 如何使用javascript检测Safari 13上是否启用了防止跨站点跟踪

javascript - 如何在coffeescript中将两个数组压缩到一个对象中?

javascript - 使用 Backbone 路由器回调突出显示所选项目

javascript - Videojs-ima 插件与 requirejs 不包括加载插件

javascript - 如何使用 requirejs 正确定义 shim 配置

javascript - 使用 RequireJS 和 JQuery onclick 事件处理 "this"