Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Support CDN Assets
Browse files Browse the repository at this point in the history
  • Loading branch information
amoshaviv committed Apr 25, 2014
1 parent 56fd75e commit 8cccae2
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 166 deletions.
77 changes: 42 additions & 35 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,81 @@
'use strict';

var _ = require('lodash'),
glob = require('glob');
glob = require('glob');

/**
* Before we begin, lets set the envrionment variable
* We'll Look for a valid NODE_ENV variable and if one cannot be found load the development NODE_ENV
*/
glob('./config/env/' + process.env.NODE_ENV + '.js', {
sync: true
sync: true
}, function(err, environmentFiles) {
process.env.NODE_ENV = environmentFiles.length ? process.env.NODE_ENV : 'development';
process.env.NODE_ENV = environmentFiles.length ? process.env.NODE_ENV : 'development';
});

// Load app configurations
module.exports = _.extend(
require('./env/all'),
require('./env/' + process.env.NODE_ENV) || {}
require('./env/all'),
require('./env/' + process.env.NODE_ENV) || {}
);

/**
* Get the modules JavaScript files
*/
module.exports.getGlobbedFiles = function(globPatterns, removeRoot) {
// For context switching
var _this = this;
// For context switching
var _this = this;

// The output array
var output = [];
// URL paths regex
var urlRegex = new RegExp('^(?:[a-z]+:)?//', 'i');

// If glob pattern is array so we use each pattern in a recursive way, otherwise we use glob
if (_.isArray(globPatterns)) {
globPatterns.forEach(function(globPattern) {
output = _.union(output, _this.getGlobbedFiles(globPattern, removeRoot));
});
} else if (_.isString(globPatterns)) {
glob(globPatterns, {
sync: true
}, function(err, files) {
if (removeRoot) {
files = files.map(function(file) {
return file.replace(removeRoot, '');
});
}
// The output array
var output = [];

output = _.union(output, files);
});
}
// If glob pattern is array so we use each pattern in a recursive way, otherwise we use glob
if (_.isArray(globPatterns)) {
globPatterns.forEach(function(globPattern) {
output = _.union(output, _this.getGlobbedFiles(globPattern, removeRoot));
});
} else if (_.isString(globPatterns)) {
if (urlRegex.test(globPatterns)) {
output.push(globPatterns);
} else {
glob(globPatterns, {
sync: true
}, function(err, files) {
if (removeRoot) {
files = files.map(function(file) {
return file.replace(removeRoot, '');
});
}

return output;
output = _.union(output, files);
});
}
}

return output;
};

/**
* Get the modules JavaScript files
*/
module.exports.getJavaScriptAssets = function(includeTests) {
var output = this.getGlobbedFiles(this.assets.lib.concat(this.assets.js), 'public/');
var output = this.getGlobbedFiles(this.assets.lib.concat(this.assets.js), 'public/');

// To include tests
if (includeTests) {
output = _.union(output, this.getGlobbedFiles(this.assets.tests));
}
// To include tests
if (includeTests) {
output = _.union(output, this.getGlobbedFiles(this.assets.tests));
}

return output;
return output;
};

