Skip to content

Instantly share code, notes, and snippets.

@Deraen
Last active December 23, 2015 12:39
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 Deraen/6636832 to your computer and use it in GitHub Desktop.
Save Deraen/6636832 to your computer and use it in GitHub Desktop.
Grunt with Connect proxy
'use strict';
module.exports = function (grunt) {
var path = require('path');
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
var yeomanConfig = {
app: 'app',
dist: 'dist',
port: 3000
};
grunt.initConfig({
yeoman: yeomanConfig,
watch: {
less: {
files: ['<%= yeoman.app %>/styles/{,*/}*.less'],
tasks: ['less:server']
},
livereload: {
files: [
'{.tmp,<%= yeoman.app %>}/{,*/}*.html',
'{.tmp,<%= yeoman.app %>}/styles/{,*/}*.css',
'{.tmp,<%= yeoman.app %>}/scripts/{,*/}*.js',
'<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
],
options: {
livereload: true
}
},
},
env: {
livereload: {
PORT: yeomanConfig.port,
NODE_ENV: 'development'
},
dist: {
NODE_ENV: 'production'
}
},
connect: {
server: {
options: {
port: yeomanConfig.port,
debug: true,
middleware: function (connect) {
return [
require('grunt-connect-proxy/lib/utils').proxyRequest,
connect.static(path.resolve('.tmp')),
connect.static(path.resolve(yeomanConfig.app)),
];
}
},
proxies: [{
context: '/api',
host: 'localhost',
port: 8080,
}, {
context: '/api-docs',
host: 'localhost',
port: 8080,
}]
}
},
clean: {
dist: {
files: [{
dot: true,
src: [
'.tmp',
'<%= yeoman.dist %>/*',
'!<%= yeoman.dist %>/.git*'
]
}]
},
server: '.tmp'
},
less: {
options: {
paths: [
'<%= yeoman.app %>/styles',
'<%= yeoman.app %>/components'
]
},
server: {
files: {
'.tmp/styles/main.css': '<%= yeoman.app %>/styles/main.less'
}
},
dist: {
files: {
'<%= yeoman.dist %>/styles/main.css': '<%= yeoman.app %>/styles/main.less'
}
}
},
useminPrepare: {
html: '<%= yeoman.app %>/index.html',
options: {
dest: '<%= yeoman.dist %>'
}
},
cssmin: {
dist: {
'<%= yeoman.dist %>/styles/main.css': '<%= yeoman.dist %>/styles/main.css'
}
},
usemin: {
html: ['<%= yeoman.dist %>/{,*/}*.html'],
css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
options: {
dirs: ['<%= yeoman.dist %>']
}
},
imagemin: {
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.app %>/images',
src: '{,*/}*.{png,jpg,jpeg}',
dest: '<%= yeoman.dist %>/images'
}]
}
},
htmlmin: {
dist: {
options: {
},
files: [{
expand: true,
cwd: '<%= yeoman.app %>',
src: ['*.html', 'views/*.html'],
dest: '<%= yeoman.dist %>'
}]
}
},
ngmin: {
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.dist %>/scripts',
src: '*.js',
dest: '<%= yeoman.dist %>/scripts'
}]
}
},
uglify: {
dist: {
files: {
'<%= yeoman.dist %>/scripts/scripts.js': [
'<%= yeoman.dist %>/scripts/scripts.js'
]
}
}
},
rev: {
dist: {
files: {
src: [
'<%= yeoman.dist %>/scripts/{,*/}*.js',
'<%= yeoman.dist %>/styles/{,*/}*.css',
'<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}',
'<%= yeoman.dist %>/styles/fonts/*'
]
}
}
},
copy: {
dist: {
files: [{
expand: true,
dot: true,
cwd: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
src: [
'*.{ico,txt}',
'.htaccess',
'images/{,*/}*.{gif,webp}',
'styles/fonts/*',
'components/bootstrap/dist/fonts/*',
'components/leaflet-dist/images/*',
]
}]
}
}
});
grunt.registerTask('server', [
'env:livereload',
'clean:server',
'less:server',
'configureProxies:server',
'connect:server',
'watch'
]);
grunt.registerTask('build', [
'env:dist',
'clean:dist',
'less:dist',
'useminPrepare',
'imagemin',
'cssmin',
'htmlmin',
'concat',
'copy',
'ngmin',
'uglify',
'rev',
'usemin'
]);
grunt.registerTask('default', ['build']);
};
{
"devDependencies": {
"matchdep": "~0.1.2",
"grunt-contrib-watch": "~0.5.3",
"grunt": "~0.4.1",
"grunt-contrib-less": "~0.7.0",
"grunt-env": "~0.4.0",
"grunt-contrib-clean": "~0.5.0",
"grunt-connect-proxy": "~0.1.5",
"grunt-contrib-connect": "~0.5.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment