基于Vue.js开发移动端工程时,一些特定的问题和场景下,只能在移动端运行工程复现、追踪问题(好比在微信端内,在App容器内),桌面端的Devtools就无法用了。数次安装Electron版本devtools后,以为调试起来太麻烦了,因此我决定把devtools搬进vConsole里javascript
若是不知道什么是vConsole,能够看这里vConsole Readmehtml
Sample Code 体验地址: CodePenvue
Github 地址 欢迎Starjava
引入很是简单webpack
import VConsole from "vconsole";
import Devtools from 'vue-vconsole-devtools'
Devtools.initPlugin(new VConsole());
复制代码
效果图:git
分析完Vue、Vue-devtools、vConsole源码之后,彷佛可行。步骤以下:github
首先一个问题就是 Vue-devtools并非一个库,因此npm上没有它,没法引用,其次这个工程也不适合做为库输出,由于它的打包方式比较特殊,还有这个工程自己的目的就是打包成一个可执行的App或者chrome插件,因此若是想引用它里面的代码,我想到最简单的方式就是拷贝了。。。 因此剥离frontend很是简单,在Vue-devtools工程的package.json中增长script: "buildvc": "cd packages/shell-dev && cross-env NODE_ENV=production webpack --progress --hide-modules",
同时在shell-dev里增长一个文件叫 inject.js:web
import { initDevTools } from '@front'
import Bridge from '@utils/bridge'
const targetWindow = window.parent;
document.body.style.overflow = "scroll";
initDevTools({
connect (cb) {
cb(new Bridge({
listen (fn) {
window.addEventListener('message', evt => {
fn(evt.data)
})
},
send (data) {
targetWindow.postMessage(data, '*')
}
}))
},
onReload (reloadFn) {
reloadFn.call();
}
})
复制代码
固然shell-dev里的webpack.config.js里 增长一个入口配置 inject: './src/inject.js'
chrome
打出来的包就是咱们要的front部分,最终嵌入iframe里。shell
上面的inject.js中已经包含了 front部分 接收和发送消息的代码了。 接下来完成backend部分的消息发送和接收,
import { initBackend } from '@back'
import Bridge from '@utils/bridge'
const initBackendWithTargetWindow = function(win,targetWindow){
const bridge = new Bridge({
listen (fn) {
win.addEventListener('message', evt => {
fn(evt.data)})
},
send (data) {
targetWindow.postMessage(data, '*')
}
})
initBackend(bridge)
}
export default { initBackendWithTargetWindow }
复制代码
这个比较麻烦,也遇到了一些兼容性问题,最终方案是:
import injectString from './inject.txt'
function inject (scriptContent, done) {
const script = document.getElementById('vue-iframe').contentWindow.document.createElement('script')
script.text = scriptContent
document.getElementById('vue-iframe').contentWindow.document.body.appendChild(script)
}
inject(injectString)
复制代码
实现一个plugin类,继承VConsolePlugin,而后实现对应的方法便可,具体文档能够查看VConsole文档vConsole Readme
class VConsoleVueTab extends VConsolePlugin {
constructor(...args) {
super(...args);
}
onRenderTab(cb){
cb(`<iframe id="vue-iframe" style="width:100%;position:absolute;top:0;bottom:0;min-height:100%;"></iframe>`);
}
onReady() {
target = document.getElementById('vue-iframe')
targetWindow = target.contentWindow;
be.initBackendWithTargetWindow(window,targetWindow);
}
onShow() {
injectOnce(injectString)
}
}
复制代码
这一步须要打包发布,而且优化
const initPlugin = function(vConsole){
var tab = new VConsoleVueTab('vue', 'Vue');
vConsole.addPlugin(tab);
}
export default {
initPlugin
}
复制代码
21-08-03更新,支持CDN引入
<script src="https://unpkg.com/vconsole/dist/vconsole.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-vconsole-devtools@0.0.7/dist/vue_plugin.js"></script>
<script> var vConsole = new window.VConsole(); const Devtools = window.vueVconsoleDevtools["default"]; Devtools.initPlugin(vConsole); </script>
复制代码