Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 872
gremo Avatar asked Jul 03 '13 19:07

gremo


1 Answers

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
})
like image 82
explunit Avatar answered Oct 19 '22 14:10

explunit