From 79777fc3a2565bf54c330341dd5a31461a922095 Mon Sep 17 00:00:00 2001 From: Aleen Date: Wed, 4 Jan 2023 10:19:48 +0800 Subject: [PATCH] chore: build assets with the entry name when it is an entry point REF: https://github.com/vitejs/vite/issues/4863#issuecomment-1367775712 --- packages/vite/src/node/plugins/css.ts | 7 +++++-- .../__tests__/backend-integration.spec.ts | 5 +++-- playground/backend-integration/vite.config.js | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 142a696662f5e3..585270f1e82423 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -553,8 +553,12 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { ? normalizePath(path.relative(config.root, chunk.facadeModuleId)) : chunk.name + const isEntry = chunk.isEntry && isPureCssChunk const lang = path.extname(cssAssetName).slice(1) - const cssFileName = ensureFileExt(cssAssetName, '.css') + const cssFileName = ensureFileExt( + isEntry ? chunk.name : cssAssetName, + '.css', + ) chunkCSS = resolveAssetUrlsInCss(chunkCSS, cssAssetName) chunkCSS = await finalizeCss(chunkCSS, true, config) @@ -566,7 +570,6 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { source: chunkCSS, }) const originalName = isPreProcessor(lang) ? cssAssetName : cssFileName - const isEntry = chunk.isEntry && isPureCssChunk generatedAssets .get(config)! .set(referenceId, { originalName, isEntry }) diff --git a/playground/backend-integration/__tests__/backend-integration.spec.ts b/playground/backend-integration/__tests__/backend-integration.spec.ts index 8d067f0616775d..fa2a3d6070bb3d 100644 --- a/playground/backend-integration/__tests__/backend-integration.spec.ts +++ b/playground/backend-integration/__tests__/backend-integration.spec.ts @@ -37,7 +37,6 @@ describe.runIf(isBuild)('build', () => { const cssAssetEntry = manifest['global.css'] const scssAssetEntry = manifest['nested/blue.scss'] const imgAssetEntry = manifest['../images/logo.png'] - const dirFooAssetEntry = manifest['../../dir/foo.css'] // '\\' should not be used even on windows expect(htmlEntry.css.length).toEqual(1) expect(htmlEntry.assets.length).toEqual(1) expect(cssAssetEntry?.file).not.toBeUndefined() @@ -47,7 +46,9 @@ describe.runIf(isBuild)('build', () => { expect(scssAssetEntry?.isEntry).toEqual(true) expect(imgAssetEntry?.file).not.toBeUndefined() expect(imgAssetEntry?.isEntry).toBeUndefined() - expect(dirFooAssetEntry).not.toBeUndefined() + // use the entry name + expect(manifest['bar.css']).not.toBeUndefined() + expect(manifest['foo.css']).toBeUndefined() }) }) diff --git a/playground/backend-integration/vite.config.js b/playground/backend-integration/vite.config.js index 245f1324ded5ed..2c301d8e79b97e 100644 --- a/playground/backend-integration/vite.config.js +++ b/playground/backend-integration/vite.config.js @@ -19,7 +19,7 @@ function BackendIntegrationExample() { .map((filename) => [path.relative(root, filename), filename]) entrypoints.push(['tailwindcss-colors', 'tailwindcss/colors.js']) - entrypoints.push(['foo.css', path.resolve(__dirname, './dir/foo.css')]) + entrypoints.push(['bar.css', path.resolve(__dirname, './dir/foo.css')]) return { build: {