I'm close to making grunt-browser-sync
work, but still not quite there yet.
I came up with this Gruntfile:
module.exports = function(grunt) {
grunt.initConfig({
pkg : grunt.file.readJSON('package.json'),
concat : {
dist : {
src : ['js/libs/*.js', 'js/custom/*.js'],
dest : 'js/build/production.js',
}
},
uglify : {
dist : {
src : 'js/build/production.js',
dest : 'js/build/production.min.js'
}
},
sass : {
dist : {
options : {
style : 'compressed',
compass : 'true',
},
files : {
'css/main.css' : 'sass/main.scss'
}
}
},
autoprefixer : {
options : {
browsers : ['> 5%', 'last 2 version', 'ie 8', 'ie 9']
},
dist : {
files : {
'css/main.css' : 'css/main.css'
}
}
},
watch : {
options : {
livereload : true
},
content : {
files : '*.html',
tasks : ['browserSync']
},
scripts : {
files : ['js/libs/*.js', 'js/custom/*.js'],
tasks : ['concat', 'uglify', 'browserSync'],
options : {
spawn : false,
},
},
css : {
files : ['sass/**/*.scss'],
tasks : ['sass', 'autoprefixer', 'browserSync'],
options : {
spawn : false,
}
}
},
browserSync : {
files : {
src : ['css/*.css', 'images/*.*', 'js/build/production.min.js', '*.html'],
},
options : {
server: {
baseDir: "./",
},
watchTask : true
}
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-browser-sync');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-newer');
grunt.registerTask('newer', ['browserSync', 'sass', 'autoprefixer', 'concat', 'uglify']);
};
What I want is the following:
grunt watch
on the terminal and automatically have my index.html
opened in a static server page on my browser.What happens with my configuration is the following:
localhost
number keeps changing rendering the plugin totally uselessI'm aware i've registered tasks : ['browserSync']
in every possible place on the file, but that's the only way browser-sync does something. I expected this to be enough:
grunt.registerTask('newer', ['browserSync', 'sass', 'autoprefixer', 'concat', 'uglify']);
But I've no luck with that. browser-sync triggers but no static server is opened.
BrowserSync author here.
The problem is that you're starting the BrowserSync task multiple times - this is not the correct way to use it.
Check the examples at http://www.browsersync.io/docs/grunt/ - you should start BrowserSync independently (and before) any other watch tasks like this.
// This shows a full config file!
module.exports = function (grunt) {
grunt.initConfig({
watch: {
files: "assets/scss/**/*.scss",
tasks: ['compass']
},
compass: {
dist: {
options: {
sassDir: 'assets/scss',
cssDir: 'assets/css',
outputStyle: 'compressed'
}
}
},
browserSync: {
dev: {
bsFiles: {
src : 'assets/css/*.css'
},
options: {
watchTask: true // < VERY important
}
}
}
});
// load npm tasks
grunt.loadNpmTasks('grunt-contrib-compass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-browser-sync');
// start browsersync and then initiate the watch AFTER
grunt.registerTask('default', ["browserSync", "watch"]);
};