Skip to content
Open
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
51 changes: 29 additions & 22 deletions src/plugin-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,21 +350,45 @@ export function pluginFactory(readFileFn?: (path: string, options: any) => Promi
logData += markdownCodeBlock(formatData("options: %o", options));
logData += markdownCodeBlock(formatData("meta: %o", meta));
}
if (chunk.isEntry && !isRootConfig(config) && config.cssStrategy !== 'none') {
// Recursively collect all CSS files that this entry point might need.
// NOTE: CSS collection moved to generateBundle hook for Vite 7 compatibility.
// In Vite 7, viteMetadata.importedCss is not populated until after renderChunk completes.
}
catch (error) {
errorOccurred = true;
throw error;
}
finally {
await writeToLog(logData);
if (errorOccurred) {
await closeLog();
}
}
},
},
async generateBundle(_options, bundle, _isWrite) {
if (viteEnv.command === 'build') {
await closeLog();
}
if (!isRootConfig(config) && config.cssStrategy !== 'none') {
// Collect CSS files from entry chunks.
// This is done here instead of renderChunk because in Vite 7,
// viteMetadata.importedCss is not populated until after renderChunk.
for (let x in bundle) {
const chunk = bundle[x];
if (chunk.type === 'chunk' && chunk.isEntry) {
const cssFiles = new Set<string>();
const processedImports = new Set<string>();
const collectCssFiles = (curChunk: RenderedChunk) => {
if (!curChunk) {
return;
}
curChunk.viteMetadata?.importedCss.forEach(css => cssFiles.add(css));
for (let imp of curChunk.imports) {
curChunk.viteMetadata?.importedCss?.forEach(css => cssFiles.add(css));
for (let imp of curChunk.imports || []) {
if (processedImports.has(imp)) {
continue;
}
processedImports.add(imp);
collectCssFiles(meta.chunks[imp]);
collectCssFiles(bundle[imp] as RenderedChunk);
}
};
collectCssFiles(chunk);
Expand All @@ -374,23 +398,6 @@ export function pluginFactory(readFileFn?: (path: string, options: any) => Promi
}
}
}
catch (error) {
errorOccurred = true;
throw error;
}
finally {
await writeToLog(logData);
if (errorOccurred) {
await closeLog();
}
}
},
},
async generateBundle(_options, bundle, _isWrite) {
if (viteEnv.command === 'build') {
await closeLog();
}
if (!isRootConfig(config) && config.cssStrategy !== 'none') {
const stringifiedCssMap = JSON.stringify(JSON.stringify(cssMap));
for (let x in bundle) {
const entry = bundle[x];
Expand Down
11 changes: 10 additions & 1 deletion tests/plugin-factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,12 +525,21 @@ describe('vite-plugin-single-spa', () => {
for (let ch of chunks) {
await (plugIn.renderChunk as RenderChunkHandler).handler('', ch, {}, meta);
}
const bundle = {
// Build the bundle object with all chunks for generateBundle.
// In Vite 7+, CSS collection happens in generateBundle where viteMetadata.importedCss
// is fully populated, so the bundle must include all chunks with their metadata.
const bundle: Record<string, any> = {
'a.js': {
type: 'chunk',
code: '"{vpss:CSS_MAP}"'
}
};
for (let ch of chunks) {
bundle[ch.fileName] = {
...ch,
type: 'chunk'
};
}

// Act.
await (plugIn.generateBundle as GenerateBundleHandler)({}, bundle);
Expand Down