通过https gulp-livereload?

pun*_*bit 11 livereload gulp gulp-livereload

我一直在使用livereload chrome扩展,它将http:// [...] /livereload.js插入到文档中.不幸的是,我正在开发一个需要https的项目,我希望能在本地复制,但我没有必要这样做,因为我可以为不同的环境更改协议,但我想知道是否可以设置gulp -livereload通过https加载?

我尝试了一些事情,比如手动添加脚本没有成功,因为我收到连接错误(GET https://127.0.0.1:35729/livereload.js?snipver=1 net :: ERR_CONNECTION_CLOSED):

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://127.0.0.1:35729/livereload.js?snipver=1';
document.getElementsByTagName('body')[0].appendChild(script)
Run Code Online (Sandbox Code Playgroud)

任何提示都表示赞赏,谢谢!

Aus*_*ray 5

我头顶有两种解决方案:

1.自己托管客户

https://github.com/livereload/livereload-js

将其安装为依赖项:

bower install livereload-js --save-dev
Run Code Online (Sandbox Code Playgroud)

要么

npm install livereload-js --save
Run Code Online (Sandbox Code Playgroud)

然后就像包含常规脚本一样包含它.但请注意这些警告.

/path/to/dist/livereload.js?host=localhost
Run Code Online (Sandbox Code Playgroud)

2.代理客户端脚本

这很复杂,但肯定会奏效.在gulpfile中创建一个HTTPS服务器(可能在watch任务中).

https://github.com/nodejitsu/node-http-proxy

https://github.com/nodejitsu/node-http-proxy#using-https

httpProxy.createServer({
  ssl: {
    key: fs.readFileSync('valid-ssl-key.pem', 'utf8'),
    cert: fs.readFileSync('valid-ssl-cert.pem', 'utf8')
  },
  target: 'https://localhost:35729',
  secure: true // Depends on your needs, could be false.
}).listen(443);
Run Code Online (Sandbox Code Playgroud)

然后,您可以引用代理脚本.

https://127.0.0.1:443/livereload.js?snipver=1
Run Code Online (Sandbox Code Playgroud)