diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 872bb14168a245..7e84ef6b23ff76 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -54,6 +54,7 @@ import { processSrcSet, removeDirectQuery, requireResolveFromRootWithFallback, + slash, stripBase, stripBomTag, } from '../utils' @@ -388,10 +389,11 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { : rollupOptionsOutput )?.assetFileNames const getCssAssetDirname = (cssAssetName: string) => { + const cssAssetNameDir = path.dirname(cssAssetName) if (!assetFileNames) { - return config.build.assetsDir + return path.join(config.build.assetsDir, cssAssetNameDir) } else if (typeof assetFileNames === 'string') { - return path.dirname(assetFileNames) + return path.join(path.dirname(assetFileNames), cssAssetNameDir) } else { return path.dirname( assetFileNames({ @@ -556,7 +558,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { const relative = config.base === './' || config.base === '' const cssAssetDirname = encodedPublicUrls || relative - ? getCssAssetDirname(cssAssetName) + ? slash(getCssAssetDirname(cssAssetName)) : undefined const toRelative = (filename: string) => { diff --git a/playground/assets/__tests__/relative-base/relative-base-assets.spec.ts b/playground/assets/__tests__/relative-base/relative-base-assets.spec.ts index 40c353127e05de..438c24678f0a78 100644 --- a/playground/assets/__tests__/relative-base/relative-base-assets.spec.ts +++ b/playground/assets/__tests__/relative-base/relative-base-assets.spec.ts @@ -132,6 +132,11 @@ describe('css url() references', () => { const bg = await getBg('.css-url-aliased') expect(bg).toMatch(cssBgAssetMatch) }) + + test('nested manual chunks', async () => { + const bg = await getBg('.css-manual-chunks-relative') + expect(bg).toMatch(cssBgAssetMatch) + }) }) describe.runIf(isBuild)('index.css URLs', () => { diff --git a/playground/assets/css/manual-chunks.css b/playground/assets/css/manual-chunks.css new file mode 100644 index 00000000000000..0b75f6ef8fa649 --- /dev/null +++ b/playground/assets/css/manual-chunks.css @@ -0,0 +1,4 @@ +.css-manual-chunks-relative { + background: url(../nested/asset.png); + background-size: 10px; +} diff --git a/playground/assets/index.html b/playground/assets/index.html index ebb938d72f75a1..e0684db9061819 100644 --- a/playground/assets/index.html +++ b/playground/assets/index.html @@ -114,6 +114,11 @@

CSS url references

CSS background (aliased)
+
+ CSS nested manual chunks relative base background +
CSS SVG background @@ -395,6 +400,7 @@

assets in noscript

import './css/fonts.css' import './css/css-url.css' import './css/icons.css' + import './css/manual-chunks.css' text('.base', `import.meta.${``}env.BASE_URL: ${import.meta.env.BASE_URL}`) diff --git a/playground/assets/vite.config-relative-base.js b/playground/assets/vite.config-relative-base.js index 451ca7090d023a..1e25168e1c0aff 100644 --- a/playground/assets/vite.config-relative-base.js +++ b/playground/assets/vite.config-relative-base.js @@ -15,6 +15,11 @@ export default defineConfig(({ isPreview }) => ({ entryFileNames: 'entries/[name].js', chunkFileNames: 'chunks/[name]-[hash].js', assetFileNames: 'other-assets/[name]-[hash][extname]', + manualChunks(id) { + if (id.includes('css/manual-chunks.css')) { + return 'css/manual-chunks' + } + }, }, }, },