Avoid duplication of "paths" configuration in RequireJS main file and r.js build file?
Asked Answered
E

1

9

Here is (part of) my folder structure:

  • node-test
    • bower_components
    • build
    • public
      • main.js
    • build.js

Running the optimizer with r.js -o build.js and the following configuration works fine:

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

However, if I remove paths configuration in build.js it doesn't work anymore:

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)

I'd like to be DRY, avoiding adding a dependency twice. Is this possible?

Equidistant answered 3/7, 2013 at 19:15 Comment(0)
G
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
})
Gomel answered 3/7, 2013 at 19:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.