2013-07-03 110 views
9

這裏是(部分)我的文件夾結構:避免在RequireJS主文件和r.js構建文件中重複「路徑」配置?

  • 節點測試
    • bower_components
    • 構建
    • 公共
      • main.js
    • build.js

運行優化與r.js -o build.js和以下配置工作正常:

// main.js file 
requirejs.config({ 
    baseUrl: '../bower_components', 
    paths: { 
     'domready': 'domready/ready', 
     'jquery': 'jquery/jquery', 
    } 
}); 

requirejs(['domready', 'jquery'], function (domReady, $) { 
    domReady(function() { 

    }); 
}); 

// build.js file 
({ 
    baseUrl: "bower_components", 
    name: "./almond/almond", 
    include: "./../public/main", 
    out: "build/main.js", 
    paths: { 
     'domready': 'domready/ready', 
     'jquery': 'jquery/jquery', 
    }, 
    preserveLicenseComments: false 
}) 

不過,如果我刪除build.jspaths配置它不工作了:

Tracing dependencies for: ./almond/almond Error: ENOENT, no such file or directory 'C:\Users\Marco\Documents\Progetti\nodejs-opt\bower_components\domready.js' In module tree: ../public/main

Error: Error: ENOENT, no such file or directory 'C:\Users\Marco\Documents\Progetti\nodejs-opt\bower_components\domready.js' In module tree: ../public/main

at Object.fs.openSync (fs.js:427:18) 

我'喜歡幹,避免兩次添加依賴項。這可能嗎?

回答

15

如果您想使用相同的配置從運行時代碼找到你的庫的位置,你可以使用mainConfigFile選項:

...if you prefer the "main" JS file configuration to be read for the build so that you do not have to duplicate the values in a separate configuration, set this property to the location of that main JS file. The first requirejs({}), require({}), requirejs.config({}), or require.config({}) call found in that file will be used.

事情是這樣的:

({ 
    baseUrl: "bower_components", 
    mainConfigFile: '/some/path/main.js', // adjust path as needed 
    name: "./almond/almond", 
    include: "./../public/main", 
    out: "build/main.js", 
    preserveLicenseComments: false 
}) 
+0

完美! !非常感謝! – gremo

相關問題