Giter VIP home page Giter VIP logo

Comments (4)

JarvisH avatar JarvisH commented on September 27, 2024 1

I should mention I have solved this by exporting all needed env values from a separate file:

// fx utils/env.ts
export const MODE = import.meta.env.MODE
export const PROD = import.meta.env.PROD

This allows for easy mocking as well during tests.

from vitest.

imjasonmiller avatar imjasonmiller commented on September 27, 2024

I ran into the same issue (import.meta.env.DEV) and I couldn't figure out what was causing it, thank you!

from vitest.

hi-ogawa avatar hi-ogawa commented on September 27, 2024

It looks like strip-literal is confused by Vue component file. v1.3.0 is where strip-literal changed from acorn to js-token https://github.com/vitest-dev/vitest/releases/tag/v1.3.0 #5136, so that may be the reason (though I'm not sure how acorn could've handle Vue either).

When I locally debug the code around here:

let s: MagicString | null = null
const cleanCode = stripLiteral(code)
const envs = cleanCode.matchAll(/\bimport\.meta\.env\b/g)

I see following transformations:

Hello.vue
  • original
<template>
  <SomeOtherComponent
    :some-prop="
      testValue
        ? 'A long value to break handling of env variables'
        : 'it needs to break into a new line'
    "
  >
    ...
  </SomeOtherComponent>
</template>

<script lang="ts">
...
    console.log(import.meta.env.MODE);
  • after stripLiteral
    • there are two additional " in :some-prop="" ... "", which is probably pushing an offset by two later in MagicString.overwrite.
<template>
  <SomeOtherComponent
    :some-prop=""
      testValue
        ? '                                               '
        : '                                 '
    ""
  >
    ...
  </SomeOtherComponent>
</template>

<script lang="  ">
...
    console.log(import.meta.env.MODE);
  • after MagicString.overwrite
<template>
  <SomeOtherComponent
    :some-prop="
      testValue
        ? 'A long value to break handling of env variables'
        : 'it needs to break into a new line'
    "
  >
    ...
  </SomeOtherComponent>
</template>

<script lang="ts">
...
    console.log(im__vite_ssr_import_meta__.envODE);

from vitest.

hi-ogawa avatar hi-ogawa commented on September 27, 2024

This is fixed in strip-literal v2.1.0. You can upgrade the transitive dependency either by re-creating lockfile or tools such as pnpm dedupe.

I confirmed the reproduction is fixed using package.json overrides:
https://stackblitz.com/edit/vitest-dev-vitest-bifxjk?file=package.json

from vitest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.