2013-08-27 1 views
16

필자는 RequireJS 2.1.8을 사용하는 백본 응용 프로그램을 보유하고 있습니다. 즉, 모든 백본보기는 자신의 종속성을 지정하기 위해 define()을 사용합니다. 모든게 잘 작동하고 이제는 r.js (NPM을 통해 설치)를 사용하여 모든 JavaScript를 하나의 파일로 연결/축소하려고합니다.RequireJS : 단일 파일로 빌드 할 때 특정 경로를 제외하는 방법은 무엇입니까?

특정 경로로 시작하는 종속성을 제외하는 r.js 구성을 어떻게 설정할 수 있습니까?

아래에 main.js 파일을 포함 시켰습니다. 이 경우 제 3 자 라이브러리 (예 : 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을 (장고 템플릿) :

<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

관련 문제