Skip to content

Commit

Permalink
fix(css): use non-nested chunk name if facadeModule is not CSS file (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Nov 28, 2023
1 parent cb3243c commit 811e392
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,15 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
}

const isEntry = chunk.isEntry && isPureCssChunk
const cssAssetName = ensureFileExt(chunk.name, '.css')
const cssFullAssetName = ensureFileExt(chunk.name, '.css')
// if facadeModuleId doesn't exist or doesn't have a CSS extension,
// that means a JS entry file imports a CSS file.
// in this case, only use the filename for the CSS chunk name like JS chunks.
const cssAssetName =
chunk.isEntry &&
(!chunk.facadeModuleId || !isCSSRequest(chunk.facadeModuleId))
? path.basename(cssFullAssetName)
: cssFullAssetName
const originalFilename = getChunkOriginalFileName(
chunk,
config.root,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ describe.runIf(isBuild)('build', () => {
// use the entry name
expect(dirFooAssetEntry.file).toMatch('assets/bar-')
})

test('CSS imported from JS entry should have a non-nested chunk name', () => {
const manifest = readManifest('dev')
const mainTsEntryCss = manifest['nested/sub.ts'].css
expect(mainTsEntryCss.length).toBe(1)
expect(mainTsEntryCss[0].replace('assets/', '')).not.toContain('/')
})
})

describe.runIf(isServe)('serve', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ <h2>CSS Asset References</h2>
</li>
</ul>

<h2>CSS imported from JS</h2>

<p class="imported">text</p>

<script type="module" src="./main.ts"></script>
<script type="module" src="./nested/sub.ts"></script>
<script type="module">
import './global.css'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../../styles/imported.css'
3 changes: 3 additions & 0 deletions playground/backend-integration/frontend/styles/imported.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.imported {
color: green;
}

0 comments on commit 811e392

Please sign in to comment.