JuniorTour / vue-template-babel-compiler

Enable Optional Chaining(?.), Nullish Coalescing(??) and many new ES syntax for Vue.js SFC based on Babel
https://www.npmjs.com/package/vue-template-babel-compiler
116 stars 9 forks source link

Any idea how to avoid inserting counters in template, only script, when I use babel-plugin-istanbul for coverage? #48

Open Losstie opened 1 year ago

Losstie commented 1 year ago

I'm tring to use babel-plugin-istanbul for coverage, and I only care about the script part of the .vue file. In some vue projects, it's ok. But there are some vue projects, which will be inserted counters by babel-plugin-istanbul in both script and template parts. I'm very confused about this.

const babelConfig = {
  presets: [
    [
      '@vue/babel-preset-app',
      {
        // 需要兼容低版本例如Android<7,ios<9 请打开注释
        exclude: ['es.symbol', 'es.symbol.description', 'es.object.to-string'],
      },
    ],
  ],
  plugins: [
    'babel-plugin-istanbul',
    '@babel/plugin-proposal-export-default-from',
    '@babel/plugin-syntax-export-default-from',
  ],
};
JuniorTour commented 1 year ago

你说的 couter 具体指什么? 是指类似cov_1b2esh9sgc() 这样被istanbul添加到编译后产物的方法吗?

是的话,我理解之所以要给 template 也添加这些辅助方法,是因为template被编译后也是js函数,也需要测试覆盖率。

《AST 编译为渲染函数 render()》https://juejin.cn/post/7085375799888445477?share_token=e6fd9fd6-a40f-4a01-ba64-139abcb77268

Losstie commented 1 year ago

是的,具体指的类似cov_1b2esh9sgc() 这样被istanbul添加到编译后产物。我也理解给template编译是理所当然,毕竟如你所说它编译后是js函数。但令我疑惑的是,一些vue项目中,template和script部分像是一起经过babel编译的,因为他们的计数器是一样的,但某些vue项目template和script部分却像单独经过编译再合并在一起的,他们有各自的计数器。 我非常想知道是什么导致这种差异

Losstie commented 1 year ago

上面说法有所错误。现在的问题是:我手里有两个vue项目,其中一个项目的vue文件经过编译不会对template部分进行插桩(就像tempate部分没有使用项目的babel配置一样),而另外一个项目的vue文件会对tempate部分进行插桩。我希望能够知道是什么导致这种差异,现在的猜测是vue-template-babel-compiler版本不一致,是否有版本对template编译时没有使用项目的babel配置而是自带了一套babelConfig.js进行编译。

JuniorTour commented 1 year ago

vue-template-babel-compiler默认会对<template> 编译出的render()函数执行一次babel编译(如下图,你可以通过在node_modules/vue-template-babel-compiler/lib/index.js 中添加 debugger 检查这个逻辑)

image

所以针对你说的2个项目,1个有插桩,一个没有的问题。 不像是这个库直接引起的,我建议你控制变量试一下,对这两个项目分别增加、删除vue-template-babel-compiler看看是否都有插桩。

如果你能提供2个项目的最小可运行DEMO,我很乐意为你检查一下这个问题。