Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
108 changes: 108 additions & 0 deletions packages/vite/src/node/plugins/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path'
import fs from 'node:fs'
import type { OutputAsset, OutputChunk, RenderedChunk } from 'rolldown'
import { viteManifestPlugin as nativeManifestPlugin } from 'rolldown/experimental'
import type { Plugin } from '../plugin'
Expand Down Expand Up @@ -145,6 +146,37 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
}
}
}

// Ensure that all input files are marked as entries in the manifest
// This is necessary because Rollup may not mark pure css chunks as entries
// when using array-based input
const rawInput = environment.config.build.rollupOptions.input
if (manifest && rawInput) {
const root = environment.config.root
const inputList =
typeof rawInput === 'string'
? [rawInput]
: Array.isArray(rawInput)
? rawInput
: Object.values(rawInput)

const inputEntries = new Set<string>()
for (const input of inputList) {
const absoluteInput = path.resolve(root, input)
const relativeInput = normalizePath(
path.relative(root, absoluteInput),
)
inputEntries.add(relativeInput)
}

for (const key in manifest) {
const item = manifest[key]
if (!item.isEntry && item.src && inputEntries.has(item.src)) {
item.isEntry = true
}
}
}

const output =
this.environment.config.build.rolldownOptions.output
const outputLength = Array.isArray(output) ? output.length : 1
Expand All @@ -167,6 +199,54 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
}
}
},
async writeBundle() {
const outDir = environment.config.build.outDir
const manifestPath = path.resolve(root, outDir, outPath)
if (!fs.existsSync(manifestPath)) return

let manifest: Manifest
try {
manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'))
} catch (_e) {
return
}

const rawInput = environment.config.build.rollupOptions.input
if (manifest && rawInput) {
const root = environment.config.root
const inputList =
typeof rawInput === 'string'
? [rawInput]
: Array.isArray(rawInput)
? rawInput
: Object.values(rawInput)

const inputEntries = new Set<string>()
for (const input of inputList) {
const absoluteInput = path.resolve(root, input)
const relativeInput = normalizePath(
path.relative(root, absoluteInput),
)
inputEntries.add(relativeInput)
}

let changed = false
for (const key in manifest) {
const item = manifest[key]
if (!item.isEntry && item.src && inputEntries.has(item.src)) {
item.isEntry = true
changed = true
}
}

if (changed) {
fs.writeFileSync(
manifestPath,
JSON.stringify(manifest, null, 2),
)
}
}
},
},
]
})
Expand Down Expand Up @@ -315,6 +395,34 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
}
}

// Ensure that all input files are marked as entries in the manifest
// This is necessary because Rollup may not mark pure css chunks as entries
// when using array-based input
Comment on lines +399 to +400
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you explain why this happens?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This happens because with array-based input, Rollup can treat CSS-only inputs as “assets” rather than JS entry chunks.
the manifest entry is emitted without isEntry: true, so pure CSS inputs don’t show up as entries. The writeBundle hook patches the manifest after generation to ensure all inputs (including CSS-only) are marked as entries.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rollup can treat CSS-only inputs as “assets” rather than JS entry chunks.

Why does this happen?

Copy link
Author

@DsChauhan08 DsChauhan08 Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With array-based input, the CSS entry files that contain only @import statements get processed into "pure CSS chunks" (chunks with no JS exports). When the CSS post-plugin emits these as assets and registers them in cssEntriesMap, the key used is chunk.name. However, for array-based input, the name derivation may not produce a key that matches the manifest entry key (which is derived from src). With object-based input, the explicit key name is used consistently throughout, so the lookup succeeds. This patch ensures any input file present in the manifest gets marked as an entry regardless of the chunk naming.

i could have tried fixing using Ensure the cssEntriesMap key is derived consistently between the CSS plugin and manifest plugin Or normalize input arrays to objects early in the build process to ensure consistent naming
but for the moment being the way i fixed seemed the best, maybe if other cases arise we could test the other fixes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding to that : The root cause is a mismatch in how CSS entry chunks are keyed between the CSS plugin and the manifest plugin.

