9

这是(部分)我的文件夹结构:

  • 节点测试
    • bower_components
    • 建造
    • 上市
      • main.js
    • 构建.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
})

但是,如果我删除其中paths的配置build.js将不再起作用:

跟踪依赖项:./almond/almond 错误:ENOENT,没有这样的文件或目录 'C:\Users\Marco\Documents\Progetti\nodejs-opt\bower_components\domready.js' 在模块树中:../public/main

错误:错误:ENOENT,没有这样的文件或目录 'C:\Users\Marco\Documents\Progetti\nodejs-opt\bower_components\domready.js' 在模块树中:../public/main

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

我想保持干爽,避免两次添加依赖项。这可能吗?

4

1 回答 1

15

If you want to use same configuration from your runtime code to find the location of your libraries, you can use the mainConfigFile option:

...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 like this:

({
    baseUrl: "bower_components",
    mainConfigFile: '/some/path/main.js', // adjust path as needed
    name: "./almond/almond",
    include: "./../public/main",
    out: "build/main.js",
    preserveLicenseComments: false
})
于 2013-07-03T19:35:45.530 回答