Skip to content

Instantly share code, notes, and snippets.

@igordelorenzi
Created September 8, 2014 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igordelorenzi/d05f9f5f75c3ff95b867 to your computer and use it in GitHub Desktop.
Save igordelorenzi/d05f9f5f75c3ff95b867 to your computer and use it in GitHub Desktop.
Gruntfile.js
(function() {
"use strict";
var LIVERELOAD_PORT, lrSnippet, mountFolder;
LIVERELOAD_PORT = 35728;
lrSnippet = require("connect-livereload")({
port: LIVERELOAD_PORT
});
// conf = require('./conf.'+process.env.NODE_ENV);
mountFolder = function(connect, dir) {
return connect["static"](require("path").resolve(dir));
};
// Globbing
// for performance reasons we're only matching one level down:
// 'test/spec/{,*}*.js'
// use this if you want to recursively match all subfolders:
// 'test/spec/**/*.js'
module.exports = function(grunt) {
var yeomanConfig;
require("load-grunt-tasks")(grunt);
require("time-grunt")(grunt);
// configurable paths
yeomanConfig = {
app: "client",
dist: "dist"
};
try {
yeomanConfig.app = require("./bower.json").appPath || yeomanConfig.app;
} catch (_error) {}
grunt.loadNpmTasks("grunt-rsync");
grunt.initConfig({
yeoman: yeomanConfig,
watch: {
coffee: {
files: ["<%= yeoman.app %>/scripts/**/**/*.coffee"],
tasks: ["coffee:dist"]
},
compass: {
files: ["<%= yeoman.app %>/styles/**/*.{scss,sass}"],
tasks: ["compass:server"]
},
livereload: {
options: {
livereload: LIVERELOAD_PORT
},
files: [
"<%= yeoman.app %>/index.html",
"<%= yeoman.app %>/views/**/*.html",
"<%= yeoman.app %>/styles/**/*.scss",
// "<%= yeoman.app %>/styles-less/**/*.less",
".tmp/styles/**/*.css",
"{.tmp,<%= yeoman.app %>}/scripts/**/*.js",
"<%= yeoman.app %>/images/**/*.{png,jpg,jpeg,gif,webp,svg}"
]
}
},
connect: {
options: {
port: 9000,
// Change this to '0.0.0.0' to access the server from outside.
hostname: "0.0.0.0"
},
livereload: {
options: {
middleware: function(connect) {
return [lrSnippet, mountFolder(connect, ".tmp"), mountFolder(connect, yeomanConfig.app)];
}
}
},
test: {
options: {
middleware: function(connect) {
return [mountFolder(connect, ".tmp"), mountFolder(connect, "test")];
}
}
},
dist: {
options: {
middleware: function(connect) {
return [mountFolder(connect, yeomanConfig.dist)];
}
}
}
},
open: {
server: {
url: "http://localhost:<%= connect.options.port %>"
}
},
clean: {
dist: {
files: [
{
dot: true,
src: [".tmp", "<%= yeoman.dist %>/*", "!<%= yeoman.dist %>/.git*"]
}
]
},
server: ".tmp"
},
jshint: {
options: {
jshintrc: ".jshintrc"
},
all: ["Gruntfile.js", "<%= yeoman.app %>/scripts/**/*.js"]
},
compass: {
options: {
sassDir: "<%= yeoman.app %>/styles",
cssDir: ".tmp/styles",
generatedImagesDir: ".tmp/styles/ui/images/",
imagesDir: "<%= yeoman.app %>/styles/ui/images/",
javascriptsDir: "<%= yeoman.app %>/scripts",
fontsDir: "<%= yeoman.app %>/fonts",
importPath: "<%= yeoman.app %>/bower_components",
httpImagesPath: "styles/ui/images/",
httpGeneratedImagesPath: "styles/ui/images/",
httpFontsPath: "fonts",
relativeAssets: true
},
dist: {
options: {
outputStyle: 'compressed',
debugInfo: false,
noLineComments: true
}
},
server: {
options: {
debugInfo: true
}
},
forvalidation: {
options: {
debugInfo: false,
noLineComments: false
}
}
},
// if you want to use the compass config.rb file for configuration:
// compass: {
// dist: {
// options: {
// config: 'config.rb'
// }
// }
// },
coffee: {
server: {
options: {
sourceMap: true,
// join: true,
sourceRoot: ""
},
files: [
{
expand: true,
cwd: "<%= yeoman.app %>/scripts",
src: "**/**/*.coffee",
dest: ".tmp/scripts",
ext: ".js"
}
]
},
dist: {
options: {
sourceMap: false,
sourceRoot: ""
},
files: [
{
expand: true,
cwd: "<%= yeoman.app %>/scripts",
src: "**/**/*.coffee",
dest: ".tmp/scripts",
ext: ".js"
}
]
}
},
useminPrepare: {
html: "<%= yeoman.app %>/index.html",
options: {
dest: "<%= yeoman.dist %>",
flow: {
steps: {
js: ["concat"],
css: ["concat"]
},
post: []
}
}
},
// css: ['concat'],
usemin: {
html: ["<%= yeoman.dist %>/**/*.html", "!<%= yeoman.dist %>/bower_components/**"],
css: ["<%= yeoman.dist %>/styles/**/*.css"],
options: {
dirs: ["<%= yeoman.dist %>"]
}
},
htmlmin: {
dist: {
options: {},
// removeCommentsFromCDATA: true,
// // https://github.com/yeoman/grunt-usemin/issues/44
// //collapseWhitespace: true,
// collapseBooleanAttributes: true,
// removeAttributeQuotes: true,
// removeRedundantAttributes: true,
// useShortDoctype: true,
// removeEmptyAttributes: true,
// removeOptionalTags: true,
files: [
{
expand: true,
cwd: "<%= yeoman.app %>",
src: ["*.html", "views/*.html"],
dest: "<%= yeoman.dist %>"
}
]
}
},
// Put files not handled in other tasks here
copy: {
dist: {
files: [
{
expand: true,
dot: true,
cwd: "<%= yeoman.app %>",
dest: "<%= yeoman.dist %>",
src: [
"favicon.ico",
"favicon.png",
// bower components that has image, font dependencies
"bower_components/font-awesome/css/*",
"bower_components/font-awesome/fonts/*",
"bower_components/weather-icons/css/*",
"bower_components/weather-icons/font/*",
"fonts/**/*",
"i18n/**/*",
"images/**/*",
"styles/bootstrap/**/*",
"styles/fonts/**/*",
"styles/img/**/*",
"styles/ui/images/**/*",
"views/**/*"
]
}, {
expand: true,
cwd: ".tmp",
dest: "<%= yeoman.dist %>",
src: ["styles/**", "assets/**"]
}, {
expand: true,
cwd: ".tmp/images",
dest: "<%= yeoman.dist %>/images",
src: ["generated/*"]
}
]
},
styles: {
expand: true,
cwd: "<%= yeoman.app %>/styles",
dest: ".tmp/styles/",
src: "**/*.css"
}
},
concurrent: {
server: ["coffee:server", "compass:server", "copy:styles"],
dist: ["coffee:dist", "compass:dist", "copy:styles", "htmlmin"]
},
concat: {
options: {
separator: grunt.util.linefeed + ';' + grunt.util.linefeed
},
dist: {
src: ["<%= yeoman.dist %>/bower_components/angular/angular.min.js"],
dest: "<%= yeoman.dist %>/scripts/vendor.js"
}
},
uglify: {
options: {
mangle: false,
compress: {
drop_console: true
}
},
dist: {
files: {
"<%= yeoman.dist %>/scripts/app.js": [".tmp/**/*.js", "<%= yeoman.app %>/scripts/**/*.js"]
}
}
},
rsync: {
options: {
args: ["--verbose"],
exclude: [".git*","*.scss",".DS_Store"],
recursive: true
},
local: {
options: {
src: "dist/",
dest: "/Users/igordla/Sites/dist",
recursive: true,
delete: true
}
},
staging: {
options: {
src: "dist/",
dest: "/var/www/pemaap-frontend",
host: "root@iep.tokenlab.com.br",
recursive: true,
delete: true
}
},
production: {
options: {
src: "dist/",
dest: "/var/www/pemaap-frontend",
host: "root@iep.tokenlab.com.br",
recursive: true,
delete: true
}
}
}
});
grunt.registerTask('fixSourceMapping', function (target) {
var concat = grunt.config('concat'),
copy = grunt.config('copy'),
files = concat.generated.files,
i, j, src, hasChanged = false, sourceMapFiles = [];
for (i in files) {
if (files[i].dest === "dist/scripts/vendor.js") {
src = files[i].src;
break;
}
}
for (j in src) {
var file1 = src[j].replace(/.js/, '.map'),
file2 = src[j] + '.map',
file3 = src[j].replace(/.js/, '.min.js');
if (grunt.file.exists(file1)) {
file1 = file1.replace(/client\//, '');
sourceMapFiles.push(file1);
}
if (grunt.file.exists(file2)) {
file2 = file2.replace(/client\//, '');
sourceMapFiles.push(file2);
}
if (grunt.file.exists(file3)) {
src[j] = file3;
hasChanged = true;
}
}
copy.dist.files.push({
expand: true,
flatten: true,
cwd: "client",
dest: "dist/scripts",
src: sourceMapFiles,
filter: 'isFile'
});
if (hasChanged) concat.generated.files[i].src = src;
if (target === 'prod') grunt.config('concat', concat);
grunt.config('copy', copy);
grunt.log.ok();
});
grunt.registerTask("server", function (target) {
if (target === "dist") {
return grunt.task.run(["build", "open", "connect:dist:keepalive"]);
}
return grunt.task.run(["clean:server", "concurrent:server", "connect:livereload", "open", "watch"]);
});
grunt.registerTask("build", ["clean:dist", "useminPrepare", "concurrent:dist", "fixSourceMapping", "copy:dist", "concat", "uglify", "usemin"]);
grunt.registerTask("build:production", ["clean:dist", "useminPrepare", "concurrent:dist", "fixSourceMapping:prod", "copy:dist", "concat", "uglify", "usemin"]);
grunt.registerTask("default", ["server"]);
};
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment