async function hoistEntryCss(graph) {
	for (const fileName in graph.bundle) {
		/** @type {ExtendedAsset | Chunk} */
		const asset = graph.bundle[fileName];
		if (asset.type !== 'asset' || !/\.html$/.test(fileName)) continue;

		let cssImport = null;
		if (asset.referencedFiles) {
			// Check if the HTML file has direct CSS imports
			cssImport = asset.referencedFiles.find(f => f.endsWith('.css'));

			// If it's not: Check for entry js css files
			if (!cssImport && asset.importedIds) {
				const jsEntry = asset.importedIds.find(f => f.endsWith('.js'));
				if (jsEntry) {
					const entry = graph.bundle[jsEntry];
					if (entry.isEntry) {
						const cssFile = entry.referencedFiles.find(f => f.endsWith('.css'));

						// Ignore if no entry css:
						if (!cssFile) continue;

						asset.referencedFiles.push(cssFile);

						asset.source = await injectHead(asset.source, {
							tag: 'link',
							attrs: { rel: 'stylesheet', href: '/' + cssFile }
						});
						continue;
					}
				}
			}
		}

		const cssAsset = /** @type {Asset} */ (graph.bundle[cssImport]);
		for (const id of asset.importedIds) {
			const chunk = graph.bundle[id];
			if (chunk.type !== 'chunk') continue;

			for (let i = 0; i < chunk.referencedFiles.length; i++) {
				const f = chunk.referencedFiles[i];
				if (!isCssFilename(f)) continue;
				if (cssAsset) {
					// eslint-disable-next-line no-console
					if (DEBUG) console.log(`Hoisting CSS "${f}" imported by ${id} into parent HTML import "${cssImport}".`);
					// @TODO: this needs to update the hash of the chunk into which CSS got merged.
					cssAsset.source += '\n' + getAssetSource(graph.bundle[f]);
					chunk.referencedFiles[i] = cssImport;
					delete graph.bundle[f];
					graph.replaceCssImport(chunk, f, false);
					const chunkInfo = graph.assetToChunkMap.get(f);
					if (chunkInfo) chunkInfo.mergedInto = cssImport;
					for (const chunkInfo of graph.assetToChunkMap.values()) {
						if (chunkInfo.mergedInto == f) {
							for (const ownerChunk of chunkInfo.chunks) {
								if (ownerChunk === id) continue;
								graph.replaceCssImport(graph.bundle[ownerChunk], f, false);
							}
							chunkInfo.mergedInto = cssImport;
						}
					}
				} else {
					// @TODO: this branch is actually unreachable
					// eslint-disable-next-line no-console
					if (DEBUG) console.log(`Hoisting CSS "${f}" imported by ${id} into parent HTML.`);
					const url = toImport(graph.publicPath, f);
					asset.source = getAssetSource(asset).replace(/<\/head>/, `<link rel="stylesheet" href=${url}></head>`);
				}
			}
		}
	}
}
Extract CSS imports from entry modules into the HTML files that reference them.
@param {ChunkGraph} graph