@wbi/cli-service
Version:
local service for wb-cli projects
63 lines (60 loc) • 2.33 kB
JavaScript
/**
* Created by Administrator on 2019/2/7.
* hot middleware事件订阅
*/
var hotClient = require('webpack-hot-middleware/client?noInfo=true')
// 修改配置文件的弹窗信息
var clientOverlayHtml = '<div style="position: fixed; top: 0; left: 0; z-index: 999999; width: 100%; height: 100%; background: rgba(0, 0, 0, .85);">'
clientOverlayHtml += '<h1 style="position: absolute; top: 50%; left: 0; margin: 0; width: 100%; font-size: 24px; line-height: 36px; color: #fff; letter-spacing: 2px; text-align: center; -webkit-transform: translateY(-50%); transform: translateY(-50%);">{file}<br>检测到以上构建配置被修改,<br>正在重启服务...</h1>'
clientOverlayHtml += '</div>'
var div = document.createElement('div')
hotClient.subscribe(function (event) {
switch (event.action) {
case 'reload':
window.location.reload()
break
case 'reloadServer':
div.innerHTML = clientOverlayHtml.replace('{file}', event.file)
document.body.appendChild(div)
setTimeout(function () {
window.location.reload()
}, 1000)
break
}
})
// 通知服务器,客户端浏览器已链接
/*const ajax = {
get: function (url, fn) {
// XMLHttpRequest对象用于在后台与服务器交换数据
var xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
xhr.onreadystatechange = function () {
// readyState == 4说明请求已完成
if (xhr.readyState === 4 && xhr.status === 200 || xhr.status === 304) {
// 从服务器获得数据
fn.call(this, xhr.responseText)
}
}
xhr.send()
},
// datat应为'a=a1&b=b1'这种字符串格式,在jq里如果data为对象会自动将对象转成这种字符串格式
post: function (url, data, fn) {
var xhr = new XMLHttpRequest()
xhr.open('POST', url, true)
// 添加http头,发送信息至服务器时内容编码类型
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 304)) {
fn.call(this, xhr.responseText)
}
}
xhr.send(data)
}
}
window.addEventListener('load', () => {
setTimeout(() => {
ajax.post('/domReady', '', res => {
// console.log(res)
})
}, 0)
}, false)*/