Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: define process variable and using import.meta.env together did not work #3944

Merged
merged 4 commits into from Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite-node/src/client.ts
Expand Up @@ -299,7 +299,7 @@ export class ViteNodeRunner {
const modulePath = cleanUrl(moduleId)
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
const href = pathToFileURL(modulePath).href
const meta = { url: href }
const meta = { url: href, env: process.env }
const exports = Object.create(null)
Object.defineProperty(exports, Symbol.toStringTag, {
value: 'Module',
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/plugins/ssrReplacer.ts
Expand Up @@ -23,7 +23,7 @@ export function SsrReplacerPlugin(): Plugin {
const startIndex = env.index!
const endIndex = startIndex + env[0].length

s.overwrite(startIndex, endIndex, 'process.env')
s.overwrite(startIndex, endIndex, '__vite_ssr_import_meta__.env')
}

const urls = cleanCode.matchAll(/\bimport\.meta\.url\b/g)
Expand Down
6 changes: 6 additions & 0 deletions test/core/test/env.test.ts
Expand Up @@ -47,3 +47,9 @@ test('custom env', () => {
test('ignores import.meta.env in string literals', () => {
expect('import.meta.env').toBe('import' + '.meta.env')
})

test('define process and using import.meta.env together', () => {
const process = {}
expect(process).toMatchObject({})
expect(import.meta.env.MODE).toEqual('test')
})