Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unify css collecting order #11671

Merged
merged 2 commits into from
Sep 22, 2023
Merged
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
37 changes: 28 additions & 9 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import glob from 'fast-glob'
import postcssrc from 'postcss-load-config'
import type {
ExistingRawSourceMap,
NormalizedOutputOptions,
OutputChunk,
RenderedChunk,
RollupError,
Expand Down Expand Up @@ -378,8 +377,8 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {

// when there are multiple rollup outputs and extracting CSS, only emit once,
// since output formats have no effect on the generated CSS.
let outputToExtractedCSSMap: Map<NormalizedOutputOptions, string>
let hasEmitted = false
let chunkCSSMap: Map<string, string>
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved

const rollupOptionsOutput = config.build.rollupOptions.output
const assetFileNames = (
Expand Down Expand Up @@ -409,8 +408,8 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
buildStart() {
// Ensure new caches for every build (i.e. rebuilding in watch mode)
pureCssChunks = new Set<RenderedChunk>()
outputToExtractedCSSMap = new Map<NormalizedOutputOptions, string>()
hasEmitted = false
chunkCSSMap = new Map()
emitTasks = []
},

Expand Down Expand Up @@ -708,10 +707,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
chunkCSS = resolveAssetUrlsInCss(chunkCSS, cssBundleName)
// finalizeCss is called for the aggregated chunk in generateBundle

outputToExtractedCSSMap.set(
opts,
(outputToExtractedCSSMap.get(opts) || '') + chunkCSS,
)
chunkCSSMap.set(chunk.fileName, chunkCSS)
}
return null
},
Expand Down Expand Up @@ -792,8 +788,31 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
})
}

let extractedCss = outputToExtractedCSSMap.get(opts)
if (extractedCss && !hasEmitted) {
function extractCss() {
let css = ''
const collected = new Set<OutputChunk>()
const prelimaryNameToChunkMap = new Map(
Object.values(bundle)
.filter((chunk): chunk is OutputChunk => chunk.type === 'chunk')
.map((chunk) => [chunk.preliminaryFileName, chunk]),
)

function collect(fileName: string) {
const chunk = bundle[fileName]
if (!chunk || chunk.type !== 'chunk' || collected.has(chunk)) return
collected.add(chunk)

chunk.imports.forEach(collect)
css += chunkCSSMap.get(chunk.preliminaryFileName) ?? ''
}

for (const chunkName of chunkCSSMap.keys())
collect(prelimaryNameToChunkMap.get(chunkName)?.fileName ?? '')
lsdsjy marked this conversation as resolved.
Show resolved Hide resolved

return css
}
let extractedCss = !hasEmitted && extractCss()
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
if (extractedCss) {
hasEmitted = true
extractedCss = await finalizeCss(extractedCss, true, config)
this.emitFile({
Expand Down