Skip to content

Commit 9508549

Browse files
authoredJun 27, 2022
fix: preserve extension of css assets in the manifest (#8768)
1 parent 6851009 commit 9508549

File tree

6 files changed

+38
-28
lines changed

6 files changed

+38
-28
lines changed
 

‎packages/vite/src/node/plugins/css.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ export const removedPureCssFilesCache = new WeakMap<
143143
Map<string, RenderedChunk>
144144
>()
145145

146+
export const cssEntryFilesCache = new WeakMap<ResolvedConfig, Set<string>>()
147+
146148
const postcssConfigCache = new WeakMap<
147149
ResolvedConfig,
148150
PostCSSConfigResult | null
@@ -179,6 +181,7 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
179181
cssModulesCache.set(config, moduleCache)
180182

181183
removedPureCssFilesCache.set(config, new Map<string, RenderedChunk>())
184+
cssEntryFilesCache.set(config, new Set())
182185
},
183186

184187
async transform(raw, id, options) {
@@ -453,6 +456,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
453456
return null
454457
}
455458

459+
const cssEntryFiles = cssEntryFilesCache.get(config)!
456460
const publicAssetUrlMap = publicAssetUrlCache.get(config)!
457461

458462
// resolve asset URL placeholders to their built file URLs
@@ -509,22 +513,24 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
509513
pureCssChunks.add(chunk.fileName)
510514
}
511515
if (opts.format === 'es' || opts.format === 'cjs') {
512-
const cssAssetName = ensureFileExt(
513-
chunk.facadeModuleId
514-
? normalizePath(path.relative(config.root, chunk.facadeModuleId))
515-
: chunk.name,
516-
'.css'
517-
)
516+
const cssAssetName = chunk.facadeModuleId
517+
? normalizePath(path.relative(config.root, chunk.facadeModuleId))
518+
: chunk.name
519+
520+
const lang = path.extname(cssAssetName).slice(1)
521+
const cssFileName = ensureFileExt(cssAssetName, '.css')
522+
523+
if (chunk.isEntry && isPureCssChunk) cssEntryFiles.add(cssAssetName)
518524

519525
chunkCSS = resolveAssetUrlsInCss(chunkCSS, cssAssetName)
520526
chunkCSS = await finalizeCss(chunkCSS, true, config)
521527

522528
// emit corresponding css file
523529
const fileHandle = this.emitFile({
524-
name: cssAssetName,
530+
name: isPreProcessor(lang) ? cssAssetName : cssFileName,
525531
fileName: assetFileNamesToFileName(
526532
resolveAssetFileNames(config),
527-
cssAssetName,
533+
cssFileName,
528534
getHash(chunkCSS),
529535
chunkCSS
530536
),

‎packages/vite/src/node/plugins/manifest.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { OutputAsset, OutputChunk } from 'rollup'
33
import type { ResolvedConfig } from '..'
44
import type { Plugin } from '../plugin'
55
import { normalizePath } from '../utils'
6+
import { cssEntryFilesCache } from './css'
67

78
export type Manifest = Record<string, ManifestChunk>
89

@@ -100,12 +101,18 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
100101
}
101102

102103
function createAsset(chunk: OutputAsset): ManifestChunk {
103-
return {
104+
const manifestChunk: ManifestChunk = {
104105
file: chunk.fileName,
105106
src: chunk.name
106107
}
108+
109+
if (cssEntryFiles.has(chunk.name!)) manifestChunk.isEntry = true
110+
111+
return manifestChunk
107112
}
108113

114+
const cssEntryFiles = cssEntryFilesCache.get(config)!
115+
109116
for (const file in bundle) {
110117
const chunk = bundle[file]
111118
if (chunk.type === 'chunk') {

‎playground/backend-integration/__tests__/backend-integration.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,17 @@ describe.runIf(isBuild)('build', () => {
3333
const manifest = readManifest('dev')
3434
const htmlEntry = manifest['index.html']
3535
const cssAssetEntry = manifest['global.css']
36+
const scssAssetEntry = manifest['nested/blue.scss']
3637
const imgAssetEntry = manifest['../images/logo.png']
3738
expect(htmlEntry.css.length).toEqual(1)
3839
expect(htmlEntry.assets.length).toEqual(1)
3940
expect(cssAssetEntry?.file).not.toBeUndefined()
41+
expect(cssAssetEntry?.isEntry).toEqual(true)
42+
expect(scssAssetEntry?.file).not.toBeUndefined()
43+
expect(scssAssetEntry?.src).toEqual('nested/blue.scss')
44+
expect(scssAssetEntry?.isEntry).toEqual(true)
4045
expect(imgAssetEntry?.file).not.toBeUndefined()
46+
expect(imgAssetEntry?.isEntry).toBeUndefined()
4147
})
4248
})
4349

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$primary: #cc0000;
2+
3+
.text-primary {
4+
color: $primary;
5+
}

‎playground/backend-integration/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
99
"preview": "vite preview"
1010
},
11-
"dependencies": {
12-
"tailwindcss": "^3.1.3"
13-
},
1411
"devDependencies": {
12+
"sass": "^1.52.3",
13+
"tailwindcss": "^3.1.3",
1514
"fast-glob": "^3.2.11"
1615
}
1716
}

‎pnpm-lock.yaml

+3-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.