Skip to content

Instantly share code, notes, and snippets.

@willmendesneto
Last active August 29, 2015 14:01
Show Gist options
  • Save willmendesneto/904c99621cdf9730175b to your computer and use it in GitHub Desktop.
Save willmendesneto/904c99621cdf9730175b to your computer and use it in GitHub Desktop.
Files requireds for create a project for create angular modules using Grunt task manager
'use strict';
// # 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'
// OBS:
// Replace the string with informations
// '<%= yeoman.moduleName %>' = Module name
// '<%= yeoman.moduleDescription %>' = Module description
// '<%= yeoman.moduleUrl %>' = Module url
module.exports = function (grunt) {
// Load grunt tasks automatically
require('load-grunt-tasks')(grunt);
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
// Define the configuration for all the tasks
grunt.initConfig({
// Project settings
yeoman: {
// configurable paths
app: require('./bower.json').appPath || 'app',
dist: 'dist',
moduleName: 'your-module-name',
moduleDescription: 'your-module-description',
moduleUrl: 'your-url-here',
docs: 'docs'
},
// Project documentation
yuidoc: {
all: {
name: '<%= yeoman.moduleName %>',
description: '<%= yeoman.moduleDescription %>',
version: '0.0.1',
url: '<%= yeoman.moduleUrl %>',
options: {
paths: ['<%= yeoman.app %>/scripts/'],
outdir: '<%= yeoman.docs %>/',
themedir: '<%= yeoman.app %>/bower_components/yuidoc-bootstrap-theme/',
helpers: ['<%= yeoman.app %>/bower_components/yuidoc-bootstrap-theme/helpers/helpers.js']
}
}
},
// Watches files for changes and runs tasks based on the changed files
watch: {
js: {
files: ['<%= yeoman.app %>/scripts/{,*/}*.js'],
tasks: ['newer:jshint:all'],
options: {
livereload: true
}
},
jsTest: {
files: ['test/spec/{,*/}*.js'],
tasks: ['newer:jshint:test', 'karma']
},
gruntfile: {
files: ['Gruntfile.js']
},
livereload: {
options: {
livereload: '<%= connect.options.livereload %>'
},
files: [
'<%= yeoman.app %>/{,*/}*.html',
'.tmp/styles/{,*/}*.css',
'<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
]
}
},
// The actual grunt server settings
connect: {
options: {
port: 9000,
// Change this to '0.0.0.0' to access the server from outside.
hostname: 'localhost',
livereload: 35729
},
proxies: [
{
context: '/api',
host: 'localhost',
port: 3000
}
],
livereload: {
options: {
middleware: function(connect, options){
if (!Array.isArray(options.base)){
options.base = [options.base];
}
// Setup the proxy
var middlewares = [require('grunt-connect-proxy/lib/utils').proxyRequest];
// Serve static files.
options.base.forEach(function(base){
middlewares.push(connect.static(base));
});
// Make directory browse-able.
var directory = options.directory || options.base[options.base.length - 1];
middlewares.push(connect.directory(directory));
return middlewares;
},
open: true,
base: [
'.tmp',
'<%= yeoman.app %>'
]
}
},
test: {
options: {
port: 9001,
base: [
'.tmp',
'test',
'<%= yeoman.app %>'
]
}
},
dist: {
options: {
base: '<%= yeoman.dist %>'
}
},
docs: {
options: {
base: '<%= yeoman.docs %>'
}
}
},
// Make sure code styles are up to par and there are no obvious mistakes
jshint: {
options: {
jshintrc: '.jshintrc',
ignores: [
'Gruntfile.js'
],
reporter: require('jshint-stylish')
},
all: [
'Gruntfile.js',
'<%= yeoman.app %>/scripts/{,*/}*.js'
],
test: {
options: {
jshintrc: 'test/.jshintrc'
},
src: ['test/spec/{,*/}*.js']
}
},
// Empties folders to start fresh
clean: {
dist: {
files: [{
dot: true,
src: [
'.tmp',
'<%= yeoman.dist %>/*',
'!<%= yeoman.dist %>/.git*'
]
}]
},
server: '.tmp'
},
// Allow the use of non-minsafe AngularJS files. Automatically makes it
// minsafe compatible so Uglify does not destroy the ng references
ngmin: {
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.app %>/scripts',
src: '**/*.js',
dest: '<%= yeoman.dist %>/scripts'
}]
}
},
// Copies remaining files to places other tasks can use
copy: {
dist: {
files: [{
expand: true,
dot: true,
cwd: '<%= yeoman.app %>/scripts/',
dest: '<%= yeoman.dist %>/scripts/',
src: '**'
}]
}
},
// Run some tasks in parallel to speed up the build process
concurrent: {
server: [],
test: [],
dist: []
},
uglify: {
options: {
mangle: false
},
dist: {
files: {
// package name
'<%= yeoman.dist %>/<%= yeoman.moduleName %>.min.js': [
// List of all files in scripts folder (if have some file in "scripts" root folder)
'<%= yeoman.app %>/scripts/*.js',
// List of all directives
'<%= yeoman.app %>/scripts/directives/**/*.js',
// List of all filters
'<%= yeoman.app %>/scripts/filters/**/*.js',
// List of all services
'<%= yeoman.app %>/scripts/services/**/*.js'
]
}
}
},
concat: {
dist: {
src: [
// List of all files in scripts folder (if have some file in "scripts" root folder)
'<%= yeoman.app %>/scripts/*.js',
// List of all directives
'<%= yeoman.app %>/scripts/directives/**/*.js',
// List of all filters
'<%= yeoman.app %>/scripts/filters/**/*.js',
// List of all services
'<%= yeoman.app %>/scripts/services/**/*.js'
],
dest: '<%= yeoman.dist %>/<%= yeoman.moduleName %>.js'
},
},
// Test settings
karma: {
unit: {
configFile: 'karma.conf.js',
singleRun: true
}
}
});
grunt.registerTask('buildNgModuleFile', 'Task for build ng module script file.', function(filename) {
var path = require('path'),
fs = require('fs'),
SCRIPTS_DIST_DIR = 'dist/'
;
// uncompressed and compressed module
var buildFile = function(filename){
var angularModule = {
contentUncompressed: {
header: '(function(window, angular, undefined) {' +
'\n' +
'\'use strict\';' +
'\n',
footer: '\n' +
'})(window, window.angular);'
},
contentCompressed: {
header: '(function(window, angular, undefined){ "use strict";',
footer: '})(window, window.angular);'
}
};
var remove = ['\'use strict\';', '"use strict";'];
// Verifying template for include in compiled file
var templateNgModule = (filename.indexOf('.min.') !== (-1)) ? angularModule.contentUncompressed : angularModule.contentCompressed;
var data = fs.readFileSync(filename, 'utf8');
data = templateNgModule.header + data.replace(new RegExp(remove[0], 'g'), '').replace(new RegExp(remove[1], 'g'), '') + templateNgModule.footer;
fs.writeFileSync(filename, data, 'utf8');
console.log('Build of filename "'+filename+'" finished with success');
};
fs.readdirSync(SCRIPTS_DIST_DIR).forEach(function(file) {
if ( file.indexOf(filename) !== (-1) ) {
buildFile(SCRIPTS_DIST_DIR + filename);
}
});
});
grunt.registerTask('serve', function (target) {
if (target === 'dist') {
return grunt.task.run(['build', 'connect:dist:keepalive']);
}
if (target === 'docs') {
return grunt.task.run(['connect:docs:keepalive']);
}
grunt.task.run([
'clean:server',
'connect:livereload',
'watch'
]);
});
grunt.registerTask('server', function (target) {
target = ':'+target || '';
grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.');
grunt.task.run(['serve'+target]);
});
grunt.registerTask('docs', [
'yuidoc'
]);
grunt.registerTask('test', [
'clean:server',
'connect:test',
'karma'
]);
grunt.registerTask('build', [
'clean:dist',
'concat',
'ngmin',
'copy:dist',
'uglify',
'buildNgModuleFile:'+grunt.config.get('yeoman')['moduleName']+'.js',
'buildNgModuleFile:'+grunt.config.get('yeoman')['moduleName']+'.min.js'
]);
grunt.registerTask('default', [
'newer:jshint',
'test',
'build'
]);
};
{
"author": "your-name <your-email>",
"name": "your-module-name",
"description": "your-module-description",
"version": "0.0.1",
"homepage": "your-module-homepage",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-autoprefixer": "~0.4.0",
"grunt-bower-install": "~0.7.0",
"grunt-concurrent": "~0.4.1",
"grunt-contrib-clean": "~0.5.0",
"grunt-contrib-coffee": "~0.7.0",
"grunt-contrib-compass": "~0.6.0",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-connect": "~0.5.0",
"grunt-contrib-copy": "~0.4.1",
"grunt-contrib-cssmin": "~0.7.0",
"grunt-contrib-htmlmin": "~0.1.3",
"grunt-contrib-imagemin": "~0.3.0",
"grunt-contrib-jshint": "~0.7.1",
"grunt-contrib-uglify": "~0.2.0",
"grunt-contrib-watch": "~0.5.2",
"grunt-google-cdn": "~0.2.0",
"grunt-newer": "~0.5.4",
"grunt-ngmin": "~0.0.2",
"grunt-rev": "~0.1.0",
"grunt-svgmin": "~0.2.0",
"grunt-usemin": "~2.0.0",
"jshint-stylish": "~0.1.3",
"load-grunt-tasks": "~0.2.0",
"time-grunt": "~0.2.1",
"karma-ng-html2js-preprocessor": "~0.1.0",
"karma-ng-scenario": "~0.1.0",
"karma": "~0.12",
"grunt-karma": "~0.8.2",
"grunt-docular": "~0.1.2",
"grunt-html": "~1.2.0",
"karma-jasmine": "~0.1.5",
"karma-chrome-launcher": "~0.1.3",
"grunt-connect-proxy": "~0.1.10",
"protractor": "~0.21.0",
"requirejs": "~2.1.11",
"karma-requirejs": "~0.2.1",
"karma-phantomjs-launcher": "~0.1.4",
"grunt-contrib-yuidoc": "~0.5.2"
},
"engines": {
"node": ">=0.8.0"
},
"scripts": {
"prestart": "npm install",
"postinstall": "bower install && ./node_modules/protractor/bin/webdriver-manager update && cp ./node_modules/protractor/example/chromeOnlyConf.js protractor_conf.js",
"start": "grunt serve",
"test": "grunt test"
}
}
@VictorQueiroz
Copy link

O gerador do yeoman já não cria toda essa estrutura de arquivos automaticamente?

@willmendesneto
Copy link
Author

@VictorQueiroz , são propósitos diferentes. Apesar de utilizar o gerador para facilitar o fluxo de criação de arquivos e testes, as tarefas do Grunt foram otimizadas para a criação de um módulo. O generator-angular do Yeoman foi utilizado para gerar a estrutura e melhorar o workflow para a criação de módulos.

As tasks para o build são diferentes das que vem por default no generator-angular e focadas no processo de build de um módulo angular. Os plugins de rotas e o arquivo app.js, por exemplo, não são necessários para criarmos um módulo angular. Existe também uma diferença nas tasks de concat e uglify e foi criada uma nova tarefa (task buildNgModuleFile) para o fechamento do módulo.

Neste link tem um exemplo de como fica a estrutura de seu módulo, com base no generator-angular, mas sem o que não é necessário para o fechamento do módulo:

https://github.com/willmendesneto/keepr/tree/master/app

Neste link tem um exemplo de como o módulo é criado após a task de build:

https://github.com/willmendesneto/keepr/blob/master/dist/keepr.js

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