grunt-requirejs-config-gen

Generates a RequireJS optimizer config file from a base config file.

grunt-requirejs-config-gen

Generates a RequireJS optimizer config file from a base config file.

For now this plugin generates:

This plugin requires Grunt ~0.4.1

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-requirejs-config-gen --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-requirejs-config-gen');

In your project's Gruntfile, add a section named requirejs_config_gen to the data object passed into grunt.initConfig().

grunt.initConfig({
  requirejs_config_gen: {
    default: {
      options: {
        input: 'path to skeleton file',
        baseUrl: 'base path for modules',
        output: 'path to output file'
      },
      includes: [ 'files to include' ]
    },
    libOnly: {
      includes: [ 'files to include' ]
    }
  },
})

Type: String Default value: null

Required. The path to the skeleton configuration file, relative to the current directory.

Type: String Default value: null

Required. The path to the output configuration file which will get generated.

Type: String Default value: ./

Same as RequireJS baseURL option - the path relative to which all modules are located.

The skeleton config file is expected to be structured similar to the following:

var config = {
  paths: {
    underscore : "lodash"
  },
  shim: {
    underscore: {
      exports: '_'
    }
  }
};
 
// <INCLUDE-JS-FILES></INCLUDE-JS-FILES> 
 
require.config(config);

Key things to note:

  • The config variable. The plugin will assume that this exists.
  • The // <INCLUDE-JS-FILES></INCLUDE-JS-FILES> tag. This gets replaced by the plugin with the config.include... calls.

Now let's assume we have two scripts we want to include - js/1.js and js/2.js, we can do them like this:

grunt.initConfig({
  requirejs_config_gen: {
    default: {
      options: {
        input: 'config.js.skel',
        baseUrl: 'js',
        output: 'config.js'
      },
      includes: [ 'js/*.js' ]
    }
  }
})

If we run it we should get config.js which looks as follows:

var config = {
  paths: {
    underscore : "lodash"
  },
  shim: {
    underscore: {
      exports: '_'
    }
  }
};
 
config.include = config.include || [];
config.include.push('js/1');
config.include.push('js/2');
 
require.config(config);

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

MIT License (c) Ramesh Nair

  • 0.1.0 initial release