Vue开发移动端项目,这个工具对你必定有帮助

基于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

  1. 剥离Vue-devtools @front部分
  2. 实现@backend 和 @front 部分通讯
  3. 实现front注入iframe
  4. iframe嵌入vConsole
  5. 制做npm包并发布

1. 剥离front

首先一个问题就是 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

2. 实现通讯

上面的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 }
复制代码

3. front嵌入iframe

这个比较麻烦,也遇到了一些兼容性问题,最终方案是:

  1. 把第一步打包的inject.js 重命名为 inject.txt
  2. 增长一个rawloader规则,识别txt
  3. 使用script.txt的方式插入到script标签中,而后插入iframe的body中
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)
复制代码

4. iframe嵌入vconsole

实现一个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)
  }
}
复制代码

5. 制做npm包并发布

这一步须要打包发布,而且优化

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>
复制代码
相关文章
相关标签/搜索