简体   繁体   English

requirejs:需要一个r.js优化文件

[英]requirejs: requiring an r.js optimized file

I've built a web app using require.js, and successfully used r.js to combine all module definitions into a single file. 我使用require.js构建了一个Web应用程序,并成功地使用r.js将所有模块定义组合到一个文件中。

Once the r.js optimized file has been created, I expected to just be able to require the optimized file on its own, but it fails to execute any of code after loading and defining the modules: 一旦创建了r.js优化文件,我希望自己能够单独要求该优化文件,但是在加载和定义模块后,它无法执行任何代码:

require([
    'app1/optimizedAppFile'
], function (optimizedApp) {

    //optimizedApp is undefined, even though it loaded 
    //the file and executed the module definitions in debugger

});

Is it appropriate to load / instantiate the app by defining the path to the top level module of the optimized file in require.config.js, and then requiring that in main.js? 通过在require.config.js中定义优化文件的顶级模块的路径,然后在main.js中要求该路径来加载/实例化应用程序是否合适? ie

requirejs.config({

    paths: {
        'optimizedApp.topLevelModule' : 'app1/optimizedAppFile'
        //optimizedApp.topLevelModule is the full module name
        //app1/optimizedAppFile is the combined file from r.js
    }

});

Yes, after optimized your code via rjs, you can just require that file. 是的,通过rjs优化代码后,您只需要该文件即可。

However, I had this problem today as well. 但是,我今天也有这个问题。 After spending hours debugging I found out the current 1.1.2 backbone version has a piece of code detecting if AMD (the function "define" exists). 经过数小时的调试,我发现当前的1.1.2骨干版本具有一段代码来检测AMD(功能“ define”是否存在)。

after removing it, backbone looks like this 删除后,主干看起来像这样

//     Backbone.js 1.1.2

//     (c) 2010-2014 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
//     Backbone may be freely distributed under the MIT license.
//     For all details and documentation:
//     http://backbonejs.org

(function(root, factory) {
  root.Backbone = factory(root, {}, root._, (root.jQuery || root.Zepto || root.ender ||     root.$));
}(this, function(root, Backbone, _, $) {

So basically what I did is make it globally accessible and add shim for it 所以基本上我所做的就是使其可以全局访问并为其添加填充

 require.config({
  baseUrl: "/static/src/scripts/js",
  paths: {
    jquery: 'vendors/jquery/jquery',
    underscore: 'vendors/underscore/underscore',
    backbone: 'vendors/backbone/backbone',
    marionette: 'vendors/backbone/backbone.marionette'
  },
  shim: {
    jquery: {
      exports: "jQuery"
    },
    underscore: {
      exports: "_"
    },
    backbone: {
      deps: ['underscore', 'jquery'],
      exports: 'Backbone'
    },
    marionette: {
      deps: ['backbone'],
      exports: 'Marionette'
    }
  }
});

Inspect element and check your console and see what it complains, and turns of optimize by setting the optimize option to false for the rjs and look for which part it errors out. 检查元素并检查您的控制台,查看其抱怨之处,并通过将rjs的optimize选项设置为false来进行优化,并查找错误的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM