Extend sails 0.10 grunt set-up - gruntjs

I am intending to add a task to sails's (0.10 rc5) grunt build system.
tasks/config/asset-versioning.js
module.exports = function(grunt) {
grunt.config.set('asset-versioning', {
copy: {
src: '.tmp/public/min/production.js',
dest: '.tmp/public/min/production2.js'
}
});
grunt.task.registerTask('asset-versioning', ['copy']);
grunt.loadNpmTasks('grunt-contrib-copy');
};
tasks/register/prod.js
module.exports = function (grunt) {
grunt.registerTask('prod', [
'compileAssets',
'concat',
'uglify',
'cssmin',
'sails-linker:prodJs',
'sails-linker:prodStyles',
'sails-linker:devTpl',
'asset-versioning' // Added the task config here
]);
};
After running sails lift --prod --verbose I am getting following error:
Grunt :: Warning: Task "asset-versioning" not found.
** Grunt :: An error occurred. **
What am I missing?
EDIT
Apparently I am missing to register my task.
Adding grunt.task.registerTask('asset-versioning'); to the first file. I am not getting any error, but my task is not running! Nothing is happening.

Okay, you're having this all mixed up. Copy is a predefined task, you just need to tweak its configs. Go to tasks/config/copy.js and add your custom configuration (in the grunt.config.set call)
productionfiles: {
src: '.tmp/public/min/production.min.js',
dest: '.tmp/public/min/production2.min.js'
}
Then, in the tasks/register/prod.js, make sure you call copy:productionfiles, and it will do the magic.
Copy.js
module.exports = function(grunt) {
grunt.config.set('copy', {
dev: {
files: [{
expand: true,
cwd: './assets',
src: ['**/*.!(coffee|less)'],
dest: '.tmp/public'
}]
},
build: {
files: [{
expand: true,
cwd: '.tmp/public',
src: ['**/*'],
dest: 'www'
}]
},
productionfiles: {
src: '.tmp/public/min/production.min.js',
dest: '.tmp/public/min/production2.min.js'
}
});
grunt.loadNpmTasks('grunt-contrib-copy');
};
Prod.js
module.exports = function (grunt) {
grunt.registerTask('prod', [
'compileAssets',
'concat',
'uglify',
'cssmin',
'sails-linker:prodJs',
'sails-linker:prodStyles',
'sails-linker:devTpl',
'sails-linker:prodJsJade',
'sails-linker:prodStylesJade',
'sails-linker:devTplJade',
'copy:productionfiles'
]);
};

Related

Gruntfile.js Syntax error: Warning: Task "default" not found. Use --force to continue

This is my very first attempt at configuring a Gruntfile.js and i can't seem to figure out what's causing this "Warning: Task "default" not found. Use --force to continue. Aborted due to warnings." error. Can someone please help me figure out what's wrong with my code. Find source code below:
'use strict';
module.exports = function (grunt) {
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
// Automatically load required Grunt tasks
require('jit-grunt')(grunt);
// Define the configuration for all the tasks
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// Make sure code styles are up to par and there are no obvious mistakes
jshint: {
options: {
jshintrc: '.jshintrc',
reporter: require('jshint-stylish')
},
all: {
src: [
'Gruntfile.js',
'app/scripts/{,*/}*.js'
]
}
}
}),
copy: {
dist: {
cwd: 'app',
src: [ '**','!styles/**/*.css','!scripts/**/*.js' ],
dest: 'dist',
expand: true
},
fonts: {
files: [
{
//for bootstrap fonts
expand: true,
dot: true,
cwd: 'bower_components/bootstrap/dist',
src: ['fonts/*.*'],
dest: 'dist'
}, {
//for font-awesome
expand: true,
dot: true,
cwd: 'bower_components/font-awesome',
src: ['fonts/*.*'],
dest: 'dist'
}
]
}
},
clean: {
build: {
src: [ 'dist/']
}
});
grunt.registerTask('build', [
'clean',
'jshint',
'copy'
]);
grunt.registerTask('build', [
'jshint'
]);
grunt.registerTask('default',['build']);
};
You have not defined a task named default. A sample, really small gruntfile.js can be found below:
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.initConfig({
copy: {
main: {
files: [
{ expand: true, src: ['**'], cwd: 'lib/font-awesome/fonts/', dest: 'fonts/', filter: 'isFile' },
]
}
}
});
grunt.registerTask('default', ['copy:main']);
};
So, in this file, I'm loading the NPM package called grunt-contrib-copy. Then in the config, I specify the config for the copy sub-task by giving it a name of main and including the rules below that. Each grunt sub-task has it's own format of options and configuration that you can follow. Finally, I'm saying that the Grunt task runner should have a task named default followed by an order of operations. The first (and only) in my case is the copy sub-task and the main ruleset. In your case, this may be copy:dist for the copy operation.
Hope that helps get you started.
I've found the syntax error. These characters ')};' closed the config in the wrong place (line 30) which disrupted the flow of the program. so i just had to remove them and put them in their rightful place way down on line 67.
Sorry it turned out to be just a silly mistake on my part and probably nothing relevant to anyone else.

Task Default Not Found , despite mentioning the Default Task

There are questions ask before but I couldn't get much out of them as I'm new to grunt.Whenever I run code , Console reads as follows
SyntaxError: Invalid or unexpected token
Warning: Task "default" not found. Use --force to continue.
My code is:
'use strict';
module.exports = function (grunt) {
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
// Automatically load required Grunt tasks
require('jit-grunt')(grunt, {
useminPrepare: 'grunt-usemin'
});
// Define the configuration for all the tasks
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// Make sure code styles are up to par and there are no obvious mistakes
jshint: {
options: {
jshintrc: '.jshintrc',
reporter: require('jshint-stylish')
},
all: {
src: [
'Gruntfile.js',
'app/scripts/{,*/}*.js'
]
}
},
useminPrepare: {
html: 'app/menu.htm
},
useminPrepare: {
html: 'app/menu.html',
options: {
dest: 'dist'
}
},
// Concat
concat: {
options: {
separator: ';'
},
// dist configuration is provided by useminPrepare
dist: {}
},
// Uglify
uglify: {
// dist configuration is provided by useminPrepare
dist: {}
},
cssmin: {
dist: {}
},
// Filerev
filerev: {
options: {
encoding: 'utf8',
algorithm: 'md5',
length: 20
},
release: {
// filerev:release hashes(md5) all assets (images, js and css )
// in dist directory
files: [{
src: [
'dist/scripts/*.js',
'dist/styles/*.css',
]
}]
}
},
// Usemin
// Replaces all assets with their revved version in html and css files.
// options.assetDirs contains the directories for finding the assets
// according to their relative paths
usemin: {
html: ['dist/*.html'],
css: ['dist/styles/*.css'],
options: {
assetsDirs: ['dist', 'dist/styles']
}
},
copy: {
dist: {
cwd: 'app',
src: [ '**','!styles/**/*.css','!scripts/**/*.js' ],
dest: 'dist',
expand: true
},
fonts: {
files: [
{
//for bootstrap fonts
expand: true,
dot: true,
cwd: 'bower_components/bootstrap/dist',
src: ['fonts/*.*'],
dest: 'dist'
}, {
//for font-awesome
expand: true,
dot: true,
cwd: 'bower_components/font-awesome',
src: ['fonts/*.*'],
dest: 'dist'
}
]
}
},
clean: {
build: {
src: [ 'dist/']
}
}
});
grunt.registerTask('build', [
'clean',
'jshint',
'useminPrepare',
'concat',
'cssmin',
'uglify',
'copy',
'filerev',
'usemin'
]);
grunt.registerTask('default' , ['build']);
};
the complete output of your grunt call shows that your gruntfile.js has a sytax error:
Loading "gruntfile.js" tasks...ERROR
>> SyntaxError: Invalid or unexpected token Warning: Task "default" not found. Use --force to continue.
Aborted due to warnings.
and indeed your file has no closing quotes on line 37:
useminPrepare: {
html: 'app/menu.htm <-----!!!!!!!!
},
useminPrepare: {
html: 'app/menu.html',
options: {
dest: 'dist'
}
},
probably a copy paste error since you have the useminPrepare twice (which is illegal in strict mode btw...)

task jade not found when running grunt

When I run grunt with the following Gruntfile, I get the error Warning: task "jade" not found. what exactly could be wrong here?
module.exports = function(grunt) {
grunt.initConfig({
jade: {
compile: {
options: {
client: false,
pretty: true
},
files: [ {
cwd: "app/views",
src: "**/*.jade",
dest: "build/templates",
expand: true,
ext: ".html"
} ]
}
}
});
grunt.registerTask('default','Convert Jade templates into html templates',
['jade','watch']);
grunt.loadNpmTasks('grunt-contrib-watch');
};
You need to load grunt jade task. The same as yo are adding grunt-contrib-watch
grunt.loadNpmTasks('grunt-contrib-jade');
This should works.

unexpected identifier error in grunt.js file

Can anyone tell me why I am getting the following error in terminal when running the following grunt.js file?
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: { // Here is where we do our concatenating
dist: {
src: [
'components/js/*.js' // everything in the src js folder
],
dest: 'js/script.js',
}
}
uglify: {
build: {
src: 'js/script.js',
dest: 'js/script.min.js'
}
}
});
//Add all the plugins here
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
//What happens when we type 'grunt' in the terminal
grunt.registerTask('default', ['concat', 'uglify']);
};
The error I get is:
Loading "gruntfile.js" tasks...ERROR
SyntaxError: Unexpected identifier
Warning: Task "default" not found. Use --force to continue.
THANKS!
You have a missing comma:
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
dist: {
src: [
'components/js/*.js'
],
dest: 'js/script.js',
}
}, // <---- here was a missing comma
uglify: {
build: {
src: 'js/script.js',
dest: 'js/script.min.js'
}
}
});
Make yourself a favor and use coffeescript!
module.exports = (grunt) ->
grunt.initConfig
pkg: grunt.file.readJSON("package.json")
concat:
dist:
src: ["components/js/*.js"]
dest: "js/script.js"
uglify:
build:
src: "js/script.js"
dest: "js/script.min.js"
grunt.loadNpmTasks "grunt-contrib-concat"
grunt.loadNpmTasks "grunt-contrib-uglify"
grunt.registerTask "default", [
"concat"
"uglify"
]

Change the configuration of the task uglify dynamically

I need to change configuration of my uglify task for only minify file as needed (as explained here for the jshint task : https://github.com/gruntjs/grunt-contrib-watch#compiling-files-as-needed)
The modification works well for the jshint task but not for uglify, i think the problem is the property path...
Any help would be appreciated ;)
Here is my Gruntfile.js :
module.exports = function (grunt) {
grunt.initConfig({
// define source files and their destinations
jshint: {
all: ['dev/**/*.js'],
},
uglify: {
dynamic_mappings: {
// Grunt will search for "**/*.js" under "dev/" when the "minify" task
// runs and build the appropriate src-dest file mappings then, so you
// don't need to update the Gruntfile when files are added or removed.
files: [{
expand: true, // Enable dynamic expansion.
cwd: 'dev/', // Src matches are relative to this path.
src: ['**/*.js'], // Actual pattern(s) to match.
dest: 'build', // Destination path prefix.
ext: '.min.js', // Dest filepaths will have this extension.
},
],
}
}
watch: {
options: { spawn: false },
js: { files: 'dev/**/*.js', tasks: [ 'uglify' ] },
}
});
// load plugins
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
// default task
grunt.registerTask('default', [ 'watch' ]);
grunt.event.on('watch', function(action, filepath) {
grunt.config(['jshint', 'all'], filepath);
grunt.config('uglify.dynamic_mappings.files', [{src: filepath }]);
});
};
The line
grunt.config('uglify.dynamic_mappings.files', [{src: filepath }]);
Is completely replacing the original configuration for uglify.dynamic_mappings.files
Instead try including the other original parameters along with the new src: filepath
How to not minify already minified on each "grunt build" in yeoman
uglify: {
onlyScripts: {
files: [{
dest: '<%= yeoman.dist %>/scripts/scripts.js',
src: ['.tmp/concat/scripts/scripts.js']
}]
}
}
Also, now uglify will not copy your vendor.js from temporary folder, so add "vendorJS" section to "copy" task:
copy:
//...
vendorJS: {
expand: true,
cwd: '.tmp/concat/scripts/',
dest: '<%= yeoman.dist %>/scripts/',
src: 'vendor.js'
}
Then, in "build" task, set target of uglify to 'onlyScripts' and copy vendor.js:
grunt.registerTask('build', [
'jshint',
'clean:dist',
//'wiredep',
// ...
'useminPrepare',
//'concurrent:dist',
'autoprefixer',
'concat',
'ngAnnotate',
'copy:dist',
'cssmin',
'uglify:onlyScripts',
'copy:vendorJS',
// ...
]);
http://eugenioz.blogspot.in/2014/08/how-to-not-minify-already-minified-on.html

Resources