grunt-browser-sync madness:一遍又一遍地打开浏览器的多个实例

时间:2014-08-24 14:11:42

标签: node.js gruntjs npm sync browser-sync

我接近让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']);
};

我想要的是以下内容:

  1. 在终端上输入grunt watch,然后在浏览器的静态服务器页面中自动打开index.html
  2. CSS,JS或图像更改时要重新加载的页面。
  3. 我的配置会发生以下情况:

    1. 只有在保存文件时才会打开新的浏览器窗口
    2. 每次我保存一些东西,多个浏览器窗口打开,localhost号码不断变化,渲染插件完全无用
    3. The output from the Grunt watch task

      我知道我已经在文件的每个可能位置注册了tasks : ['browserSync'],但这是浏览器同步某事的唯一方式。我希望这足够了:

      grunt.registerTask('newer', ['browserSync', 'sass', 'autoprefixer', 'concat', 'uglify']);

      但我没有运气。浏览器同步触发器,但没有打开静态服务器。

1 个答案:

答案 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"]);
};