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(css): use non-nested chunk name if facadeModule is not CSS file #15155

Merged
merged 4 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
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 @@ -613,7 +613,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 be have a non-nested chunk name', () => {
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
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;
}
Loading