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

标签 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' ]);

我的其他服务器几乎使用相同的配置,只需将“build”路径替换为“bin”即可。 我尝试遵循 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。 我一直在考虑连接的多任务,但设置起来似乎很复杂。

最佳答案

主要问题是您必须确保每个设置之间的连接/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'
    ]);
};

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

更新

中间件 + 自定义 livereload 端口破坏可能是一个已知问题

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

Uuups, yes there is a reason for this behaviour. the connect-livereload middleware rewrites res.write, res.writeHead and res.end and stores the original versions.

关于javascript - 使用 Grunt Connect 和 Livereload 配置多个服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24203241/

相关文章:

javascript - 使用原生 reduce 代替 prototype 1.6.0.3 reduce

javascript - 为什么这个表的最后一列太宽了?

javascript - require 在 node.js 中的行为

javascript - 使用 ngAnimate 作为依赖项时出现 MVC4 和 AngularJS 错误

ember.js - 结合使用grunt Handlebars 和 Ember ,将模板拆分为单独的文件

javascript - 使用 GruntJS 将 CSS 和 JS 文件嵌入到 HTML 中?

javascript - 使用 Grunt.js 迭代完全独特但重叠的主题文件

css - 在 Less 中为 CSS 类中的 Sprite 从宽度转换为背景宽度

javascript - 使用最新值更新 GIT 文件夹

javascript - 尝试订阅 Firebase Cloud Messaging 主题时出现错误