Skip to content

Commit 741611a

Browse files
author
Stanislav Lashmanov
committed
fix(build): replace 'names' with unmangled 'name' for CSS assets
This removes the 'names' field which had incorrect CSS entrypoint names. Instead, a 'name' field is added that uses CSS entrypoint name as-is.
1 parent 0a6048a commit 741611a

File tree

6 files changed

+21
-12
lines changed

6 files changed

+21
-12
lines changed

packages/vite/src/node/plugins/asset.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const inlineRE = /[?&]inline\b/
4646
const assetCache = new WeakMap<Environment, Map<string, string>>()
4747

4848
/** a set of referenceId for entry CSS assets for each environment */
49-
export const cssEntriesMap = new WeakMap<Environment, Set<string>>()
49+
export const cssEntriesMap = new WeakMap<Environment, Map<string, string>>()
5050

5151
// add own dictionary entry by directly assigning mrmime
5252
export function registerCustomMime(): void {
@@ -148,7 +148,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
148148

149149
buildStart() {
150150
assetCache.set(this.environment, new Map())
151-
cssEntriesMap.set(this.environment, new Set())
151+
cssEntriesMap.set(this.environment, new Map())
152152
},
153153

154154
resolveId: {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
863863
source: chunkCSS,
864864
})
865865
if (isEntry) {
866-
cssEntriesMap.get(this.environment)!.add(referenceId)
866+
cssEntriesMap.get(this.environment)!.set(chunk.name, referenceId)
867867
}
868868
chunk.viteMetadata!.importedCss.add(this.getFileName(referenceId))
869869
} else if (this.environment.config.consumer === 'client') {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,25 +122,25 @@ export function manifestPlugin(): Plugin {
122122
function createAsset(
123123
asset: OutputAsset,
124124
src: string,
125-
isEntry?: boolean,
125+
name?: string,
126126
): ManifestChunk {
127127
const manifestChunk: ManifestChunk = {
128128
file: asset.fileName,
129129
src,
130130
}
131-
if (isEntry) {
131+
if (name) {
132132
manifestChunk.isEntry = true
133-
manifestChunk.names = asset.names
133+
manifestChunk.name = name
134134
}
135135
return manifestChunk
136136
}
137137

138138
const entryCssReferenceIds = cssEntriesMap.get(this.environment)!
139-
const entryCssAssetFileNames = new Set()
140-
for (const id of entryCssReferenceIds) {
139+
const entryCssAssetFileNames = new Map()
140+
for (const [name, id] of entryCssReferenceIds) {
141141
try {
142142
const fileName = this.getFileName(id)
143-
entryCssAssetFileNames.add(fileName)
143+
entryCssAssetFileNames.set(fileName, name)
144144
} catch {
145145
// The asset was generated as part of a different output option.
146146
// It was already handled during the previous run of this plugin.
@@ -157,8 +157,8 @@ export function manifestPlugin(): Plugin {
157157
chunk.originalFileNames.length > 0
158158
? chunk.originalFileNames[0]
159159
: `_${path.basename(chunk.fileName)}`
160-
const isEntry = entryCssAssetFileNames.has(chunk.fileName)
161-
const asset = createAsset(chunk, src, isEntry)
160+
const name = entryCssAssetFileNames.get(chunk.fileName)
161+
const asset = createAsset(chunk, src, name)
162162

163163
// If JS chunk and asset chunk are both generated from the same source file,
164164
// prioritize JS chunk as it contains more information

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe.runIf(isBuild)('build', () => {
5454
const scssAssetEntry = manifest['nested/blue.scss']
5555
const imgAssetEntry = manifest['../images/logo.png']
5656
const dirFooAssetEntry = manifest['../../dir/foo.css']
57+
const customNameAssetEntry = manifest['../../dir/custom.css']
5758
const iconEntrypointEntry = manifest['icon.png']
5859
const waterContainerEntry = manifest['water-container.svg']
5960
expect(htmlEntry.css.length).toEqual(1)
@@ -74,7 +75,8 @@ describe.runIf(isBuild)('build', () => {
7475
expect(dirFooAssetEntry).not.toBeUndefined() // '\\' should not be used even on windows
7576
// use the entry name
7677
expect(dirFooAssetEntry.file).toMatch('assets/bar-')
77-
expect(dirFooAssetEntry.names).toStrictEqual(['bar.css'])
78+
expect(dirFooAssetEntry.name).toStrictEqual('bar.css')
79+
expect(customNameAssetEntry.name).toStrictEqual('bar.custom')
7880
expect(iconEntrypointEntry?.file).not.toBeUndefined()
7981
expect(waterContainerEntry?.file).not.toBeUndefined()
8082
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.custom {
2+
color: red;
3+
}

playground/backend-integration/vite.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ function BackendIntegrationExample() {
2323

2424
entrypoints.push(['tailwindcss-colors', 'tailwindcss/colors.js'])
2525
entrypoints.push(['bar.css', path.resolve(__dirname, './dir/foo.css')])
26+
entrypoints.push([
27+
'bar.custom',
28+
path.resolve(__dirname, './dir/custom.css'),
29+
])
2630

2731
return {
2832
server: {

0 commit comments

Comments
 (0)