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 @@