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(lib): only resolve css bundle name if have styles #18530

Merged
merged 1 commit into from
Oct 31, 2024
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
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