Skip to content

Instantly share code, notes, and snippets.

@ramsunvtech
Created April 13, 2016 03:50
Show Gist options
  • Save ramsunvtech/c185bef79f9e765b0e0928c0259497f2 to your computer and use it in GitHub Desktop.
Save ramsunvtech/c185bef79f9e765b0e0928c0259497f2 to your computer and use it in GitHub Desktop.
Grunt Watch
module.exports = function( grunt ) {
'use strict';
// Load grunt tasks automatically
require( 'load-grunt-tasks' )( grunt );
grunt.loadTasks( 'tasks' );
// Time how long tasks take. Can help when optimizing build times
require( 'time-grunt' )( grunt );
var bowerOptions = {
stripAffix: true,
expand: true,
process: function( content, srcPath ) {
if ( srcPath.match( ".css" ) ) {
grunt.log.writeln( 'Updating paths for ' + srcPath );
return content.replace( /[\.\/]*(fonts?|images?)/g, '$1' ).replace( /(font|image)\//g, '$1s/' );
} else {
return content;
}
},
noProcess: [
'**/*.{png,gif,jpg,ico,psd,ttf,otf,eot,woff,woff2,svg,swf}'
],
packageSpecific: {
'backbone-modal': {
files: [
'backbone.modal.js',
'backbone.marionette.modals.js',
'backbone.modal.css',
'backbone.modal.theme.css'
]
},
'bootstrap': {
files: [
"dist/css/bootstrap.css",
"dist/css/bootstrap.css.map",
"dist/js/bootstrap.js",
"dist/fonts/glyphicons-halflings-regular.eot",
"dist/fonts/glyphicons-halflings-regular.svg",
"dist/fonts/glyphicons-halflings-regular.ttf",
"dist/fonts/glyphicons-halflings-regular.woff",
"dist/fonts/glyphicons-halflings-regular.woff2"
]
},
'handlebars': {
files: [
'handlebars.js',
'handlebars.runtime.js'
]
},
'jQuery-linkify': {
files: [
'linkify.amd.js',
'linkify-string.amd.js',
'linkify-jquery.amd.js',
'linkify-plugin-hashtag.amd.js'
]
},
'jquery-validation': {
files: [
'dist/jquery.validate.js',
'dist/additional-methods.js'
]
},
'imagesloaded': {
files: [
'imagesloaded.pkgd.js'
]
},
'parsleyjs': {
files: [
'dist/parsley.js',
'src/parsley.css'
]
},
'typeahead.js': {
files: [
'dist/bloodhound.js',
'dist/typeahead.jquery.js'
]
},
'video.js': {
files: [
"dist/video-js/video.js",
"dist/video-js/video-js.css",
"dist/video-js/video-js.swf",
"dist/video-js/font/vjs.eot",
"dist/video-js/font/vjs.svg",
"dist/video-js/font/vjs.ttf",
"dist/video-js/font/vjs.woff"
]
},
'videojs-ga': {
files: [
"dist/videojs.ga.js"
]
}
}
};
var modRewrite = require( 'connect-modrewrite' );
grunt.initConfig( {
pkg: grunt.file.readJSON( 'package.json' ),
sourceDir: 'src',
buildDir: 'dist',
'git-describe': {
options: {},
build: {}
},
writeChangelog: {
options: {
file: '<%= buildDir %>/CHANGELOG.md'
}
},
uglify: {
options: {
preserveComments: 'some', // Preserve 3rd party licence comments
screwIE8: true,
compress: false,
banner: '/*! <%= grunt.option("git-revision") %> - ' +
'<%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %> */'
},
build: {
options: {
report: 'min',
compress: {
drop_console: true
}
},
files: [ {
expand: true,
src: [ '<%= buildDir %>/js/vendor.js', '<%= buildDir %>/js/main.js' ]
} ]
}
},
jshint: {
options: {
force: true,
reporter: require( 'jshint-stylish' )
},
build: {
options: {
force: false
},
files: [ {
expand: true,
src: [ '<%= sourceDir %>/js/**/*.js', '!<%= sourceDir%>/js/vendor/**/*.js' ]
} ]
},
dev: {
files: [ {
expand: true,
src: [ '<%= sourceDir %>/js/**/*.js', '!<%= sourceDir%>/js/vendor/**/*.js' ]
} ]
}
},
clean: {
pre: [ '<%= buildDir %>' ],
dev: [
'<%= buildDir %>/js/**/*.{js,hbs}',
'!<%= buildDir %>/js/**/*.map',
'!<%= buildDir %>/js/config/*',
'!<%= buildDir %>/js/main.js',
'!<%= buildDir %>/js/vendor.js'
],
build: [
'<%= buildDir %>/js/**/*.{js,hbs,map}',
'<%= buildDir %>/vendor',
'!<%= buildDir %>/js/config/*',
'!<%= buildDir %>/js/main.js',
'!<%= buildDir %>/js/vendor.js'
],
vendor: [ '<%= sourceDir %>/vendor' ]
},
cleanempty: {
options: {
folders: true,
noJunk: true
},
build: {
files: [ {
expand: true,
cwd: '<%= buildDir %>',
src: [ '**' ]
} ]
}
},
less: {
all: {
options: {
paths: [ '<%= sourceDir %>/css' ],
strictImports: true,
sourceMap: true,
outputSourceFiles: true,
sourceMapFileInline: false,
sourceMapURL: './base.css.map',
modifyVars: {
// Video.js overrides
// TODO: Override these in our CSS, not here
'big-play-bg-color': '#78c2b1',
'big-play-width': '1em',
'big-play-height': '1em',
},
plugins: [
new( require( 'less-plugin-autoprefix' ) )( {
browsers: [ 'last 3 versions' ]
} )
]
},
files: {
'<%= buildDir %>/css/base.css': '<%= sourceDir %>/css/base.less'
}
}
},
cssmin: {
options: {
sourceMap: true
},
main: {
files: {
'<%= buildDir %>/css/base.css': '<%= buildDir %>/css/base.css'
}
},
vendor: {
files: {
'<%= buildDir %>/css/vendor.css': [ '<%= buildDir %>/vendor/**/*.css' ]
}
},
build: {
files: {
'<%= buildDir %>/css/vendor.css': [ '<%= sourceDir %>/vendor/**/*.css' ]
}
}
},
copy: {
// Copy project code
js: {
files: [ {
expand: true,
cwd: '<%= sourceDir %>/js',
src: [ '**/*.{js,json}' ],
dest: '<%= buildDir %>/js'
} ]
},
// Copy templates
hbs: {
files: [ {
expand: true,
cwd: '<%= sourceDir %>/js',
src: [ '**/*.hbs' ],
dest: '<%= buildDir %>/js'
} ]
},
// Copy project and vendor images
images: {
files: [ {
expand: true,
cwd: '<%= sourceDir %>/css',
src: [ '**/images/**' ],
dest: '<%= buildDir %>/css'
} ]
},
// Copy project and vendor fonts
fonts: {
files: [ {
expand: true,
cwd: '<%= sourceDir %>/css',
src: '**/fonts/**',
dest: '<%= buildDir %>/css'
} ]
},
// Copy vendor assets
vendorAssets: {
files: [ {
expand: true,
flatten: true,
filter: 'isFile',
cwd: '<%= buildDir %>/vendor',
src: '**/images/**',
dest: '<%= buildDir %>/css/images'
}, {
expand: true,
flatten: true,
filter: 'isFile',
cwd: '<%= buildDir %>/vendor',
src: '**/font*/**',
dest: '<%= buildDir %>/css/fonts'
} ]
},
/* When we do a build, all the vendor assets are
in the source directory (because the optimizer needs
everything available there in order to run) so we
need this target in order to fetch those items (such
as fonts and images) */
vendorBuildAssets: {
files: [ {
expand: true,
flatten: true,
filter: 'isFile',
cwd: '<%= sourceDir %>/vendor',
src: '**/images/**',
dest: '<%= buildDir %>/css/images'
}, {
expand: true,
flatten: true,
filter: 'isFile',
cwd: '<%= sourceDir %>/vendor',
src: '**/font*/**',
dest: '<%= buildDir %>/css/fonts'
} ]
},
// Copy logos folder
logos: {
files: [ {
expand: true,
cwd: '<%= sourceDir %>/logos',
src: '**',
dest: '<%= buildDir %>/logos'
} ]
},
// Copy project wrapper files
html: {
files: [ {
expand: true,
cwd: '<%= sourceDir %>',
src: [ '*.html' ],
dest: '<%= buildDir %>'
} ]
},
// Make sure require is in the correct place
// Ensure this runs after 'bower'
requirejs: {
src: 'bower_components/requirejs/require.js',
dest: '<%= buildDir %>/js/require.js'
}
},
bower: {
dev: {
// Everything goes into the vendor folder, this ensures
// that components with relative paths don't need to be
// modified
dest: '<%= buildDir %>/vendor',
options: bowerOptions
},
build: {
// We need items in the source dir for the optimizer to find when compiling
dest: '<%= sourceDir %>/vendor',
options: bowerOptions
}
},
requirejs: {
options: {
baseUrl: "<%= sourceDir %>/js",
dir: "<%= buildDir %>/js",
mainConfigFile: "<%= sourceDir %>/js/config.js",
removeCombined: true,
findNestedDependencies: true,
skipDirOptimize: true,
inlineText: true,
useStrict: true,
wrap: true,
keepBuildDir: true,
useSourceUrl: false,
optimize: "none",
paths: {
handlebars: '../../<%= sourceDir %>/vendor/handlebars/handlebars.runtime',
'handlebars-compiler': '../../<%= sourceDir %>/vendor/handlebars/handlebars'
},
packages: [ {
name: 'hbs',
location: '../../<%= sourceDir %>/vendor/requirejs-hbs',
main: 'hbs'
} ],
hbs: {
compilerPath: '../../<%= sourceDir %>/vendor/handlebars/handlebars'
},
modules: [ {
name: "vendor",
exclude: [ 'handlebars-compiler' ]
}, {
name: "main",
exclude: [ "vendor", "handlebars-compiler" ]
} ]
},
build: {
options: {
preserveLicenseComments: true
}
},
dev: {
options: {
optimize: "uglify2",
generateSourceMaps: true,
preserveLicenseComments: false,
uglify2: {
preserveComments: 'some',
screwIE8: true,
preamble: '/*! <%= grunt.option("git-revision") %> - ' +
'<%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %> */',
report: 'min',
output: {
beautify: true
},
compress: {
drop_console: false,
sequences: false,
global_defs: {
DEBUG: true
}
},
warnings: true,
mangle: false
}
}
}
},
connect: {
options: {
useAvailablePort: true,
livereload: 35731,
hostname: '*',
port: 4000
},
server: {
options: {
base: [ '../', '<%= buildDir %>', '.' ],
open: 'http://localhost:4000/consumer',
debug: false,
middleware: function( connect, options, middlewares ) {
middlewares.unshift( modRewrite( [ '^[^\\.]*$ /index.html [L]' ] ) );
return middlewares;
}
}
}
},
watch: {
options: {
livereload: '<%= connect.options.livereload %>'
},
stylesheets: {
files: '<%= sourceDir %>/css/**/*.less',
tasks: [ 'less' ]
},
scripts: {
files: [
'<%= sourceDir %>/js/**/*.js',
'../common/**/*.js'
],
tasks: [ 'newer:jshint:dev', 'copy:js' ]
},
templates: {
files: '<%= sourceDir %>/js/**/*.hbs',
tasks: [ 'copy:hbs' ]
},
html: {
files: '<%= sourceDir %>/*.html',
tasks: [ 'copy:html' ]
}
},
env: {
azure: {
AZURE_STORAGE_ACCOUNT: 'appscriptcdnstaging',
AZURE_STORAGE_ACCESS_KEY: 'V4VELu2+uF9slxBddVRzpa6/N8m1CiBEYfIDAKqfopxlei76zIz/U37eJK9UarEqYIf12SgfA2oP1Y77sapNHg=='
}
},
'azure-blob': {
options: {
containerName: 'appscript-dev',
containerDelete: false,
gzip: true
},
js: {
files: [ {
expand: true,
cwd: '<%= buildDir %>/js/',
dest: 'js/',
src: [ '**/*' ]
} ]
},
css: {
files: [ {
expand: true,
cwd: '<%= buildDir %>/css/',
dest: 'css/',
src: [ '**/*' ]
} ]
}
}
} );
grunt.event.on( 'watch', function( action, filepath ) {
grunt.config( [ 'jshint', 'dev' ], filepath );
} );
grunt.registerTask( 'dev', 'Start development', function( compile ) {
var taskList = [ 'clean:pre', 'less', 'bower:dev', 'cssmin:main', 'cssmin:vendor', 'copy', 'connect',/* 'proxy', */ 'watch' ];
grunt.log.writeln( "Starting development..." );
grunt.task.run( taskList );
} );
grunt.registerTask( 'build', 'Produce a production-ready build', function( target, local ) {
var taskList = [ 'clean:pre', 'bower:build', 'less', 'cssmin:main', 'cssmin:build', 'copy', 'requirejs:build', 'save-revision', 'uglify:build', 'clean:build', 'cleanempty', 'copy:requirejs', 'clean:vendor', 'env', 'azure-blob' ];
var localTaskList = [ 'clean:pre', 'bower', 'less', 'cssmin', 'copy', 'requirejs:dev', 'clean:dev', 'cleanempty', 'copy:requirejs', 'clean:vendor', 'proxy', 'connect:server:keepalive' ];
if ( local ) {
grunt.task.run( localTaskList );
} else {
if (target == 'old-staging') {
grunt.config(['azure-blob', 'options', 'containerName'], 'consumer');
}
if (target == 'staging') {
grunt.config(['azure-blob', 'options', 'containerName'], 'appscript-stage');
}
if (target == 'qa') {
grunt.config(['azure-blob', 'options', 'containerName'], 'appscript-qa');
}
if (target == 'production') {
grunt.config(['azure-blob', 'options', 'containerName'], 'appscript-prod');
}
grunt.task.run( taskList );
}
} );
grunt.registerTask( 'default', [ 'dev' ] );
};
module.exports = function(grunt) {
var _ = require('lodash');
// var buildRequireTargets = function(appList) {
// var requireTargets = {},
// buildConfig = {
// baseUrl: "<%= sourceDir %>/js",
// dir: "<%= buildDir %>/js",
// mainConfigFile: "<%= sourceDir %>/js/config.js",
// removeCombined: true,
// findNestedDependencies: true,
// generateSourceMaps: true,
// preserveLicenseComments: true,
// skipDirOptimize: true,
// inlineText: true,
// useStrict: true,
// wrap: true,
// keepBuildDir: true,
// optimize: "none",
// paths: {
// handlebars: 'vendor/handlebars/handlebars.runtime',
// 'handlebars-compiler': 'vendor/handlebars/handlebars'
// }
// };
// _.each(appList, function (app) {
// requireTargets[app] = {
// options: _.extend({
// map: {
// "*": {
// "app": app + "/app"
// }
// },
// modules: [
// {
// name: "vendor",
// exclude: ['handlebars-compiler']
// },
// {
// name: app + "/main",
// exclude: ["vendor", "handlebars-compiler"]
// }
// ]
// }, buildConfig)
// };
// });
// grunt.config("requirejs", requireTargets);
// };
grunt.renameTask('changelog', 'writeChangelog');
grunt.registerTask('changelog', 'Changelog generation with options commit/tag and version options.', function (commit, version) {
if (commit) {
grunt.config(["writeChangelog", "options", "from"], commit);
}
if (version) {
grunt.config(["writeChangelog", "options", "version"], version);
}
grunt.task.run('writeChangelog');
});
grunt.registerTask('save-revision', function () {
grunt.event.once('git-describe', function (rev) {
if (rev.dirty) {
grunt.option('git-dirty', true);
}
grunt.option('git-revision', rev.toString());
});
grunt.task.run('git-describe');
});
grunt.registerTask('tag-revision', 'Tag the current build revision', function () {
grunt.task.requires('git-describe');
grunt.file.write(grunt.config('buildDir') + '/version.json', JSON.stringify({
version: grunt.config('pkg.version'),
revision: grunt.option('git-revision'),
clean: grunt.option('git-dirty') ? false : true,
date: grunt.template.today()
}));
});
grunt.registerTask('version', 'Create a version.json file to tag this build', ['save-revision', 'tag-revision']);
grunt.registerTask('proxy', 'Start the development proxy', function(target) {
var http = require('http'),
httpProxy = require('http-proxy'),
connect = require('connect'),
allowCrossDomain = function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OTHER');
res.setHeader('Access-Control-Allow-Headers', 'content-length, content-type, auth-token, Authorization, *');
res.setHeader('Access-Control-Expose-Headers', 'content-length, content-type, auth-token, Authorization, *');
next();
},
webProxy = function(req, res, next) {
if (req.method === 'OPTIONS') {
res.writeHeader(200);
res.end();
} else {
grunt.log.writeln("Proxy Request:", req.url, req.method, "\n");
req.headers['hostname'] = host;
req.headers['host'] = host;
proxy.on('error', function(e) {
grunt.log.errorlns("Error: " + e.message);
// grunt.log.errorlns(e.stack);
});
proxy.web(req, res, {
target: {
hostname: host,
method: req.method,
port: 80,
path: req.url,
headers: req.headers
}
});
}
},
host = 'apistaging.appscript.net';
grunt.log.writeln("Using host", host);
var proxy = httpProxy.createProxyServer();
var app = connect()
.use(allowCrossDomain)
.use(webProxy)
.listen(8080);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment