GRUNT | Error: Unable to compile; no valid source files were found - gruntjs

I have installed the Graphicmagik and created the following Gruntfile.js:
module.exports = function(grunt) {
grunt.initConfig({
responsive_images: {
dev: {
options: {
engine: 'gm',
sizes: [{width: 320, name: 'small'},
{width: 640, name: 'medium'},
{width: 800, name: 'large' }
]
}
},
/*
You don't need to change this part if you don't change
the directory structure.
*/
files: [{
expand: true,
src: ['**/*.jpg'],
cwd: 'images_src/',
dest: 'images/'
}]
},
/* Clear out the images directory if it exists */
clean: {
dev: {
src: ['images'],
},
},
/* Generate the images directory if it is missing */
mkdir: {
dev: {
options: {
create: ['images']
},
},
},
/* Copy the "fixed" images that don't go through processing into the images/directory */
copy: {
dev: {
files: [{
expand: true,
src: 'images_src/fixed/*.{gif,jpg,png}',
dest: 'images/'
}]
},
},
});
grunt.loadNpmTasks('grunt-responsive-images');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-mkdir');
grunt.registerTask('default', ['clean', 'mkdir', 'copy', 'responsive_images']);
};
Project folder tree:
project
css
images_src
fixed
node_modules
grunt-contrib-clean
grunt-contrib-copy
grunt-contrib-mkdir
grunt-responsive-images
"other grunt dependencies"
When I $grunt responsive_images, I get the error following error:
Running "responsive_images:dev" (responsive_images) task
>> Unable to compile; no valid source files were found.
>> Unable to compile; no valid source files were found.
>> Unable to compile; no valid source files were found.
Running "responsive_images:files" (responsive_images) task
Fatal error: Unable to read configuration.
Have you specified a target? See: http://gruntjs.com/configuring-tasks
I changed the cwd and src several times but can't make it work! I get that the cwd ('current working directory') is the folder where the images are stored, the src is where I identify the file/file extention that I want to convert. So I think that this setup is correct. But why does it gives this error?

The files are defined outside of the responsive_images:dev target. Move the files block inside the dev target to fix:
responsive_images: {
dev: {
options: {
engine: 'gm',
sizes: [{width: 320, name: 'small'},
{width: 640, name: 'medium'},
{width: 800, name: 'large' }
]
},
files: [{
expand: true,
src: ['**/*.jpg'],
cwd: 'images_src/',
dest: 'images/'
}],
},
},

Related

Create two configurations for a grunt plug-in that is not multi-taskable

I use a grunt package called grunt-preprocess, Obviously, it doesn't support multi-tasks.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
preprocess: {
options: {
context: {
ENV: grunt.option('env') || 'prod'
},
},
all_from_dir: {
src: '*.*',
cwd: 'src/',
dest: 'src',
expand: true
}
},
})
Now I want to execute preprocess twice, once from the src directory, and once from the dist directory. How should I configure this package to achieve that?
I have tried this configuration;
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
preprocess: {
first: {
options: {
context: {
ENV: grunt.option('env') || 'prod'
},
},
all_from_dir: {
src: '*.*',
cwd: 'src/',
dest: 'src',
expand: true
}
},
second: {
options: {
context: {
ENV: grunt.option('env') || 'prod'
},
},
all_from_dir: {
src: '*.*',
cwd: 'dist/',
dest: 'dist',
expand: true
}
}
}
})
and then execute grunt preprocess:first. However it does not work:
PS D:\workspace\environment-compile> grunt preprocess:first
Running "preprocess:first" (preprocess) task
Done.
Yes, you're right preprocess is single Task only, therefore doesn't allow multiple targets to be defined.
You'll need to create another custom task that dynamically configures the preprocess Task and then runs it.
For example:
Gruntfile.js
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-preprocess');
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
preprocess: {
options: {
context: {
ENV: grunt.option('env') || 'prod'
},
},
all_from_dir: {
src: '*.*',
cwd: 'src/',
dest: 'src',
expand: true
}
},
// ...
});
// Custom task dynamically configures the `preprocess` Task and runs it.
grunt.registerTask('preprocess_dist', function() {
grunt.config.set('preprocess.all_from_dir.cwd', 'dist/');
grunt.config.set('preprocess.all_from_dir.dest', 'dist');
grunt.task.run(['preprocess']);
});
grunt.registerTask('preprocessBoth', [ 'preprocess', 'preprocess_dist' ]);
};
Running:
Via your CLI run the following single command to execute the preprocess task twice, once from the src directory, once from the dist directory:
grunt preprocessBoth
Explanation:
The custom Task named preprocess_dist dynamically configures the values for the cwd and dest properties, setting them to 'dist/' and 'dist' respectively. This is done via the grunt.config.set method
Then the task is run via the grunt.task.run method.
The last line of code that reads:
grunt.registerTask('preprocessBoth', [ 'preprocess', 'preprocess_dist' ]);
creates a task named preprocessBoth and adds the following two tasks to to the taskList:
preprocess
preprocess_dist
Essentially what happens when you run grunt preprocessBoth is:
The preprocess task runs using files from the src directory.
Then the custom preprocess_dist task runs using files from the dist directory.
If you prefer, you can also run each task independently via CLI, i.e:
grunt preprocess
and
grunt preprocess_dist

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.

Grunt Watch LiveReload on site on server

I am developing a WordPress site on a server (not local). I want to refresh the page in my browser whenever I modify a sass file. I've got some grunt tasks listed, but right now I just want it to refresh on any sass modification. Right now, it catches whenever a file is modified, but it does not refresh the page.
Gruntfile:
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
scripts: {
options: { livereload: true },
files: ['**/*.scss'],
//tasks: ['criticalcss:front', 'criticalcss:page', 'cssmin', 'postcss'],
}
},
postcss: {
options: {
processors: [
require('autoprefixer')({browsers: 'last 6 versions'}), // add vendor prefixes
//require('cssnano')() // minify the result
]
},
dist: {
src: 'style.css',
dest: 'style.css'
}
},
criticalcss: {
front : {
options: {
url: "https://grandeurflooring.ca/grand_dev/",
minify: true,
width: 1500,
height: 900,
outputfile: "critical_css/critical-front.css",
filename: "style.css",
buffer: 800*1024,
ignoreConsole: true
}
},
page : {
options: {
url: "https://grandeurflooring.ca/grand_dev/sample-page/",
minify: true,
width: 1500,
height: 900,
outputfile: "critical_css/critical-page.css",
filename: "style.css",
buffer: 800*1024,
ignoreConsole: true
}
}
},
cssmin: {
target: {
files: [{
expand: true,
cwd: 'critical_css',
src: ['*.css', '!*.min.css'],
dest: 'critical_css',
ext: '.min.css'
}]
}
}
});
// Load the plugin that provides the "critical" task.
grunt.loadNpmTasks('grunt-criticalcss');
// Load the plugin that provides the "cssmin" task.
grunt.loadNpmTasks('grunt-contrib-cssmin');
// Load the plugin that provides the "watch" task.
grunt.loadNpmTasks('grunt-contrib-watch');
// Load the plugin that provides the "PostCSS" task.
grunt.loadNpmTasks('grunt-postcss');
// Critical task.
grunt.registerTask('critical', ['criticalcss:front']);
};
In footer.php, before wp_footer(), I put the script:
<script src="http://localhost:35729/livereload.js"></script>
You can configure Grunt to watch the compiled css file in your dist directory, which would be updated every time the Sass is recompiled.
Here is my watch configuration which is achieving what you want:
watch: {
options: {
livereload: true,
},
html: {
files: ['index.html'],
},
js: {
files: ['js/**/*.js'],
tasks: ['jshint'],
},
sass: {
options: {
livereload: false
},
files: ['css/scss/**/*.scss'],
tasks: ['sass'],
},
css: {
files: ['dist/css/master.css'],
tasks: []
}
}
You might need to change spawn: false to spawn: true depending on your setup as well.
EDIT: Additionally, you can use the grunt-contrib-watch plugin which allows you to:
Run predefined tasks whenever watched file patterns are added, changed or deleted
This plugin contains numerous additional options for live-reloading, watching, etc. which you may find useful.

Running "responsive_images:dev" (responsive_images) task >> Unable to compile; no valid source files were found

I am trying to set grunt, I have already download GraphickMagick and ImageMAgick.
My OS is LINUX MINT.
When I try to run $grunt I get the following error:
Running "responsive_images:dev" (responsive_images) task
Unable to compile; no valid source files were found.
Unable to compile; no valid source files were found.
My current directory is /project-server:
project-server/images/
project-server/img/
project-server/node_modules/
project-server/Gruntfile.js
project-server/package.json
Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
responsive_images: {
dev: {
options: {
engine: 'im',
sizes: [{
name: 'small',
width: '30%',
suffix: '_small',
quality: 20
},{
name: 'large',
width: '50%',
suffix: '_large',
quality: 40
}]
},
files: [{
expand: true,
src: ['*.{gif,jpg,png}'],
cwd: 'images/',
dest: 'images/'
}]
}
},
});
grunt.loadNpmTasks('grunt-responsive-images');
grunt.registerTask('default', ['responsive_images']);
};
package.json
{
"name": "project-server",
"version": "0.1.0",
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-nodeunit": "~0.4.1",
"grunt-contrib-uglify": "~0.5.0",
"grunt-responsive-images": "^1.10.1"
}}
So,
src: ['*.{gif,jpg,png}'],
cwd: 'images/',
dest: 'images/'
Attention to cwd value, it says that my current directory is /images, so whenever he tries to find the value for src, it will search under /images. BUT my files {.png,.jpeg} arent under /images.
Then, all I had to do was to comment the cwd value. And it worked fine!

Grunt less won't replace css file

I use grunt-contrib-less with grunt-contrib-watch to compile my less files automatically upon change.
When the css is not present, grunt compiles it ok. When a css already exists and watch sees the less file changed, the css file is not modified. I have to remove it every time and let grunt recreate it with the modifications.
less config:
less: {
options: {
banner: '<%= meta.banner %>'
},
dev: {
options: {
sourceMap: true,
sourceMapFileInline: true,
compress: true
},
src: '<%= meta.dev.less %>/main.less',
dest: '<%= meta.prod.css %>/main.css'
},
prod: {
options: {
plugins: [
new( require( 'less-plugin-clean-css' ) )( {
'advanced': true,
'compatibility': 'ie9'
} )
],
},
src: '<%= meta.dev.less %>/main.less',
dest: '<%= meta.prod.css %>/main.css'
}
},
I'm under Windows 10 and every user have the rights to modifiy/delete the files in my dist folder.
How can I let grunt modify the css file?
EDIT
watch config
watch: {
options: {
livereload: 6325
},
js: {
files: [ '<%= meta.dev.js %>/main.js', '<%= meta.dev.js %>/plugins/*.js' ],
tasks: [ 'newer:concat' ]
},
images: {
files: '<%= meta.dev.img %>/**/*.{png,jpg,gif,svg}',
tasks: [ 'newer:imagemin' ]
},
css: {
files: '<%= meta.dev.less %>/**/*.less',
tasks: [ 'newer:less:dev' ]
}
}
Registration
grunt.registerTask( 'default', [ 'less:dev', 'concat', 'imagemin', 'copy', 'watch' ] );
Grunt output (verbose)
>> File "dev\less\elements\menu.less" changed.
Initializing
Command-line options: --verbose
Reading "Gruntfile.js" Gruntfile...OK
Registering Gruntfile tasks.
Reading package.json...OK
Parsing package.json...OK
Initializing config...OK
Loading "Gruntfile.js" tasks...OK
+ default, prod
Running tasks: newer:less:dev
Loading "grunt-newer" plugin
Registering "D:\[...]\static\node_modules\grunt-newer\tasks" tasks.
Loading "newer.js" tasks...OK
+ any-newer, newer, newer-clean, newer-postrun
Running "newer:less:dev" (newer) task
Options: cache="D:\\[...]\\static\\node_modules\\grunt-newer\\.cache", override=undefined
Files: dev/less/main.less -> dist/css/main.css
No newer files to process.
Here's what happens:
you modify menu.less
watch detects that and runs newer:less:dev
less:dev uses file main.less as only source (not menu.less)
that file hasn't changed, so newer concludes it doesn't need to run the task again
I suppose main.less includes menu.less, but newer doesn't know it.
So my suggested fix would be to get rid of the newer part.
Have you added correct watch config for less ?
watch: {
less: {
files: ['less/**/*.less'], // which files to watch
tasks: ['less'],
options: {
nospawn: true
}
}
}
To run grunt with your profile : grunt.registerTask('myprofile', ['less:dev', 'watch']);

Resources