Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
09704aa
[AI] Replace bare import.meta.env with null
brillout Feb 4, 2026
55b8ee7
wip
brillout Feb 4, 2026
6eee910
update to-do
brillout Feb 4, 2026
cfb7198
[AI] Replace import.meta.env with __VIKE_IS_NOT_EXTERNAL
brillout Feb 4, 2026
4d0a444
wip
brillout Feb 4, 2026
c2732c0
simplify
brillout Feb 4, 2026
c173b97
minor refactor: rename __VIKE__IS_NOT_EXTERNAL => __VIKE__IS_NO_EXTERNAL
brillout Feb 4, 2026
72d5375
minor refactor
brillout Feb 4, 2026
9ef1c39
minor refactor: rename __VIKE__IS_NO_EXTERNAL => __VIKE__NO_EXTERNAL
brillout Feb 4, 2026
4850228
wip
brillout Feb 4, 2026
4cb0f84
wip
brillout Feb 4, 2026
ab00385
wip
brillout Feb 4, 2026
1a99240
wip
brillout Feb 4, 2026
c4baa71
debug
brillout Feb 4, 2026
085252c
Revert "debug"
brillout Feb 4, 2026
3819246
update to-do
brillout Feb 4, 2026
86ecc26
[AI] Remove redundant code.replaceAll line
brillout Feb 4, 2026
98e460d
[AI] Fix regex lastIndex issue with bareImportMetaEnvRegex
brillout Feb 4, 2026
eec57b3
update docs
brillout Feb 4, 2026
44717a6
update docs
brillout Feb 4, 2026
7946594
Revert "update docs"
brillout Feb 4, 2026
912a0d4
Reapply "update docs"
brillout Feb 4, 2026
fc3cddc
comment
brillout Feb 4, 2026
4872922
docs
brillout Feb 4, 2026
fb5da01
inspect
brillout Feb 4, 2026
8670449
Revert "inspect"
brillout Feb 4, 2026
de26bfc
remove old inspect
brillout Feb 4, 2026
611a5a1
wip
brillout Feb 4, 2026
cf326c7
comment
brillout Feb 4, 2026
cf48676
comment
brillout Feb 4, 2026
b203daf
test
brillout Feb 4, 2026
6c1c7f2
Revert "test"
brillout Feb 4, 2026
5a9d1eb
comment
brillout Feb 4, 2026
16c1ab8
minor refactor
brillout Feb 4, 2026
7e45eda
minor refactor
brillout Feb 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/pages/blog/vike-server/+Page.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Link } from '@brillout/docpress'
import { Link, ImportMeta } from '@brillout/docpress'
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs'
import '../../../components/tabs.css'
import { BlogHeader } from '../BlogHeader'
Expand All @@ -8,7 +8,7 @@ import { BlogHeader } from '../BlogHeader'
We (<Link href="/team">Joël, Dani, and Rom</Link>) have been working on a <Link href="/vike-server">new Vike extension `vike-server`</Link> which can integrate Vite with any server (Express.js, Hono, Fastify, Elysia, H3, ...) and any deployment (VPS, Netlify, Cloudflare, Vercel, ...).

With `vike-server` you get:
- Server code **transpiled by Vite** (say goodbye to `ts-node`/`tsx`/`vavite` and hello to Vite utilities such as `import.meta.env`)
- Server code **transpiled by Vite** (say goodbye to `ts-node`/`tsx`/`vavite` and hello to Vite utilities such as <ImportMeta prop="env" />)
- **Zero-config** (Vike is automatically added to your server)
- **HMR** (no more full server reloads)

Expand Down
14 changes: 7 additions & 7 deletions docs/pages/env/+Page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ DATABASE_URL=postgresql://database.example.com:5432

## Access

Use `import.meta.env` to access environment variables.
Use <ImportMeta prop="env"/> to access environment variables.

```ts
// /pages/movies/+data.ts
Expand Down Expand Up @@ -79,12 +79,12 @@ function ContactUs() {

> Keep in mind that <code>{'import.meta.env.SOME_ENV'}</code> <Link href="#static-replacement">is statically replaced</Link>:
> ```ts
> // ✅ Works
> import.meta.env.SOME_ENV
> // ❌ Won't work
> import.meta.env['SOME_ENV']
> // ❌ Won't work
> const { SOME_ENV } = import.meta.env
> // ✅ Works
> import.meta.env.SOME_ENV
> ```


Expand All @@ -102,7 +102,7 @@ For improved DX, consider using [`zod`](https://github.com/colinhacks/zod), for
For better [tree shaking](https://rollupjs.org/introduction/#tree-shaking) (aka dead code elimination), `import.meta.env.SOME_ENV` is statically replaced with its value. For example:

```js
// src/someFile.js [source code in Git repository]
// src/someFile.js [source code in your Git repository]

console.log('value:', import.meta.env.DISABLE_TRACKING)

Expand All @@ -128,14 +128,14 @@ console.log("value:", "true");
console.log("Tracking is disabled");
```

Note how the `import()` is completely removed, resulting in more lightweight production bundles.
Note how `import()` is completely removed, resulting in a more lightweight production bundle.


## Config files

In config files, [`import.meta.env` isn't available](https://github.com/vikejs/vike/issues/1726#issuecomment-2208626928) (neither `vite.config.js` nor `+config.js`).
In config files, [<ImportMeta prop="env"/> isn't available](https://github.com/vikejs/vike/issues/1726#issuecomment-2208626928) (neither `vite.config.js` nor `+config.js`).

Use [`process.env`](https://nodejs.org/en/learn/command-line/how-to-read-environment-variables-from-nodejs) instead of `import.meta.env`.
Use [`process.env`](https://nodejs.org/en/learn/command-line/how-to-read-environment-variables-from-nodejs) instead of <ImportMeta prop="env"/>.


## Public allowlist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getFilePathToShowToUserModule } from '../shared/getFilePath.js'
import { normalizeId } from '../shared/normalizeId.js'
import { isViteServerSide_extraSafe } from '../shared/isViteServerSide.js'
import { getMagicString } from '../shared/getMagicString.js'
import pc from '@brillout/picocolors'

const PUBLIC_ENV_PREFIX = 'PUBLIC_ENV__'
const PUBLIC_ENV_ALLOWLIST = [
Expand All @@ -29,7 +30,7 @@ const PUBLIC_ENV_ALLOWLIST = [
// - Or stop using Vite's `mode` implementation and have Vike implement its own `mode` feature? (So that the only dependencies are `$ vike build --mode staging` and `$ MODE=staging vike build`.)

// === Rolldown filter
const skipIrrelevant = 'import.meta.env.'
const skipIrrelevant = 'import.meta.env'
const filterRolldown = {
/* We don't do that, because vike-react-sentry uses import.meta.env.PUBLIC_ENV__SENTRY_DSN
id: {
Expand Down Expand Up @@ -118,6 +119,22 @@ function pluginReplaceConstantsEnvVars(): Plugin[] {
magicString.replaceAll(new RegExp(regExpStr, 'g'), JSON.stringify(replacement))
})

// Replace bare `import.meta.env` expression with `null` in the user-land.
// - Otherwise Vite replaces it with an object missing PUBLIC_ENV__ variables which is confusing for users.
// - We purposely don't support replacing `import.meta.env` with an object to incentivize users to write tree-shaking friendly code.
// - `define: { 'import.meta.env': JSON.stringify(null) }` doesn't work because it also replaces `import.meta.env` inside `import.meta.env.SONE_ENV`
const bareImportMetaEnvRegex = /\bimport\.meta\.env(?!\.)/g
const isUserLand = !id.includes('node_modules') && id.startsWith(config.root) // skip node_modules/ as well as linked dependencies
if (isUserLand && bareImportMetaEnvRegex.test(code)) {
assertWarning(
false,
`The bare ${pc.cyan('import.meta.env')} expression in ${getFilePathToShowToUserModule(id, config)} is replaced with ${pc.cyan('null')} — use ${pc.cyan('import.meta.env.SONE_ENV')} instead ${pc.underline('https://vike.dev/env')}`,
{ onlyOnce: true },
)
bareImportMetaEnvRegex.lastIndex = 0 // Reset state after .test() since the /g flag makes the RegExp stateful
magicString.replaceAll(bareImportMetaEnvRegex, JSON.stringify(null))
}

return getMagicStringResult()
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ declare global {
/** Like `import.meta.env.SSR` but works for `node_modules/` packages with `ssr.external` */
var __VIKE__IS_CLIENT: boolean
var __VIKE__IS_DEBUG: boolean
/** Whether the code is processed by Vite, e.g. `true` when server code is `ssr.noExternal` */
var __VIKE__NO_EXTERNAL: true | undefined
}

const VIRTUAL_FILE_ID_constantsGlobalThis = 'virtual:vike:server:constantsGlobalThis'
Expand Down Expand Up @@ -55,6 +57,7 @@ function pluginReplaceConstantsGlobalThis(): Plugin[] {
define: {
'globalThis.__VIKE__IS_DEV': JSON.stringify(isDev),
'globalThis.__VIKE__IS_DEBUG': JSON.stringify(isDebugVal),
'globalThis.__VIKE__NO_EXTERNAL': 'true',
},
}
},
Expand Down
4 changes: 1 addition & 3 deletions packages/vike/src/shared-server-client/route/abort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,11 @@ function assertStatusCode(statusCode: number, expected: number[], caller: 'rende
assert(import.meta.env.DEV === globalThis.__VIKE__IS_DEV)
} else {
assert(!isBrowser())
if (import.meta.env) {
if (globalThis.__VIKE__NO_EXTERNAL) {
assert(typeof globalThis.__VIKE__IS_DEV === 'boolean')
assert(typeof globalThis.__VIKE__IS_CLIENT === 'boolean')
assert(import.meta.env.SSR === true)
assert(import.meta.env.DEV === globalThis.__VIKE__IS_DEV)
} else {
// import.meta.env isn't defined when 'vike' is ssr.external
}
}

Expand Down
Loading