Skip to content

Commit

Permalink
fix(lib): only resolve css bundle name if have styles (#18530)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Oct 31, 2024
1 parent a50ff60 commit 5d6dc49
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
// since output formats have no effect on the generated CSS.
let hasEmitted = false
let chunkCSSMap: Map<string, string>
let cssBundleName: string

const rollupOptionsOutput = config.build.rollupOptions.output
const assetFileNames = (
Expand Down Expand Up @@ -463,6 +462,21 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
}
}

function getCssBundleName() {
const cached = cssBundleNameCache.get(config)
if (cached) return cached

const cssBundleName = config.build.lib
? resolveLibCssFilename(
config.build.lib,
config.root,
config.packageCache,
)
: defaultCssBundleName
cssBundleNameCache.set(config, cssBundleName)
return cssBundleName
}

return {
name: 'vite:css-post',

Expand All @@ -472,14 +486,6 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
hasEmitted = false
chunkCSSMap = new Map()
codeSplitEmitQueue = createSerialPromiseQueue()
cssBundleName = config.build.lib
? resolveLibCssFilename(
config.build.lib,
config.root,
config.packageCache,
)
: defaultCssBundleName
cssBundleNameCache.set(config, cssBundleName)
},

async transform(css, id) {
Expand Down Expand Up @@ -844,7 +850,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
}
} else {
// resolve public URL from CSS paths, we need to use absolute paths
chunkCSS = resolveAssetUrlsInCss(chunkCSS, cssBundleName)
chunkCSS = resolveAssetUrlsInCss(chunkCSS, getCssBundleName())
// finalizeCss is called for the aggregated chunk in generateBundle

chunkCSSMap.set(chunk.fileName, chunkCSS)
Expand Down Expand Up @@ -918,7 +924,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
hasEmitted = true
extractedCss = await finalizeCss(extractedCss, true, config)
this.emitFile({
name: cssBundleName,
name: getCssBundleName(),
type: 'asset',
source: extractedCss,
})
Expand Down

0 comments on commit 5d6dc49

Please sign in to comment.