/**
* Get the modules CSS files
*/
module.exports.getCSSAssets = function() {
var output = this.getGlobbedFiles(this.assets.css, 'public/');
return output;
var output = this.getGlobbedFiles(this.assets.css, 'public/');
return output;
};
8 changes: 8 additions & 0 deletions config/env/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
module.exports = {
db: process.env.MONGOHQ_URL || process.env.MONGOLAB_URI || 'mongodb://localhost/mean',
assets: {
lib: [
'public/lib/angular/angular.js',
'public/lib/angular-resource/angular-resource.js',
'public/lib/angular-animate/angular-animate.js',
'public/lib/angular-ui-router/release/angular-ui-router.js',
'public/lib/angular-ui-utils/ui-utils.js',
'public/lib/angular-bootstrap/ui-bootstrap-tpls.js'
],
css: 'public/dist/application.min.css',
js: 'public/dist/application.min.js'
},
Expand Down
244 changes: 125 additions & 119 deletions gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,130 +1,136 @@
'use strict';

var config = require('./config/config');

module.exports = function(grunt) {
// Project Configuration
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
serverViews: {
files: ['app/views/**'],
options: {
livereload: true,
}
},
serverJS: {
files: ['gruntfile.js', 'server.js', 'config/**/*.js', 'app/**/*.js'],
tasks: ['jshint'],
options: {
livereload: true,
}
},
clientViews: {
files: ['public/modules/**/views/*.html'],
options: {
livereload: true,
}
},
clientJS: {
files: ['public/js/**/*.js', 'public/modules/**/*.js'],
tasks: ['jshint'],
options: {
livereload: true,
}
},
clientCSS: {
files: ['public/**/css/*.css'],
tasks: ['csslint'],
options: {
livereload: true,
}
}
},
jshint: {
all: {
src: ['gruntfile.js', 'server.js', 'config/**/*.js', 'app/**/*.js', 'public/js/**/*.js', 'public/modules/**/*.js'],
options: {
jshintrc: true
}
}
},
csslint: {
options: {
csslintrc: '.csslintrc',
},
all: {
src: ['public/modules/**/css/*.css']
}
},
uglify: {
production: {
options: {
mangle: false
},
files: {
'public/dist/application.min.js': config.assets.js
}
}
},
cssmin: {
combine: {
files: {
'public/dist/application.min.css': config.assets.css
}
}
},
nodemon: {
dev: {
script: 'server.js',
options: {
nodeArgs: ['--debug']
}
}
},
concurrent: {
tasks: ['nodemon', 'watch'],
options: {
logConcurrentOutput: true
}
},
env: {
test: {
NODE_ENV: 'test'
}
},
mochaTest: {
src: ['app/tests/**/*.js'],
options: {
reporter: 'spec',
require: 'server.js'
}
},
karma: {
unit: {
configFile: 'karma.conf.js'
}
}
});
// Project Configuration
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
serverViews: {
files: ['app/views/**'],
options: {
livereload: true,
}
},
serverJS: {
files: ['gruntfile.js', 'server.js', 'config/**/*.js', 'app/**/*.js'],
tasks: ['jshint'],
options: {
livereload: true,
}
},
clientViews: {
files: ['public/modules/**/views/*.html'],
options: {
livereload: true,
}
},
clientJS: {
files: ['public/js/**/*.js', 'public/modules/**/*.js'],
tasks: ['jshint'],
options: {
livereload: true,
}
},
clientCSS: {
files: ['public/**/css/*.css'],
tasks: ['csslint'],
options: {
livereload: true,
}
}
},
jshint: {
all: {
src: ['gruntfile.js', 'server.js', 'config/**/*.js', 'app/**/*.js', 'public/js/**/*.js', 'public/modules/**/*.js'],
options: {
jshintrc: true
}
}
},
csslint: {
options: {
csslintrc: '.csslintrc',
},
all: {
src: ['public/modules/**/css/*.css']
}
},
uglify: {
production: {
options: {
mangle: false
},
files: {
'public/dist/application.min.js': '<%= applicationJavaScriptFiles %>'
}
}
},
cssmin: {
combine: {
files: {
'public/dist/application.min.css': '<%= applicationCSSFiles %>'
}
}
},
nodemon: {
dev: {
script: 'server.js',
options: {
nodeArgs: ['--debug']
}
}
},
concurrent: {
tasks: ['nodemon', 'watch'],
options: {
logConcurrentOutput: true
}
},
env: {
test: {
NODE_ENV: 'test'
}
},
mochaTest: {
src: ['app/tests/**/*.js'],
options: {
reporter: 'spec',
require: 'server.js'
}
},
karma: {
unit: {
configFile: 'karma.conf.js'
}
}
});

// Load NPM tasks
require('load-grunt-tasks')(grunt);

// Making grunt default to force in order not to break the project.
grunt.option('force', true);

//Load NPM tasks
require('load-grunt-tasks')(grunt);
// A Task for loading the configuration object
grunt.task.registerTask('loadConfig', 'Task that loads the config into a grunt option.', function() {
var config = require('./config/config');

//Making grunt default to force in order not to break the project.
grunt.option('force', true);
grunt.config.set('applicationJavaScriptFiles', config.assets.js);
grunt.config.set('applicationCSSFiles', config.assets.css);
});

//Default task(s).
grunt.registerTask('default', ['jshint', 'csslint', 'concurrent']);
// Default task(s).
grunt.registerTask('default', ['jshint', 'csslint', 'concurrent']);

//Lint task(s).
grunt.registerTask('lint', ['jshint', 'csslint']);
// Lint task(s).
grunt.registerTask('lint', ['jshint', 'csslint']);

//Build task(s).
grunt.registerTask('build', ['jshint', 'csslint', 'uglify', 'cssmin']);
// Build task(s).
grunt.registerTask('build', ['jshint', 'csslint', 'loadConfig' ,'uglify', 'cssmin']);

//Build task(s).
grunt.registerTask('heroku-deploy', ['jshint', 'csslint', 'uglify', 'cssmin']);
// Build task(s).
grunt.registerTask('heroku-deploy', ['jshint', 'csslint', 'uglify', 'cssmin']);

//Test task.
grunt.registerTask('test', ['env:test', 'mochaTest', 'karma:unit']);
// Test task.
grunt.registerTask('test', ['env:test', 'mochaTest', 'karma:unit']);
};
13 changes: 1 addition & 12 deletions public/dist/application.min.js

Large diffs are not rendered by default.

1 comment on commit 8cccae2

@ryanlatham
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amoshaviv Not sure if this is the direction you want to go in, but I did make a suggestion to move grunt tasks into the environment specific configurations. That would mean potentially one less file that needed to be changed if you wanted to deploy to different environments.

Here is my suggestion (let me know if it is not OK to link to that site, and I will remove it):
https://github.com/linnovate/mean/pull/413/files

Please sign in to comment.