gruntjs / grunt-contrib-requirejs

Optimize RequireJS projects using r.js.
http://gruntjs.com/
MIT License
504 stars 105 forks source link

Issue with using a JSON RequireJS config file. #34

Closed emmerich closed 10 years ago

emmerich commented 11 years ago

The task doesn't work with a typical RequireJS config file, which may look like:

({
    appDir: 'src',
    name: 'application',
    out: 'bin/built.js'
})

This is because the config is passed to RequireJS as the mainConfigFile property rather than as the config object itself.

To get around this issue, you have to make it an extension to the config i.e

require.config({
    appDir: 'src',
    name: 'application',
    out: 'bin/built.js'
});

However, this doesn't work with the command line r.js: node r.js -o app.build.js > Unexpected token ;

Would it be possible to have the task parse a file in the first format and use it as the main require js config file?

emmerich commented 11 years ago

A potential solution would be to determine the type of the options property. If it's a string parse the file at the given location as the options object. Otherwise, go to the current behaviour. Thoughts?

ChrisWren commented 11 years ago

Not sure if this is exactly what are you looking for, but I made a grunt plugin, grunt-requirejs-config, to keep all your require.js config in your Gruntfile and then preprend it to your main.js/bootstrap script file. Let me know if it helps!

vladkotu commented 11 years ago

You can read json from config file and use lodash extend:

requirejs: {
    compile: {
      options: grunt.util._.extend(grunt.file.readJSON('requirejs-config.json', {
        // build related options here
        baseUrl: './'
      }))
    }
}

RequireJS config file should be valid json:

{
    "appDir": 'src',
    "name": 'application',
    "out": 'bin/built.js'
}
gnrlwart commented 11 years ago

@buhuru RequireJs build configs are usually js files, which allows adding comments and functions. Here is the example config file that shows all the possible options: https://github.com/jrburke/r.js/blob/master/build/example.build.js

I've been reading in my existing config file with some code like this:

var buildOptionsFile = grunt.file.read( buildOptionsPath );
  var buildOptions = eval( buildOptionsFile );
  // ... Adjust path settings appDir, baseUrl, and mainConfigFile if needed
  gruntConfig.requirejs = {
    compile : {
      options : buildOptions
    }
  };

Though, so match your more inline code layout, you could do this:

requirejs: {
    compile: {
      options: grunt.util._.extend( eval( grunt.file.read( 'my.build.js' ) ), {
        // additional build related options here
        baseUrl: './'
      } )
    }
}