When a CSS entry file contains only @import statements (no actual CSS rules), it becomes a "pure CSS chunk" (isPureCssChunk = true). In the cssPostPlugin, when this chunk is emitted as an asset, it gets registered in cssEntriesMap using chunk.name as the key:

cssEntriesMap.get(this.environment)!.set(chunk.name, referenceId)
Later, the manifest plugin tries to look up entries using entryCssAssetFileNames.get(chunk.fileName) to get the name and mark entries with isEntry: true.

The issue is that with array-based input, the chunk.name for pure CSS chunks may not be derived the same way as with object-based input:

Object input { 'main-css': 'src/main.css' } → chunk.name = 'main-css' (explicit)
Array input ['src/main.css'] → chunk.name is derived from the file path, but for import-only CSS files, this derivation can differ from what the manifest expects
This is why the lookup in createAsset() fails to find a matching name, so isEntry never gets set.

Two possible approaches:

1.Patch the manifest (current approach) - after manifest generation, check if any src matches an input file and mark it as isEntry
2. Fix the root cause - ensure cssEntriesMap uses a consistent key that matches how manifest entries are keyed (e.g., use the normalized source path instead of chunk.name)

Would you prefer I investigate option 2 for a more fundamental fix, or is the patching approach acceptable for this regression?

const inputOptions = buildOptions.rollupOptions.input
if (inputOptions) {
const inputs = new Set<string>()
const inputList = Array.isArray(inputOptions)
? inputOptions
: typeof inputOptions === 'object'
? Object.values(inputOptions)
: [inputOptions]

for (const input of inputList) {
const absoluteInput = path.resolve(root, input)
const relativeInput = normalizePath(
path.relative(root, absoluteInput),
)
inputs.add(relativeInput)
}

for (const name in manifest) {
const item = manifest[name]
if (!item.isEntry && item.src && inputs.has(item.src)) {
item.isEntry = true
}
}
}

state.outputCount++
const output = buildOptions.rollupOptions.output
const outputLength = Array.isArray(output) ? output.length : 1
Expand Down
9 changes: 8 additions & 1 deletion playground/fs-serve/__tests__/fs-serve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,14 @@ describe('cross origin', () => {
let page2: Page
beforeEach(async () => {
page2 = await browser.newPage()
await page2.goto('http://vite.dev/404')
await page2.route(/^http:\/\/example\.com/, async (route) => {
await route.fulfill({
status: 200,
contentType: 'text/html',
body: '<html><body>External Origin</body></html>',
})
})
await page2.goto('http://example.com/404')
})
afterEach(async () => {
await page2.close()
Expand Down
11 changes: 11 additions & 0 deletions playground/manifest-css-entry/__tests__/manifest-css-entry.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, expect, test } from 'vitest'
import { isBuild, readManifest } from '~utils'

describe.runIf(isBuild)('manifest-css-entry', () => {
test('import-only css entry should have isEntry: true', () => {
const manifest = readManifest()
const mainCssEntry = manifest['frontend/entrypoints/main.css']
expect(mainCssEntry).toBeDefined()
expect(mainCssEntry.isEntry).toBe(true)
})
})
2 changes: 2 additions & 0 deletions playground/manifest-css-entry/frontend/entrypoints/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import './styles/theme.css';
@import './styles/main.css';
1 change: 1 addition & 0 deletions playground/manifest-css-entry/frontend/entrypoints/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('main.js')
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* styles/main.css */
body {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* styles/theme.css */
:root {
--primary: blue;
}
16 changes: 16 additions & 0 deletions playground/manifest-css-entry/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from 'vite'
import path from 'node:path'

Check failure on line 2 in playground/manifest-css-entry/vite.config.js

View workflow job for this annotation

GitHub Actions / Lint: node-24, ubuntu-latest

`node:path` import should occur before import of `vite`

export default defineConfig({
build: {
manifest: true,
rollupOptions: {
input: [
path.resolve(__dirname, 'frontend/entrypoints/main.css'),
path.resolve(__dirname, 'frontend/entrypoints/main.js'),
path.resolve(__dirname, 'frontend/entrypoints/styles/main.css'),
path.resolve(__dirname, 'frontend/entrypoints/styles/theme.css'),
],
},
},
})
Loading