KeJunMao / unplugin-preprocessor-directives

preprocessor directives for jsx,tsx,js,ts,html,css,vue and more
MIT License
70 stars 7 forks source link

This plugin breaks vitest coverage report #4

Closed Sec-ant closed 9 months ago

Sec-ant commented 10 months ago

When using this plugin and adding // #v-ifdef DEV and // #v-endif comments in my code, vitest marks all code as 100% covered (istanbul).

image

If I remove the directives, the coverage works again:

image

Similar issues can be found at:

  1. https://github.com/unplugin/unplugin-vue-components/issues/333
  2. https://github.com/unplugin/unplugin-vue-components/issues/219 (maybe related, this is referred to in the previous link)

This should be easily reproducible.

Here is a minimal reproduction: https://stackblitz.com/edit/vitest-dev-vitest-4tlh7n?file=src%2Findex.ts

Is there a workaround to solve this problem?

Sec-ant commented 10 months ago

As I only do testing in dev mode and use this conditional compile plugin in build mode, I find a workaround to force this plugin to only work at build process:

In my vite.config.ts, I extended the plugin return object with apply: "build" and it solves my problem

plugins: [{ ...ConditionalCompile(), apply: "build" }]