Cas*_*ynn 7 javascript amd coffeescript requirejs
我的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()
Run Code Online (Sandbox Code Playgroud)
这是我的优化器配置.在将'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
Run Code Online (Sandbox Code Playgroud)
我在chrome中看到的具体错误是:"未捕获的TypeError:无法读取未定义的属性'默认值'"
这与此代码段相关:
/* Set the defaults for DataTables initialisation */
$.extend( true, $.fn.dataTable.defaults, {
Run Code Online (Sandbox Code Playgroud)
知道可能出了什么问题吗?谢谢!
Pet*_*ong 14
我遇到了同样的问题.我认为发生此错误的原因是因为DT_bootstrap.js它不是AMD模块,而是取决于一个的副作用.在这种情况下jquery.dataTables.js.
当RequireJS优化器将您引用的所有模块组合到一个大型JS文件中时,raw DT_bootstrap.js位于其中间的某个位置,有些位于之后jquery.dataTables.js.问题是DT_bootstrap.js在加载组合的js文件时立即评估.它希望$.fn.dataTable在遇到该行时被定义:
$.extend( true, $.fn.dataTable.defaults, {
Run Code Online (Sandbox Code Playgroud)
由于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
}));
Run Code Online (Sandbox Code Playgroud)
它为我解决了这个问题.
| 归档时间: |
|
| 查看次数: |
5151 次 |
| 最近记录: |