2013-07-03 45 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

相关问题