使用Grunt Connect和Livereload配置多个服务器

时间:2014-06-13 10:30:15

标签: javascript gruntjs

我正在尝试用2台服务器进行grunt连接任务。虽然在文档中对此进行了解释,但我使用livereload启动它并且无法使其正常工作。

    connect : {
        proxies : [ {
            context : '/rest',
            host : 'localhost',
            port : 8080,
            https : false,
            changeOrigin : false,
            rewrite : {
                'rest' : 'paf/rest'
            }
        }, {
            context : '/logout',
            host : 'localhost',
            port : 8080,
            https : false,
            changeOrigin : false,
            rewrite : {
                'logout' : 'paf/logout'
            }
        } ],
        options : {
            base : 'build',
            port : 9000,
            hostname : '0.0.0.0'
        },
        livereload : {
            options : {
                middleware : function(connect) {
                    return [ proxySnippet, lrSnippet, mountFolder(connect, '../target/build') ];
                }
            }
        }
    }

要启动我的服务器,我使用:

grunt.registerTask('server', [ 'configureProxies', 'connect:livereload', 'watch' ]);

我的其他服务器几乎使用相同的配置,只需用“bin”替换“build”路径。 我尝试使用2个服务器声明来跟踪文档但是,我无法正确启动它。

    connect : {
        dev: {
            proxies : [ { ...}],
            options : { ... },
            livereload : {}
        },
        prod: {
            proxies : [ { ...}],
            options : { ... },
            livereload : {}
        }
    }


grunt.registerTask('serverDev', [ 'configureProxies', 'connect:dev:livereload', 'watch' ]);
grunt.registerTask('serverProd', [ 'configureProxies', 'connect:prod:livereload', 'watch' ]);

但是,它只调用connect:dev而不是livereload。 我一直在考虑连接的多任务,但设置似乎很复杂。

1 个答案:

答案 0 :(得分:2)

主要问题是你必须确保你的connect / livereload端口不会在每次设置之间发生冲突。

这是我做过的最简单的工作示例:

'use strict';

module.exports = function (grunt) {

    require('time-grunt')(grunt);
    require('load-grunt-tasks')(grunt);

    grunt.initConfig({
        connect: {
            options: {
                open: true,
                hostname: 'localhost'
            },
            first: {
                options: {
                    port: 8000,
                    livereload: 3500,
                    base: './'
                }
            },
            second: {
                options: {
                    open: {
                        target: 'http://<%= connect.options.hostname %>:<%= connect.second.options.port %>/two.html'
                    },
                    port: 8001,
                    livereload: 3501,
                    base: './',
                    index: 'two.html'
                }
            }
        },
        watch: {
            first: {
                options: { livereload: 3500 },
                files: ['style.css', 'index.html']
            },
            second: {
                options: { livereload: 3501 },
                files: ['style.css', 'two.html']
            },
        }
    });

    grunt.registerTask('default', [
        'connect:first',
        'watch:first'
    ]);

    grunt.registerTask('second', [
        'connect:second',
        'watch:second'
    ]);
};

然而,我遇到了使用更复杂的连接设置(中间件等)的问题。我遇到的最大障碍是注入正确的livereload端口?我还不能确定它。

更新

中间件+自定义livereload port clobbering可能是一个已知问题

来自:https://github.com/gruntjs/grunt-contrib-connect/issues/65#issuecomment-59759233

  

Uuups,是的,有这种行为的原因。 connect-livereload中间件重写res.write,res.writeHead和res.end并存储原始版本。