Skip to content

mainConfigFile not resolving paths defined in deps. #406

Open

Description

@jasisk

Hi James. First off, thanks for your work.

I have an issue that may very well just be beyond the scope of r.js, however I figured I'd ask just in case.

The basic problem is that I have a config defined in mainConfigFile. This config pulls in additional configs and the main app code via deps. The reason I'm having to use this oddly nested, "config loading configs" structure is because I'm using a build tool that automatically generates a require.config (similar to how JamJS does it).

This structure works just fine in the browser however r.js barfs, presumably because the require.config() method isn't actually called but I'm not sure.

I've put together a (contrived) testcase which you can find in this gist.

Activity

  1. jrburke commented on Mar 15, 2013

    @jrburke
    Member

    On first look, it seems like mainConfigFile should point to app/vendor/config.js, but if you need both configs loaded, then you are better off constructing a program in node to pull out the configs and merging them if you cannot get by with just the one and do not want to copy/paste one of them into the build config.

    Closing as part of bug triage, but feel free to continue discussion here and can reopen later if an issue is discovered.

  2. jasisk commented on Mar 15, 2013

    @jasisk
    Author

    I knew I'd left something out of my example.

    The app/config also has some configuration details (paths, shims, and configs, specifically) independent from those defined in app/vendor/config (which are programmatically generated by this build tool).

    The decisions which lead me to this point were that the vendor stuff should be versioned yet easily updated. The application should assume vendor libraries are defined but shouldn't worry about resolving the vendor paths or anything. Thus, the build tool generates a require.config specifically for the vendor libraries which the application digests while maintaining it's own config.

    I can definitely have the build tool merge the two, this just felt like a way to keep the two more isolated.

    That all said, I could very well be approaching this in the wrong way (it's been a long day); am I over-thinking it? Is there a more simple solution I'm overstepping?

  3. jasisk commented on Mar 15, 2013

    @jasisk
    Author

    Also, part of the reason this feels more like an issue is because I can specify config data (as in module.config()) inside one of those nested configs, and those objects are available.

    e.g.,

    // inside app/vendor/config.js
    
    require.config({
      "config": {
        "library": {
          "val": "ohboy"
        }
      },
      "paths": {
        "vendor": "vendor/vendor-library"
      }
    });

    In this case, module.config().val appropriately resolves to ohboy inside library.js (assuming the path for library.js is resolved implicitly).

    edit: I should clarify, I understand why that happens (module.config() being called in the application code, while paths need to be resolved as part of the build step), I just wish the paths could be used as well.

  4. jrburke commented on Mar 20, 2013

    @jrburke
    Member

    This has come up in different forms before, where some folks have two blocks of config. What I can look at for 2.2.0 is allowing an array value for mainConfigFile, to allow specifying multiple files to scan for config calls.

  5. jasisk commented on Mar 20, 2013

    @jasisk
    Author

    Thanks for the reply.

    After we spoke initially, I went ahead and wrote a task to merge the configs in the meantime.

    One thing I noticed (and let me know if you'd like me to open a separate issue for this) while playing around a bit is that load order can be significant when using the deps property with multiple files containing require.config statements. E.g.,

    // config.js
    require.config({
      deps: ["selectors", "lib/config", "main"],
      packages: [
        // ...
      ],
      shims: [
        // ...
      ]
    });
    // lib/config.js
    require.config({
      packages: [
        // ...
      ],
      shims: [
        // ...
      ]
    });
    // selectors.js
    require.config({
      config: {
        example: {
          selector: "#example"
        }
      }
    });

    This works in the browser. My build task merges lib/config.js with my build config (where mainConfigFile points to config.js) but then fails to take into account selectors.js. If, however, I swap the order of the deps in config.js to:

    // ...
    deps: ["lib/config", "selectors", "main"],
    // ...

    Then I can use module.config().selector in example.js.

    I know this is a miserably contrived example and that I'm clearly doing things in a way you'd never intended but figured I'd mention it just in case. Also, I try to avoid filing an issue without a PR so I'd be happy to give it an attempt when things calm down over here.

  6. jrburke commented on Mar 20, 2013

    @jrburke
    Member

    Yes, the order is important, and would be mentioned as part of any array support for mainConfigFile. The rule will basically be: configs are applied in order of the mainConfigFile array, so the last one would win in any conflict. Also note that explicit build config wins over mainConfigFile array, and command line r.js args win over anything else.

  7. KidkArolis commented on Jul 2, 2013

    @KidkArolis

    +1 on mainConfigFile array.

    Exactly for the reasons stated above, would be useful for keeping the manually editable config separate from the generated config.

  8. is-already-taken commented on Apr 22, 2014

    @is-already-taken

    @jrburke:
    I'm facing the same problem as the issue holder, I guess. Though my setup is a bit simpler.
    r.js and RequireJS seem to ignore my paths configuration.

    Tree fragment:

    ├── Gruntfile.js
    ├── src
    │   ├── js
    │   │   ├── router.js
    │   │   ├── main.js
    │   │   ├── backend-views
    │   │   │   └── ...
    │   │   ├── collections
    │   │   │   └── ...
    │   │   ├── models
    │   │   │   └── ...
    │   │   └── views
    │   │       └── ...
    │   ├── libs
    │   │   └── ...
     ...
    

    The libaries have version suffixes (except almond.js).

    main.js fragment:

    var requireJsConfig = {
        paths: {
            "jquery": "../libs/jquery-2.1.0",
            "underscore": "../libs/underscore-1.5.0",
            "backbone": "../libs/backbone-1.0.0"
        },
    
        shim: {
            "backbone": {
                deps: ["jquery", "underscore"],
                exports: "Backbone"
            },
            "underscore": {
                exports: "_"
            },
            "jquery": {
                exports: "$"
            }
        }
    };
    
    if (typeof exports != "undefined") {
    
        // NodeJs context
        module.exports.requireJsConfig = requireJsConfig;
    }
    
    if (typeof requirejs != "undefined") {  
        // Grunt/RequireJS or Browser context
    
        requirejs.config(requireJsConfig);
    
        require(["jquery", "backbone", "router"], function($, Backbone, Router){
    
            ...
    
        });
    }
    

    The reason why I'm exporting the config is, that I'm using it with RequireJs (in Node-Context) in a Grunt task. So actually the config is used at three points: the browser (for development), r.js (to build one .js file) and the Grunt task (to process things offline using the models and collections).

    Gruntfile fragment:

    requirejs: {
        options: {
            baseUrl: "./src/js/",
    
            mainConfigFile: "./src/js/main.js",
    
            name: "../libs/almond",
            include: "main",
    
            findNestedDependencies: true,
            // almond: true,
            /* optimize: "uglify",
            uglify: {
                beautify: true
            }, */
            out: "target/main.js"
        },
    
        build: { }
    },
    

    (I've tried almond: true without name and include as I thought it's the shorthand for a single-app. I further removed the optimizer stuff as I wanted to exclude that this is causing it.)

    Error Output:

    Options: logLevel=0, done=undefined, baseUrl="./src/js/", mainConfigFile="./src/js/main.js", name="../libs/almond", include="main", findNestedDependencies, out="target/main.js"
    >> Tracing dependencies for: ../libs/almond
    >> Error: ENOENT, no such file or directory
    >> '/home/XXXXXX/XXXXXX/gallery/src/js/jquery.js'
    >> In module tree:
    >>     main
    

    As you can see, r.js/RequireJs is looking up jquery at the wrong place not respecting the paths config.

    • NodeJS 0.10.15
    • Grunt 0.4.4
    • RequireJs 2.1.11
    • grunt-contrib-requirejs 0.4.3

    I also looked at these questions:

    Some of them have solutions (that I followed or just tried with out luck) some have no solution.

  9. is-already-taken commented on Apr 23, 2014

    @is-already-taken

    OK, after debugging r.js I found the problem.

    I'll update this issue these days giving more details (and hopefully a improvement to r.js).
    Short: mainConfigFile wont recognize configurations when passed as previously declared variable.

    I'm working on a r.js improvement to support this case. (Don't hesitate to ask me if I do not react the next days. ;)

  10. brewern commented on Nov 27, 2014

    @brewern

    @is-already-taken Did you finalize a solution to this problem? I also have 2 requirejs.config calls in a single file, but only the first is being read.

  11. andrey-buligin commented on Apr 1, 2015

    @andrey-buligin

    Any updates?

  12. modified the milestones: 2.2.0, 3.0 on Mar 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions