Grunt livereload not working - gruntjs

I'm quite new to grunt, and I'm trying to enable livereload.
The problem is that I can't seem to get it working.
Gruntfile.js:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
options: {
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %> */'
},
my_target: {
files: {
'dest/complete.min.js': ['scripts/*.js']
}
}
},
jshint: {
files: ['gruntfile.js', 'srcipts/*.js'],
options: {
// options here to override JSHint defaults
globals: {
jQuery: true,
console: true,
module: true,
document: true
}
}
},
livereload : {
options : {
base : '/',
},
files : ['/**/*']
},
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-livereload');
grunt.registerTask('default', ['jshint', 'uglify','livereload']);
};
package.json:
{
"name": "my-app",
"version": "0.0.0",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-uglify": "~0.2.0",
"grunt-contrib-jshint": "~0.6.0",
"grunt-contrib-livereload": "0.1.2",
"matchdep": "~0.1.2"
}
}
I don't get any errors,
I've tried: "grunt" and "grunt livereaload-start".
When going to my website via localhost - it won't connect.
And viewing the website via the file:// protocol will not reload when changes are made to css files.
Thanks in advance.

Live reloading has been moved to the watch task. Please see: https://github.com/gruntjs/grunt-contrib-watch#live-reloading
grunt.initConfig({
watch: {
livereload: {
options: { livereload: true },
files: ['dist/**/*'],
},
},
});

Related

Why it says Task "speak" not found. Use --force to continue

I have a problem with grunt. My Grunt version is:
grunt-cli v1.4.3
grunt v1.4.1
An Error occurs as I run any task.
Warning: Task "speak" not found. Use --force to continue.
This occurs with every function I run (for example grunt css or grunt default function) . I tried to take out whole code and make a test function and it worked... So I can sort out that the global installation is the problem. It must be my Gruntfile that is wrong.
Meanwhile I found out that some tasks were not in the right syntax structure. I changed that. Now it run my default task and other tasks without errors and firstly it looked like all runs fine. But as I tested it. The Changes in SCSS file doesn't show up in the compiled CSS File...
So something must be still wrong. So I added the new Gruntfile and replaced my name in Banner with name (so don't wonder about that). Maybe somebody know this issue... I also not know if it really would make the sourcemaps with this gruntfile code without additional plugins... But most important is that it compiles the scss ... without the whole reason of using grunt is gone...
Does anybody have an idea whats wrong
module.exports = function(grunt) {
grunt.initConfig({
package: grunt.file.readJSON('package.json'),
concat: {
js: {
src: 'scripts/js/**/_*.js',
dest:'scripts/js/app.js'
},
css: {
src: 'scripts/css/**/_*.css',
dest: 'scripts/css/app.css'
},
options: {
stripBanners: true,
banner: '//<%= package.name %> - Minified main Javascript from *Name* - <%= grunt.template.today("yyyy-mm-dd") %>'
}
},
uglify: {
options: {
banner: '//<%= package.name %> - Minified main Javascript from *Name* - <%= grunt.template.today("yyyy-mm-dd") %>',
mangle: {
reserved: 'jQuery'
},
compress: {
global_defs: {
"DEBUG": false
},
},
dead_code: false,
unused: false,
drop_console: true
},
build: {
src: 'scripts/js/app.js',
dest: 'build/scripts/js/app.min.js'
}
},
cssmin: {
options: {
specialComments: 0,
mergeIntoShorthands: false,
roundingPrecision: -1
},
target: {
files: {
'build/scripts/css/app.css': 'scripts/css/app.css'
}
}
},
sass: {
build: {
options: {
style: 'expanded',
debugInfo: true,
sourcemap: true
},
files: [{
expand: true,
cwd: 'scripts/css/',
src: ['scripts/scss/**/_*.scss'],
dest: 'scripts/css/',
ext: '.css'
}]
}
},
watch: {
js: {
files: ['scripts/js/_*.js'],
tasks: ['concat', 'uglify']
},
css: {
files: ['scripts/scss/_*.scss'],
tasks: ['concat', 'sass', 'cssmin']
},
php: {
files: ['*.html'],
tasks: ['htmlmin']
}
},
htmlmin: {
options: {
collapseWhitespace: true
},
target: {
files: {
src: '*.html',
dest: 'build/index.html'
}
}
}
});
//Load Plugins of Grunt
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-htmlmin');
grunt.loadNpmTasks('grunt-contrib-cssmin');
//Grunt Tasks
grunt.registerTask('default', ['sass', 'concat', 'cssmin', 'uglify']);
grunt.registerTask('all', ['sass', 'concat', 'cssmin','uglify']);
grunt.registerTask('css', ['sass', 'concat', 'cssmin']);
grunt.registerTask('js', 'uglify');
grunt.registerTask('publish', ['sass', 'concat', 'cssmin', 'uglify', 'htmlmin']);
grunt.registerTask('speak', function() {
console.log('I am running!');
});
};
Packages installes for grunt in package.json:
"devDependencies": {
"debug": "^4.3.1",
"grunt": "^1.4.1",
"grunt-cli": "^1.4.3",
"grunt-contrib-concat": "^1.0.1",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-sass": "^2.0.0",
"grunt-contrib-uglify": "^5.0.1",
"grunt-contrib-watch": "^1.1.0",
"webpack": "^5.40.0",
"webpack-cli": "^4.7.2"
}
}

Grunt: How do I run seperate processes for CSS and JS

As you can see I have tasks for CSS ['sass:main'] and for JS ['jshint:main', 'concat:main', 'uglify:main'], but I want to do separate tasks for separate files (JS and CSS) and listen for changes (watch). Can someone point me in the correct direction, I'm not really sure what I should be searching for. Is this something that watch can handle, or is there another plugin? I'm a little new to grunt so still trying to figure out how to use it. Thanks
GruntFile.js:
module.exports = function(grunt) {
var config = {
pkg: grunt.file.readJSON('package.json'),
jshint: {
options: {
globals: {
jQuery: true,
console: true,
module: true,
document: true
}
},
main: {
src: [
'assets/templates/main/js/crm/*.js',
]
}
},
concat: {
options: {
separator: '\n\n'
},
main: {
src: [
'assets/templates/main/js/crm/*.js',
],
dest: 'assets/templates/main/js/crm.min.js'
}
},
sass: {
options: {
style: 'compressed'
},
main: {
files: {
'assets/templates/main/css/main.min.css': 'assets/templates/main/sass/main.scss',
}
}
},
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
main: {
src: 'assets/templates/main/js/crm.min.js',
dest: 'assets/templates/main/js/crm.min.js'
}
},
watch: {
mainjs: {
files: ['assets/templates/main/js/crm/*.js'],
tasks: ['jshint:main', 'concat:main', 'uglify:main'],
},
mainsass: {
files: ['assets/templates/main/sass/*.scss''],
tasks: ['sass:main'],
}
},
concurrent: {
maincss: ['sass:main'],
mainjs: ['jshint:main', 'concat:main', 'uglify:main']
}
};
grunt.initConfig(config);
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-concurrent');
grunt.registerTask('main', ['jshint:main', 'concat:main', 'uglify:main', 'sass:main']);
grunt.registerTask('main-watch', ['jshint:main', 'concat:main', 'uglify:main', 'sass:main', 'concurrent:mainsass']);
};
When I try run tasks:
$ grunt main-watch
Loading "Gruntfile.js" tasks...ERROR
SyntaxError: Invalid or unexpected token
Warning: Task "main-watch" not found. Use --force to continue.
Aborted due to warnings.
It sounds like you want to perform two concurrent watch tasks. You can do that using a configuration like this:
...
concurrent: {
options: { logConcurrentOutput: true },
watch: ['watch:mainjs', 'watch:mainsass']
}
};
...
grunt.registerTask('main-watch', ['concurrent:watch']);
Note that logConcurrentOutput defaults to false, so if you want to see output logged to the console, you need to set it to true.

Bootstrap-Sass using Grunt

I have installed bootstrap using sass, however my scss files are not compiling to the css folder.
I was able to install the bootstrap, using bower.
This is my gruntfile.js
/*jslint node: true */
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
sass: {
options: {
loadPath: ['./bower_components/bootstrap-sass/assets/stylesheets']
},
dist: {
options: {
outputStyle: 'expanded',
sourceMap: false
},
files: {
'css/bootstrap.css' : 'stylesheets/_bootstrap.scss',
}
}
}, // sass
compass: {
dev: {
options: {
config: 'config.rb'
}
} // dev
}, //compass
watch: {
options: {
livereload: true,
dateFormat: function(time) {
grunt.log.writeln('The watch finished in ' + time + 'ms at ' + (new Date()).toString());
grunt.log.writeln('Waiting for more changes...');
} //date format function
}, //options
scripts: {
files: ['*.js']
}, // scripts
//Live Reload of SASS
sass: {
files: ['stylesheets/*.scss', 'stylesheets/bootstrap/*.scss'],
tasks: ['sass']
}, //sass
css: {
files: ['stylesheets/*.scss', 'stylesheets/bootstrap/*.scss'],
tasks: ['compass']
},
html: {
files: ['*.html']
}
}, //watch
postcss: {
options: {
processors: [
require('autoprefixer-core')({
browsers: 'last 2 versions'
})
]
}
}, //post css
jshint: {
options: {
reporter: require('jshint-stylish')
},
target: ['*.js', 'js/*.js']
} //jshint
});
grunt.loadNpmTasks('grunt-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-compass');
grunt.loadNpmTasks('grunt-postcss');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.registerTask('build', ['sass']);
grunt.registerTask('default', ['build', 'watch', 'compass', 'jshint']);
}
I would like to know, what step I am missing, because I have followed the directory paths correctly. My config.rb file, I have changed the sass directory to stylesheets.
When I run, sudo grunt, no errors are found but the stylesheet is not compiling.
In SCSS, files starting with _ (underscore) are typically ignored. See: http://sass-lang.com/documentation/file.SASS_REFERENCE.html#partials
This also applies to grunt-sass.

Grunt: convert sass to css after file changes (watch)

so this is my gruntfile.js:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
folders: {
'dist': 'dist',
'app': 'app'
},
// configure jshint to validate js files -----------------------------------
jshint: {
options: {
reporter: require('jshint-stylish'),
ignores: [
'Grunfile.js',
'app/assets/lib/**/*'
]
},
all: [
'app/**/**/*.js',
]
},
// configure uglify to minify js files -------------------------------------
uglify: {
options: {
banner: '/*\n <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> \n*/\n'
},
build: {
files: {
'dist/js/app.min.js': 'app/js/main.js'
}
}
},
// compile sass stylesheets to css -----------------------------------------
sass: {
dist: {
options: {
style: 'expanded'
},
files: {
'app/assets/css/main.css': 'app/assets/sass/main.sass',
'app/assets/css/variables.css': 'app/assets/sass/variables.sass',
'app/assets/css/typography.css': 'app/assets/sass/typography.sass',
'app/assets/css/reset.css': 'app/assets/sass/reset.sass'
}
}
},
// starting an express server ----------------------------------------------
express: {
dev: {
options: {
port: 9000,
hostname: '0.0.0.0',
bases: ['app'],
livereload: true
}
}
},
// configure watch to auto update ------------------------------------------
watch: {
sass: {
files: ['**/*.{scss,sass}'],
tasks: ['sass'],
options: {
livereload: true
}
},
reload: {
options: {
livereload: true
},
files: ['app/**/*'],
},
}
});
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-express');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', ['jshint', 'uglify', 'sass', 'express', 'watch:reload']);
};
My problem:
When I start grunt, it checks for my sass files and automatically convert it to css files. Check. But if i start grunt and then edit a sass file, it recognizes the changes but dont convert the sass to css after it again.
Does anybody see the mistake?
Cheers!
Found the "problem": Had to register the task 'watch' instead of 'watch:reload'!
Cheers

grunt doesnot reload server on watch livereload

I have been trying to reload the server if the any of the file changes. I can watch the files which has been changed but it does not reload my server.
GruntFile.js
module.exports = function(grunt){
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
jshint: {
all:{
src:'**/*.js',
}
},
concat: {
options: {
banner: '(function() {',
footer: '})();'
},
releaseLocalHybrid: {
src: ['config/config.local.js','lib/fuse.js','src/model.js','src/templates/hybrid.js','src/controller/hybrid.js'],
dest: 'dist/widgets.js'
}
},
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
},
releaseLocalHybrid: {
files: {
'widgets.js': ['<%= concat.releaseLocalHybrid.dest %>']
}
}
},
connect:
{
server:
{
options:
{
hostname: 'localhost',
port: 8082,
base: {
path:'.',
options: {
index:'index.html',
maxAge: 300000
},
},
livereload: true
}
}
},
watch: {
options: {
livereload: true
},
concat: {
files: 'config/*.js',
tasks: 'jshint',
options:
{
spawn:false
},
},
}
});
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', [ 'concat:releaseLocalHybrid','uglify:releaseLocalHybrid','connect', 'watch']);
grunt.registerTask('server', ['concat:releaseLocalHybrid','uglify:releaseLocalHybrid','connect','watch']);
};
Any suggestion or help will be grateful.
I was watching all the files in watch so jshint:all keeps watching and doesnot livereload but if i watch releaseLocalHybrid it worked. Thanks every one.
You need to specify a port for livereload. I have been using livereload options as :
watch: {
less: {
files : ['less/**/*.less']
},
css: {
files: ['css/*.css'],
options: {
livereload: {
port: 35750
}
}
}
}

Resources