Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requirejs ignoring my mainConfigFile #78

Open
starangelam opened this issue Oct 16, 2013 · 7 comments
Open

requirejs ignoring my mainConfigFile #78

starangelam opened this issue Oct 16, 2013 · 7 comments
Assignees
Labels

Comments

@starangelam
Copy link

Project Intro

My project is a single page storefront. The project has multiple modules, and each module contains a set of controller.js, view.js and model.js files, as well as a template.html file. And uses requirejs to manage dependencies.

Problem Statement

I want to use mainConfigFile to provide paths to reference modules in grunt-requirejs.
Part of my mainConfigFile's require.config is stored in separate file (base.dependency.config), and require.config.paths are pieced together by underscore at runtime.

base.dependency.config

config = {
    baseDependencyConfig: {
        paths: { ... }
        shim: { ... }
    }
}

main.js

var dependencies = config.baseDependencyConfig;
var basePaths = config.baseDependencyConfig.paths;
var extensionPaths = {
    // extra sets of paths
};

// combine base paths and extension paths at runtime using underscore
var dependencyPaths = _.extend(basePaths, extensionPaths);

dependencies.paths = dependencyPaths;
require.config(dependencies);

// application startup
require(['app', 'eventbus']) {
    // code
}

Error

However, grunt requirejs is ignoring mainConfigFile, grunt requirejs tries to find 'app.js' under root, when in fact, 'app' is defined under require.config paths as

'app': 'modules/base/app/base.app.controller'

my gruntFile:

module.exports = function (grunt) {
  grunt.initConfig({
    // ... other plugin config
    requirejs: {
      options: {
        baseUrl: 'public',
        // the paths for the named modules such as 'app' are defined 
        // in main.js under require.config paths
        name: 'main',
        include: [
           'app',
           'cart',
           'category'
        ],
        out: 'public/build/app-optimized.js',
        mainConfigFile: 'public/main.js',
        findNestedDependencies: true,
        optimizeCss: 'none',
        cssImportIgnore: 'style/style.css, style/mocha.css',
      }
    }
  })
}

my file structure

public
    |--modules/
    |       |--base/
    |       |       |--cart
    |       |       |--category
    |       |               |--category.controller.js
    |       |               |--category.view.js
    |       |               |--category.model.js
    |       |               |--category.template.html
    |       |
    |       |--extension/
    |
    |--style/
    |--image/
    |--main.js <-- main config file
    |--other .js files
  • mainConfigFile, main.js lives in root, along with a few other application startup js files
  • main bulk of application files lives inside modules folder
  • each module folder contains its controller, view and model js file, as well as a template.html file
@bencevans
Copy link

@starangelam had any luck with this issue since you filed it?

@starangelam
Copy link
Author

@bencevans No, unfortunately. I posted in stackOverflow as well. But no insight offered yet. http://stackoverflow.com/questions/19415369/grunt-requirejs-ignores-paths-from-my-mainconfigfile

@bencevans
Copy link

@starangelam That's a shame, I'm having a look around for working examples. This one seems to be using the mainConfigFile ok... https://github.com/neoziro/bootstart

@starangelam
Copy link
Author

@bencevans I think I should clarify that this gruntFile was working when I had this setup for mainConfigFile:

main.js

require.config({
    paths: { ... },
    shim: { ... }
});

// application startup
require(['app', 'eventbus']) {
    // code
}

@ghost ghost assigned asciidisco Oct 30, 2013
@is-already-taken
Copy link

I had the same project setup and error and found the cause.

r.js won't recognize RequireJS configs in the file referenced at mainConfigFile when the config object is not passed as JSON object, because it's passed as previously declared variable, for example.

I've created a patch (actually two). I could provide diffs to fix r.js. I'm currently trying to build and write tests for r.js to incorporate my patch. I'll create an improvement issue these days.

@asciidisco
Copy link
Owner

@is-already-taken 👍

@is-already-taken
Copy link

Uh. It's more complicated contributing to r.js than I thought.

Tests of the latest release are failing ony my system. Unable to post proposals on the mailing list (post deleted?). Required to sign a CLA for more than two changed lines of code? (I know: maybe even 5 or 10 lines might not require a CLA, but it looks quite bureaucratic).

The only workaround I could recommend is the approach we used in another project: Do not use mainConfigFile. Export the config object based on environment detection (runnign in browser or as NodeJS module). Require the config in Grunt and pass the object as value to the Grunt config.

main.js

// ...
if (typeof exports != "undefined") {
    // NodeJs context
    module.exports.requireJsConfig = requireJsConfig;
}
// ...

Gruntfile

var requireConfig = require("/path/to/main.js").requireJsConfig;

requireConfig = extend(requireConfig, { /* modifications */ });

// ...

grunt.initConfig({
  requirejs: {
    options: requireConfig
  }
});

I'll go with this way (workaround) at first. Actually I started implementing the code and Grunt this way at first then finding mainConfigFile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants