如何调试Vue3源码

3,589 阅读1分钟

哎!假期结束听说Vue3出了源码,赶紧git clone https://github.com/vuejs/vue-next.git了一份。但是不知道怎么断点调试,遂查找了下方法。

Vue.js贡献指南中提到使用yarn dev命令,但是我还没下过yarn。简单来说,Yarn就是一个类似于 npm 的包管理工具。

  1. npm install -g yarn
  2. yarn install

安装完yarn和依赖后,在vue-next项目中找到rollup.config.js文件加上 output.sourcemap = true

然后在tsconfig.json文件把"sourceMap": false改成"sourceMap": true

最后写个测试的html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="./vue-next/packages/vue/dist/vue.global.js"></script>
</head>
<body>
  <div id="app">
  </div>
  <script>
    const App = {
      template:`
      <p>{{name}}</p>
      `,
      data(){
        return{
          name:"vue"
        }
      },
    }
    let vm = Vue.createApp(App).mount(App,"#app");
  </script>
</body>
</html>

就可以打开浏览器控制台打断点了

如果需要利用Vue3里的测试案例,可以使用以下命令

#运行所有测试 
$ yarn test

#在监视模式下运行测试 
$ yarn test --watch

#在runtime-core包下运行所有测试 
$ yarn test runtime-core

#在特定文件中运行测试 
$ yarn test packages/reactivity/__tests__/reactive.spec.ts 

#在一个特定的文件中运行特定的测试 
$ yarn test packages/reactivity/__tests__/reactive.spec.ts -t 'reactivity/collections'