【问题标题】:r.js cannot resolve dependencies mentioned in shimr.js 无法解析 shim 中提到的依赖项
【发布时间】:2015-10-14 15:59:15
【问题描述】:

我最近加入了一个使用 Backbonejs 构建的项目,(使用 Marionette 进行视图渲染)+ nodejs。他们还使用 requirejs 来加载backbonejs 文件。想在这个阶段补充一点,我以前从未使用过backbonejs或requirejs,因此我正在努力解决我稍后描述的问题。

一些有助于解释我遇到的问题的代码(所有这些代码已经由以前的开发人员编写)

文件夹结构:

/public
 /js
  /collection (consists all Backbone.js collections files)
  /lib
   /bower_components
    /backone
    /marionette
    /etc
  /models (consists all Backbone.js models files)
  /views (consists all Backbone.js view files)
  /main.js
  /main.build.js
  /app.js
  /controller.js
  /router.js

我认为与问题相关的文件中的代码:

main.js

requirejs.config({
    paths: {
        'async': 'lib/bower_components/requirejs-plugins/src/async',
        'jquery': 'lib/bower_components/jquery/dist/jquery.min',
        'underscore': 'lib/bower_components/underscore/underscore-min',
        'lodash': 'lib/bower_components/lodash/dist/lodash.min',
        'backbone': 'lib/bower_components/backbone/backbone',
        'marionette': 'lib/bower_components/marionette/lib/backbone.marionette.min',
        'markercluster':'lib/markercluster',
        'jquerymobile': 'lib/jquery.mobile-1.4.0.min',
        'hogan': 'lib/template-2.0.0.min',
        'templates': '/templates',
        'real': 'lib/mainjs',
        'touch': 'lib/jquery.touchSwipe.min',
        'mouse': 'lib/jquery.mousewheel',
        'moment': 'lib/moment-2.5.1.min',
        'humanize': 'lib/bower_components/humanize-plus/public/dist/humanize.min',
        'validator': 'lib/bower_components/validator-js/validator.min',
        'real': 'lib/mainfile'
    },
    shim: {
        backbone: {
            deps: ["underscore"]
        },
        marionette: {
            deps: ["backbone"]
        },
        templates: {
            deps: ["hogan", "jquery"]
        },
        real: {
            deps: ["jquery", "jquerymobile", "touch", "mouse"]
        },
        markercluster: {
            exports: "MarkerClusterer"
        },
        humanize: {
            exports: "humanize"
        }
    },
    waitSeconds: 0
});

define('gmaps', ['async!http://maps.google.com/maps/api/js?v=3&key=AIzaSyBiV8f88yLWJ_IMSdP1fVNO1-gt3eLVSgg&sensor=true&callback=gMapsCallback'], function(){
// define('gmaps', ['http://maps.google.com/maps/api/js?v=3&sensor=false'], function(){
    return window.google.maps;
});

require(['app', 'templates', 'real'], function(app) {
    app.start({
        version: "0.9.9"
    });
});

main.build.js

({
    baseUrl: ".",
    name: "main",
    wrapShim: true,
    out: "main-built.js"
})

app.js

define(['underscore', 'controller', 'router', 'models/Cache', 'views/RootView'], function(_, Controller, Router, Cache, RootView) {
    var Application = Marionette.Application.extend({
        propertyListPageSize: 3,
        initialize: function() {
            _.templateSettings = { interpolate : /\{\{(.+?)\}\}/g };
        },
        onStart: function(options){
            new RootView();
            this.controller = new Controller();
            this.router = new Router({controller: this.controller});
            this.cache = new Cache();
            this.context = {};
            //this.evHistory = [];//@todo remove once BB/marionette navigation is in place
            if(Backbone.history) Backbone.history.start({ pushState: false });
            if(Backbone.history.fragment === "") this.navigate('home');
        },
        navigate: function(fragment, trigger, replace){
            this.router.navigate(fragment, {trigger:trigger, replace:replace});
        },
        back: function() {
            window.history.back();
        }
    });
    app = new Application();
    return app;

});

