vitejs / vite-plugin-vue

Vite Vue Plugins
MIT License
486 stars 153 forks source link

Support import.meta.env.VITE_FOO in Vue templates #66

Open mahnunchik opened 1 year ago

mahnunchik commented 1 year ago

Description

Add support environment variables import.meta.env.VITE_FOO in Vue templates.

// text.vue
<template>
  <p>{{ import.meta.env.VITE_FOO }}</p>
  <div v-if="import.meta.env.VITE_FOO">
    content
  </div>
</template>

Currently the code above raises the error:

[plugin:vite:vue] Error parsing JavaScript expression: import.meta may appear only with 'sourceType: "module"' (1:1)

Suggested solution

Just support it 😉

Alternative

No response

Additional context

Related issues:

Validations

sapphi-red commented 1 year ago

This seems to be happening because sourceType: 'module' is not set here. https://github.com/vuejs/core/blob/fe77e2bddaa5930ad37a43fe8e6254ddb0f9c2d7/packages/compiler-core/src/transforms/transformExpression.ts#L250-L252

sapphi-red commented 1 year ago

https://github.com/vuejs/rfcs/issues/566