我接近让grunt-browser-sync
工作,但还没有完成。
我想出了这个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']);
};
我想要的是以下内容:
grunt watch
,然后在浏览器的静态服务器页面中自动打开index.html
。我的配置会发生以下情况:
localhost
号码不断变化,渲染插件完全无用
我知道我已经在文件的每个可能位置注册了tasks : ['browserSync']
,但这是浏览器同步某事的唯一方式。我希望这足够了:
grunt.registerTask('newer', ['browserSync', 'sass', 'autoprefixer', 'concat', 'uglify']);
但我没有运气。浏览器同步触发器,但没有打开静态服务器。
答案 0 :(得分:7)
BrowserSync作者在这里。
问题在于您多次启动BrowserSync任务 - 这不是正确使用它的方法。
检查http://www.browsersync.io/docs/grunt/处的示例 - 您应该独立(以及之前)任何其他监视任务启动BrowserSync。
// 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"]);
};