2013-07-03 26 views
9

Dưới đây là (một phần của) cấu trúc thư mục của tôi:Tránh sao chép cấu hình "đường dẫn" trong tệp chính của RequireJS và tệp xây dựng r.js?

  • nút kiểm tra
    • bower_components
    • build
    • công
      • main.js
    • build.js

Chạy tôi ưu hoa với r.js -o build.js và cấu hình sau hoạt động tốt:

// 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 
}) 

Tuy nhiên, nếu tôi loại bỏ paths cấu hình trong build.js nó không hoạt động nữa:

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) 

tôi 'muốn được DRY, tránh thêm phụ thuộc hai lần. Điều này có thể không?

Trả lời

15

Nếu bạn muốn sử dụng cùng cấu hình từ mã thời gian chạy của bạn để tìm vị trí của thư viện, bạn có thể sử dụng tùy chọn 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.

Something như thế này:

({ 
    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

Perfect !! ! Cảm ơn bạn rất nhiều! – gremo

Các vấn đề liên quan