Skip to content

Instantly share code, notes, and snippets.

@collinschaafsma
Created April 10, 2012 03:43
Show Gist options
  • Save collinschaafsma/2348189 to your computer and use it in GitHub Desktop.
Save collinschaafsma/2348189 to your computer and use it in GitHub Desktop.
Grunt config for Rails
// This is the main application configuration file. It is a Grunt
// configuration file, which you can learn more about here:
// https://github.com/cowboy/grunt/blob/master/docs/configuring.md
//
module.exports = function(grunt) {
grunt.initConfig({
// The clean task ensures all files are removed from the dist/ directory so
// that no files linger from previous builds.
clean: ["../public/assets/js/debug", "../public/assets/js/release"],
// The lint task will run the build configuration and the application
// JavaScript through JSHint and report any errors. You can change the
// options for this task, by reading this:
// https://github.com/cowboy/grunt/blob/master/docs/task_lint.md
lint: {
files: [
"build/config.js", "app/**/*.js"
]
},
// The jshint option for scripturl is set to lax, because the anchor
// override inside main.js needs to test for them so as to not accidentally
// route.
jshint: {
options: {
scripturl: true
}
},
// The jst task compiles all application templates into JavaScript
// functions with the underscore.js template function from 1.2.4. You can
// change the namespace and the template options, by reading this:
// https://github.com/tbranyen/build-tasks/tree/master/jst
//
// The concat task depends on this file to exist, so if you decide to
// remove this, ensure concat is updated accordingly.
jst: {
"../public/assets/js/debug/templates.js": [
"app/templates/**/*.html"
]
},
// The concatenate task is used here to merge the almond require/define
// shim and the templates into the application code. It's named
// dist/debug/require.js, because we want to only load one script file in
// index.html.
concat: {
"../public/assets/js/debug/require.js": [
"assets/js/libs/almond.js",
"../public/assets/js/debug/templates.js",
"../public/assets/js/debug/require.js"
]
},
// This task uses the MinCSS Node.js project to take all your CSS files in
// order and concatenate them into a single CSS file named index.css. It
// also minifies all the CSS as well. This is named index.css, because we
// only want to load one stylesheet in index.html.
mincss: {
"../public/assets/css/release/index.css": [
"assets/css/style.css"
]
},
// Takes the built require.js file and minifies it for filesize benefits.
min: {
"../public/assets/js/release/require.js": [
"../public/assets/js/debug/require.js"
]
},
// Running the server without specifying an action will run the defaults,
// port: 8080 and host: 127.0.0.1. If you would like to change these
// defaults, simply add in the properties `port` and `host` respectively.
//
// Changing the defaults might look something like this:
//
// server: {
// host: "127.0.0.1", port: 9001
// debug: { ... can set host and port here too ...
// }
//
// To learn more about using the server task, please refer to the code
// until documentation has been written.
server: {
debug: {
folders: {
"app": "../public/assets/js/debug",
"app/templates": "app/templates",
"assets/js/libs": "../public/assets/js/debug"
}
},
release: {
folders: {
"app": "../public/assets/js/release",
"app/templates": "app/templates",
"assets/js/libs": "../public/assets/js/release",
"assets/css": "../public/assets/js/release"
}
}
},
// This task uses James Burke's excellent r.js AMD build tool. In the
// future other builders may be contributed as drop-in alternatives.
requirejs: {
// Include the main configuration file
mainConfigFile: "app/config.js",
// Output file
out: "../public/assets/js/debug/require.js",
// Root application module
name: "config",
// Do not wrap everything in an IIFE
wrap: false
}
});
// The default task will remove all contents inside the dist/ folder, lint
// all your code, precompile all the underscore templates into
// dist/debug/templates.js, compile all the application code into
// dist/debug/require.js, and then concatenate the require/define shim
// almond.js and dist/debug/templates.js into the require.js file.
grunt.registerTask("default", "clean lint jst requirejs concat");
// The debug task is simply an alias to default to remain consistent with
// debug/release.
grunt.registerTask("debug", "default");
// The release task will run the debug tasks and then minify the
// dist/debug/require.js file and CSS files.
grunt.registerTask("release", "default min mincss");
};
@ryrych
Copy link

ryrych commented Apr 16, 2013

I’m new to Grunt. Could you describe the flow, please? What commands should I issue, what steps should I take to integrate Grunt with an existing Rails app? Thanks in advance!

@Luiz-N
Copy link

Luiz-N commented Jul 4, 2013

I'm not finding some of these tasks such as "lint", "min, and "server" on the grunt site to install... Am i missing something?

@silvenon
Copy link

No, some of the links are indeed broken or outdated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment