Require.js + TypeError:优化后未定义Backbone

时间:2012-09-23 07:00:13

标签: javascript scala backbone.js playframework requirejs

Backbone应用程序可以很好地预先优化。在使用r.js(使用杏仁)后,我成功生成了一个单独的输出文件,该文件似乎总是在主干之后的模块中抛出'TypeError:Backbone is undefined'错误。

例如,当r.js的输出如下所示:

/consumer/public/javascripts/vendor/almond.js
/consumer/public/javascripts/vendor/jquery-1.7.2.js
/consumer/public/javascripts/vendor/underscore.js
/consumer/public/javascripts/vendor/bootstrap.js
/consumer/public/javascripts/vendor/backbone.js
/consumer/public/javascripts/rjs/src/mnp/prop_m.js
/consumer/public/javascripts/rjs/src/mnp/prop_c.js
/consumer/public/javascripts/vendor/play-mustache.js
...
/consumer/public/javascripts/main.js

在浏览器中加载优化文件后,在引用Backbone时,会在'prop_m.js'模块中抛出上述错误。使用require 2.0.5,Backbone 0.9.2和almond 0.1.4。在此先感谢您的帮助。

PS:build.js看起来像这样:

    ({
    baseUrl:   "./",
    mainConfigFile: "main.js",
    paths: {
    'almond': 'vendor/almond',

        // need a path to the cs-compiled .js file
        'app' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/app',
        'mnp/router' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/router',
        'mnp/search_form_v' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/search_form_v',
        'mnp/prop_c' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/prop_c',
        'mnp/prop_m' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/prop_m',
        'mnp/prop_list_v' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/prop_list_v',
        'mnp/prop_item_v' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/prop_item_v'
    },


    /*not compatible with modules*/
    name: 'vendor/almond',
    include: ["main"],

    out: "dist/app.js",
    /*not compatible with modules*/

    keepBuildDir: false,

    preserveLicenseComments: false,
    optimize: "none", 
    wrap: {
        start: "(function(global, define) {\n"+
           // check for amd loader on global namespace
            "  var globalDefine = global.define;\n",

        end:   "  var library = require('main');\n"+
            "  if(typeof module !== 'undefined' && module.exports) {\n"+
            // export library for node
            "    module.exports = library;\n"+
            "  } else if(globalDefine) {\n"+
            // define library for global amd loader that is already present
            "    (function (define) {\n"+
            "      define(function () { return library; });\n"+
            "    }(globalDefine));\n"+
            "  } else {\n"+
            // define library on global namespace for inline script loading
            "    global['main'] = library;\n"+
            "  }\n"+
            "}(this));\n"
    }


})`

3 个答案:

答案 0 :(得分:2)

FWIW:如果有人遇到这个问题......使用骨干和下划线的amd版本似乎可以解决问题。即。 amd版本工作,垫片没有

Loading Backbone and Underscore using RequireJS

答案 1 :(得分:1)

当我忘记包含shim时,我遇到了这个问题。我通过在构建配置中将mainConfigFile设置为我的main.js文件来解决此问题,例如:如下所示:https://github.com/jrburke/r.js/blob/master/build/example.build.js

答案 2 :(得分:0)

您可以尝试使用inlineText: true来确保主干位于已编译的.js文件中

此外,为了避免必须留意存储路径的2个对象,我建议像mainConfigFile: "main.js"这样的东西 - 像魅力一样工作;) 编辑:为什么要设置mainConfigFile选项路径?