1

我一直在尝试为我的报道报告启用 livereload。我生成此报告的工具是karma-coverage,我在以下文件夹中生成此报告:test/coverage/html-report

 -test
 |
 |-- coverage
     |
     |-- report-html
         |
         |-- index.html

生成报告后,我使用grunt-contrib-connect并使用以下内容提供生成的报告:

 var COVERAGE_BASE = './test/coverage/report-html',

   ...
   ...
   ...
   connect: {
      server: {
        options: {
          port: 9000,
          base: '.',
          open: false
        }
      },
      dist:{
        options: {
          keepalive: true,
          port: 9000,
          base: './dist',
          open: false
        }
      },
      coverage: {
        options: {
          keepalive: true,
          port: 9001,
          base: COVERAGE_BASE ,
          open: false
        }
      }
    }

当我执行我的任务grunt coverage时,我的配置是

COVERAGE_TASKS = ['shell:test','open:coverage', 'connect:coverage','watch'];

grunt.registerTask('coverage', COVERAGE_TASKS);

我的想法是“拦截” index.html 并注入一个脚本来 livereload

<script src="//localhost:35729/livereload.js"></script>

在与连接包一起服务并处理它以添加此脚本之前,是否存在某种方法来拦截 index.html?

4

1 回答 1

0

这可能会有所帮助:您可以添加可用于拦截对连接服务器的请求的中间件,如下所示:

grunt.initConfig({
  connect: {
    server: {
      options: {
        middleware: [
          function myMiddleware(req, res, next) {
            
            // DO YOUR THING HERE!!!!
            res.end('Hello, world!');
          }
        ],
      },
    },
  },
});

于 2016-05-06T13:54:01.883 回答