rootView.js

define(['marionette', 'views/HomeView', 'views/HeaderView', 'views/FooterView', 'views/MenuView', 'views/VideoView', 'views/LocationSearchView', 'views/LoginView', 'views/FindView', 'views/ServicesView', 'views/ValueView', 'views/PropertyListView', 'views/SideBySideView', 'views/ConfirmRegistrationView', 'views/ForgotPasswordView', 'views/CreateAccountView', 'views/UserHomeView', 'views/MyBrokerView', 'views/GiveFeedbackView', 'views/SeeFeedbackView', 'views/ViewingScheduleView', 'views/MyViewingsSummaryView', 'views/MyAccountView', 'views/ViewingConfirmView', 'views/ValueAddressPropertyListView'],
    function(Marionette, HomeView, HeaderView, FooterView, MenuView, VideoView, LocationView, LoginView, FindView, ServicesView, ValueView, PropertyListView, SideBySideView, ConfirmRegistrationView, ForgotPasswordView, CreateAccountView, UserHomeView, MyBrokerView, GiveFeedbackView, SeeFeedbackView, ViewingScheduleView, MyViewingsSummaryView, MyAccountView, ViewingConfirmView, ValueAddressPropertyListView) {
    var RootView = Marionette.LayoutView.extend({

    ...some view code

});

我要解决的用例:

所以当我在浏览器中访问该站点时,我注意到在调试器中它一开始就加载了所有 js 文件。在加载过程中,我的网站是空白的,用户必须等待一段时间才能使用该网站。

所以我能够理解的是,当 app 在 main.js 中“启动”时,app.js 会创建一个 rootView.js 的实例,而该实例又会将所有视图列为依赖项。这会触发所有其他视图的下载请求,这反过来会解决它们自己的依赖关系并下载所有相关的模型和集合。因此,当用户访问该站点时,所有文件都会被下载。

我一直在尝试的解决方案:

由于在使用requirejs,所以我尝试使用r.js来优化和合并所有js文件以减少下载次数。

我遇到的问题:

当我运行 r.js 时。我收到以下错误

Tracing dependencies for: main
Error: ENOENT: no such file or directory, open '/var/node_projects/rm/rm.src.server/src/public/js/underscore.js'
In module tree:
    main
      app

Error: Error: ENOENT: no such file or directory, open '/var/node_projects/rm/rm.src.server/src/public/js/underscore.js'
In module tree:
    main
      app

    at Error (native)

如果我将 underscore.js 文件直接添加到错误中的指定路径,那么对于 marionette.js,我会收到相同的错误。我认为正在发生的事情是 app.js 没有识别填充的依赖项,因此它试图直接在错误的指定路径中查找文件。

我尝试过的事情: - 我在 main.build.js 文件中添加了 wrapShim: true 但这没有帮助

老实说,我已经坐了几天了,我不确定下一步我能做什么,因此这篇文章。

任何帮助/指导将不胜感激。

【问题讨论】:

  • "node r.js -o main.build.js " 是我运行的命令,因此我的 r.js 配置文件将是 main.build.js。

标签: backbone.js requirejs r.js


【解决方案1】:

您需要在构建文件中包含相同的 shim 配置,因为 wrapShim 是不够的。

如果在运行时在应用程序中使用了 shim 配置,请在此处复制配置。如果使用 shim 配置是必需的,以便将 shim 的依赖项包含在构建中。不过,使用“mainConfigFile”是传递此信息的更好方法,因此它只列在一个地方。但是,如果 mainConfigFile 不是一个选项,则可以将 shim 配置内联到构建配置中。

https://github.com/jrburke/r.js/blob/master/build/example.build.js

【讨论】:

    猜你喜欢
    • 2019-03-23
    • 2017-05-06
    • 2016-06-17
    • 2021-11-07
    • 2020-05-27
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多