gpt4 book ai didi

javascript - requirejs:需要 r.js 优化文件

转载 作者:行者123 更新时间:2023-11-28 08:18:52 25 4
gpt4 key购买 nike

我使用 require.js 构建了一个 Web 应用程序,并成功使用 r.js 将所有模块定义合并到一个文件中。

创建 r.js 优化文件后,我希望能够单独请求优化文件,但在加载和定义模块后它无法执行任何代码:

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

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

});

通过在 require.config.js 中定义优化文件的顶级模块的路径,然后在 main.js 中 require 来加载/实例化应用程序是否合适?即

requirejs.config({

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

});

最佳答案

是的,通过 rjs 优化代码后,您可以只需要该文件。

但是,我今天也遇到了这个问题。经过几个小时的调试,我发现当前的 1.1.2 主干版本有一段代码检测 AMD 是否存在(“define”函数存在)。

删除后,主干看起来像这样

//     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, _, $) {

所以基本上我所做的就是使其全局可访问并为其添加填充

 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'
}
}
});

检查元素并检查您的控制台,看看它提示什么,以及优化的轮次通过将 rjs 的优化选项设置为 false 并查找出错的部分。

关于javascript - requirejs:需要 r.js 优化文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23248478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com