underfin / vite-plugin-vue2

Vue2 plugin for Vite
620 stars 83 forks source link

fix: error when SFC script contains variable named render, staticRenderFns, etc #145

Closed thebanjomatic closed 2 years ago

thebanjomatic commented 2 years ago

This plugin injects code into the same scope as the script code, but also introduces variables. These variables can lead to errors when a script also contains a symbol with the same name. I have applied some additional namespacing to make it more unlikely that these variable names would collide with a user's code, and additionally less likely to collide with other generated code.

To reproduce the error, you can create a .vue file with the following content:

<script lang="ts">
import Vue from "vue";

const render = function() {
  console.log('test');
}

export default Vue.extend({
  mounted() {
    render();
  }
});
</script>

image

Looking at the output, likely candidates for collisions with user-generated code are:

And there are a few that use different styles of escaping already:

I wasn't sure if those were based on conventions used elsewhere and as a result didn't touch them, however, I'm not exactly happy with the new situation of having __*, __*__ and __vue2_* all in the same file, so I'm open to which ever form of escaping the project maintainers are willing to accept.

thebanjomatic commented 2 years ago

@underfin / @antfu do you have any thoughts on this bug fix or suggestions on the prefixing strategy to use?