2013-08-27 9 views
16

私は、RequireJS 2.1.8を使用するバックボーンアプリを持っています。つまり、すべての私のバックボーンビューは依存関係を指定するためにdefine()を使います。すべてがうまくいきましたが、現在はr.js(NPM経由でインストール)を使用してJavaScriptをすべて1つのファイルに連結/縮小しています。RequireJS:単一ファイルにビルドするときに特定のパスを除外する方法は?

特定のパスから始まる依存関係を除外するr.js設定をどのようにセットアップできますか?

下記のmain.jsファイルが含まれています。この場合、「構築済み」の出力ファイルにサードパーティのライブラリ(jquery、backboneなど)を除外します。また、「webapp /」(「webapp/dynamic_cfg」など)から始まる依存関係を除外して、動的に生成されたJavaScriptファイル/モジュール用のDjangアプリケーションにリクエストが送信されるようにしたい。

フォルダ構造:

|--static/ 
    |--main.js 
    |--myapp/ 
     |--views/ 
      |-- MyView.js 
      |-- ... 
    |--lib 
     |--backbone-1.0/ 
     |--underscore-1.5.1/ 
     |-- ... 

のindex.html(Djangoテンプレート):

<script src="{% static 'lib/requirejs-2.1.8/require.min.js' %}" data-main="{% static 'main.js' %}" ></script> 

main.js:へのパスを設定

requirejs.config({ 

    paths: { 
     "jquery": 'lib/jquery-1.10.2/jquery.min', 
     "text_loader": 'lib/requirejs-text-2.0.10/requirejs-text', 
     "fuelux": 'lib/fuelux-2.3.1', 
     "backbone": 'lib/backbone-1.0/backbone.min', 
     "underscore": 'lib/underscore-1.5.1/underscore.min', 

     // Any module that requires 'webapp/*' will result Require.js 
     // making a request to the server webapp. 
     "webapp": '..' 
    }, 

    shim: { 

     'backbone': { 
      deps: ['underscore', 'jquery'], // Load these dependencies first 
      exports: 'Backbone' // Create global var with this name for the module 
     }, 
     'underscore': { 
      exports: '_' 
     } 
    } 

}); 

// Startup 
require(['webapp/dynamic_cfg', 'myapp/util/logger', 'myapp/view/AppView', 'myapp/AppRouter', 'fuelux/all'], 

    // Dependencies are loaded and passed to this function 
    function(cfg, logger, AppView, AppRouter, fuelUx) { 

     logger.info("Starting up with config:", cfg); 

     var appView = new AppView(); 
     var appRouter = new AppRouter(); 
    } 
); 
+0

いどちらか[これ](http://stackoverflow.com/questions/17171133/exclude-all-starting-with-x-in-requirejs-module)または[本](HTTP:/ /stackoverflow.com/questions/9219113/requirejs-optimize-tool-exclude-folders)必要なものをカバーしていますか? – explunit

答えて

30

"空" で私のr.js設定が機能しました。例:

// This is a RequireJS config file 
(function(){ 
    return { 

     // Name of input file (without the .js extention) 
     "name": "main", 

     // Directory containing input file 
     "baseUrl": "static/", 

     // Look in this file for the require.config() call and extract it 
     "mainConfigFile": "static/main.js", 

     "paths": { 
      // Don't attempt to include dependencies whose path begins with webapp/ 
      "webapp": "empty:", 

      // Ditto for the following 3rd-party libraries 
      "jquery": "empty:", 
      "fuelux": "empty:", 
      "backbone": "empty:", 
      "underscore": "empty:" 
     }, 

     "optimize": "uglify2", 
    }; 
})() 
+1

grunt-contrib-requirejsとgrunt CLI v0.1.9およびgrunt v0.4.2は動作しません。 – kaiser

+1

grunt v0.4.5 + grunt-contrib-requirejs v0.4.4の作品 – johnr

関連する問題