MCPcopy
hub / github.com/vitest-dev/vitest / transform

Function transform

packages/vitest/src/node/plugins/metaEnvReplacer.ts:12–45  ·  view source on GitHub ↗
(code, id)

Source from the content-addressed store, hash-verified

10 name: 'vitest:meta-env-replacer',
11 enforce: 'pre',
12 transform(code, id) {
13 if (!/\bimport\.meta\.env\b/.test(code)) {
14 return null
15 }
16
17 let s: MagicString | null = null
18 const cleanCode = stripLiteral(code)
19 const envs = cleanCode.matchAll(/\bimport\.meta\.env\b/g)
20
21 for (const env of envs) {
22 s ||= new MagicString(code)
23
24 const startIndex = env.index!
25 const endIndex = startIndex + env[0].length
26
27 s.overwrite(
28 startIndex,
29 endIndex,
30 `Object.assign(/* istanbul ignore next */ globalThis.__vitest_worker__?.metaEnv ?? import.meta.env)`,
31 )
32 }
33
34 if (s) {
35 return {
36 code: s.toString(),
37 map: s.generateMap({
38 hires: 'boundary',
39
40 // Remove possible query parameters, e.g. vue's "?vue&type=script&src=true&lang.ts"
41 source: cleanUrl(id),
42 }),
43 }
44 }
45 },
46 }
47}

Callers

nothing calls this directly

Calls 3

cleanUrlFunction · 0.90
testMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected