带有文本文件的r.js.

时间:2013-11-17 00:14:55

标签: requirejs r.js

我在http://gifcept.com有一个使用require.js的项目。我正在使用r.js优化资源,但是当我设置inlineText时,我无法让它工作:true。

我有以下目录树,用于r.js

的测试目的
└── uncompiled
    ├── js
    │   ├── bookmarklet
    │   ├── collections
    │   ├── libs
    │   │   ├── backbone
    │   │   ├── jquery
    │   │   ├── jquery.fileupload
    │   │   ├── jquery.iframe-transport
    │   │   ├── jquery.jscrollpane
    │   │   ├── jquery.mousewheel
    │   │   ├── jquery.scrolltofixed
    │   │   ├── jquery.simple-dialog
    │   │   ├── jquery.tag-it
    │   │   ├── jquery.timeago
    │   │   ├── jquery.ui
    │   │   ├── modernizr
    │   │   ├── require
    │   │   └── underscore
    │   ├── models
    │   └── views
    └── templates

我的build.js文件如下所示

({
    dir: "compiled",
    baseUrl: "uncompiled/js",        
    stubModules: ['text'],
    findNestedDependencies: true,
    preserveLicenseComments: false,
    optimizeAllPluginResources: true,
    removeCombined: true,
    optimize: "uglify",
    inlineText: true,
    modules: [
        {
            name: "main"
        }
    ],
    paths: {
        jquery: 'libs/jquery/jquery-2.0.0',
        underscore: 'libs/underscore/underscore-1.4.4',
        backbone: 'libs/backbone/backbone-1.0.0',
        modernizr: 'libs/modernizr/modernizr-2.6.2',
        jqueryui: 'libs/jquery.ui/jquery.ui-1.10.3',
        jquerytagit: 'libs/jquery.tag-it/jquery.tag-it-2.0',
        jqueryiframetransport: 'libs/jquery.iframe-transport/jquery.iframe-transport-1.7',
        jqueryfileupload: 'libs/jquery.fileupload/jquery.fileupload-5.31.6',
        jquerysimpledialog: 'libs/jquery.simple-dialog/jquery.simple-dialog-0.0.1',
        jqueryscrolltofixed: 'libs/jquery.scrolltofixed/jquery.scrolltofixed',
        jquerytimeago: 'libs/jquery.timeago/jquery.timeago-1.3.0',
        jquerymousewheel: 'libs/jquery.mousewheel/jquery.mousewheel',
        jqueryjscrollpane: 'libs/jquery.jscrollpane/jquery.jscrollpane'
    },
})

我的main.js文件看起来像这样

require.config({ 
    baseUrl: "/js",
    paths: { 
        jquery: 'libs/jquery/jquery-2.0.0',
        underscore: 'libs/underscore/underscore-1.4.4',
        backbone: 'libs/backbone/backbone-1.0.0',
        modernizr: 'libs/modernizr/modernizr-2.6.2',
        jqueryui: 'libs/jquery.ui/jquery.ui-1.10.3',
        jquerytagit: 'libs/jquery.tag-it/jquery.tag-it-2.0',
        jqueryiframetransport: 'libs/jquery.iframe-transport/jquery.iframe-transport-1.7',
        jqueryfileupload: 'libs/jquery.fileupload/jquery.fileupload-5.31.6',
        jquerysimpledialog: 'libs/jquery.simple-dialog/jquery.simple-dialog-0.0.1',
        jqueryscrolltofixed: 'libs/jquery.scrolltofixed/jquery.scrolltofixed',
        jquerytimeago: 'libs/jquery.timeago/jquery.timeago-1.3.0',
        jquerymousewheel: 'libs/jquery.mousewheel/jquery.mousewheel',
        jqueryjscrollpane: 'libs/jquery.jscrollpane/jquery.jscrollpane'
    },
    shim: {
        jquery: {
            exports: '$'
        },
        underscore: {
            exports: '_'
        },
        backbone: {
            exports: 'Backbone',
            deps: ['underscore', 'jquery']
        },
        modernizr: {},
        jqueryui: {
            deps: ['jquery']
        },
        jquerytagit: {
            deps: ['jquery', 'jqueryui']
        },
        jqueryiframetransport: {
            deps: ['jquery']
        },
        jqueryfileupload: {
            deps: ['jquery', 'jqueryui']
        },
        jquerysimpledialog: {
            deps: ['jquery']
        },
        jqueryscrolltofixed: {
            deps: ['jquery'],
        },
        jquerytimeago: {
            deps: ['jquery'],
        },
        jquerymousewheel: {
            deps: ['jquery'],
        },
        jqueryjscrollpane: {
            deps: ['jquery', 'jquerymousewheel'],
        }
    }
}); 

require([ 
    // Load our app module and pass it to our definition function 
    'app',          
    'events',
    'modernizr',
    'jquery',
    'jqueryscrolltofixed',
    'jqueryui',
    'jquerytagit',          
    'jquerysimpledialog',
    'jqueryiframetransport',
    'jqueryfileupload',
    'jquerytimeago',
    'jquerymousewheel',
    'jqueryjscrollpane'
], function(Application, Events){
...
...
});

使用文本资源的定义示例

define([
    'underscore',
    'backbone',
    'helpers',
    'events',
    'router',
    'views/destroyableView',
    'text!/templates/comment.html'
], function(_, Backbone, Helpers, Events, Router, DestroyableView, CommentTemplate){
...
...
});

我已经能够获得一个编译文件,在build.js中转换inlineText:false并删除'text!*'定义。当inlineText设置为true并且保留文本资源定义时,我得到的错误如下:

Tracing dependencies for: main
RangeError: Maximum call stack size exceeded
In module tree:
    main
      app
        views/appView
          views/menuView
            views/notificationCollectionView
              views/notificationView
                views/viewGifPopupView
                  views/commentCollectionView
                    views/commentView
                      text

Error: RangeError: Maximum call stack size exceeded
In module tree:
    main
      app
        views/appView
          views/menuView
            views/notificationCollectionView
              views/notificationView
                views/viewGifPopupView
                  views/commentCollectionView
                    views/commentView
                      text

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

我唯一能提供的小工作示例:

<强> main.js

require.config({
    paths : {
        jquery : 'jquery-2.0.3',
        backbone: 'backbone',
        underscore: 'underscore'
    },
    shim : {
        underscore: {exports: '_'},
        backbone: {deps: ['underscore'], exports: 'Backbone'}
    },
    map : {
        '*' : {
            'text' : 'text' // real path tot text.js plugin
        }
    }
});

require(['jquery', 'backbone', 'text!./partials/view.html'], function($, Backbone, view) {
    console.log('Type of $: ' + typeof $);
    console.log('Type of Backbone: ' + typeof Backbone);
    var element = $(view);
    $('body').append(element);
});

<强>索引prod.html

<!doctype html>
<html>
    <head></head>
    <body>
        <script src="require.js"></script>
        <script src="main-built.js"></script>
    </body>
</html>

<强> build.js

({
   baseUrl : ".",
   name : 'main',
   mainConfigFile : "main.js",
   out : "main-built.js",
   findNestedDependencies : true,
   inlineText : true,
   stubModules: ['text'],
   preserveLicenseComments: false,
   optimizeAllPluginResources: true,
   removeCombined: true,
   optimize: "uglify",
})

<强>的index.html

<!doctype html>
<html>
    <head></head>
    <body>
        <script data-main="main" src="require.js"></script>
    </body>
</html>

<强>泛音/ view.html

<div>Some view</div>

构建过程没有错误,文本资源在main-built.js

中内联