Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,12 @@ async function handleAssetsManifest(
if (isViteServerSide(config, viteEnv)) {
const outDir = options.dir
assert(outDir)
// Replace __VITE_ASSETS_MANIFEST__ in server builds
// Replace ASSETS_MANIFEST in server builds
// - Always replace it in dist/server/
// - Also in some other server builds such as dist/vercel/ from vike-vercel
// - Don't replace it in dist/rsc/ from vike-react-rsc since __VITE_ASSETS_MANIFEST__ doesn't exist there
// - Don't replace it in dist/rsc/ from vike-react-rsc since ASSETS_MANIFEST doesn't exist there
const noop = await set_macro_ASSETS_MANIFEST(globalObject.assetsJsonFilePath, bundle, outDir)
if (isSsrEnv) assert(!noop) // dist/server should always contain __VITE_ASSETS_MANIFEST__
if (isSsrEnv) assert(!noop) // dist/server should always contain ASSETS_MANIFEST
}
}
async function writeAssetsManifestFile(assetsJsonFilePath: string, config: ResolvedConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { virtualFileIdGlobalEntryServer } from '../../../../shared-server-node/v
import { PROJECT_VERSION } from '../../../../utils/PROJECT_VERSION.js'
import { assert } from '../../../../utils/assert.js'
import { requireResolveDistFile } from '../../../../utils/requireResolve.js'
import { preventConstantFolding } from '../../../../utils/preventConstantFolding.js'
import fs from 'node:fs/promises'
import path from 'node:path'
import type { Plugin, ResolvedConfig, Rollup } from 'vite'
Expand All @@ -15,7 +16,11 @@ import { getOutDirs } from '../../shared/getOutDirs.js'
import { getViteConfigRuntime } from '../../shared/getViteConfigRuntime.js'
import '../../assertEnvVite.js'
type Bundle = Rollup.OutputBundle
const ASSETS_MANIFEST = '__VITE_ASSETS_MANIFEST__'
const ASSETS_MANIFEST = `__VITE_ASSETS_MANIFEST_${
// Prevent ASSETS_MANIFEST to be found in `node_modules/vike/` when server runtime imports Vike's Vite plugin via `import { prerender } from 'vike/api'` with ssr.noExternal.includes('vike')
// https://github.com/vikejs/vike/issues/3113
preventConstantFolding()
}_`

function pluginProdBuildEntry(): Plugin[] {
let config: ResolvedConfig
Expand Down Expand Up @@ -70,7 +75,7 @@ function getServerProductionEntryCode(config: ResolvedConfig): string {
async function set_macro_ASSETS_MANIFEST(assetsJsonFilePath: string | undefined, bundle: Bundle, outDir: string) {
assert(outDir)
const chunkPath = find_ASSETS_MANIFEST(bundle)
// Some server builds don't contain __VITE_ASSETS_MANIFEST__ such as dist/rsc/ from vike-react-rsc
// Some server builds don't contain ASSETS_MANIFEST such as dist/rsc/ from vike-react-rsc
if (!chunkPath) {
const noop = true // no operation
return noop
Expand Down
6 changes: 6 additions & 0 deletions packages/vike/src/utils/preventConstantFolding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** Prevent compilers from constant folding `'a' + 'b'` into `'ab'`*/
export function preventConstantFolding() {
const undefined_ = (globalThis as any).__vike__this_property_is_never_defined
if (undefined_) return '__vike__this_string_is_never_used' as never
return '' as const
}
Loading